@Override public RouteContext negotiateContentType() { response.contentType(request); return this; }
@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"); } } }
/** * Gets the character encoding of the response. * * @return the character encoding */ public String getCharacterEncoding() { return getHttpServletResponse().getCharacterEncoding(); }
/** * Writes the string content directly to the response. * <p>This method commits the response.</p> * * @param content */ public void send(CharSequence content) { checkCommitted(); commit(content); }
/** * 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)); }
@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); } }
context.getResponse().ok().contentType(TEXT_XML).contentLength(result.length); try (OutputStream output = context.getResponse().getOutputStream()) { output.write(result); output.flush(); context.getResponse().internalError().commit();
errorHandler.handle(HttpServletResponse.SC_NOT_FOUND, routeContext); ROUTE_CONTEXT_THREAD_LOCAL.remove(); log.debug("Returned status code {} for {} '{}' (IGNORED)", response.getStatus(), requestMethod, requestPath); if (!response.isCommitted()) { if (response.getStatus() == 0) { log.debug("Status code not set for {} '{}'", requestMethod, requestPath); response.notFound(); if (response.getStatus() >= HttpServletResponse.SC_BAD_REQUEST) { errorHandler.handle(response.getStatus(), routeContext); } else { response.commit(); } finally { routeContext.runFinallyRoutes(); log.debug("Returned status code {} for {} '{}'", response.getStatus(), requestMethod, requestPath); ROUTE_CONTEXT_THREAD_LOCAL.remove();
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) { } } }
public PrintWriter getWriter() { checkCommitted(); finalizeResponse(); if (getContentType() == null) { contentType(HttpConstants.ContentType.TEXT_HTML); } try { return httpServletResponse.getWriter(); } catch (IOException 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); } }
@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"); } }
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); } } }
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()); } }
@Override public void handle(RouteContext routeContext) { Map<String, String> settingsMap = settingsToMap(routeContext.getSettings()); Response response = routeContext.getResponse().noCache().text(); try (BufferedWriter writer = new BufferedWriter(response.getWriter())) { writeSettings(settingsMap, writer); writer.flush(); } catch (IOException e) { log.error(e.getMessage(), 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); } }
private void finalizeResponse() { // add headers for (Map.Entry<String, String> header : getHeaderMap().entrySet()) { httpServletResponse.setHeader(header.getKey(), header.getValue()); } // add cookies for (Cookie cookie : getCookies()) { httpServletResponse.addCookie(cookie); } // set status to OK if it's not set if (getStatus() == 0 || getStatus() == Integer.MAX_VALUE) { ok(); } // call finalize listeners if ((finalizeListeners != null) && !finalizeListeners.isEmpty()) { finalizeListeners.onFinalize(this); } }
String contentType = routeContext.getResponse().getContentType(); contentType = routeContext.getResponse().getContentType(); 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);