List of usage examples for javax.servlet.http HttpServletResponse getOutputStream
public ServletOutputStream getOutputStream() throws IOException;
From source file:com.urservices.urerp.ecole.views.PdfReportView.java
@Override protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter pdfWriter, HttpServletRequest request, HttpServletResponse response) throws Exception { try {/*w w w . j a v a 2 s . c o m*/ PdfReader pdfTemplate = new PdfReader((InputStream) model.get("template")); PdfStamper stamper = new PdfStamper(pdfTemplate, response.getOutputStream()); stamper.setFormFlattening(true); stamper.getAcroFields().setField("txtNom", "Kamdoum"); stamper.getAcroFields().setField("txtPrenom", "Samuel"); stamper.close(); pdfTemplate.close(); System.out.println("This PDF has " + pdfTemplate.getNumberOfPages() + " pages."); } catch (Exception e) { System.out.println("PdfReportView.buildPdfDocument() " + e.getMessage()); } }
From source file:com.dreamwork.web.FrontJsonController.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); ServletOutputStream out = response.getOutputStream(); String message = "Not supported"; out.write(message.getBytes("UTF-8")); out.flush();//ww w .j a v a 2 s . c om }
From source file:org.apache.hadoop.gateway.hdfs.dispatch.WebHdfsHaHttpClientDispatchTest.java
@Test public void testConnectivityFailover() throws Exception { String serviceName = "WEBHDFS"; HaDescriptor descriptor = HaDescriptorFactory.createDescriptor(); descriptor.addServiceConfig(//from w w w . j a v a 2s . co m HaDescriptorFactory.createServiceConfig(serviceName, "true", "1", "1000", "2", "1000")); HaProvider provider = new DefaultHaProvider(descriptor); URI uri1 = new URI("http://unreachable-host"); URI uri2 = new URI("http://reachable-host"); ArrayList<String> urlList = new ArrayList<String>(); urlList.add(uri1.toString()); urlList.add(uri2.toString()); provider.addHaService(serviceName, urlList); FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class); ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class); EasyMock.expect(filterConfig.getInitParameter(WebHdfsHaHttpClientDispatch.RESOURCE_ROLE_ATTRIBUTE)) .andReturn(serviceName).anyTimes(); EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes(); EasyMock.expect(servletContext.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)) .andReturn(provider).anyTimes(); BasicHttpParams params = new BasicHttpParams(); HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpRequestBase.class); EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes(); EasyMock.expect(outboundRequest.getURI()).andReturn(uri1).anyTimes(); EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes(); HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class); EasyMock.expect(inboundRequest.getRequestURL()).andReturn(new StringBuffer(uri2.toString())).once(); EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(0)) .once(); EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(1)) .once(); HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class); EasyMock.expect(outboundResponse.getOutputStream()).andAnswer(new IAnswer<ServletOutputStream>() { @Override public ServletOutputStream answer() throws Throwable { return new ServletOutputStream() { @Override public void write(int b) throws IOException { throw new IOException("unreachable-host"); } }; } }).once(); EasyMock.replay(filterConfig, servletContext, outboundRequest, inboundRequest, outboundResponse); Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName)); WebHdfsHaHttpClientDispatch dispatch = new WebHdfsHaHttpClientDispatch(); dispatch.init(filterConfig); long startTime = System.currentTimeMillis(); try { dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse); } catch (IOException e) { //this is expected after the failover limit is reached } long elapsedTime = System.currentTimeMillis() - startTime; Assert.assertEquals(uri2.toString(), provider.getActiveURL(serviceName)); //test to make sure the sleep took place Assert.assertTrue(elapsedTime > 1000); }
From source file:com.redoute.datamap.servlet.GenerateGraph.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from ww w. j a v a 2s .c o m * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/png"); ServletOutputStream os = response.getOutputStream(); try { String stream = request.getParameter("stream"); ApplicationContext appContext = WebApplicationContextUtils .getWebApplicationContext(this.getServletContext()); IDatamapService datamapService = appContext.getBean(IDatamapService.class); BufferedImage graph = datamapService.dataImplementedByCriteria("stream", stream); ImageIO.write(graph, "png", os); os.close(); } finally { os.close(); } }
From source file:com.tianjunwei.download.DownLoadController.java
@RequestMapping(value = { "/download" }, method = { RequestMethod.GET }) public void download(HttpServletResponse response) throws IOException { response.setContentType("application/octet-stream"); response.addHeader("Content-Disposition", "attachment; filename=download.txt"); OutputStream outputStream = response.getOutputStream(); outputStream.write("sdsdfs".getBytes()); outputStream.close();// w w w. j a va 2 s. c o m }
From source file:com.indeed.imhotep.web.QueryServlet.java
static void handleError(HttpServletResponse resp, boolean json, Throwable e, boolean status500, boolean isEventStream) throws IOException { if (!(e instanceof Exception || e instanceof OutOfMemoryError)) { throw Throwables.propagate(e); }// w w w . j a v a 2 s. co m // output parse/execute error if (!json) { final ServletOutputStream outputStream = resp.getOutputStream(); final PrintStream printStream = new PrintStream(outputStream); if (isEventStream) { resp.setContentType("text/event-stream"); final String[] stackTrace = Throwables.getStackTraceAsString(e).split("\\n"); printStream.println("event: servererror"); for (String s : stackTrace) { printStream.println("data: " + s); } printStream.println(); } else { resp.setStatus(500); e.printStackTrace(printStream); printStream.close(); } } else { if (status500) { resp.setStatus(500); } // construct a parsed error object to be JSON serialized String clause = ""; int offset = -1; if (e instanceof IQLParseException) { final IQLParseException IQLParseException = (IQLParseException) e; clause = IQLParseException.getClause(); offset = IQLParseException.getOffsetInClause(); } final String stackTrace = Throwables.getStackTraceAsString(Throwables.getRootCause(e)); final ErrorResult error = new ErrorResult(e.getClass().getSimpleName(), e.getMessage(), stackTrace, clause, offset); resp.setContentType("application/json"); final ObjectMapper jsonMapper = new ObjectMapper(); final ServletOutputStream outputStream = resp.getOutputStream(); jsonMapper.defaultPrettyPrintingWriter().writeValue(outputStream, error); outputStream.close(); } }
From source file:fi.okm.mpass.shibboleth.profile.impl.BuildMetaRestResponse.java
/** {@inheritDoc} */ @Override//from w w w. j a v a2s . c o m @Nonnull public Event execute(@Nonnull final RequestContext springRequestContext) { ComponentSupport.ifNotInitializedThrowUninitializedComponentException(this); final HttpServletRequest httpRequest = getHttpServletRequest(); pushHttpResponseProperties(); final HttpServletResponse httpResponse = getHttpServletResponse(); try { final Writer out = new OutputStreamWriter(httpResponse.getOutputStream(), "UTF-8"); if (!HttpMethod.GET.toString().equals(httpRequest.getMethod())) { log.warn("{}: Unsupported method attempted {}", getLogPrefix(), httpRequest.getMethod()); out.append(makeErrorResponse(HttpStatus.SC_METHOD_NOT_ALLOWED, httpRequest.getMethod() + " not allowed", "Only GET is allowed")); } else if (metaDTO != null) { final Gson gson = new Gson(); out.append(gson.toJson(getMetaDTO())); httpResponse.setStatus(HttpStatus.SC_OK); } else { out.append( makeErrorResponse(HttpStatus.SC_NOT_IMPLEMENTED, "Not implemented on the server side", "")); } out.flush(); } catch (IOException e) { log.error("{}: Could not encode the JSON response", getLogPrefix(), e); httpResponse.setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE); return ActionSupport.buildEvent(this, EventIds.IO_ERROR); } return ActionSupport.buildProceedEvent(this); }
From source file:org.apache.hadoop.gateway.hdfs.dispatch.WebHdfsHaDispatchTest.java
@Test public void testConnectivityFailover() throws Exception { String serviceName = "WEBHDFS"; HaDescriptor descriptor = HaDescriptorFactory.createDescriptor(); descriptor.addServiceConfig(/*from w ww.j av a2 s .c om*/ HaDescriptorFactory.createServiceConfig(serviceName, "true", "1", "1000", "2", "1000", null, null)); HaProvider provider = new DefaultHaProvider(descriptor); URI uri1 = new URI("http://unreachable-host"); URI uri2 = new URI("http://reachable-host"); ArrayList<String> urlList = new ArrayList<String>(); urlList.add(uri1.toString()); urlList.add(uri2.toString()); provider.addHaService(serviceName, urlList); FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class); ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class); EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes(); EasyMock.expect(servletContext.getAttribute(HaServletContextListener.PROVIDER_ATTRIBUTE_NAME)) .andReturn(provider).anyTimes(); BasicHttpParams params = new BasicHttpParams(); HttpUriRequest outboundRequest = EasyMock.createNiceMock(HttpRequestBase.class); EasyMock.expect(outboundRequest.getMethod()).andReturn("GET").anyTimes(); EasyMock.expect(outboundRequest.getURI()).andReturn(uri1).anyTimes(); EasyMock.expect(outboundRequest.getParams()).andReturn(params).anyTimes(); HttpServletRequest inboundRequest = EasyMock.createNiceMock(HttpServletRequest.class); EasyMock.expect(inboundRequest.getRequestURL()).andReturn(new StringBuffer(uri2.toString())).once(); EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(0)) .once(); EasyMock.expect(inboundRequest.getAttribute("dispatch.ha.failover.counter")).andReturn(new AtomicInteger(1)) .once(); HttpServletResponse outboundResponse = EasyMock.createNiceMock(HttpServletResponse.class); EasyMock.expect(outboundResponse.getOutputStream()) .andAnswer(new IAnswer<SynchronousServletOutputStreamAdapter>() { @Override public SynchronousServletOutputStreamAdapter answer() throws Throwable { return new SynchronousServletOutputStreamAdapter() { @Override public void write(int b) throws IOException { throw new IOException("unreachable-host"); } }; } }).once(); EasyMock.replay(filterConfig, servletContext, outboundRequest, inboundRequest, outboundResponse); Assert.assertEquals(uri1.toString(), provider.getActiveURL(serviceName)); WebHdfsHaDispatch dispatch = new WebHdfsHaDispatch(); dispatch.setHttpClient(new DefaultHttpClient()); dispatch.setHaProvider(provider); dispatch.init(); long startTime = System.currentTimeMillis(); try { dispatch.executeRequest(outboundRequest, inboundRequest, outboundResponse); } catch (IOException e) { //this is expected after the failover limit is reached } long elapsedTime = System.currentTimeMillis() - startTime; Assert.assertEquals(uri2.toString(), provider.getActiveURL(serviceName)); //test to make sure the sleep took place Assert.assertTrue(elapsedTime > 1000); }
From source file:jetbrick.template.web.springmvc.JetTemplateView.java
@Override protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { JetWebContext context = new JetWebContext(request, response, model); JetTemplate template = JetWebEngine.getEngine().getTemplate(getUrl()); template.render(context, response.getOutputStream()); }
From source file:uk.org.iay.mdq.server.CertificateController.java
/** * Returns the certificate being used for signing responses. * /*from w ww .jav a 2 s . com*/ * @param response {@link HttpServletResponse} in which to build the response * * @throws Exception if anything goes wrong */ @RequestMapping("") void getCertificate(@Nonnull final HttpServletResponse response) throws Exception { log.debug("queried for certificate"); final InputStream in = certificateResource.getInputStream(); final OutputStream out = response.getOutputStream(); response.setContentType("text/plain"); ByteStreams.copy(in, out); }