Package javango.http

Examples of javango.http.HttpResponse


  public void testSimpleHeaders() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
   
    HttpResponse resp = new SimpleHttpResponse("Hello World");
    resp.render(request, response);
   
    assertEquals(new Integer(200), new Integer(response.getStatus()));
    assertEquals("text/html", response.getContentType())
  }
View Full Code Here


    if (log.isDebugEnabled()) log.debug("Processing path '" + path + "'" );

    ListIterator<Middleware> middlewareIterator=settings.getMiddlewares().listIterator();
    for (; middlewareIterator.hasNext(); ) {
      Middleware m = middlewareIterator.next();
      HttpResponse r = m.processRequest(request);
      if (r != null) {
        return r;
      }
    }
   
    HttpResponse response = null;
    Urls urls = getUrls();
    for(Url url: urls.getUrlPatterns()) {
      if (url.matches(path)) {
        try {
          MethodObjectParams mop = url.getMethodObjectParams(path, request);

          for (Middleware m : settings.getMiddlewares()) {
            m.processMethodAndParams(request, mop);
          }
         
          // TODO Move somewhere else
          for (Annotation annotation : mop.getMethod().getAnnotations()) {
            // TODO Should this return the response,  or let it fall through to the middleware?
            if (annotation instanceof LoginRequired) {
              if (request.getUser() == null || request.getUser() instanceof AnonymousUser) {
                response = new SimpleHttpResponse("Login Required");
              }
            } else if (annotation instanceof RoleRequired) {
              RoleRequired rr = (RoleRequired)annotation;
              if (request.getUser() == null || request.getUser() instanceof AnonymousUser || !request.getUser().hasRole(rr.role())) {             
                response = new SimpleHttpResponse("Not Authorized");
              }
            }
          }
         
          if (response == null ) response = invoke(mop, injector);
          break;
        } catch (Exception e) {
          for (; middlewareIterator.hasPrevious(); ) {
            Middleware m = middlewareIterator.previous();
            response = m.processException(request, response, e);
          }
         
          // got to be a better solution to this...
          //middlewareIterator=settings.getMiddlewares().listIterator();
          for (; middlewareIterator.hasNext(); middlewareIterator.next()) ;
         
          if( response == null) {
            if (e instanceof HttpException) {
              throw (HttpException)e;
            }
            throw new HttpException(e);
          }
        }
      }
    }
   
    if (response != null && response.getContext() != null) {
      // TODO move message handling to template context processor
      // should be an object that will automatically remove messages/errors if they are read from the template
      response.getContext().put("messages", request.getSession().getAndClearMessages());
      response.getContext().put("errors", request.getSession().getAndClearErrors());
      response.getContext().put("absolute_path", request.getContext());
     
      String media_url = settings.getSettings().get("MEDIA_URL");
      if (media_url != null) {
        response.getContext().put("MEDIA_URL", media_url);
      }
     
      for (ContextProcessor rc : settings.getContextProcessors() ) {
        response.getContext().putAll(rc.evaluate(request));
      }
    }
   
    for (; middlewareIterator.hasPrevious(); ) {
      Middleware m = middlewareIterator.previous();
View Full Code Here

    injector.getInstance(HibernateUtil.class).closeSession();
  }


  public void testIndex() throws Exception {
    HttpResponse resp = c.get("");

    assertTrue(resp instanceof FreemarkerResponse);
    FreemarkerResponse freeResp = (FreemarkerResponse)resp;
//    assertEquals(freeResp.getTemplate(), "src/main/webapp/templates/index.html");
   
View Full Code Here

    }
    injector.getInstance(HibernateUtil.class).getSession().getTransaction().commit();
  }

  public void testAdminHomePage() throws Exception {
    HttpResponse r = c.get("admin/");
    assertTrue(r instanceof FreemarkerResponse);
    FreemarkerResponse fr = (FreemarkerResponse)r;
    Writer writer = new CharArrayWriter();
    fr.renderToWriter(writer);
    //System.out.println(writer.toString());
View Full Code Here

    assertTrue(writer.toString().contains("<a href=\"./javango.contrib.admin.tests.Choice/\">Your Choice</a>"));

  }
 
  public void testPollList() throws Exception {
    HttpResponse r = c.get("admin/javango.contrib.admin.tests.Poll/");
    assertTrue(r instanceof FreemarkerResponse);
    FreemarkerResponse fr = (FreemarkerResponse)r;
    Writer writer = new CharArrayWriter();
    fr.renderToWriter(writer);
    //System.out.println(writer.toString());
View Full Code Here

    fr.renderToWriter(writer);
    assertTrue(writer.toString().contains("AdminTestQuestion : 0"));
  }
 
  public void testAddPoll() throws Exception {
    HttpResponse r = c.get("admin/javango.contrib.admin.tests.Poll/add/");
    assertTrue(r instanceof FreemarkerResponse);
    FreemarkerResponse fr = (FreemarkerResponse)r;
   
    Writer writer = new CharArrayWriter();
    fr.renderToWriter(writer);
    System.out.println(writer.toString());
    String expected =
        "<tr><th><label for='id_pubDate'>Pub Date</label></th><td><input id=\"id_pubDate\" type=\"text\" name=\"pubDate\" /></td></tr>\n" +
        "<tr><th><label for='id_question'>Question</label></th><td><input maxlength=\"255\" id=\"id_question\" type=\"text\" name=\"question\" /></td></tr>\n";
   
    // TODO This is not 100% correct as the form could possibly be in this order even without the fieldset
    assertTrue(writer.toString().contains(expected));
   
    Map<String, String[]> params = new HashMap<String, String[]>();
    params.put("question", new String[] {"Do you like cheese?"});
    params.put("pubDate", new String[] {"01/02/2008"});
    r = c.post("admin/javango.contrib.admin.tests.Poll/add/", params);
    assertEquals(HttpResponseRedirect.class, r.getClass());
   
    r = c.get("admin/javango.contrib.admin.tests.Poll/");
    assertTrue(r instanceof FreemarkerResponse);
    fr = (FreemarkerResponse)r;
    writer = new CharArrayWriter();
View Full Code Here

    injector.getInstance(HibernateUtil.class).closeSession();
  }


  public void testIndex() throws Exception {
    HttpResponse resp = c.get("");

    assertTrue(resp instanceof FreemarkerResponse);
    FreemarkerResponse freeResp = (FreemarkerResponse)resp;
//    assertEquals(freeResp.getTemplate(), "src/main/webapp/templates/index.html");
   
View Full Code Here

        request.setUser(new AnonymousUser());
      } else {
        request.setUser(new PrincipalUser(servletRequest));
      }
     
      HttpResponse response = requestProcessor.service(request);
      response.render(servletRequest, servletResponse);
     
      // tell the request that we are done so it can notify any listeners.  good place to close database sessions, etc.
      request.close();
    } catch (Http404 e) {
      String level = injector.getInstance(Settings.class).get(BROKEN_LINK_LOG_LEVEL);
      if (level != null) {
        if ("trace".equalsIgnoreCase(level)) {
          log.trace(e,e);
        } else if ("debug".equalsIgnoreCase(level)) {
          log.debug(e,e);
        } else if ("info".equalsIgnoreCase(level)) {
          log.info(e,e);
        } else if ("warn".equalsIgnoreCase(level)) {
          log.warn(e,e);
        } else if ("error".equalsIgnoreCase(level)) {
          log.error(e,e);
        } else if ("fatal".equalsIgnoreCase(level)) {
          log.fatal(e,e);
        }
      }
      if (!injector.getInstance(Settings.class).isDebug()) {
        // TODO ability to specify 404 handler
        SimpleHttpResponse response = new SimpleHttpResponse("Page not found");
        response.setStatusCode(404);
        try {
          response.render(servletRequest, servletResponse);
        } catch (HttpException e2) {
          log.error(e2,e2);
          throw new ServletException(e);
        }
      } else {
        throw new ServletException(e);
      }
    } catch (FileUploadException e) {
      log.error(e,e);
      try {
        request.close();
      } catch (HttpException e1) {
        log.error(e1,e1);
      }
      // TODO Pretty print the exception and stack trace,  request information, etc
      if (!injector.getInstance(Settings.class).isDebug()) {
        HttpResponse response = new SimpleHttpResponse("Unrecoverable error processing uploaded files, please contact support");
        try {
          response.render(servletRequest, servletResponse);
        } catch (HttpException e2) {
          log.error(e2,e2);
          throw new ServletException(e);
        }
      }
    } catch (HttpException e) {
      log.error(String.format("HttpException while processing for user '%s' %s:%s", request.getUser(), context, path));
      log.error(e,e);
      try {
        request.close();
      } catch (HttpException e1) {
        log.error(e1,e1);
      }
      // TODO Pretty print the exception and stack trace,  request information, etc
      // TODO can this exception be handled by middleware?  middleware is not really avaliable here as it is handled in the requestProcessor...
      if (!injector.getInstance(Settings.class).isDebug()) {
        HttpResponse response = new SimpleHttpResponse("Unrecoverable error, please contact support");
        try {
          response.render(servletRequest, servletResponse);
        } catch (HttpException e2) {
          log.error(e2,e2);
          throw new ServletException(e);
        }
      } else {
View Full Code Here

TOP

Related Classes of javango.http.HttpResponse

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.