Example usage for javax.servlet ServletOutputStream write

List of usage examples for javax.servlet ServletOutputStream write

Introduction

In this page you can find the example usage for javax.servlet ServletOutputStream write.

Prototype

public abstract void write(int b) throws IOException;

Source Link

Document

Writes the specified byte to this output stream.

Usage

From source file:be.fedict.eid.dss.webapp.DocumentViewerServlet.java

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

    LOG.debug("doGet");

    String resource = request.getParameter(RESOURCE_PARAM);
    if (null != resource) {

        // download/view request
        handleDownloadRequest(resource, request, response);
        return;//  w w w  .j a  va 2  s .  c o  m
    }

    // get browser plugins
    List<String> plugins = BrowserInfoServlet.getPlugins(request.getSession());
    for (String plugin : plugins) {
        LOG.debug("Plugin: " + plugin);
    }

    // get browser mimeTypes
    List<MimeType> mimeTypes = BrowserInfoServlet.getMimeTypes(request.getSession());
    for (MimeType mimeType : mimeTypes) {
        LOG.debug("MimeTypes: type=" + mimeType.getType() + " plugin=" + mimeType.getPlugin());
    }

    HttpSession httpSession = request.getSession();
    DocumentRepository documentRepository = new DocumentRepository(httpSession);
    String contentType = documentRepository.getDocumentContentType();
    if (null == contentType) {
        response.setContentType("text/plain");
        PrintWriter printWriter = response.getWriter();
        printWriter.println("No document to be signed.");
        return;
    }

    byte[] documentData = documentRepository.getDocument();

    DSSDocumentService documentService = super.findDocumentService(contentType);
    if (null != documentService) {
        LOG.debug("document visualization transformation");
        String language = (String) httpSession.getAttribute(View.LANGUAGE_SESSION_ATTRIBUTE);
        DocumentVisualization documentVisualization;
        try {
            documentVisualization = documentService.visualizeDocument(documentData, language, mimeTypes,
                    "./document?" + RESOURCE_PARAM + "=");
        } catch (Exception e) {
            throw new ServletException("error visualizing the document: " + e.getMessage(), e);
        }
        if (null != documentVisualization) {
            contentType = documentVisualization.getBrowserContentType();
            documentData = documentVisualization.getBrowserData();
        }
    }

    setResponseHeaders(request, response);

    response.setContentType(contentType);
    response.setContentLength(documentData.length);
    ServletOutputStream out = response.getOutputStream();
    out.write(documentData);
    out.flush();
}

From source file:org.wso2.adminui.AdminUIServletFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    try {//ww w  .  j a  v  a 2  s .  c  o m

        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        if (contextRoot == null) {
            //Context root could be "" or some othervalue
            this.contextRoot = httpServletRequest.getContextPath();
        }

        String requestURI = httpServletRequest.getRequestURI();
        int indexOfDot = requestURI.lastIndexOf(".");
        boolean isFile = false;
        if (indexOfDot != -1) {
            isFile = requestURI.substring(indexOfDot).matches("\\.(.)*");
        }
        if (!isFile && requestURI.lastIndexOf("/") != requestURI.length() - 1) {
            requestURI += "/";
        }
        Map generatedPages = (Map) servletContext.getAttribute(AdminUIConstants.GENERATED_PAGES);
        if (requestURI.equals(contextRoot) || requestURI.equals(contextRoot + "/")) {
            response.setContentType("text/html");
            boolean enableConsole = ((Boolean) servletContext.getAttribute(AdminUIConstants.ENABLE_CONSOLE))
                    .booleanValue();
            if (!enableConsole) {
                ServletOutputStream out = response.getOutputStream();
                out.write(("<b>Management Console has been disabled.</b> "
                        + "Enable it in the server.xml and try again.").getBytes());
                ((HttpServletResponse) response).setStatus(HttpServletResponse.SC_FORBIDDEN);
                out.flush();
                out.close();
                return;
            }
            String fileContents = (String) generatedPages.get("index.html");
            if (fileContents != null) {
                ServletOutputStream op = response.getOutputStream();
                response.setContentLength(fileContents.getBytes().length);
                op.write(fileContents.getBytes());
                return;
            }
        } else {
            String urlKey;
            if (contextRoot.equals("/")) {
                urlKey = requestURI.substring(contextRoot.length(), requestURI.length());
            } else {
                urlKey = requestURI.substring(1 + contextRoot.length(), requestURI.length());
            }

            if (generatedPages != null) {
                String fileContents = (String) generatedPages.get(urlKey);
                if (fileContents != null) {
                    ServletOutputStream op = response.getOutputStream();
                    response.setContentType("text/html");
                    response.setContentLength(fileContents.getBytes().length);
                    op.write(fileContents.getBytes());
                    return;
                }
            }

            /*
             || has been used to support any client who wants to access the "global_params.js"
             regardless of where they want to access.
            */
            if (urlKey.equals(GLOBAL_PARAMS_JS) || urlKey.indexOf(GLOBAL_PARAMS_JS) > -1) {
                initGlobalParams((HttpServletResponse) response);
                return;
            }
        }
    } catch (Exception e) {
        String msg = "Exception occurred while processing Request";
        log.error(msg, e);
        throw new ServletException(msg, e);
    }
    filterChain.doFilter(request, response);
}

From source file:com.seer.datacruncher.profiler.spring.ProfilerLoadController.java

public ModelAndView getColumnNames(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession session = request.getSession();

    ObjectMapper mapper = new ObjectMapper();

    ServletOutputStream out;
    response.setContentType("application/text");
    out = response.getOutputStream();/*from  w ww.  j ava2 s  . co  m*/

    out.write(mapper.writeValueAsBytes(session.getAttribute("columnNames")));
    out.flush();
    out.close();

    return null;
}

From source file:com.seer.datacruncher.profiler.spring.ProfilerLoadController.java

public ModelAndView getTableNames(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession session = request.getSession();

    ObjectMapper mapper = new ObjectMapper();

    ServletOutputStream out;
    response.setContentType("application/text");
    out = response.getOutputStream();//from   w w w.  ja  va 2s.com

    out.write(mapper.writeValueAsBytes(session.getAttribute("tableNames")));
    out.flush();
    out.close();

    return null;
}

From source file:br.vbathke.jenkins.UITestCaptureProjectAction.java

public void doAjaxVerifyResults(StaplerRequest request, StaplerResponse response) {
    //System.out.println("getLastBuild().getRootDir():          "+project.getLastBuild().getRootDir().toString());
    //System.out.println("project.getLastBuild().getWorkspace():   "+project.getLastBuild().getWorkspace().toString());

    try {//from  w  w  w  .j  ava2s  .c om
        String testStreamLocal = getTestStream();
        String tmpHash = md5Hash(testStreamLocal);
        String[] testStreamSplit;
        String outResponse = "";

        //Se arquivo possu mudanas
        if (testStreamPossuiDiferenca()) {
            Job job = new Job(getName());
            Execution exec = new Execution(request.getParameter("exec"), job.getId());

            //unstack from file and record on db
            testStreamSplit = testStreamLocal.split("\\n");
            for (int i = 0; i < testStreamSplit.length; i++) {
                if (!testStreamSplit[i].equals("")) {
                    JsonParseSingleQuote jsonLinha = new JsonParseSingleQuote(testStreamSplit[i]);

                    Test test = new Test(getName(), jsonLinha.get("metodo"));
                    test.setIdJob(job.getId());
                    test.setTest(jsonLinha.get("metodo"));
                    test.setTestClass(jsonLinha.get("classe"));
                    test.save();

                    //record the result
                    Result result = new Result(exec.getId(), test.getTest());
                    result.setStatus(jsonLinha.get("status"));
                    result.setStacktrace(FileUtils.readFileToString(new File(
                            project.getRootDir().getCanonicalPath() + "/workspace/target/surefire-reports/"
                                    + jsonLinha.get("classe").trim() + ".txt"),
                            "UTF-8"));
                    result.save();

                    testStreamLocal = testStreamLocal.replace(testStreamSplit[i] + "\n", "");
                }
            }
            hash = tmpHash;

            //se aps o processamento o arquivo NO foi alterado, desempilhe
            if (!testStreamPossuiDiferenca()) {
                try {
                    Files.write(Paths.get(fileString), testStreamLocal.getBytes(StandardCharsets.UTF_8));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            outResponse = "hash: " + hash + " lines discovered: " + testStreamSplit.length;
        } else {
            outResponse = "hash: " + hash + " lines: 0";
        }
        ServletOutputStream out = response.getOutputStream();
        out.write(outResponse.getBytes("UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.hp.security.jauth.admin.controller.BaseController.java

@RequestMapping("index")
public void index(HttpServletRequest request, HttpServletResponse response)
        throws IOException, TemplateException {
    Map<String, Object> root = new HashMap<String, Object>();
    String view = freemarkerUtil.buildView(root, "main");
    response.setContentType("text/html");
    ServletOutputStream out = response.getOutputStream();
    out.write(view.getBytes());
    out.flush();/*from   w w  w  .  j  a v a  2  s.  c om*/
    out.close();
}

From source file:org.pf4j.demo.web.mbean.PluginsMBean.java

/**
 * Download./*from   www.  java  2 s.c o m*/
 *
 * @param p the p
 * @param plugin the plugin
 * @return the string
 */
public String download(Person p, ExporterBase plugin) {

    FacesContext fc = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
    ServletOutputStream out = null;
    try {
        out = response.getOutputStream();

        out.write(plugin.export(p));
        response.setContentType(plugin.getContentType());
        response.addHeader("Content-Disposition", "attachment; filename=\"Person\"");
        out.flush();
    } catch (IOException e1) {
    }
    try {
        if (out != null) {
            out.close();
        }
        FacesContext.getCurrentInstance().responseComplete();
    } catch (IOException e) {

    }

    return "";
}

From source file:com.hp.security.jauth.admin.controller.BaseController.java

@RequestMapping("login")
public void login(HttpServletRequest request, HttpServletResponse response)
        throws IOException, TemplateException {
    String error = null;/*from   w  w w. j ava2 s  .  c o  m*/
    if (null != request.getAttribute("error")) {
        error = request.getAttribute("error").toString();
    }
    Map<String, Object> root = new HashMap<String, Object>();
    root.put("error", error);
    String view = freemarkerUtil.buildView(root, "login");
    response.setContentType("text/html");
    ServletOutputStream out = response.getOutputStream();
    out.write(view.getBytes());
    out.flush();
    out.close();
}

From source file:com.hp.security.jauth.admin.controller.BaseController.java

@RequestMapping("exception")
public void exception(HttpServletRequest request, HttpServletResponse response)
        throws IOException, TemplateException {
    Map<String, Object> root = new HashMap<String, Object>();
    String code = "";
    Exception ex = (Exception) request.getAttribute("ex");
    if (null != ex && ex instanceof AuthException) {
        AuthException aex = (AuthException) ex;
        code = aex.getCode();/*from www.  j  a  v  a2 s  .c o  m*/
    }
    root.put("code", code);
    root.put("ex", ex);
    String view = freemarkerUtil.buildView(root, "exception");
    response.setContentType("text/html");
    ServletOutputStream out = response.getOutputStream();
    out.write(view.getBytes());
    out.flush();
    out.close();
}

From source file:com.hzc.framework.ssh.controller.WebUtil.java

/**
 * ?response?//  w w  w .j  a v a2  s . co m
 *
 * @param attachFile
 * @throws IOException
 */
public static void write(byte[] attachFile) throws RuntimeException {
    try {
        HttpServletResponse resp = ActionContext.getResp();
        ServletOutputStream outputStream = resp.getOutputStream();
        outputStream.write(attachFile);
        outputStream.flush();
        //            outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}