Example #1
0
  public static ResteasyUriInfo extractUriInfo(HttpExchange exchange) {
    String host = exchange.getLocalAddress().getHostName();
    if (exchange.getLocalAddress().getPort() != 80 && exchange.getLocalAddress().getPort() != 443) {
      host += ":" + exchange.getLocalAddress().getPort();
    }
    String uri = exchange.getRequestURI().toString();

    String protocol =
        exchange.getHttpContext().getServer() instanceof HttpsServer ? "https" : "http";

    URI absoluteURI = URI.create(protocol + "://" + host + uri);

    String contextPath = exchange.getHttpContext().getPath();
    String path = PathHelper.getEncodedPathInfo(absoluteURI.getRawPath(), contextPath);
    if (!path.startsWith("/")) {
      path = "/" + path;
    }

    URI baseURI = absoluteURI;
    if (!path.trim().equals("")) {
      String tmpContextPath = contextPath;
      if (!tmpContextPath.endsWith("/")) tmpContextPath += "/";
      baseURI =
          UriBuilder.fromUri(absoluteURI).replacePath(tmpContextPath).replaceQuery(null).build();
    } else {
      baseURI = UriBuilder.fromUri(absoluteURI).replaceQuery(null).build();
    }
    URI relativeURI = UriBuilder.fromUri(path).replaceQuery(absoluteURI.getRawQuery()).build();
    // System.out.println("path: " + path);
    // System.out.println("query string: " + request.getQueryString());
    ResteasyUriInfo uriInfo = new ResteasyUriInfo(baseURI, relativeURI);
    return uriInfo;
  }
 public void handle(HttpExchange ex) throws IOException {
   if (ex == null) {
     throw new NullPointerException();
   }
   HttpContext context = ex.getHttpContext();
   new Filter.Chain(context.getFilters(), context.getHandler()).doFilter(ex);
 }
 @Override
 @Property(MessageContext.PATH_INFO)
 public String getPathInfo() {
   URI requestUri = httpExchange.getRequestURI();
   String reqPath = requestUri.getPath();
   String ctxtPath = httpExchange.getHttpContext().getPath();
   if (reqPath.length() > ctxtPath.length()) {
     return reqPath.substring(ctxtPath.length());
   }
   return null;
 }
Example #4
0
  public void handleEvent(HttpExchange he) {
    try {
      CCActivityServer.logger.info("received something");
      final Headers headers = he.getResponseHeaders();
      for (List<String> list : headers.values())
        for (String s : list) CCActivityServer.logger.info(s);
      final String requestMethod = he.getRequestMethod().toUpperCase();
      CCActivityServer.logger.info(requestMethod);
      switch (requestMethod) {
        case METHOD_GET:
          final Map<String, List<String>> requestParameters =
              getRequestParameters(he.getRequestURI());
          for (String key : requestParameters.keySet()) {
            CCActivityServer.logger.info("Chiave: " + key + "\nvalori:");
            for (String s : requestParameters.get(key)) CCActivityServer.logger.info(s);
          }
          ClientServerResult result;
          CCActivityServer.logger.info(he.getHttpContext().getPath());

          switch (he.getHttpContext().getPath()) {
            case "/selectTest":
              try {
                result = DBManager.select(requestParameters, this);
              } catch (Exception e) {
                result = null;
                e.printStackTrace();
              }
              break;
            case API_PATH + API_FANTAPLAYERS:
              FootballParser fp = new FootballParser();
              System.out.println(
                  "parsing\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
              fp.parse();
              System.out.println(
                  "parsed\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
              fp.setToList(
                  requestParameters.get("options") != null
                      && requestParameters.get("options").contains("list"));
              Object players = fp.getPlayers(requestParameters.get("players"));
              try {
                result = ClientServerGenericResult.createResult(players);
                break;
              } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
              }
            default:
              result = ClientServerGenericResult.createResult("");
              result.resultType = ClientServerResult.RESULTFAIL;
          }
          final String responseBody = JSonParser.getJSon(result, false);
          CCActivityServer.logger.info(responseBody);
          headers.set(HEADER_CONTENT_TYPE, String.format("application/json; charset=%s", CHARSET));
          final byte[] rawResponseBody = responseBody.getBytes(CHARSET);
          he.sendResponseHeaders(STATUS_OK, rawResponseBody.length);
          he.getResponseBody().write(rawResponseBody);
          System.out.println(
              "finito\t" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
          break;
        case METHOD_OPTIONS:
          headers.set(HEADER_ALLOW, ALLOWED_METHODS);
          he.sendResponseHeaders(STATUS_OK, NO_RESPONSE_LENGTH);
          break;
        default:
          headers.set(HEADER_ALLOW, ALLOWED_METHODS);
          he.sendResponseHeaders(STATUS_METHOD_NOT_ALLOWED, NO_RESPONSE_LENGTH);
          break;
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      he.close();
    }
  }