@Override public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException { HttpServletRequest httpReq = (HttpServletRequest) req; assertEquals("Invalid HTTP method", method, httpReq.getMethod()); resp.setContentLength(0); ((HttpServletResponse) resp).setStatus(200); }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (null != request.getAttribute("FLAG")) { chain.doFilter(request, response); return; } request.setAttribute("FLAG", FLAG); String encoding = this.encoding; HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; String resourcePath = req.getServletPath(); RequestContext requestContext = new RequestContext(config.getServletContext(), req, resourcePath); request.setAttribute("context", requestContext); for (Decorator decorator : decorators) { int returned = decorator.parseRequest(req, res, requestContext); switch (returned) { case Decorator.OK: // do nothing break; case Decorator.REDIRECT: res.sendRedirect(requestContext.getResourcePath()); return; case Decorator.MOVED_PERMANENTLY: res.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); res.setHeader("Location", requestContext.getResourcePath()); return; default: // do nothing } } if (pattern.matcher(requestContext.getResourcePath()).matches()) { WebContent content = new WebContent(); if (requestContext.getResourcePath().startsWith("/WEB-INF/admin/")) { encoding = "UTF-8"; } ByteArrayResponseWrapper wrapper = new ByteArrayResponseWrapper(res); request.getRequestDispatcher(requestContext.getResourcePath()).include(req, wrapper); wrapper.finish(); content.setWholeContent(wrapper.getString(encoding)); response.setContentType(wrapper.getContentType()); content.finish(); for (Decorator decorator : decorators) { decorator.decorate(requestContext, content, content); content.finish(); } byte[] theContent = content.toString().getBytes(encoding); response.setContentLength(theContent.length); response.getOutputStream().write(theContent); return; } request.getRequestDispatcher(requestContext.getResourcePath()).forward(req, res); }
/** * @param pServletRequest parameter * @param pServletResponse parameter * @throws IOException exception * @throws ServletException exception */ @Override public void handleService(ServletRequest pServletRequest, ServletResponse pServletResponse) throws IOException, ServletException { final ByteArrayOutputStream pdf = new ByteArrayOutputStream(); final String deliveryId = pServletRequest.getParameter(DELIVERY_ID_PARAM); RepositoryItem orderBOItem = mOrderManager.getFactureBO(deliveryId); OutputStream out = null; if (orderBOItem != null) { RepositoryItem orderFOItem = mOrderManager.getOrderFO(orderBOItem); String profileId = (String) orderFOItem.getPropertyValue(CastoConstantesOrders.ORDER_PROPERTY_PROFILEID); String currProfileId = getProfileServices().getCurrentProfileId(); if (!currProfileId.equalsIgnoreCase(profileId)) { if (isLoggingError()) { logError( "Facture with id=" + deliveryId + " doesn't belong to profile with id=" + currProfileId); } return; } Map params = new HashMap(); params.put( "url", pServletRequest.getScheme() + "://" + pServletRequest.getServerName() + ":" + pServletRequest.getServerPort()); try { PrintPdfHelper.getInstance().generateInvoicePdf(pdf, orderBOItem, params, mCountryList); // Set header pServletResponse.setContentType(CONTENT_TYPE_PDF); pServletResponse.setContentLength(pdf.size()); out = pServletResponse.getOutputStream(); pdf.writeTo(out); out.flush(); } catch (IOException e) { logError(e); } finally { if (out != null) { out.close(); } } } else { logError("Facture with id=" + deliveryId + " was not found."); } // end if-else }
@Override public void onWritePossible() throws IOException { if (!written) { written = true; response.setContentLength(5); servletOutputStream.write("data\n".getBytes()); } if (servletOutputStream.isReady()) { asyncContext.complete(); } }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; if (!isIncluded(httpRequest) && acceptsGZipEncoding(httpRequest) && !response.isCommitted()) { // Client accepts zipped content if (log.isTraceEnabled()) { log.trace("{} Written with gzip compression", httpRequest.getRequestURL()); } // Create a gzip stream final ByteArrayOutputStream compressed = new ByteArrayOutputStream(); final GZIPOutputStream gzout = new GZIPOutputStream(compressed); // Handle the request final GZipServletResponseWrapper wrapper = new GZipServletResponseWrapper(httpResponse, gzout); wrapper.setDisableFlushBuffer(true); chain.doFilter(request, wrapper); wrapper.flush(); gzout.close(); // double check one more time before writing out // repsonse might have been committed due to error if (response.isCommitted()) { return; } // return on these special cases when content is empty or unchanged switch (wrapper.getStatus()) { case HttpServletResponse.SC_NO_CONTENT: case HttpServletResponse.SC_RESET_CONTENT: case HttpServletResponse.SC_NOT_MODIFIED: return; default: } // Saneness checks byte[] compressedBytes = compressed.toByteArray(); boolean shouldGzippedBodyBeZero = GZipResponseUtil.shouldGzippedBodyBeZero(compressedBytes, httpRequest); boolean shouldBodyBeZero = GZipResponseUtil.shouldBodyBeZero(httpRequest, wrapper.getStatus()); if (shouldGzippedBodyBeZero || shouldBodyBeZero) { // No reason to add GZIP headers or write body if no content was written or status code // specifies no // content response.setContentLength(0); return; } // Write the zipped body GZipResponseUtil.addGzipHeader(httpResponse); response.setContentLength(compressedBytes.length); response.getOutputStream().write(compressedBytes); } else { // Client does not accept zipped content - don't bother zipping if (log.isTraceEnabled()) { log.trace( "{} Written without gzip compression because the request does not accept gzip", httpRequest.getRequestURL()); } chain.doFilter(request, response); } }
public void setContentLength(int length) { response.setContentLength(length); }
public void service(ServletRequest request, ServletResponse response) { int font; pdflib p = null; int i, blockcontainer, page; String infile = "boilerplate.pdf"; /* This is where font/image/PDF input files live. Adjust as necessary. * * Note that this directory must also contain the LuciduxSans font * outline and metrics files. */ String searchpath = "../data"; String[][] data = { {"name", "Victor Kraxi"}, {"business.title", "Chief Paper Officer"}, {"business.address.line1", "17, Aviation Road"}, {"business.address.city", "Paperfield"}, {"business.telephone.voice", "phone +1 234 567-89"}, {"business.telephone.fax", "fax +1 234 567-98"}, {"business.email", "*****@*****.**"}, {"business.homepage", "www.kraxi.com"}, }; byte[] buf; ServletOutputStream out; try { p = new pdflib(); // Generate a PDF in memory; insert a file name to create PDF on disk if (p.begin_document("", "") == -1) { throw new Exception("Error: " + p.get_errmsg()); } /*Set the search path for fonts and PDF files */ p.set_parameter("SearchPath", searchpath); p.set_info("Creator", "businesscard.java"); p.set_info("Author", "Thomas Merz"); p.set_info("Title", "PDFlib block processing sample (Java)"); blockcontainer = p.open_pdi(infile, "", 0); if (blockcontainer == -1) { throw new Exception("Error: " + p.get_errmsg()); } page = p.open_pdi_page(blockcontainer, 1, ""); if (page == -1) { throw new Exception("Error: " + p.get_errmsg()); } p.begin_page_ext(20, 20, ""); // dummy page size // This will adjust the page size to the block container's size. p.fit_pdi_page(page, 0, 0, "adjustpage"); // Fill all text blocks with dynamic data for (i = 0; i < (int) data.length; i++) { if (p.fill_textblock(page, data[i][0], data[i][1], "embedding encoding=unicode") == -1) { System.err.println("Warning: " + p.get_errmsg()); } } p.end_page_ext(""); // close page p.close_pdi_page(page); p.end_document(""); // close PDF document p.close_pdi(blockcontainer); buf = p.get_buffer(); response.setContentType("application/pdf"); response.setContentLength(buf.length); out = response.getOutputStream(); out.write(buf); out.close(); } catch (PDFlibException e) { System.err.print("PDFlib exception occurred in businesscard sample:\n"); System.err.print( "[" + e.get_errnum() + "] " + e.get_apiname() + ": " + e.get_errmsg() + "\n"); } catch (Exception e) { System.err.println(e.getMessage()); } finally { if (p != null) { p.delete(); /* delete the PDFlib object */ } } }
/** @see javax.faces.context.ExternalContext#setResponseContentLength(int) */ @Override public void setResponseContentLength(int length) { response.setContentLength(length); }