List of usage examples for javax.servlet.http HttpServletResponse getOutputStream
public ServletOutputStream getOutputStream() throws IOException;
From source file:com.stratelia.webactiv.servlets.OnlineFileServer.java
/** * This method writes the result of the preview action. * @param res - The HttpServletResponse where the html code is write * @param htmlFilePath - the canonical path of the html document generated by the parser tools. if * this String is null that an exception had been catched the html document generated is empty !! * also, we display a warning html page// ww w. j av a 2s . co m */ private void display(HttpServletResponse res, String htmlFilePath) throws IOException { OutputStream out2 = res.getOutputStream(); int read; BufferedInputStream input = null; // for the html document generated SilverTrace.info("peasUtil", "OnlineFileServer.display()", "root.MSG_GEN_ENTER_METHOD", " htmlFilePath " + htmlFilePath); try { input = new BufferedInputStream(new FileInputStream(htmlFilePath)); read = input.read(); SilverTrace.info("peasUtil", "OnlineFileServer.display()", "root.MSG_GEN_ENTER_METHOD", " BufferedInputStream read " + read); if (read == -1) { displayWarningHtmlCode(res); } else { while (read != -1) { out2.write(read); // writes bytes into the response read = input.read(); } } } catch (Exception e) { SilverTrace.warn("peasUtil", "OnlineFileServer.doPost", "root.EX_CANT_READ_FILE", "file name=" + htmlFilePath); displayWarningHtmlCode(res); } finally { SilverTrace.info("peasUtil", "OnlineFileServer.display()", "", " finally "); // we must close the in and out streams try { if (input != null) { input.close(); } out2.close(); } catch (Exception e) { SilverTrace.warn("peasUtil", "OnlineFileServer.display", "root.EX_CANT_READ_FILE", "close failed"); } } }
From source file:fr.putnami.pwt.plugin.ajaxbot.controller.SiteMapController.java
@RequestMapping(value = "/sitemap.txt", method = RequestMethod.GET) public void welcomePage(HttpServletResponse response) { try {/*from www. ja v a2s. com*/ InputStream is = new FileInputStream(sitemap); response.setContentType("text/plain"); response.setContentLength((int) sitemap.length()); IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (IOException ex) { throw new RuntimeException("IOError writing file to output stream", ex); } }
From source file:annis.gui.servlets.ResourceServlet.java
@Override @SuppressWarnings("unchecked") public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { OutputStream outStream = response.getOutputStream(); String completePath = request.getPathInfo(); if (completePath == null) { response.sendError(404, "must provide a valid and existing path with a vistype"); return;//from w w w .j a va2s .co m } // remove trailing / completePath = completePath.substring(1); String[] pathComponents = completePath.split("/"); String vistype = pathComponents[0]; if (pathComponents.length < 2) { response.sendError(404, "must provide a valid and existing path"); return; } String path = StringUtils.join(Arrays.copyOfRange(pathComponents, 1, pathComponents.length), "/"); // get the visualizer for this vistype ResourcePlugin vis = resourceRegistry.get(vistype); if (vis == null) { response.sendError(500, "There is no resource with the short name " + vistype); } else if (path.endsWith(".class")) { response.sendError(403, "illegal class path access"); } else { URL resource = vis.getClass().getResource(path); if (resource == null) { response.sendError(404, path + " not found"); } else { // check if it is new URLConnection resourceConnection = resource.openConnection(); long resourceLastModified = resourceConnection.getLastModified(); long requestLastModified = request.getDateHeader("If-Modified-Since"); if (requestLastModified != -1 && resourceLastModified <= requestLastModified) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } else { response.addDateHeader("Last-Modified", resourceLastModified); if ("localhost".equals(request.getServerName())) { // does always expire right now response.addDateHeader("Expires", new Date().getTime()); } else { // expires in one minute per default response.addDateHeader("Expires", new Date().getTime() + 60000); } // not in cache, stream out String mimeType = getServletContext().getMimeType(path); response.setContentType(mimeType); if (mimeType.startsWith("text/")) { response.setCharacterEncoding("UTF-8"); } OutputStream bufferedOut = new BufferedOutputStream(outStream); InputStream resourceInStream = new BufferedInputStream(resource.openStream()); try { int v = -1; while ((v = resourceInStream.read()) != -1) { bufferedOut.write(v); } } finally { resourceInStream.close(); bufferedOut.flush(); outStream.flush(); } } } } }
From source file:com.meltmedia.cadmium.servlets.ErrorPageFilterSelectionTest.java
/** * Calls doFilter on the ErrorPageFilter, capturing the output. The output is compared to the expected output. * /*from w w w. j ava 2s .c om*/ * @throws IOException if there is an error in the test. * @throws ServletException if there is an error in the test. */ @Test public void testFilterChainError() throws IOException, ServletException { final ByteArrayOutputStream resultWriter = new ByteArrayOutputStream(); // Return the result writer for output. HttpServletResponse response = mock(HttpServletResponse.class); when(response.getOutputStream()).thenReturn(new ServletOutputStream() { @Override public void write(int i) throws IOException { resultWriter.write(i); } }); HttpServletRequest request = mock(HttpServletRequest.class); when(request.getRequestURI()).thenReturn(pathInfo); // act filter.doFilter(request, response, chain); // assert assertEquals("The wrong error content was returned.", expectedContent, resultWriter.toString()); }
From source file:com.yunmel.syncretic.utils.io.IOUtils.java
/** * /* w w w .j a v a 2 s. c om*/ * * @param filePath * @param fileName ?? * @param inline ?? * @throws Exception */ public static void downloadFile(HttpServletResponse response, File file, String fileName, boolean inline) throws Exception { OutputStream outp = null; FileInputStream br = null; int len = 0; try { br = new FileInputStream(file); response.reset(); outp = response.getOutputStream(); outp.flush(); response.setContentType("application/octet-stream"); response.setContentLength((int) file.length()); String header = (inline ? "inline" : "attachment") + ";filename=" + new String(fileName.getBytes(), "ISO8859-1"); response.addHeader("Content-Disposition", header); byte[] buf = new byte[1024]; while ((len = br.read(buf)) != -1) { outp.write(buf, 0, len); } outp.flush(); outp.close(); } finally { if (br != null) { if (0 == br.available()) { br.close(); } } } }
From source file:de.betterform.agent.web.servlet.XFormsErrorServlet.java
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { new BufferedWriter(new OutputStreamWriter(response.getOutputStream())).write(getHTML(request)); response.getOutputStream().flush();/* w ww .j a v a 2 s.com*/ response.getOutputStream().close(); }
From source file:controllers.FreeOptionController.java
@RequestMapping("/getXls") public void getXls(Map<String, Object> model, HttpServletResponse response) throws Exception { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=Options.xls"); freeOptionService.getXls().write(response.getOutputStream()); }
From source file:org.openxdata.server.servlet.DataImportServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out = response.getOutputStream(); try {//from w ww . java2 s .c o m // authenticate user User user = getUser(request.getHeader("Authorization")); if (user != null) { log.info("authenticated user:"); // check msisdn String msisdn = request.getParameter("msisdn"); if (msisdn != null && !msisdn.equals("")) { // if an msisdn is sent, then we retrieve the user with that phone number authenticateUserBasedOnMsisd(msisdn); } // can be empty or null, then the default is used. this parameter is a key in the settings table indicating the classname of the serializer to use String serializer = request.getParameter("serializer"); // input stream // first byte contains number of forms (x) // followed by x number of UTF strings (use writeUTF method in DataOutput) formDownloadService.submitForms(request.getInputStream(), out, serializer); } else { response.setHeader("WWW-Authenticate", "BASIC realm=\"openxdata\""); response.sendError(HttpServletResponse.SC_UNAUTHORIZED); } } catch (UserNotFoundException userNotFound) { out.println("Invalid msisdn"); response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } catch (Exception e) { log.error("Could not import data", e); out.println(e.getMessage()); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { out.close(); } }
From source file:org.unitedinternet.cosmo.dav.impl.DavCard.java
public void writeBody(final HttpServletResponse response) throws IOException { Item content = getItem();/*w ww. j a v a 2 s . c o m*/ final byte[] calendar = content.getCalendar().getBytes(StandardCharsets.UTF_8); IOUtils.copy(new ByteArrayInputStream(calendar), response.getOutputStream()); }
From source file:com.webpagebytes.cms.controllers.ExportImportController.java
public void exportContent(HttpServletRequest request, HttpServletResponse response, String requestUri) throws WPBException { try {/*w w w . j a v a2 s . co m*/ response.setContentType("application/zip"); storageExporter.exportToZip(response.getOutputStream()); } catch (IOException e) { Map<String, String> errors = new HashMap<String, String>(); errors.put("", WPBErrors.WB_CANNOT_EXPORT_PROJECT); httpServletToolbox.writeBodyResponseAsJson(response, jsonObjectConverter.JSONObjectFromMap(null), errors); } }