Example #1
0
    protected HttpResponse buildFailureResponse(RequestLine requestLine, MuleMessage message)
        throws TransformerException {
      EndpointURI uri = endpoint.getEndpointURI();
      String failedPath =
          uri.getScheme()
              + "://"
              + uri.getHost()
              + ":"
              + uri.getPort()
              + message.getProperty(HttpConnector.HTTP_REQUEST_PATH_PROPERTY);

      if (logger.isDebugEnabled()) {
        logger.debug("Failed to bind to " + failedPath);
      }

      HttpResponse response = new HttpResponse();
      response.setStatusLine(requestLine.getHttpVersion(), HttpConstants.SC_NOT_FOUND);
      response.setBody(HttpMessages.cannotBindToAddress(failedPath).toString());
      RequestContext.setEvent(
          new DefaultMuleEvent(
              new DefaultMuleMessage(response),
              endpoint,
              new DefaultMuleSession(service, connector.getMuleContext()),
              true));
      // The DefaultResponseTransformer will set the necessary headers
      return transformResponse(response);
    }
  @Route(method = HttpRequest.Method.POST, urlPattern = "/register")
  public HttpResponse register(
      HttpRequest request,
      @Param("name") String name,
      @Param("surname") String surname,
      @Param("email") String email,
      @Param("password") String password) {

    User user = new User(DB.getInstance().getNewId(), name, surname, email, password);
    DB.getInstance().addUser(user);

    HttpResponse response = new HttpResponse("Successfully created an user", 200);

    Cookie c = new Cookie("auth", request);
    response.addCookie(c);

    String sessionId = SessionManager.getSessionIdForRequest(request);
    SessionManager.getInstance().addSession(sessionId, new Integer(user.id));

    try {
      String body = TemplateProcessor.process("profile.html", user.getJsonData().build());
      response.setBody(body);
    } catch (IOException e) {
      response.setStatusCode(500);
    }

    return response;
  }
Example #3
0
 protected HttpResponse doBad(RequestLine requestLine) throws MuleException {
   MuleMessage message = new DefaultMuleMessage(NullPayload.getInstance());
   MuleEvent event =
       new DefaultMuleEvent(
           message,
           endpoint,
           new DefaultMuleSession(message, new NullSessionHandler(), connector.getMuleContext()),
           true);
   OptimizedRequestContext.unsafeSetEvent(event);
   HttpResponse response = new HttpResponse();
   response.setStatusLine(requestLine.getHttpVersion(), HttpConstants.SC_BAD_REQUEST);
   response.setBody(HttpMessages.malformedSyntax().toString() + HttpConstants.CRLF);
   return transformResponse(response);
 }
Example #4
0
 protected HttpResponse doOtherValid(RequestLine requestLine, String method)
     throws MuleException {
   MuleMessage message = new DefaultMuleMessage(NullPayload.getInstance());
   MuleEvent event =
       new DefaultMuleEvent(
           message,
           endpoint,
           new DefaultMuleSession(message, new NullSessionHandler(), connector.getMuleContext()),
           true);
   OptimizedRequestContext.unsafeSetEvent(event);
   HttpResponse response = new HttpResponse();
   response.setStatusLine(requestLine.getHttpVersion(), HttpConstants.SC_METHOD_NOT_ALLOWED);
   response.setBody(HttpMessages.methodNotAllowed(method).toString() + HttpConstants.CRLF);
   return transformResponse(response);
 }
  @Route(method = HttpRequest.Method.GET, urlPattern = "/register")
  public HttpResponse GET(HttpRequest request) {

    HttpResponse response = new HttpResponse();
    response.setStatusCode(200);

    try {
      String body = TemplateProcessor.process("register.html", null);
      response.setBody(body);
    } catch (IOException e) {
      response.setStatusCode(500);
    }

    return response;
  }
    private HttpResponse processResponse(HttpURLConnection conn) throws IOException {
      int responseCode = conn.getResponseCode();

      if (throwExceptionOnFailure && responseCode != HttpURLConnection.HTTP_OK) {
        final InputStream err = conn.getErrorStream();
        if (err != null) {
          try {
            throw new IOException(read(err));
          } finally {
            err.close();
          }
        }
      }
      final InputStream in = conn.getInputStream();
      try {
        HttpResponse result = new HttpResponse();
        result.setStatusCode(responseCode);
        result.setBody(read(in));
        return result;
      } finally {
        in.close();
      }
    }
Example #7
0
 public void handle(HttpRequest request, HttpResponse response) {
   String msg = ("Shutting down server.");
   System.out.println(msg);
   response.setBody(msg);
   ShutdownHook.shutdown();
 }