Example usage for javax.servlet ServletResponse getWriter

List of usage examples for javax.servlet ServletResponse getWriter

Introduction

In this page you can find the example usage for javax.servlet ServletResponse getWriter.

Prototype

public PrintWriter getWriter() throws IOException;

Source Link

Document

Returns a PrintWriter object that can send character text to the client.

Usage

From source file:org.nuxeo.license.filter.LicenseGuardFilter.java

protected void handleDuplicicatedConnection(ServletRequest request, ServletResponse response,
        Principal principal, String sid) throws IOException {
    String message = "User" + principal.getName() + " has a duplicated connection on Session " + sid;
    log.warn(message);//w  ww.j a v  a2s  .  c om
    response.getWriter().write(message);
    // XXX redirect or whatever
}

From source file:com.thoughtworks.go.server.web.BackupFilter.java

private void generateResponseForIsBackupFinishedAPI(ServletResponse response) {
    response.setContentType("application/json");
    JsonObject json = new JsonObject();
    json.addProperty("is_backing_up", backupService.isBackingUp());
    try {/*  w  ww.ja  va2 s . c  o m*/
        response.getWriter().print(json);
    } catch (IOException e) {
        LOGGER.error("General IOException: {}", e.getMessage());
    }
}

From source file:se.vgregion.javg.web.filter.WebErrorHandlingFilter.java

public void doFilter(ServletRequest request, ServletResponse arg1, FilterChain arg2)
        throws IOException, ServletException {
    // System.out.println("in doFilter(ServletRequest");
    try {/*from   www .j  a  v  a  2  s  .  co  m*/

        arg2.doFilter(request, arg1);
    } catch (Exception e) {

        arg1.getWriter()
                .write(createTyckTillPopupLink(e.toString(), ((HttpServletRequest) request).getRemoteUser()));
    }
}

From source file:MyServlet.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    String password = ((HttpServletRequest) request).getParameter("password");

    if (password.equals("opensesame")) {
        chain.doFilter(request, response);
    } else {//from   w w  w  .  j  a  v  a2 s.co  m
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>");
        out.println("Incorrect Password");
        out.println("</TITLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<H1>Incorrect Password</H1>");
        out.println("Sorry, that password was incorrect.");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}

From source file:com.netsteadfast.greenstep.sys.GreenStepBaseFormAuthenticationFilter.java

protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
    if (isAjaxRequest((HttpServletRequest) request)) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.getWriter().write(Constants.NO_LOGIN_JSON_DATA);
        return;/*from   ww w. j  a  va  2  s  .c  o  m*/
    }
    if (this.isDojoxContentPane((HttpServletRequest) request)) { //  dojox.layout.ContentPane ??? login.action ??          
        WebUtils.issueRedirect(request, response, Constants.DOJOX_CONTENT_PANE_XHR_RE_LOGIN_PAGE);
        return;
    }
    WebUtils.issueRedirect(request, response, getLoginUrl());
}

From source file:io.hops.hopsworks.api.tensorflow.TensorboardProxyServlet.java

private void sendErrorResponse(ServletResponse servletResponse, String message) throws IOException {
    servletResponse.setContentType("text/html");
    PrintWriter out = servletResponse.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title></title>");
    out.println("</head>");
    out.println("<body>");
    out.println(message);//ww  w  . j  av a 2s .com
    out.println("</body>");
    out.println("</html>");
}

From source file:com.netsteadfast.greenstep.sys.GreenStepMobileFormAuthenticationFilter.java

protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
    if (isAjaxRequest((HttpServletRequest) request)) {
        response.setCharacterEncoding(Constants.BASE_ENCODING);
        response.setContentType("application/json");
        response.getWriter().write(Constants.NO_LOGIN_JSON_DATA);
        return;/*w  ww  .  j av  a2 s .c  o m*/
    }
    if (this.isIframeMode((HttpServletRequest) request)) { // iframe ??? login.action ?          
        WebUtils.issueRedirect(request, response, "/pages/system/error_static.jsp");
        return;
    }
    if (this.isDojoxContentPane((HttpServletRequest) request)) { //  dojox.layout.ContentPane ??? login.action ??          
        WebUtils.issueRedirect(request, response, Constants.DOJOX_CONTENT_PANE_XHR_RE_LOGIN_PAGE);
        return;
    }
    WebUtils.issueRedirect(request, response, getLoginUrl());
}

From source file:org.ovirt.vdsmfake.servlet.XmlRpcFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    try {/*from ww w .j  a v  a  2  s .c  o  m*/
        final String method = ((HttpServletRequest) request).getMethod();

        if ("GET".equals(method)) {
            response.setContentType("text/plain");
            response.getWriter().write("Hello, this is VDSM Fake.");
            return;
        }

        if (!"POST".equals(method)) {
            response.setContentType("text/plain");
            response.getWriter().write("Unsupported method: " + method);
            return;
        }

        ContextHolder.init();
        ContextHolder.setServerName(request.getServerName());

        final AppConfig conf = AppConfig.getInstance();

        final CustomHttpServletRequestWrapper wrapper = new CustomHttpServletRequestWrapper(
                (HttpServletRequest) request);

        final CustomHttpServletResponseWrapper responseWrapper = new CustomHttpServletResponseWrapper(
                (HttpServletResponse) response);

        // fix method call name - add namespace of method (required by Apache XMLRPC)
        final Document doc = XMLUtils.parseDocument(wrapper.getInputStream());
        final XPath xp = XPath.newInstance("/methodCall/methodName");
        final Element el = (Element) xp.selectSingleNode(doc);
        final String methodName = el.getText();

        final long tm = System.currentTimeMillis();

        // append namespace
        if (!conf.isProxyActive()) {
            el.setText(METHOD_CALL_NS + "." + el.getText());
        }

        // write back the new XML data
        wrapper.setInputStreamData(XMLUtils.serializeDocument(doc));

        log.info("[{}] {} starting...", new Object[] { request.getServerName(), methodName });

        chain.doFilter(wrapper, responseWrapper);

        // write communication into files
        if (conf.isLogDirSet() && conf.isMethodLoggingEnabled(methodName)) {
            int fIndex = logCounter.incrementAndGet();

            FileUtils.writeByteArrayToFile(new File(conf.getLogDir(), getPrefix(fIndex) + "_req_" + methodName),
                    wrapper.getInputStreamData());
            FileUtils.writeByteArrayToFile(new File(conf.getLogDir(), getPrefix(fIndex) + "_res_" + methodName),
                    responseWrapper.getOutputStreamData());
        }

        // write response
        responseWrapper.writeDataToStream();

        // log request info
        log.info("[{}] {} done. ({} ms, {} bytes)", new Object[] { request.getServerName(), methodName,
                Long.valueOf(System.currentTimeMillis() - tm), responseWrapper.getOutputStreamData().length });
    } catch (Exception e) {
        log.error("Cannot filter request", e);
        throw new ServletException("Error during request processing", e);
    } finally {
        ContextHolder.clear();
    }
}

From source file:MyServlet.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    GregorianCalendar calendar = new GregorianCalendar();
    Date date1 = new Date();
    calendar.setTime(date1);//from  w  w  w .  ja  va2 s.c om
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    if (hour < 9 || hour > 17) {
        chain.doFilter(request, response);
    } else {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>");
        out.println("Get Back to Work!");
        out.println("</TITLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<H1>Get Back to Work!</H1>");
        out.println("Sorry, that resource is not available now.");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}

From source file:chiron.maxscore.servlet.SecurityServlet.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpSession session = ((HttpServletRequest) request).getSession();
    Object role = session.getAttribute("role");
    if (role == null) {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = new HashMap<>();
        map.put("success", false);
        map.put("msg", "");
        mapper.writeValue(response.getWriter(), map);
    }// w w  w.j  a  v a 2s . co m
    chain.doFilter(request, response);
}