List of usage examples for javax.servlet.http HttpServletResponse setIntHeader
public void setIntHeader(String name, int value);
From source file:com.codeabovelab.dm.gateway.proxy.common.HttpProxy.java
private boolean doResponseRedirectOrNotModifiedLogic(HttpProxyContext proxyContext, HttpResponse proxyResponse, int statusCode) throws ServletException, IOException { HttpServletResponse servletResponse = proxyContext.getResponse(); // Check if the proxy response is a redirect // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) { Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION); if (locationHeader == null) { throw new ServletException("Received status code: " + statusCode + " but no " + HttpHeaders.LOCATION + " header was found in the response"); }//from ww w. ja v a 2s .co m // Modify the redirect to go to this proxy servlet rather that the proxied host String locStr = rewriteUrlFromResponse(proxyContext, locationHeader.getValue()); servletResponse.sendRedirect(locStr); return true; } // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) { servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0); servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } return false; }
From source file:com.cubusmail.gwtui.server.services.RetrieveAttachmentServlet.java
@Override public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { WebApplicationContext context = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getSession().getServletContext()); try {/*from www.j av a2s . c o m*/ String messageId = request.getParameter("messageId"); String attachmentIndex = request.getParameter("attachmentIndex"); boolean view = "1".equals(request.getParameter("view")); if (messageId != null) { IMailbox mailbox = SessionManager.get().getMailbox(); Message msg = mailbox.getCurrentFolder().getMessageById(Long.parseLong(messageId)); List<MimePart> attachmentList = MessageUtils.attachmentsFromPart(msg); int index = Integer.valueOf(attachmentIndex); MimePart retrievePart = attachmentList.get(index); ContentType contentType = new ContentType(retrievePart.getContentType()); String fileName = retrievePart.getFileName(); if (StringUtils.isEmpty(fileName)) { fileName = context.getMessage("message.unknown.attachment", null, SessionManager.get().getLocale()); } StringBuffer contentDisposition = new StringBuffer(); if (!view) { contentDisposition.append("attachment; filename=\""); contentDisposition.append(fileName).append("\""); } response.setHeader("cache-control", "no-store"); response.setHeader("pragma", "no-cache"); response.setIntHeader("max-age", 0); response.setIntHeader("expires", 0); if (!StringUtils.isEmpty(contentDisposition.toString())) { response.setHeader("Content-disposition", contentDisposition.toString()); } response.setContentType(contentType.getBaseType()); // response.setContentLength( // MessageUtils.calculateSizeFromPart( retrievePart ) ); BufferedInputStream bufInputStream = new BufferedInputStream(retrievePart.getInputStream()); OutputStream outputStream = response.getOutputStream(); byte[] inBuf = new byte[1024]; int len = 0; int total = 0; while ((len = bufInputStream.read(inBuf)) > 0) { outputStream.write(inBuf, 0, len); total += len; } bufInputStream.close(); outputStream.flush(); outputStream.close(); } } catch (Exception ex) { logger.error(ex.getMessage(), ex); } }
From source file:com.cubusmail.server.services.RetrieveAttachmentServlet.java
@Override public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { WebApplicationContext context = WebApplicationContextUtils .getRequiredWebApplicationContext(request.getSession().getServletContext()); try {/*from ww w . ja v a 2 s . c o m*/ String messageId = request.getParameter("messageId"); String attachmentIndex = request.getParameter("attachmentIndex"); boolean view = "1".equals(request.getParameter("view")); if (messageId != null) { IMailbox mailbox = SessionManager.get().getMailbox(); Message msg = mailbox.getCurrentFolder().getMessageById(Long.parseLong(messageId)); List<MimePart> attachmentList = MessageUtils.attachmentsFromPart(msg); int index = Integer.valueOf(attachmentIndex); MimePart retrievePart = attachmentList.get(index); ContentType contentType = new ContentType(retrievePart.getContentType()); String fileName = retrievePart.getFileName(); if (StringUtils.isEmpty(fileName)) { fileName = context.getMessage("message.unknown.attachment", null, SessionManager.get().getLocale()); } StringBuffer contentDisposition = new StringBuffer(); if (!view) { contentDisposition.append("attachment; filename=\""); contentDisposition.append(fileName).append("\""); } response.setHeader("cache-control", "no-store"); response.setHeader("pragma", "no-cache"); response.setIntHeader("max-age", 0); response.setIntHeader("expires", 0); if (!StringUtils.isEmpty(contentDisposition.toString())) { response.setHeader("Content-disposition", contentDisposition.toString()); } response.setContentType(contentType.getBaseType()); // response.setContentLength( // MessageUtils.calculateSizeFromPart( retrievePart ) ); BufferedInputStream bufInputStream = new BufferedInputStream(retrievePart.getInputStream()); OutputStream outputStream = response.getOutputStream(); byte[] inBuf = new byte[1024]; int len = 0; int total = 0; while ((len = bufInputStream.read(inBuf)) > 0) { outputStream.write(inBuf, 0, len); total += len; } bufInputStream.close(); outputStream.flush(); outputStream.close(); } } catch (Exception ex) { log.error(ex.getMessage(), ex); } }
From source file:be.milieuinfo.core.proxy.controller.ProxyServlet.java
private boolean doResponseRedirectOrNotModifiedLogic(HttpServletRequest servletRequest, HttpServletResponse servletResponse, HttpResponse proxyResponse, int statusCode) throws ServletException, IOException { // Check if the proxy response is a redirect // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) { Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION); if (locationHeader == null) { throw new ServletException("Received status code: " + statusCode + " but no " + HttpHeaders.LOCATION + " header was found in the response"); }//from w w w . j av a 2s .c o m // Modify the redirect to go to this proxy servlet rather that the proxied host String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue()); servletResponse.sendRedirect(locStr); return true; } // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) { servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0); servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } return false; }
From source file:uk.ac.ebi.phenotype.web.proxy.ExternalUrlConfiguratbleProxyServlet.java
private boolean doResponseRedirectOrNotModifiedLogic(HttpServletRequest servletRequest, HttpServletResponse servletResponse, HttpResponse proxyResponse, int statusCode) throws ServletException, IOException { // Check if the proxy response is a redirect // The following code is adapted from // org.tigris.noodle.filters.CheckForRedirect if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) { Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION); if (locationHeader == null) { throw new ServletException("Recieved status code: " + statusCode + " but no " + HttpHeaders.LOCATION + " header was found in the response"); }/*from w w w.j av a 2s .com*/ // Modify the redirect to go to this proxy servlet rather that the // proxied host String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue()); servletResponse.sendRedirect(locStr); return true; } // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) { servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0); servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } return false; }
From source file:org.fireflow.webdesigner.servlet.handlers.GetProcessDefsHandler.java
protected void outputXmlWithinHtml(String processXml, HttpServletResponse resp, HttpServletRequest req) throws ServletException, IOException { HttpServletRequest request = req;/*from w w w. ja v a 2 s. c o m*/ String contextPath = request.getContextPath(); if (contextPath == null) contextPath = ""; if (contextPath.endsWith("/")) { contextPath = contextPath.substring(0, contextPath.length() - 1); } StringBuffer sbuf = new StringBuffer(); sbuf.append("<!DOCTYPE html>\n").append("<html>\n").append("<head>\n").append("<script src=\"") .append(contextPath).append(ClientWidgetBase.FIREFLOW_RESOURCE_SERVLET) .append("/org/fireflow/webdesigner/resources/google-code-prettify/prettify.js\"></script>\n") .append("<link rel=\"stylesheet\" href=\"").append(contextPath) .append(ClientWidgetBase.FIREFLOW_RESOURCE_SERVLET) .append("/org/fireflow/webdesigner/resources/google-code-prettify/prettify.css\"/>\n") .append("<style>\n").append("body { margin: 0; padding: 0;font-size:12px }\n") .append("pre { margin: 0 }\n").append("</style>\n").append("</head>\n") .append("<body onload=\"prettyPrint();\">\n").append("<pre class=\"prettyprint lang-xml\">"); String tmp = StringEscapeUtils.escapeXml(processXml); sbuf.append(tmp); sbuf.append("</pre>\n").append("</body>\n").append("</html>"); // contentType String encoding = Utils.findXmlCharset(processXml); resp.setContentType("text/html"); resp.setCharacterEncoding(encoding); // ??? resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setIntHeader("Expires", -1); byte[] byteArr = sbuf.toString().getBytes(encoding); OutputStream outStream = resp.getOutputStream(); outStream.write(byteArr); }
From source file:org.ajax4jsf.webapp.CacheContent.java
/** * Send saved headers to http responce.// w w w. j ava2s. c o m * * @param response */ public void sendHeaders(HttpServletResponse response) { // set real content-length. // / if (null != content) { int realContentLength = 0; if (filledOutputStream) { if (null != content && content.length > 0) { realContentLength = content.length; } else if (null != outputStream && outputStream.getLength() > 0) { realContentLength = outputStream.getLength(); } } // TODO - calculate content-lenght for writer ? if (realContentLength <= 0 && contentLength != null) { realContentLength = contentLength.intValue(); } for (Iterator<Entry<String, Object>> iter = headers.entrySet().iterator(); iter.hasNext();) { Entry<String, Object> element = iter.next(); String header = (String) element.getKey(); Object headerValue = element.getValue(); try { if (headerValue instanceof Long) { Long time = (Long) headerValue; response.setDateHeader(header, time.longValue()); } else if (headerValue instanceof Integer) { Integer value = (Integer) headerValue; // Check real content length. if ("Content-Length".equals(header)) { if (realContentLength <= 0) { realContentLength = value.intValue(); } else { //do nothing } } else { response.setIntHeader(header, value.intValue()); } } else { // Don't send "chunked" transfer-encoding type with real content-length if (!(realContentLength > 0 && "Transfer-Encoding".equals(header) && "chunked".equals(headerValue))) { response.setHeader(header, (String) headerValue); } } } catch (Exception e) { _log.error("Error set response header " + header + "for value " + headerValue, e); } if (realContentLength > 0) { response.setContentLength(realContentLength); } if (null != contentType) { response.setContentType(this.contentType); } } }
From source file:org.ocpsoft.rewrite.servlet.config.proxy.ProxyServlet.java
protected boolean doResponseRedirectOrNotModifiedLogic(HttpServletRequest servletRequest, HttpServletResponse servletResponse, HttpResponse proxyResponse, int statusCode) throws ServletException, IOException { /*//from ww w .j a v a 2 s .c o m * Check if the proxy response is a redirect. The following code is adapted from * org.tigris.noodle.filters.CheckForRedirect */ if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */ && statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) { Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION); if (locationHeader == null) { throw new ServletException("Received status code: " + statusCode + " but no " + HttpHeaders.LOCATION + " header was found in the response"); } /* * Modify the redirect to go to this proxy servlet rather that the proxied host */ String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue()); servletResponse.sendRedirect(locStr); return true; } /* * 304 needs special handling. See: http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 . We get a 304 * whenever passed an 'If-Modified-Since' header and the data on disk has not changed; server responds w/ a 304 * saying I'm not going to send the body because the file has not changed. */ if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) { servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0); servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } return false; }
From source file:org.realityforge.proxy_servlet.AbstractProxyServlet.java
private boolean doResponseRedirectOrNotModifiedLogic(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse, final HttpResponse proxyResponse, final int statusCode) throws ServletException, IOException { // Check if the proxy response is a redirect // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES && /* 300 */ statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */ ) { final Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION); if (null == locationHeader) { final String message = "Received status code: " + statusCode + " but no " + HttpHeaders.LOCATION + " header was found in the response"; throw new ServletException(message); }// w w w . ja va 2s. c om // Modify the redirect to go to this proxy servlet rather that the proxied host final String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue()); servletResponse.sendRedirect(locStr); return true; } // 304 needs special handling. See: // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 // We get a 304 whenever passed an 'If-Modified-Since' // header and the data on disk has not changed; server // responds w/ a 304 saying I'm not going to send the // body because the file has not changed. if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) { servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0); servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return true; } return false; }