/** * Sets the Response content-type to application/json. */ public Response json() { return contentType(HttpConstants.ContentType.APPLICATION_JSON); }
/** * Sets the Response content-type to application/x-yaml. */ public Response yaml() { return contentType(HttpConstants.ContentType.APPLICATION_X_YAML); }
/** * Sets the Response content-type to text/plain. */ public Response text() { return contentType(HttpConstants.ContentType.TEXT_PLAIN); }
@Override public RouteContext negotiateContentType() { response.contentType(request); return this; }
/** * Sets the Response content-type to text/html. */ public Response html() { return contentType(HttpConstants.ContentType.TEXT_HTML); }
/** * Sets the Response content-type to application/xml. */ public Response xml() { return contentType(HttpConstants.ContentType.APPLICATION_XML); }
@Override public void setResponseContentType(String contentType) { getResponse().contentType(contentType); }
/** * 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(); } } }
public PrintWriter getWriter() { checkCommitted(); finalizeResponse(); if (getContentType() == null) { contentType(HttpConstants.ContentType.TEXT_HTML); } try { return httpServletResponse.getWriter(); } catch (IOException e) { throw new PippoRuntimeException(e); } }
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); } }
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); } }
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()); } }
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); } }
@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); } }
/** * 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); } }
return contentType(engine.getContentType());
@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); } }
/** * 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); } }
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);
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) { } } }