/** * Serializes the object as XML using the registered <code>application/xml</code> * ContentTypeEngine and writes it to the response. * <p>This method commits the response.</p> * * @param object */ public void xml(Object object) { send(object, HttpConstants.ContentType.APPLICATION_XML); }
/** * Serializes the object as YAML using the registered <code>application/x-yaml</code> * ContentTypeEngine and writes it to the response. * <p>This method commits the response.</p> * * @param object */ public void yaml(Object object) { send(object, HttpConstants.ContentType.APPLICATION_X_YAML); }
/** * Serializes the object as JSON using the registered <code>application/json</code> * ContentTypeEngine and writes it to the response. * <p>This method commits the response.</p> * * @param object */ public void json(Object object) { send(object, HttpConstants.ContentType.APPLICATION_JSON); }
/** * Serializes the object as plain text using the registered <code>text/plain</code> * ContentTypeEngine and writes it to the response. * <p>This method commits the response.</p> * * @param object */ public void text(Object object) { send(object, HttpConstants.ContentType.TEXT_PLAIN); }
@Override public void send(CharSequence content) { response.send(content); }
@Override public void send(Object object) { response.send(object); }
/** * Renders a template and writes the output directly to the response. * <p>This method commits the response.</p> * * @param templateName * @param model */ public void render(String templateName, Map<String, Object> model) { send(renderToString(templateName, model)); }
/** * Serializes the object using the registered ContentTypeEngine matching * the pre-specified content-type. * <p>This method commits the response.</p> * * @param object */ public void send(Object object) { if (object instanceof File) { file((File) object); } else { send(object, getContentType()); } }
@Override public void handle(RouteContext routeContext) { Response response = routeContext.getResponse().noCache().text(); SortedMap<String, HealthCheck.Result> healthChecks = healthCheckRegistry.runHealthChecks(); if (healthChecks.isEmpty()) { response.notImplemented().send("The health checks are empty"); } else { boolean notHealthy = healthChecks.values().stream().anyMatch(hc -> !hc.isHealthy()); if (notHealthy) { response.internalError().send("The health is bad"); } else { response.ok().send("The health is good"); } } }
@Override public void handle(RouteContext routeContext) { routeContext.getResponse().noCache().text().send("pong"); }
@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); } }
@Override public void handle(RouteContext routeContext) { Response response = routeContext.getResponse().noCache().text(); if (threadDump != null) { threadDump.dump(response.getOutputStream()); } else { response.internalError().send("Sorry your runtime environment does not allow to dump threads"); } }
/** * 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); } }
private void send(Object object, String contentType) { if (StringUtils.isNullOrEmpty(contentType)) { throw new PippoRuntimeException("You must specify a content type!"); } ContentTypeEngine contentTypeEngine = contentTypeEngines.getContentTypeEngine(contentType); if (contentTypeEngine == null) { throw new PippoRuntimeException("You must set a content type engine for '{}'", contentType); } header(HttpConstants.Header.CONTENT_TYPE, contentTypeEngine.getContentType()); send(contentTypeEngine.toString(object)); }
@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);
@Override public void handle(RouteContext routeContext) { Response response = routeContext.getResponse().noCache().text(); SortedMap<String, HealthCheck.Result> healthChecks = healthCheckRegistry.runHealthChecks(); if (healthChecks.isEmpty()) { response.notImplemented().send("The health checks are empty"); } else { boolean notHealthy = healthChecks.values().stream().anyMatch(hc -> !hc.isHealthy()); if (notHealthy) { response.internalError().send("The health is bad"); } else { response.ok().send("The health is good"); } } }
protected void serveSpecification(RouteContext ctx, String specificationName) { String extension = Files.getFileExtension(specificationName); if (extension.isEmpty()) { // default to json specificationName += ".json"; } final String finalDocumentName = specificationName; final String finalExtension = Files.getFileExtension(finalDocumentName); String document = specifications.get(finalDocumentName.toLowerCase()); if (document == null) { ctx.getResponse().notFound(); } else { ctx.getResponse().ok(); httpCacheToolkit.addEtag(ctx, startTime); if ("json".equals(finalExtension)) { ctx.json(); } else if ("yaml".equals(finalExtension)) { ctx.yaml(); } else { ctx.text(); } if (HttpMethod.GET.equals(ctx.getRequestMethod())) { ctx.getResponse().send(document); } } }
@Override public void handle(RouteContext routeContext) { Response response = routeContext.getResponse().noCache().text(); if (threadDump != null) { threadDump.dump(response.getOutputStream()); } else { response.internalError().send("Sorry your runtime environment does not allow to dump threads"); } }