ro.pippo.core.Response.contentType()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中ro.pippo.core.Response.contentType方法的一些代码示例,展示了Response.contentType的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Response.contentType方法的具体详情如下:
包路径:ro.pippo.core.Response
类名称:Response
方法名:contentType

Response.contentType介绍

[英]Sets the content type of the response.
[中]设置响应的内容类型。

代码示例

代码示例来源:origin: pippo-java/pippo

/**
 * Sets the Response content-type to application/json.
 */
public Response json() {
  return contentType(HttpConstants.ContentType.APPLICATION_JSON);
}

代码示例来源:origin: pippo-java/pippo

/**
 * Sets the Response content-type to application/x-yaml.
 */
public Response yaml() {
  return contentType(HttpConstants.ContentType.APPLICATION_X_YAML);
}

代码示例来源:origin: pippo-java/pippo

/**
 * Sets the Response content-type to text/plain.
 */
public Response text() {
  return contentType(HttpConstants.ContentType.TEXT_PLAIN);
}

代码示例来源:origin: pippo-java/pippo

@Override
public RouteContext negotiateContentType() {
  response.contentType(request);
  return this;
}

代码示例来源:origin: pippo-java/pippo

/**
 * Sets the Response content-type to text/html.
 */
public Response html() {
  return contentType(HttpConstants.ContentType.TEXT_HTML);
}

代码示例来源:origin: pippo-java/pippo

/**
 * Sets the Response content-type to application/xml.
 */
public Response xml() {
  return contentType(HttpConstants.ContentType.APPLICATION_XML);
}

代码示例来源:origin: pippo-java/pippo

@Override
public void setResponseContentType(String contentType) {
  getResponse().contentType(contentType);
}

代码示例来源:origin: pippo-java/pippo

/**
 * Specify the Response content-type by...
 * <ol>
 * <li>setting the first Produces content type</li>
 * <li>negotiating with the Request if multiple content-types are specified in Produces</li>
 * </ol>
 *
 * @param routeContext
 */
protected void specifyContentType(RouteContext routeContext) {
  if (!declaredProduces.isEmpty()) {
    // Specify first Produces content-type
    String defaultContentType = declaredProduces.get(0);
    routeContext.getResponse().contentType(defaultContentType);
    if (declaredProduces.size() > 1) {
      // negotiate content-type from Request Accept/Content-Type
      routeContext.negotiateContentType();
    }
  }
}

代码示例来源:origin: pippo-java/pippo

public PrintWriter getWriter() {
  checkCommitted();
  finalizeResponse();
  if (getContentType() == null) {
    contentType(HttpConstants.ContentType.TEXT_HTML);
  }
  try {
    return httpServletResponse.getWriter();
  } catch (IOException e) {
    throw new PippoRuntimeException(e);
  }
}

代码示例来源:origin: pippo-java/pippo

private void commit(CharSequence content) {
  checkCommitted();
  finalizeResponse();
  // content type to TEXT_HTML if it's not set
  if (getContentType() == null) {
    contentType(HttpConstants.ContentType.TEXT_HTML);
  }
  try {
    if (content != null) {
      contentLength(content.toString().getBytes().length);
      httpServletResponse.getWriter().append(content);
    }
    log.trace("Response committed");
    if (chunked) {
      httpServletResponse.flushBuffer();
    }
    finishGZip();
  } catch (IOException e) {
    throw new PippoRuntimeException(e);
  }
}

代码示例来源:origin: pippo-java/pippo

public ServletOutputStream getOutputStream() {
  checkCommitted();
  finalizeResponse();
  // content type to OCTET_STREAM if it's not set
  if (getContentType() == null) {
    contentType(HttpConstants.ContentType.APPLICATION_OCTET_STREAM);
  }
  try {
    return httpServletResponse.getOutputStream();
  } catch (IOException e) {
    throw new PippoRuntimeException(e);
  }
}

代码示例来源:origin: pippo-java/pippo

protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException {
  String filename = resourceUrl.getFile();
  String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename);
  if (!StringUtils.isNullOrEmpty(mimeType)) {
    // stream the resource
    log.debug("Streaming as resource '{}'", resourceUrl);
    routeContext.getResponse().contentType(mimeType);
    routeContext.getResponse().ok().resource(resourceUrl.openStream());
  } else {
    // stream the file
    log.debug("Streaming as file '{}'", resourceUrl);
    routeContext.getResponse().ok().file(filename, resourceUrl.openStream());
  }
}

代码示例来源:origin: pippo-java/pippo

protected void setResponseHeaders(URL resourceUrl, RouteContext routeContext) {
  try {
    long lastModified = resourceUrl.openConnection().getLastModified();
    routeContext.getApplication().getHttpCacheToolkit().addEtag(routeContext, lastModified);
    String filename = resourceUrl.getFile();
    String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename);
    if (!StringUtils.isNullOrEmpty(mimeType)) {
      routeContext.getResponse().contentType(mimeType);
    }
  } catch (Exception e) {
    throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl);
  }
}

代码示例来源:origin: pippo-java/pippo

@Override
protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException {
  try {
    // compile sass to css
    log.trace("Send css for '{}'", resourceUrl);
    ScssContext.UrlMode urlMode = ScssContext.UrlMode.ABSOLUTE;
    String identifier = resourceUrl.getFile();
    ScssStylesheet scssStylesheet = ScssStylesheet.get(identifier);
    if (scssStylesheet == null) {
      throw new Exception("ScssStylesheet is null for '" + identifier + "'");
    }
    String content = scssStylesheet.toString();
    String result = sourceMap.get(content);
    if (result == null) {
      scssStylesheet.compile(urlMode);
      Writer writer = new StringWriter();
      scssStylesheet.write(writer, minify);
      result = writer.toString();
      if (routeContext.getApplication().getPippoSettings().isProd()) {
        sourceMap.put(content, result);
      }
    }
    // send css
    routeContext.getResponse().contentType("text/css");
    routeContext.getResponse().ok().send(result);
  }  catch (Exception e) {
    throw new PippoRuntimeException(e);
  }
}

代码示例来源:origin: pippo-java/pippo

/**
 * Copies the input stream to the response output stream and closes the input stream upon completion.
 * <p>This method commits the response.</p>
 *
 * @param input
 */
public void resource(InputStream input) {
  checkCommitted();
  finalizeResponse();
  // content type to OCTET_STREAM if it's not set
  if (getContentType() == null) {
    contentType(HttpConstants.ContentType.APPLICATION_OCTET_STREAM);
  }
  try {
    send(input);
  } catch (IOException e) {
    throw new PippoRuntimeException(e);
  }
}

代码示例来源:origin: pippo-java/pippo

return contentType(engine.getContentType());

代码示例来源:origin: pippo-java/pippo

@Override
protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException {
  try {
    // compile less to css
    log.trace("Send css for '{}'", resourceUrl);
    LessSource.URLSource source = new LessSource.URLSource(resourceUrl);
    String content = source.getContent();
    String result = sourceMap.get(content);
    if (result == null) {
      ThreadUnsafeLessCompiler compiler = new ThreadUnsafeLessCompiler();
      LessCompiler.Configuration configuration = new LessCompiler.Configuration();
      configuration.setCompressing(minify);
      LessCompiler.CompilationResult compilationResult = compiler.compile(resourceUrl, configuration);
      for (LessCompiler.Problem warning : compilationResult.getWarnings()) {
        log.warn("Line: {}, Character: {}, Message: {} ", warning.getLine(), warning.getCharacter(), warning.getMessage());
      }
      result = compilationResult.getCss();
      if (routeContext.getApplication().getPippoSettings().isProd()) {
        sourceMap.put(content, result);
      }
    }
    // send css
    routeContext.getResponse().contentType("text/css");
    routeContext.getResponse().ok().send(result);
  } catch (Exception e) {
    throw new PippoRuntimeException(e);
  }
}

代码示例来源:origin: pippo-java/pippo

/**
 * Copies the input stream to the response output stream as a download and closes the input stream upon completion.
 * <p>This method commits the response.</p>
 *
 * @param filename
 * @param input
 */
public void file(String filename, InputStream input) {
  checkCommitted();
  // content type to OCTET_STREAM if it's not set
  if (getContentType() == null) {
    contentType(mimeTypes.getContentType(filename, HttpConstants.ContentType.APPLICATION_OCTET_STREAM));
  }
  if (isHeaderEmpty(HttpConstants.Header.CONTENT_DISPOSITION)) {
    filenameHeader(filename);
  }
  finalizeResponse();
  try {
    send(input);
  } catch (IOException e) {
    throw new PippoRuntimeException(e);
  }
}

代码示例来源:origin: pippo-java/pippo

Error error = prepareError(statusCode, routeContext);
try {
  routeContext.getResponse().contentType(contentType).send(error);
} catch (Exception e) {
  log.error("Unexpected error generating '{}' as '{}'", Error.class.getName(), contentType, e);

代码示例来源:origin: com.gitblit.fathom/fathom-rest

protected void writeObject(Context context, Object result) {
    try {
      context.getResponse()
        .contentType(CONTENT_TYPE)
        .header(CLASS_NAME, result == null ? NULL : result.getClass().getName());

      if (result != null) {
        ObjectOutputStream replyStream = new ObjectOutputStream(context.getResponse().getOutputStream());
        replyStream.writeObject(result);
        replyStream.flush();
      }
    } catch (IOException e) {
    }
  }
}

相关文章