@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 executeRequest(
        final Callable<HttpResponse> task, final long timeout, final TimeUnit unit)
        throws TimeoutException, IOException {
      ExecutorService executor = Executors.newSingleThreadExecutor();
      try {
        Throwable lastCause = null;
        long endTime = System.currentTimeMillis() + unit.toMillis(timeout);
        while (System.currentTimeMillis() < endTime) {
          Future<HttpResponse> result = executor.submit(task);
          try {
            return result.get(timeout, unit);
          } catch (InterruptedException ex) {
            throw new IllegalStateException(ex);
          } catch (ExecutionException ex) {
            lastCause = ex.getCause();

            // HttpURLConnection throws FileNotFoundException on 404 so handle this
            if (lastCause instanceof FileNotFoundException) {
              HttpResponse httpResult = new HttpResponse();
              httpResult.setStatusCode(HttpURLConnection.HTTP_NOT_FOUND);
              return httpResult;
            } else {
              continue;
            }
          }
        }
        TimeoutException toex = new TimeoutException();
        if (lastCause != null) {
          toex.initCause(lastCause);
        }
        throw toex;
      } finally {
        executor.shutdownNow();
      }
    }
  @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;
  }
  /**
   * Creates and initializes an HttpResponse object suitable to be passed to an HTTP response
   * handler object.
   *
   * @param method The HTTP method that was invoked to get the response.
   * @param request The HTTP request associated with the response.
   * @return The new, initialized HttpResponse object ready to be passed to an HTTP response handler
   *     object.
   * @throws IOException If there were any problems getting any response information from the
   *     HttpClient method object.
   */
  private HttpResponse createResponse(
      HttpRequestBase method, Request<?> request, org.apache.http.HttpResponse apacheHttpResponse)
      throws IOException {
    HttpResponse httpResponse = new HttpResponse(request, method);

    if (apacheHttpResponse.getEntity() != null) {
      httpResponse.setContent(apacheHttpResponse.getEntity().getContent());
    }

    httpResponse.setStatusCode(apacheHttpResponse.getStatusLine().getStatusCode());
    httpResponse.setStatusText(apacheHttpResponse.getStatusLine().getReasonPhrase());
    for (Header header : apacheHttpResponse.getAllHeaders()) {
      httpResponse.addHeader(header.getName(), header.getValue());
    }

    return httpResponse;
  }
 private HttpResponse getHttpResult(HttpMethod method, int retry)
     throws ConnectException, HttpHelperException {
   HttpResponse httpResponse = new HttpResponse();
   this.initMethod(method, retry);
   InputStream is = null;
   BufferedReader reader = null;
   HttpClient client = createHttpClient();
   try {
     client.executeMethod(method);
   } catch (HttpException e1) {
     throw new ConnectException(e1);
   } catch (IOException e1) {
     throw new HttpHelperException(e1);
   }
   try {
     is = method.getResponseBodyAsStream();
     reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
     StringBuilder sb = new StringBuilder();
     char[] ch = new char[3096];
     while (reader.read(ch) != -1) {
       sb.append(ch);
     }
     httpResponse.setStatusCode(method.getStatusCode());
     httpResponse.setStatusText(method.getStatusText());
     httpResponse.setResponseText(sb.toString());
     return httpResponse;
   } catch (Exception e) {
     throw new HttpHelperException(e);
   } finally {
     method.releaseConnection();
     try {
       if (reader != null) {
         reader.close();
       }
     } catch (IOException e) {
       throw new HttpHelperException(e);
     }
     try {
       if (is != null) {
         is.close();
       }
     } catch (IOException e) {
       throw new HttpHelperException(e);
     }
   }
 }
    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();
      }
    }