Package javax.servlet.http

Examples of javax.servlet.http.HttpServletResponse


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

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

        res.setContentType("text/html;charset=ISO-8859-1");
        if (req.getMethod().equalsIgnoreCase("GET")) {
            event.suspend();
           
            Broadcaster bc = event.getBroadcaster();
            String clusterType = event.getAtmosphereConfig().getInitParameter(CLUSTER);
            if (!filterAdded.getAndSet(true) && clusterType != null){
                if (clusterType.equals("jgroups")){
                    event.getAtmosphereConfig().getServletContext().log("JGroupsFilter enabled");
                    bc.getBroadcasterConfig().addFilter(
                            new JGroupsFilter(bc, event.getAtmosphereConfig().getWebServerName()));
                }
            }

            //Simple Broadcast
            bc.getBroadcasterConfig().addFilter(new XSSHtmlFilter());
            Future<String> f = bc.broadcast(event.getAtmosphereConfig().getWebServerName()
                    + "**has suspended a connection from "
                    + req.getRemoteAddr());

            try {
                // Wait for the push to occurs. This is blocking a thread as the Broadcast operation
                // is usually asynchronous, e.g executed using an {@link ExecuturService}
                f.get();
            } catch (InterruptedException ex) {
                logger.error("", ex);
            } catch (ExecutionException ex) {
                logger.error("", ex);
            }

            // Ping the connection every 30 seconds
            bc.scheduleFixedBroadcast(req.getRemoteAddr() + "**is still listening", 30, TimeUnit.SECONDS);

            // Delay a message until the next broadcast.
            bc.delayBroadcast("Delayed Chat message");
        } else if (req.getMethod().equalsIgnoreCase("POST")) {
            String action = req.getParameterValues("action")[0];
            String name = req.getParameterValues("name")[0];

            if ("login".equals(action)) {
                req.getSession().setAttribute("name", name);
                event.getBroadcaster().broadcast("System Message from "
                        + event.getAtmosphereConfig().getWebServerName() + "**" + name + " has joined.");

            } else if ("post".equals(action)) {
                String message = req.getParameterValues("message")[0];
                event.getBroadcaster().broadcast(name + "**" + message);
            } else {
                res.setStatus(422);
            }
            res.getWriter().write("success");
            res.getWriter().flush();
        }
    }
View Full Code Here


     */
    public void onStateChange(AtmosphereResourceEvent<HttpServletRequest,
            HttpServletResponse> event) throws IOException {

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

        if (event.getMessage() == null) return;

        String e = event.getMessage().toString();

        String name = e;
        String message = "";

        if (e.indexOf("**")> 0){
            name = e.substring(0,e.indexOf("**"));
            message = e.substring(e.indexOf("**")+2);
        }

        String msg = BEGIN_SCRIPT_TAG + toJsonp(name, message) + END_SCRIPT_TAG;

        if (event.isCancelled()){
            event.getResource().getBroadcaster()
                    .broadcast(req.getSession().getAttribute("name") + " has left");
        } else if (event.isResuming() || event.isResumedOnTimeout()) {
            String script = "<script>window.parent.app.listen();\n</script>";

            res.getWriter().write(script);
        } else {
            res.getWriter().write(msg);
        }
        res.getWriter().flush();
    }
View Full Code Here

     * @param atmoResource An {@link AtmosphereResourceEvent}
     * @throws java.io.IOException
     */
    public void onRequest(AtmosphereResource<HttpServletRequest, HttpServletResponse> atmoResource) throws IOException{
        HttpServletRequest request = atmoResource.getRequest();
        HttpServletResponse response = atmoResource.getResponse();       
       
        String action = request.getParameter("action");     

        String sessionId = request.getSession().getId();
        HttpSession session = request.getSession();
        Broadcaster myBroadcasterFollower = (Broadcaster) session.getAttribute(sessionId);
        if (action != null) {
            if ("login".equals(action)) {
                response.setContentType("text/plain");
                response.setCharacterEncoding("UTF-8");
                              
                String name = request.getParameter("name");
               
                if (name == null) {
                    logger.error("Name cannot be null");
                    return;
                }
               
                session.setAttribute("name", name);
               
                myBroadcasterFollower.broadcast(BEGIN_SCRIPT_TAG
                        + toJsonp("Welcome back", name)
                        + END_SCRIPT_TAG);

                // Store the Broadcaster associated with this user so
                // we can retrieve it for supporting follower.
                atmoResource.getAtmosphereConfig().getServletContext().setAttribute(name, myBroadcasterFollower);
            } else if ("post".equals(action)) {
                String message = request.getParameter("message");
                String callback = request.getParameter("callback");
               
                if (message == null) {
                    logger.error("Message cannot be null");
                    return;
                }
               
                if (callback == null) {
                    callback = "alert";
                }

                if (myBroadcasterFollower != null){
                    myBroadcasterFollower.broadcast("<script id='comet_" + counter++ + "'>"
                            + "window.parent." + callback + "(" + message + ");</script>");
                } else {
                    throw new RuntimeException("Broadcaster was null");
                }
                response.getWriter().println("ok");
            } else if ("start".equals(action)) {
                String message = "{ message : 'Welcome'}";
                response.setContentType("text/html;charset=ISO-8859-1");

                String callback = request.getParameter("callback");
                if (callback == null) {
                    callback = "alert";
                }

                // Use one Broadcaster per AtmosphereResource
                try {
                    atmoResource.setBroadcaster(BroadcasterFactory.getDefault().get());
                } catch (Throwable t){
                    throw new IOException(t);
                }

                // Create a Broadcaster based on this session id.
                myBroadcasterFollower = atmoResource.getBroadcaster();
                atmoResource.suspend();

                session.setAttribute("atmoResource", atmoResource);
                session.setAttribute(sessionId, myBroadcasterFollower);

                response.getWriter().println("<script id='comet_" + counter++ + "'>"
                        + "window.parent." + callback + "(" + message + ");</script>");
                response.getWriter().println(startingMessage);
                response.getWriter().flush();
            } else if ("following".equals(action)) {
                response.setContentType("text/html");
                String follow = request.getParameter("message");
                String name = (String)session.getAttribute("name");
               
                if (follow == null) {
                    logger.error("Message cannot be null");
View Full Code Here

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

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

        res.setContentType("text/html;charset=ISO-8859-1");
        res.addHeader("Cache-Control", "private");
        res.addHeader("Pragma", "no-cache");

        if (req.getMethod().equalsIgnoreCase("GET")) {
            event.suspend();

            Broadcaster bc = event.getBroadcaster();
            bc.getBroadcasterConfig().addFilter(new XSSHtmlFilter());
            bc.broadcast(event.getAtmosphereConfig().getWebServerName()
                    + "**has suspended a connection from "
                    + req.getRemoteAddr());
        } else if (req.getMethod().equalsIgnoreCase("POST")) {
            res.setCharacterEncoding("UTF-8");
            String action = req.getParameterValues("action")[0];
            String name = req.getParameterValues("name")[0];

            if ("login".equals(action)) {
                req.getSession().setAttribute("name", name);
                event.getBroadcaster().broadcast("System Message from "
                        + event.getAtmosphereConfig().getWebServerName() + "**" + name + " has joined.");
                res.getWriter().write("success");
                res.getWriter().flush();
            } else if ("post".equals(action)) {
                String message = req.getParameterValues("message")[0];
                event.getBroadcaster().broadcast(name + "**" + message);
                res.getWriter().write("success");
                res.getWriter().flush();
            } else {
                res.setStatus(422);

                res.getWriter().write("success");
                res.getWriter().flush();
            }
        }
    }
View Full Code Here

     */
    public void onStateChange(AtmosphereResourceEvent<HttpServletRequest,
            HttpServletResponse> event) throws IOException {

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

        if (event.getMessage() == null) return;

        String e = event.getMessage().toString();

        String name = e;
        String message = "";

        if (e.indexOf("**")> 0){
            name = e.substring(0,e.indexOf("**"));
            message = e.substring(e.indexOf("**")+2);
        }

        String msg = BEGIN_SCRIPT_TAG + toJsonp(name, message) + END_SCRIPT_TAG;

        if (event.isCancelled()){
            event.getResource().getBroadcaster()
                    .broadcast(req.getSession().getAttribute("name") + " has left");
        } else if (event.isResuming() || event.isResumedOnTimeout()) {
            String script = "<script>window.parent.app.listen();\n</script>";

            res.getWriter().write(script);
            res.getWriter().flush();
        } else {
            res.getWriter().write(msg);
            res.getWriter().flush();
        }
    }
View Full Code Here

    private int counter;

    public void onRequest(AtmosphereResource<HttpServletRequest,
            HttpServletResponse> event) throws IOException {
        HttpServletRequest req = event.getRequest();
        HttpServletResponse res = event.getResponse();
        logger.info("onEvent: {}", req.getRequestURI());

        String[] actionValues = req.getParameterValues("action");
        if (actionValues != null && actionValues[0] != null) {
            String action = req.getParameterValues("action")[0];
            if ("post".equals(action)) {
                String message = req.getParameterValues("message")[0];
                String callback = req.getParameterValues("callback")[0];
                if (callback == null) {
                    callback = "alert";
                }

                event.getBroadcaster().broadcast("<script id='comet_" + counter++ + "'>"
                        + "window.parent."
                        + callback + "(" + message + ");</script>");
                res.getWriter().write("ok");
                res.getWriter().flush();
            } else if ("start".equals(action)) {
                res.setContentType("text/html;charset=ISO-8859-1");
                res.addHeader("Cache-Control", "private");
                res.addHeader("Pragma", "no-cache");
                String callback = req.getParameterValues("callback")[0];
                if (callback == null) {
                    callback = "alert";
                }
               
                event.suspend();
                String message = "{ message : 'Welcome'}";
                res.getWriter().write("<script id='comet_" + counter++ + "'>"
                        + "window.parent."
                        + callback + "(" + message + ");</script>");
                res.getWriter().write("<html><head><title>Atmosphere Flickr " +
                        "Demo</title></head><body bgcolor=\"#FFFFFF\">");
                res.getWriter().flush();
            }
        }
    }
View Full Code Here

    }

    public void onStateChange(AtmosphereResourceEvent<HttpServletRequest,
            HttpServletResponse> event) throws IOException {

        HttpServletResponse res = event.getResource().getResponse();
        if (event.isResuming()) {
            res.getWriter().write("Atmosphere Sample closed<br/>");
            res.getWriter().write("</body></html>");
        } else {
            res.getWriter().write(event.getMessage().toString());
        }
        res.getWriter().flush();
    }
View Full Code Here

    }
 
    public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {
      HttpServletRequest httpRequest = (HttpServletRequest)request;
        HttpServletResponse httpResponse = (HttpServletResponse)response;
        String url = httpRequest.getServletPath();
        if (url.equals(SETUP_URL)) {
            ConfigEntity config = getDao().getConfigDao().getConfig();
            if (config == null || config.getVersion() == null) {
              SetupBean setupBean = getBusiness().getSetupBean();
              setupBean.clear();
              setupBean.setup();
              logger.info("Setup was successfully completed.");
              setupBean.loadDefaultSite();
            }
          httpResponse.sendRedirect("/");
          return;
        }
        //startProfile();
        chain.doFilter(request, response);
        //endProfile(httpRequest.getRequestURL().toString());
View Full Code Here

  }

  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    HttpSession session = httpRequest.getSession();
        String url = httpRequest.getServletPath();
        VosaoContext ctx = VosaoContext.getInstance();
        autoLogin(httpRequest);
        String userEmail = (String)session.getAttribute(USER_SESSION_ATTR);
        UserEntity user = getDao().getUserDao().getByEmail(userEmail);
    if (user == null) {
      session.removeAttribute(USER_SESSION_ATTR);
      ctx.setUser(null);
      if (url.startsWith(CMS)) {
        String originalUrl = httpRequest.getRequestURI()
          + (httpRequest.getQueryString() == null ? "" :
            "?" + httpRequest.getQueryString());
        session.setAttribute(ORIGINAL_VIEW_KEY, originalUrl);
        httpResponse.sendRedirect(httpRequest.getContextPath()
            + LOGIN_VIEW);
        return;
      }
    }
    else {
      ctx.setUser(user);
      if (url.startsWith(CMS) && ctx.getUser().isSiteUser()) {
        httpResponse.sendRedirect("/");
        return;
      }     
    }
    chain.doFilter(request, response);
  }
View Full Code Here

          }       
      }
     
    public void service(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) throws ServletException, IOException {
          HttpServletRequest req = (HttpServletRequest) servletRequest;
          HttpServletResponse resp = (HttpServletResponse) servletResponse;
          try {
              originalRequest.set(req);
              originalResponse.set(resp);
              Request request = new WebdavServletRequest(req);
              Response response = new ServletResponse(resp);
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.