Package javax.servlet.http

Examples of javax.servlet.http.HttpServletResponse


     * @throws ServletException if a servlet exception ocours
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
        ServletRequest sessionBindingRequest = new HttpServletRequestWrapper((HttpServletRequest) servletRequest);
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        ResponseFactory.addNoCacheHeaderDirective(response);
        filterChain.doFilter(sessionBindingRequest, servletResponse);
    }
View Full Code Here


      messages.add(message);
      return;
    }

    HttpServletResponse response = (HttpServletResponse) context.getResponse();
    response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
    response.setContentLength((int) data.length);
    response.setContentType(contentType);

    try
    {
      OutputStream out = response.getOutputStream();

      //      // Copy the contents of the file to the output stream
      //      byte[] buf = new byte[1024];
      //      int count;
      //      while ((count = in.read(buf)) >= 0)
View Full Code Here

        return timedoutAction;
    }

    void invokeAtmosphereHandler(AtmosphereResourceImpl r) throws IOException {
        HttpServletRequest req = r.getRequest();
        HttpServletResponse response = r.getResponse();
        String disableOnEvent = r.getAtmosphereConfig().getInitParameter(AtmosphereServlet.DISABLE_ONSTATE_EVENT);

        try{
            if (!r.getResponse().equals(response)) {
                logger.warn("Invalid response: {}", response);
View Full Code Here

    private String contentType = "txt/html";
    private boolean isCommited = false;
    private Locale locale;

    public WebSocketHttpServletResponse(A webSocketSupport) {
        super(new HttpServletResponse() {

            public void addCookie(Cookie cookie) {
               
            }
View Full Code Here

  /**
   Fetch a {@link PredictionList}, its {@link Prediction}s, and its average score, and place them all in request scope.
  */
  @Override public ResponsePage execute() throws AppException {
    HttpServletResponse response = getRequestParser().getResponse();
    //This setter does not 'stick' - need to set in JSP using a page directive
    response.setContentType("application/json");
   
    List<Prediction> predictions = fDAO.list(getIdParam(LIST_ID));
    addToRequest(ITEMS_FOR_LISTING, predictions);
   
    addToRequest("averageScore", Prediction.calculateAverageScore(predictions));
View Full Code Here

     * @throws IOException
     */
    public void onRequest(AtmosphereResource<HttpServletRequest, HttpServletResponse> event) throws IOException {

        HttpServletRequest req = event.getRequest();
        HttpServletResponse res = event.getResponse();

        res.setContentType("text/html");
        res.addHeader("Cache-Control", "private");
        res.addHeader("Pragma", "no-cache");
        if (req.getMethod().equalsIgnoreCase("GET")) {
            event.suspend();
        } else if (req.getMethod().equalsIgnoreCase("POST")) {

            // There are better ways to do this, but it's the simplest way to
            // ensure that there is data consistency
            synchronized (game) {
                int cell = -1;
                String cellStr = req.getParameter("cell");
                PrintWriter writer = res.getWriter();
                writer.println("cell is '" + cellStr + "'");
                if (cellStr == null) {
                    writer.println("error - cell not set");
                    return;
                }
View Full Code Here

     * URL handler for the given path.
     */
    public void service(ServletRequest req1, ServletResponse res1)
            throws ServletException {
        final HttpServletRequest req = (HttpServletRequest) req1;
        final HttpServletResponse res = (HttpServletResponse) res1;
        final HTTPRequestHeader http_header = new HTTPRequestHeader();

        if (req.getServletPath().equals("/admin")) try {
            log.debug("Forwarding /admin request back to self");
            req.getRequestDispatcher("WebMail/admin").forward(req1, res1);
            return;
        } catch (IOException ioe) {
            log.fatal("Forward from '/admin' failed", ioe);
            throw new ServletException(ioe.getMessage());
        }

        final Enumeration en = req.getHeaderNames();
        while (en.hasMoreElements()) {
            final String s = (String) en.nextElement();
            http_header.setHeader(s, req.getHeader(s));
        }

        http_header.setPath(
                req.getPathInfo() == null ? "/" : req.getPathInfo());

        InetAddress addr;
        try {
            addr = InetAddress.getByName(req.getRemoteHost());
        } catch (final UnknownHostException e) {
            try {
                addr = InetAddress.getByName(req.getRemoteAddr());
            } catch (final Exception ex) {
                throw new ServletException("Remote host must identify!");
            }
        }

        HTMLDocument content = null;
        final int err_code = 400;
        HTTPSession sess = null;

        /*
         * Here we try to parse the MIME content that the Client sent in his
         * POST since the JServ doesn't do that for us:-( At least we can use
         * the functionality provided by the standalone server where we need to
         * parse the content ourself anyway.
         */
        try {
            final BufferedOutputStream out =
                    new BufferedOutputStream(res.getOutputStream());

            /*
             * First we try to use the Servlet API's methods to parse the
             * parameters. Unfortunately, it doesn't know how to handle MIME
             * multipart POSTs, so we will have to handle that ourselves
             */

            /*
             * First get all the parameters and set their values into
             * http_header
             */
            Enumeration enum2 = req.getParameterNames();
            while (enum2.hasMoreElements()) {
                final String s = (String) enum2.nextElement();
                http_header.setContent(s, req.getParameter(s));
                // log.info("Parameter "+s);
            }

            /* Then we set all the headers in http_header */
            enum2 = req.getHeaderNames();
            while (enum2.hasMoreElements()) {
                final String s = (String) enum2.nextElement();
                http_header.setHeader(s, req.getHeader(s));
            }

            /*
             * In Servlet API 2.2 we might want to fetch the attributes also,
             * but this doesn't work in API 2.0, so we leave it commented out
             */
            // enum2=req.getAttributeNames();
            // while(enum2.hasMoreElements()) {
            // String s=(String)enum2.nextElement();
            // log.info("Attribute "+s);
            // }

            /* Now let's try to handle multipart/form-data posts */

            if (req.getContentType() != null
                    && req.getContentType().toUpperCase().
                    startsWith("MULTIPART/FORM-DATA")) {
                final int size = Integer.parseInt(WebMailServer.
                        getStorage().getConfig("max attach size"));
                final MultipartParser mparser = new MultipartParser(req, size);
                Part p;
                while ((p = mparser.readNextPart()) != null) {
                    if (p.isFile()) {
                        final ByteStore bs = ByteStore.getBinaryFromIS(
                                ((FilePart) p).getInputStream(), size);
                        bs.setName(((FilePart) p).getFileName());
                        bs.setContentType(getStorage().getMimeType(
                                    ((FilePart) p).getFileName()));
                        http_header.setContent(p.getName(), bs);
                        log.info("File name " + bs.getName());
                        log.info("Type      " + bs.getContentType());

                    } else if (p.isParam()) {
                        http_header.setContent(p.getName(),
                                ((ParamPart) p).getStringValue());
                    }

                    // log.info("Parameter "+p.getName());
                }
            }

            try {
                final String url = http_header.getPath();

                try {
                    /* Find out about the session id */
                    sess = req.getSession(false) == null
                            ? null
                            : (HTTPSession) req.getSession(false).
                            getAttribute("webmail.session");

                    /*
                     * If the user was logging on, he doesn't have a session id,
                     * so generate one. If he already had one, all the better,
                     * we will take the old one
                     */
                    if (sess == null && url.startsWith("/login")) {
                        sess = newSession(req, http_header);
                    } else if (sess == null && url.startsWith("/admin/login")) {
                        http_header.setHeader("LOGIN", "Administrator");
                        sess = newAdminSession(req, http_header);
                    }
                    if (sess == null && !url.equals("/")
                            && !url.startsWith("/passthrough")
                            && !url.startsWith("/admin")) {
                        content = getURLHandler().handleURL(
                                "/logout", sess, http_header);
                    } else {
                        /* Ensure that the session state is up-to-date */
                        if (sess != null) {
                            sess.setEnv();
                        }

                        /* Let the URLHandler determine the result of the query */
                        content = getURLHandler().
                                handleURL(url, sess, http_header);
                    }
                } catch (final InvalidPasswordException e) {
                    log.error("Connection to " + addr.toString()
                            + ": Authentication failed!");
                    if (url.startsWith("/admin/login")) {
                        content = getURLHandler().
                                handleURL("/admin", null, http_header);
                    } else if (url.startsWith("/login")) {
                        content = getURLHandler().
                                handleURL("/", null, http_header);
                    } else
                        // content=new
                        // HTMLErrorMessage(getStorage(),e.getMessage());
                        throw new ServletException("Invalid URL called!");
                } catch (final Exception ex) {
                    content = getURLHandler().
                            handleException(ex, sess, http_header);
                    log.debug("Some strange error while handling request", ex);
                }

                /*
                 * Set some HTTP headers: Date is now, the document should
                 * expire in 5 minutes, proxies and clients shouldn't cache it
                 * and all WebMail documents must be revalidated when they think
                 * they don't have to follow the "no-cache".
                 */
                res.setDateHeader("Date:", System.currentTimeMillis());
                res.setDateHeader(
                        "Expires", System.currentTimeMillis() + 300000);
                res.setHeader("Pragma", "no-cache");
                res.setHeader("Cache-Control", "must-revalidate");

                synchronized (out) {
                    res.setStatus(content.getReturnCode());

                    if (content.hasHTTPHeader()) {
                        final Enumeration enumVar = content.getHTTPHeaderKeys();
                        while (enumVar.hasMoreElements()) {
                            final String s = (String) enumVar.nextElement();
                            res.setHeader(s, content.getHTTPHeader(s));
                        }
                    }

                    /*
                     * What we will send is an image or some other sort of
                     * binary
                     */
                    if (content instanceof HTMLImage) {
                        final HTMLImage img = (HTMLImage) content;
                        /*
                         * the HTMLImage class provides us with most of the
                         * necessary information that we want to send
                         */
                        res.setHeader("Content-Type", img.getContentType());
                        res.setHeader("Content-Transfer-Encoding",
                                img.getContentEncoding());
                        res.setHeader("Content-Length", "" + img.size());
                        res.setHeader("Connection", "Keep-Alive");

                        /* Send 8k junks */
                        int offset = 0;
                        while (offset + chunk_size < img.size()) {
                            out.write(img.toBinary(), offset, chunk_size);
                            offset += chunk_size;
                        }
                        out.write(img.toBinary(), offset, img.size() - offset);
                        out.flush();

                        out.close();
                    } else {
                        final byte[] encoded_content =
                                content.toString().getBytes("UTF-8");

                        /*
                         * We are sending HTML text. Set the encoding to UTF-8
                         * for Unicode messages
                         */
                        res.setHeader("Content-Length",
                                "" + (encoded_content.length + 2));
                        res.setHeader("Connection", "Keep-Alive");
                        res.setHeader(
                                "Content-Type", "text/html; charset=\"UTF-8\"");

                        out.write(encoded_content);
                        out.write("\r\n".getBytes());

View Full Code Here

    String fileExt = "";

    try
    {
      ExternalContext context = facesContext.getExternalContext();
      HttpServletResponse response = (HttpServletResponse) context.getResponse();
      OutputStream out = response.getOutputStream();
     
      switch (type)
      {
        case PDF:
          contentType = "application/pdf";
          fileExt = "pdf";
         
          data = JasperRunManager.runReportToPdf(reportsDirectoryPath + reportFileName + ".jasper", params, conn);

          response.setHeader("Content-Disposition", "attachment;filename=\"" + outputFileName + "." + fileExt + "\"");
          response.setHeader("Cache-Control", "no-cache");
          response.setContentLength((int) data.length);
          response.setContentType(contentType);

          out.write(data, 0, data.length);
          break;
         
        case XLS:
          contentType = "application/xls";
          fileExt = "xls";

          JasperReport report = JasperCompileManager.compileReport(reportsDirectoryPath + reportFileName + ".jrxml");
          JasperPrint print = JasperFillManager.fillReport(report, params, conn);

          JRXlsExporter exporter = new JRXlsExporter();

          exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
          exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, out);
          exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
          exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
          exporter.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE, Boolean.TRUE);
          exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
                   
          response.setHeader("Content-disposition", "inline; filename=\"" + outputFileName + "." + fileExt + "\"");
          response.setHeader("Cache-Control", "no-cache");
          response.setContentType(contentType);
         
          exporter.exportReport();         
          break;
      }
View Full Code Here

     * @throws java.io.IOException
     * @throws javax.servlet.ServletException
     */
    public void event(CometEvent cometEvent) throws IOException, ServletException {
        HttpServletRequest req = cometEvent.getHttpServletRequest();
        HttpServletResponse res = cometEvent.getHttpServletResponse();
        req.setAttribute(TomcatCometSupport.COMET_EVENT, cometEvent);

        if (!isCometSupportSpecified && !isCometSupportConfigured.getAndSet(true)) {
            synchronized (cometSupport) {
                if (!cometSupport.getClass().equals(TomcatCometSupport.class)) {
View Full Code Here

     * @throws java.io.IOException
     * @throws javax.servlet.ServletException
     */
    public void event(HttpEvent httpEvent) throws IOException, ServletException {
        HttpServletRequest req = httpEvent.getHttpServletRequest();
        HttpServletResponse res = httpEvent.getHttpServletResponse();
        req.setAttribute(JBossWebCometSupport.HTTP_EVENT, httpEvent);

        if (!isCometSupportSpecified && !isCometSupportConfigured.getAndSet(true)) {
            synchronized (cometSupport) {
                if (!cometSupport.getClass().equals(JBossWebCometSupport.class)) {
View Full Code Here

TOP

Related Classes of javax.servlet.http.HttpServletResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.