Example usage for javax.servlet.http HttpServletResponse getOutputStream

List of usage examples for javax.servlet.http HttpServletResponse getOutputStream

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse getOutputStream.

Prototype

public ServletOutputStream getOutputStream() throws IOException;

Source Link

Document

Returns a ServletOutputStream suitable for writing binary data in the response.

Usage

From source file:org.shredzone.commons.view.impl.ViewContextImpl.java

@Override
@SuppressWarnings("unchecked")
public <T> T getValueOfType(Class<T> type) throws ViewContextException {
    if (typedValueMap.containsKey(type)) {
        return (T) typedValueMap.get(type);
    }//from ww  w  .j ava 2  s  .  c  o  m

    if (type.isAssignableFrom(HttpServletRequest.class)) {
        return (T) req;
    }

    if (type.isAssignableFrom(HttpSession.class)) {
        return (T) req.getSession();
    }

    if (type.isAssignableFrom(Locale.class)) {
        return (T) req.getLocale();
    }

    if (type.isAssignableFrom(OutputStream.class)) {
        try {
            HttpServletResponse resp = getValueOfType(HttpServletResponse.class);
            return (T) resp.getOutputStream();
        } catch (IOException ex) {
            throw new ViewContextException("Could not get OutputStream", ex);
        }
    }

    if (type.isAssignableFrom(PrintWriter.class)) {
        try {
            HttpServletResponse resp = getValueOfType(HttpServletResponse.class);
            return (T) resp.getWriter();
        } catch (IOException ex) {
            throw new ViewContextException("Could not get Writer", ex);
        }
    }

    if (type.isAssignableFrom(InputStream.class)) {
        try {
            return (T) req.getInputStream();
        } catch (IOException ex) {
            throw new ViewContextException("Could not get InputStream", ex);
        }
    }

    if (type.isAssignableFrom(BufferedReader.class)) {
        try {
            return (T) req.getReader();
        } catch (IOException ex) {
            throw new ViewContextException("Could not get Reader", ex);
        }
    }

    throw new ViewContextException("No value for type " + type.getName());
}

From source file:com.gu.management.logging.Log4JManagerServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    OutputStreamWriter fileWriter = new OutputStreamWriter(response.getOutputStream());

    fileWriter.write(MANAGEMENT_PAGE_HEAD);

    @SuppressWarnings({ "unchecked" })
    List<Logger> loggers = sortLoggers(LogManager.getCurrentLoggers());

    for (Logger logger : loggers) {
        fileWriter.write(String.format(LOGGER_TABLE_ROW, logger.getName(), generateOptionsFor(logger),
                logger.getEffectiveLevel()));
    }/*from w  w  w .  ja  v a  2s.  c o m*/

    fileWriter.write(MANAGEMENT_PAGE_FOOT);

    fileWriter.flush();
}

From source file:com.anuz.dummyclient.controller.ContentController.java

@RequestMapping(value = "files/{directory}/{fileName}", method = RequestMethod.GET)
public void image(@PathVariable("directory") String directory, @PathVariable("fileName") String fileName,
        @RequestHeader HttpHeaders headers, HttpServletResponse response) {
    try {//from w w  w  . ja v  a2  s .com
        logger.info(fileName);

        File file = new File(CONSTANT.CONTENTS + directory + "/" + fileName);
        Files.copy(file.toPath(), response.getOutputStream());

    } catch (IOException ex) {
        logger.info("Image not found");
        logger.debug(ex.getMessage());
    }
}

From source file:com.hp.autonomy.frontend.find.core.view.ViewController.java

@RequestMapping(value = VIEW_DOCUMENT_PATH, method = RequestMethod.GET)
public void viewDocument(@RequestParam(REFERENCE_PARAM) final String reference,
        @RequestParam(DATABASE_PARAM) final S database,
        @RequestParam(value = HIGHLIGHT_PARAM, required = false) final String highlightExpression,
        final HttpServletResponse response) throws E, IOException {
    response.setContentType(MediaType.TEXT_HTML_VALUE);
    ViewContentSecurityPolicy.addContentSecurityPolicy(response);
    viewServerService.viewDocument(reference, database, highlightExpression, response.getOutputStream());
}

From source file:org.openxdata.server.servlet.FormDownloadServlet.java

private void downloadUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
    formDownloadService.downloadUsers(response.getOutputStream(),
            request.getParameter(OpenXDataConstants.REQUEST_PARAM_USER_SERIALIZER));
}

From source file:com.sample.RSSAdapterResource.java

public void execute(HttpUriRequest req, HttpServletResponse resultResponse)
        throws ClientProtocolException, IOException, IllegalStateException, SAXException {
    HttpResponse RSSResponse = client.execute(host, req);
    ServletOutputStream os = resultResponse.getOutputStream();
    if (RSSResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        resultResponse.addHeader("Content-Type", "application/json");
        String json = XML.toJson(RSSResponse.getEntity().getContent());
        os.write(json.getBytes(Charset.forName("UTF-8")));

    } else {/*from w  w w  .j av  a  2 s.co  m*/
        resultResponse.setStatus(RSSResponse.getStatusLine().getStatusCode());
        RSSResponse.getEntity().getContent().close();
        os.write(RSSResponse.getStatusLine().getReasonPhrase().getBytes());
    }
    os.flush();
    os.close();
}

From source file:com.era7.bioinfo.blastxviewer7.server.servlet.UploadBlastAndGetCoverageXMLServlet.java

protected void servletLogic(HttpServletRequest request, HttpServletResponse resp)
        throws javax.servlet.ServletException, java.io.IOException {

    OutputStream out = resp.getOutputStream();

    String temp = request.getParameter(Request.TAG_NAME);

    try {//  ww w  .  j  a  v a 2s .  co  m

        Request myReq = new Request(temp);

        String method = myReq.getMethod();

        if (method.equals(RequestList.UPLOAD_BLAST_AND_GET_COVERAGE_XML_REQUEST)) {

            boolean isMultipart = ServletFileUpload.isMultipartContent(request);

            if (!isMultipart) {
                Response response = new Response();
                response.setError("No file was uploaded");
                out.write(response.toString().getBytes());
            } else {

                System.out.println("all controls passed!!");

                FileItem fileItem = FileUploadUtilities.getFileItem(request);
                InputStream uploadedStream = fileItem.getInputStream();
                BufferedReader inBuff = new BufferedReader(new InputStreamReader(uploadedStream));
                String line = null;
                StringBuilder stBuilder = new StringBuilder();
                while ((line = inBuff.readLine()) != null) {
                    //System.out.println("line = " + line);
                    stBuilder.append(line);
                }

                System.out.println("before blastExporter");

                String resultExport = BlastExporter
                        .exportBlastXMLtoIsotigsCoverage(new BlastOutput(stBuilder.toString()));

                //System.out.println("resultExport = " + resultExport);

                uploadedStream.close();

                System.out.println("after blastexporter");

                String responseSt = "<response status=\"" + Response.SUCCESSFUL_RESPONSE + "\" method=\""
                        + RequestList.UPLOAD_BLAST_AND_GET_COVERAGE_XML_REQUEST + "\" >\n" + resultExport
                        + "\n</response>";

                System.out.println("writing response");

                resp.setContentType("text/html");

                byte[] byteArray = responseSt.getBytes();
                out.write(byteArray);
                resp.setContentLength(byteArray.length);

                System.out.println("doneee!!");

            }

        } else {
            Response response = new Response();
            response.setError("There is no such method");
            out.write(response.toString().getBytes());

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.sap.dirigible.runtime.registry.RepositoryServlet.java

@Override
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String repositoryPath = null;
    final OutputStream out = response.getOutputStream();
    try {/*  w w w.j a v a2 s  .  c  o  m*/
        repositoryPath = extractRepositoryPath(request);
        IEntity entity = getEntity(repositoryPath, request);
        if (entity != null) {
            getRepository(request).removeResource(repositoryPath);
        }
    } catch (IllegalArgumentException ex) {
        logger.error(String.format(REQUEST_PROCESSING_FAILED_S, repositoryPath) + ex.getMessage());
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
    } catch (MissingResourceException ex) {
        logger.error(String.format(REQUEST_PROCESSING_FAILED_S, repositoryPath) + ex.getMessage());
        response.sendError(HttpServletResponse.SC_NO_CONTENT, ex.getMessage());
    } finally {
        out.flush();
        out.close();
    }
}

From source file:edu.usu.sdl.openstorefront.web.action.BaseAction.java

protected Resolution streamResults(Object data) {
    return new StreamingResolution(MediaType.APPLICATION_JSON) {

        @Override/*from  w ww .  ja v  a 2 s  .co  m*/
        protected void stream(HttpServletResponse response) throws Exception {
            objectMapper.writeValue(response.getOutputStream(), data);
        }
    };
}

From source file:DeleteClobFromServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    Connection conn = null;/* ww w.  j av  a 2s  .c  o m*/
    PreparedStatement pstmt = null;
    String id = "0001";
    ServletOutputStream out = response.getOutputStream();
    response.setContentType("text/html");
    out.println("<html><head><title>Delete CLOB Record</title></head>");

    try {
        conn = getHSQLConnection();
        pstmt = conn.prepareStatement("delete from DataFiles where id = ?");
        pstmt.setString(1, id);
        pstmt.executeUpdate();
        out.println("<body><h4>deleted CLOB record with id=" + id + "</h4></body></html>");
    } catch (Exception e) {
        out.println("<body><h1>Error=" + e.getMessage() + "</h1></body></html>");
    } finally {
        try {
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}