Exemple #1
0
  public void sendRequest(MetaInfo meta) {
    System.out.println(Thread.currentThread().getId() + " start sendRequest");
    URI uri = null;
    try {
      System.out.println(meta.getParams());
      uri = new URI(meta.getUrl());
    } catch (URISyntaxException e) {
      e.printStackTrace(); // To change body of catch statement use File | Settings | File
      // Templates.
    }
    String host = uri.getHost();
    int port = 80;

    HttpRequest request =
        new DefaultHttpRequest(
            HttpVersion.HTTP_1_1, HttpMethod.valueOf(meta.getMethod()), uri.toASCIIString());
    meta.buildHttpRequestHeader(request);

    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    Channel channel = future.getChannel();
    channel.getPipeline().addLast("handler", new DownloaderHandler());
    GlobalVar.metaInfoVar.set(channel, meta);

    future.addListener(new ConnectOk(request));
    channel.getCloseFuture().awaitUninterruptibly().addListener(new ConnectClose());
    System.out.println(Thread.currentThread().getId() + " end sendRequest");
  }
  protected HttpContext read(HttpServletRequest request) throws IOException {
    String url = request.getPathInfo();
    HttpMethod httpMethod = HttpMethod.valueOf(request.getMethod());
    HttpHeaders headers = this.headers(request);
    String content = this.content(request);

    return new HttpContext(url, httpMethod, headers, content);
  }
  private HttpUriRequest newHcRequest(HttpRequest request) throws IOException {
    URI uri = request.getUri().toJavaUri();
    HttpUriRequest httpUriRequest = HttpMethod.valueOf(request.getMethod()).newMessage(uri);

    for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) {
      httpUriRequest.addHeader(entry.getKey(), StringUtils.join(entry.getValue(), ','));
    }
    return httpUriRequest;
  }
Exemple #4
0
 /** Returns the value of the {@code Access-Control-Allow-Methods} response header. */
 public List<HttpMethod> getAccessControlAllowMethods() {
   List<HttpMethod> result = new ArrayList<HttpMethod>();
   String value = getFirst(ACCESS_CONTROL_ALLOW_METHODS);
   if (value != null) {
     String[] tokens = value.split(",\\s*");
     for (String token : tokens) {
       result.add(HttpMethod.valueOf(token));
     }
   }
   return result;
 }
Exemple #5
0
 /**
  * Return the set of allowed {@link HttpMethod HTTP methods}, as specified by the {@code Allow}
  * header.
  *
  * <p>Returns an empty set when the allowed methods are unspecified.
  */
 public Set<HttpMethod> getAllow() {
   String value = getFirst(ALLOW);
   if (StringUtil.isNotBlank(value)) {
     List<HttpMethod> allowedMethod = new ArrayList<HttpMethod>(5);
     String[] tokens = value.split(",\\s*");
     for (String token : tokens) {
       allowedMethod.add(HttpMethod.valueOf(token));
     }
     return EnumSet.copyOf(allowedMethod);
   } else {
     return EnumSet.noneOf(HttpMethod.class);
   }
 }
 @Override
 public void service(Request request, Response response) throws Exception {
   String uri = request.getRequestURI();
   Map<HttpMethod, GrizzletHandler> handlerByMethod = handlers.get(uri);
   GrizzletHandler handler = null;
   if (handlerByMethod != null) {
     handler = handlerByMethod.get(HttpMethod.valueOf(request.getMethod().toString()));
   }
   if (handler != null) {
     handler.handle(request, response);
     SimpleGrizzlyAdapterChain.requestServiced();
   }
 }
  @Override
  public void parseValidateAddRoute(String route, Object target) {
    try {
      int singleQuoteIndex = route.indexOf(SINGLE_QUOTE);
      String httpMethod = route.substring(0, singleQuoteIndex).trim().toLowerCase();
      String url = route.substring(singleQuoteIndex + 1, route.length() - 1).trim().toLowerCase();

      // Use special enum stuff to get from value
      HttpMethod method;
      try {
        method = HttpMethod.valueOf(httpMethod);
      } catch (IllegalArgumentException e) {
        LOG.error(
            "The @Route value: " + route + " has an invalid HTTP method part: " + httpMethod + ".");
        return;
      }
      addRoute(method, url, target);
    } catch (Exception e) {
      LOG.error("The @Route value: " + route + " is not in the correct format", e);
    }
  }
Exemple #8
0
  private boolean isHttpMethodValid(HttpServletRequest request) {
    boolean result = false;
    if (allowAllMethods) {
      result = true;
    } else {
      String requestMethodString = request.getMethod();
      HttpMethod requestMethod = null;
      boolean isKnownHttpMethod;
      try {
        requestMethod = HttpMethod.valueOf(requestMethodString);
        isKnownHttpMethod = true;
      } catch (IllegalArgumentException e) {
        isKnownHttpMethod = false;
      }
      if (isKnownHttpMethod) {
        result = allowedKnownHttpMethods.contains(requestMethod);
      } else {
        result = allowedUnknownHttpMethods.contains(requestMethodString);
      }
    }

    return result;
  }
  /**
   * @see com.consol.citrus.message.MessageSender#send(org.springframework.integration.Message)
   * @throws CitrusRuntimeException
   */
  public void send(Message<?> message) {
    String endpointUri;
    if (endpointUriResolver != null) {
      endpointUri = endpointUriResolver.resolveEndpointUri(message, getRequestUrl());
    } else {
      endpointUri = getRequestUrl();
    }

    log.info("Sending HTTP message to: '" + endpointUri + "'");

    if (log.isDebugEnabled()) {
      log.debug("Message to be sent:\n" + message.getPayload().toString());
    }

    HttpMethod method = requestMethod;
    if (message.getHeaders().containsKey(CitrusHttpMessageHeaders.HTTP_REQUEST_METHOD)) {
      method =
          HttpMethod.valueOf(
              (String) message.getHeaders().get(CitrusHttpMessageHeaders.HTTP_REQUEST_METHOD));
    }

    HttpEntity<?> requestEntity = generateRequest(message, method);

    restTemplate.setErrorHandler(new InternalResponseErrorHandler(message));
    ResponseEntity<?> response =
        restTemplate.exchange(endpointUri, method, requestEntity, String.class);

    log.info("HTTP message was successfully sent to endpoint: '" + endpointUri + "'");

    informReplyMessageHandler(
        buildResponseMessage(
            response.getHeaders(),
            response.getBody() != null ? response.getBody() : "",
            response.getStatusCode()),
        message);
  }
 @Provides
 @RequestScoped
 HttpMethod provideRequestMethod() {
   return HttpMethod.valueOf(RequestHandlerServlet.getRequest().getMethod());
 }
Exemple #11
0
 /** Returns the value of the {@code Access-Control-Request-Method} request header. */
 public HttpMethod getAccessControlRequestMethod() {
   String value = getFirst(ACCESS_CONTROL_REQUEST_METHOD);
   return (value != null ? HttpMethod.valueOf(value) : null);
 }
 @Override
 protected HttpMessage createMessage(String[] initialLine) throws Exception {
   return new DefaultHttpRequest(
       HttpVersion.valueOf(initialLine[2]), HttpMethod.valueOf(initialLine[0]), initialLine[1]);
 }
 public Builder httpMethod(String httpMethod) {
   this.httpMethod = HttpMethod.valueOf(httpMethod);
   return this;
 }
Exemple #14
0
  private void initHttpMethodValidityVerification() {

    assert (null == allowedUnknownHttpMethods);
    assert (null != defaultAllowedHttpMethods);
    assert (null == allHttpMethods);
    allHttpMethods = EnumSet.allOf(HttpMethod.class);

    // Configure our permitted HTTP methods

    allowedUnknownHttpMethods = Collections.emptySet();
    allowedKnownHttpMethods = defaultAllowedHttpMethods;

    String[] methods;
    String allowedHttpMethodsString =
        servletConfig.getServletContext().getInitParameter(ALLOWED_HTTP_METHODS_ATTR);
    if (null != allowedHttpMethodsString) {
      methods = allowedHttpMethodsString.split("\\s+");
      assert (null != methods); // assuming split always returns a non-null array result
      allowedUnknownHttpMethods = new HashSet(methods.length);
      List<String> allowedKnownHttpMethodsStringList = new ArrayList<String>();
      // validate input against allHttpMethods data structure
      for (String cur : methods) {
        if (cur.equals("*")) {
          allowAllMethods = true;
          allowedUnknownHttpMethods = Collections.emptySet();
          return;
        }
        boolean isKnownHttpMethod;
        try {
          HttpMethod.valueOf(cur);
          isKnownHttpMethod = true;
        } catch (IllegalArgumentException e) {
          isKnownHttpMethod = false;
        }

        if (!isKnownHttpMethod) {
          if (LOGGER.isLoggable(Level.WARNING)) {
            HttpMethod[] values = HttpMethod.values();
            Object[] arg = new Object[values.length + 1];
            arg[0] = cur;
            System.arraycopy(values, HttpMethod.OPTIONS.ordinal(), arg, 1, values.length);
            LOGGER.log(Level.WARNING, "warning.webapp.facesservlet.init_invalid_http_method", arg);
          }
          // prevent duplicates
          if (!allowedUnknownHttpMethods.contains(cur)) {
            allowedUnknownHttpMethods.add(cur);
          }
        } else {
          // prevent duplicates
          if (!allowedKnownHttpMethodsStringList.contains(cur)) {
            allowedKnownHttpMethodsStringList.add(cur);
          }
        }
      }
      // Optimally initialize allowedKnownHttpMethods
      if (5 == allowedKnownHttpMethodsStringList.size()) {
        allowedKnownHttpMethods =
            EnumSet.of(
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(0)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(1)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(2)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(3)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(4)));
      } else if (4 == allowedKnownHttpMethodsStringList.size()) {
        allowedKnownHttpMethods =
            EnumSet.of(
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(0)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(1)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(2)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(3)));

      } else if (3 == allowedKnownHttpMethodsStringList.size()) {
        allowedKnownHttpMethods =
            EnumSet.of(
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(0)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(1)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(2)));

      } else if (2 == allowedKnownHttpMethodsStringList.size()) {
        allowedKnownHttpMethods =
            EnumSet.of(
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(0)),
                HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(1)));

      } else if (1 == allowedKnownHttpMethodsStringList.size()) {
        allowedKnownHttpMethods =
            EnumSet.of(HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(0)));

      } else {
        List<HttpMethod> restList =
            new ArrayList<HttpMethod>(allowedKnownHttpMethodsStringList.size() - 1);
        for (int i = 1; i < allowedKnownHttpMethodsStringList.size() - 1; i++) {
          restList.add(HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(i)));
        }
        HttpMethod first = HttpMethod.valueOf(allowedKnownHttpMethodsStringList.get(0));
        HttpMethod[] rest = new HttpMethod[restList.size()];
        restList.toArray(rest);
        allowedKnownHttpMethods = EnumSet.of(first, rest);
      }
    }
  }