public <T> T execute(Command<T> command) {
   ClientRequest restRequest = new ClientRequest(url);
   try {
     restRequest.body(MediaType.APPLICATION_XML, new JaxbCommandsRequest(deploymentId, command));
     ClientResponse<Object> response = restRequest.post(Object.class);
     if (response.getResponseStatus() == Status.OK) {
       JaxbCommandsResponse commandResponse = response.getEntity(JaxbCommandsResponse.class);
       List<JaxbCommandResponse<?>> responses = commandResponse.getResponses();
       if (responses.size() == 0) {
         return null;
       } else if (responses.size() == 1) {
         JaxbCommandResponse<?> responseObject = responses.get(0);
         if (responseObject instanceof JaxbExceptionResponse) {
           JaxbExceptionResponse exceptionResponse = (JaxbExceptionResponse) responseObject;
           String causeMessage = exceptionResponse.getCauseMessage();
           throw new RuntimeException(
               exceptionResponse.getMessage()
                   + (causeMessage == null ? "" : " Caused by: " + causeMessage));
         } else {
           return (T) responseObject.getResult();
         }
       } else {
         throw new RuntimeException("Unexpected number of results: " + responses.size());
       }
     } else {
       // TODO error handling
       throw new RuntimeException("REST request error code " + response.getResponseStatus());
     }
   } catch (Exception e) {
     throw new RuntimeException("Unable to execute REST request: " + e.getMessage(), e);
   }
 }
 private void checkResponse(ClientResponse<?> responseObj) throws Exception {
   ClientResponse<?> test = BaseClientResponse.copyFromError(responseObj);
   responseObj.resetStream();
   if (test.getResponseStatus() == javax.ws.rs.core.Response.Status.BAD_REQUEST) {
     throw new BadRequestException(test.getEntity(String.class));
   } else if (test.getResponseStatus() != javax.ws.rs.core.Response.Status.OK) {
     throw new Exception(
         "Request operation failed. Response status = "
             + test.getResponseStatus()
             + "\n\n"
             + test.getEntity(String.class));
   } else {
     logger.debug("Response: " + test.getEntity(String.class));
   }
 }
示例#3
0
 private <T> T doHttpMethod(String url, Class<T> clazz, MediaType accept, HTTPCommand<T> command) {
   ClientRequest request = createClientRequest(url);
   request.accept(accept);
   ClientResponse<T> result = null;
   try {
     addParams(request);
     result = command.execute(request, clazz);
     if (result.getResponseStatus() == Response.Status.OK
         || result.getResponseStatus() == Response.Status.ACCEPTED) {
       T entity;
       LOGGER.info(
           "O servico '{}' respondeu com a seguinte mensagem: {}",
           url,
           (entity = result.getEntity(clazz)).toString());
       return entity;
     }
   } catch (Exception e) {
     LOGGER.warn("Erro durante chamada na URL: {}.", url, e);
   } finally {
     releaseConnection(result);
   }
   return null;
 }
示例#4
0
 public Status doGet(String url, MediaType mediaType) throws Exception {
   ClientRequest request = createClientRequest(url);
   ClientResponse<String> result = null;
   try {
     if (mediaType != null) {
       request.accept(mediaType);
     }
     addParams(request);
     result = request.get(String.class);
     LOGGER.info("O servico '{}' respondeu com a seguinte mensagem: {}.", url, result.getEntity());
     return result.getResponseStatus();
   } catch (Exception e) {
     LOGGER.warn("Erro durante chamada ao metodo GET da URL: {}.", url, e);
     throw e;
   } finally {
     releaseConnection(result);
   }
 }
  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());
  }
示例#6
0
 public Status doPut(String url, MediaType mediaType) throws Exception {
   ClientRequest request = createClientRequest(url);
   ClientResponse<String> result = null;
   try {
     addParams(request);
     String type;
     if (mediaType != null) {
       LOGGER.info(String.format("Content-Type:%s", type = mediaType.toString()));
     } else {
       LOGGER.info(String.format("Content-Type:%s", type = "text/plain"));
     }
     addBody(request, type);
     result = request.put(String.class);
     LOGGER.info("O servico '{}' respondeu com a seguinte mensagem: {}.", url, result.getEntity());
     return result.getResponseStatus();
   } catch (Exception e) {
     LOGGER.warn("Erro durante chamada ao metodo PUT da URL: {}.", url, e);
     throw e;
   } finally {
     releaseConnection(result);
   }
 }
 private void assertStatus(ClientResponse response, Status expected) {
   assertEquals("incorrect status", expected, response.getResponseStatus());
 }