public void run() {
   try {
     ClientRequest req = new ClientRequest(url);
     req.header("Accept-Wait", acceptWaitTime);
     ClientResponse<?> response = req.post();
     response.releaseConnection();
     isFinished = true;
   } catch (Exception e) {
     failed = true;
   }
 }
  @Test
  public void testGetCustomerByIdUsingClientRequest() throws Exception {
    // deploymentUrl = new URL("http://localhost:8180/test/");
    // GET http://localhost:8080/test/rest/customer/1
    ClientRequest request =
        new ClientRequest(deploymentUrl.toString() + RESOURCE_PREFIX + "/customer/1");
    request.header("Accept", MediaType.APPLICATION_XML);

    // we're expecting a String back
    ClientResponse<String> responseObj = request.get(String.class);

    Assert.assertEquals(200, responseObj.getStatus());
    System.out.println("GET /customer/1 HTTP/1.1\n\n" + responseObj.getEntity());

    String response = responseObj.getEntity().replaceAll("<\\?xml.*\\?>", "").trim();
    Assert.assertEquals("<customer><id>1</id><name>Acme Corporation</name></customer>", response);
  }
  public <T> OpenStackResponse request(OpenStackRequest<T> request) {
    ClientRequest client =
        new ClientRequest(
            UriBuilder.fromUri(request.endpoint() + "/" + request.path()),
            ClientRequest.getDefaultExecutor(),
            providerFactory);

    for (Map.Entry<String, List<Object>> entry : request.queryParams().entrySet()) {
      for (Object o : entry.getValue()) {
        client = client.queryParameter(entry.getKey(), String.valueOf(o));
      }
    }

    for (Entry<String, List<Object>> h : request.headers().entrySet()) {
      StringBuilder sb = new StringBuilder();
      for (Object v : h.getValue()) {
        sb.append(String.valueOf(v));
      }
      client.header(h.getKey(), sb);
    }

    if (request.entity() != null) {
      client.body(request.entity().getContentType(), request.entity().getEntity());
    }

    ClientResponse<T> response;

    try {
      response = client.httpMethod(request.method().name(), request.returnType());
    } catch (Exception e) {
      throw new RuntimeException("Unexpected client exception", e);
    }

    if (response.getStatus() == HttpStatus.SC_OK
        || response.getStatus() == HttpStatus.SC_CREATED
        || response.getStatus() == HttpStatus.SC_NO_CONTENT) {
      return new RESTEasyResponse(response);
    }

    response.releaseConnection();

    throw new OpenStackResponseException(
        response.getResponseStatus().getReasonPhrase(), response.getStatus());
  }
예제 #4
0
  public ClientResponse execute(ClientExecutionContext ctx) throws Exception {
    ClientRequest request = ctx.getRequest();
    if (!request.getHttpMethod().equals("GET")) {
      return ctx.proceed();
    }

    BrowserCache.Entry entry = getEntry(request);
    if (entry == null) {
      return cache(request, ctx.proceed());
    }

    if (entry.expired()) {
      cache.remove(request.getUri(), entry.getMediaType());
      BrowserCache.Header[] headers = entry.getValidationHeaders();
      for (BrowserCache.Header header : headers) {
        request.header(header.getName(), header.getValue());
      }
      return handleExpired(ctx, request, entry);
    }

    return createClientResponse(request, entry);
  }
예제 #5
0
  @Override
  public boolean push(ClientMessage message) {
    ActiveMQRestLogger.LOGGER.debug("Pushing " + message);
    String uri = createUri(message);
    for (int i = 0; i < registration.getMaxRetries(); i++) {
      long wait = registration.getRetryWaitMillis();
      System.out.println("Creating request from " + uri);
      ClientRequest request = executor.createRequest(uri);
      request.followRedirects(false);
      ActiveMQRestLogger.LOGGER.debug("Created request " + request);

      for (XmlHttpHeader header : registration.getHeaders()) {
        ActiveMQRestLogger.LOGGER.debug(
            "Setting XmlHttpHeader: " + header.getName() + "=" + header.getValue());
        request.header(header.getName(), header.getValue());
      }
      HttpMessageHelper.buildMessage(message, request, contentType, jmsOptions);
      ClientResponse<?> res = null;
      try {
        ActiveMQRestLogger.LOGGER.debug(method + " " + uri);
        res = request.httpMethod(method);
        int status = res.getStatus();
        ActiveMQRestLogger.LOGGER.debug("Status of push: " + status);
        if (status == 503) {
          String retryAfter = res.getStringHeaders().getFirst("Retry-After");
          if (retryAfter != null) {
            wait = Long.parseLong(retryAfter) * 1000;
          }
        } else if (status == 307) {
          uri = res.getLocation().toString();
          wait = 0;
        } else if ((status >= 200 && status < 299) || status == 303 || status == 304) {
          ActiveMQRestLogger.LOGGER.debug("Success");
          return true;
        } else if (status >= 400) {
          switch (status) {
            case 400: // these usually mean the message you are trying to send is crap, let dead
                      // letter logic take over
            case 411:
            case 412:
            case 413:
            case 414:
            case 415:
            case 416:
              throw new RuntimeException(
                  "Something is wrong with the message, status returned: "
                      + status
                      + " for push registration of URI: "
                      + uri);
            case 401: // might as well consider these critical failures and abort.  Immediately
                      // signal to disable push registration depending on config
            case 402:
            case 403:
            case 405:
            case 406:
            case 407:
            case 417:
            case 505:
              return false;
            case 404: // request timeout, gone, and not found treat as a retry
            case 408:
            case 409:
            case 410:
              break;
            default: // all 50x requests just retry (except 505)
              break;
          }
        }
      } catch (Exception e) {
        ActiveMQRestLogger.LOGGER.debug("failed to push message to " + uri, e);
        e.printStackTrace();
        return false;
      } finally {
        if (res != null) res.releaseConnection();
      }
      try {
        if (wait > 0) Thread.sleep(wait);
      } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted");
      }
    }
    return false;
  }