/** * Controls the chunked transfer encoding. * * @param chunked * @return the response */ public Response chunked(boolean chunked) { checkCommitted(); this.chunked = chunked; return this; }
/** * Sets the content length of the response. * * @param length * @return the response */ public Response contentLength(long length) { checkCommitted(); httpServletResponse.setContentLength((int) length); return this; }
/** * Sets the status code of the response. * * @param status * @return the response */ public Response status(int status) { checkCommitted(); httpServletResponse.setStatus(status); this.status = status; return this; }
/** * Sets the content type of the response. * * @param contentType * @return the response */ public Response contentType(String contentType) { checkCommitted(); header(HttpConstants.Header.CONTENT_TYPE, contentType); httpServletResponse.setContentType(contentType); return this; }
/** * 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); }
/** * Sets the character encoding of the response. * * @param charset * @return the response */ public Response characterEncoding(String charset) { checkCommitted(); getHttpServletResponse().setCharacterEncoding(charset); return this; }
/** * Sets a header. * * @param name * @param value * @return the response */ public Response header(String name, String value) { checkCommitted(); getHeaderMap().put(name, value); return this; }
/** * This method resets the response. * Clears any data that exists in the buffer as well as the status code, cookies, locals and headers. * In the end, {@link HttpServletResponse#reset()} is called. */ public void reset() { checkCommitted(); // reset all headers headers = new HashMap<>(); // reset all cookies cookies = new HashMap<>(); // reset all locales locals = new HashMap<>(); // set status to 0 or INT MAX status = 0; try { httpServletResponse.reset(); } catch (Exception e) { throw new PippoRuntimeException(e); } }
private void addCookie(Cookie cookie) { checkCommitted(); if (StringUtils.isNullOrEmpty(cookie.getPath())) { cookie.setPath(StringUtils.addStart(contextPath, "/")); } getCookieMap().put(cookie.getName(), cookie); }
/** * Sets a header. * * @param name * @param value * @return the response */ public Response header(String name, Date value) { checkCommitted(); getHeaderMap().put(name, DateUtils.formatForHttpHeader(value)); return this; }
/** * Replaces '{}' in the format string with the supplied arguments and * writes the string content directly to the response. * <p>This method commits the response.</p> * * @param format * @param args */ public void send(String format, Object... args) { checkCommitted(); commit(StringUtils.format(format, args)); }
/** * Redirect the browser to a location which may be... * <ul> * <li>relative to the current request * <li>relative to the servlet container root (if location starts with '/') * <li>an absolute url * </ul> * If you want a context-relative redirect, use the * {@link ro.pippo.core.Response#redirectToContextPath(String)} method. * If you want an application-relative redirect, use the * {@link ro.pippo.core.Response#redirectToApplicationPath(String)} method. * <p>This method commits the response.</p> * * @param location * Where to redirect */ public void redirect(String location) { checkCommitted(); finalizeResponse(); try { httpServletResponse.sendRedirect(location); } catch (IOException e) { throw new PippoRuntimeException(e); } }
/** * Sets this response as not cacheable. * * @return the response */ public Response noCache() { checkCommitted(); // no-cache headers for HTTP/1.1 header(HttpConstants.Header.CACHE_CONTROL, "no-store, no-cache, must-revalidate"); // no-cache headers for HTTP/1.1 (IE) header(HttpConstants.Header.CACHE_CONTROL, "post-check=0, pre-check=0"); // no-cache headers for HTTP/1.0 header(HttpConstants.Header.PRAGMA, "no-cache"); // set the expires to past httpServletResponse.setDateHeader("Expires", 0); return this; }
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); } }
/** * A permanent (3XX status code) redirect. * <p>This method commits the response.</p> * * @param location * @param statusCode */ public void redirect(String location, int statusCode) { checkCommitted(); finalizeResponse(); status(statusCode); header(HttpConstants.Header.LOCATION, location); header(HttpConstants.Header.CONNECTION, "close"); try { httpServletResponse.sendError(statusCode); } catch (IOException 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); } }
/** * 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); } }