示例#1
0
  public <T> T putWor(
      String url,
      String resourcePath,
      Object object,
      Class<T> responseClass,
      Map<String, Object> queryParams,
      String worToken) {
    WebTarget target = client.target("https://" + url).path(resourcePath);
    if (queryParams != null) {
      for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
        target = target.queryParam(entry.getKey(), entry.getValue());
      }
    }

    Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
    setHeaders(invocationBuilder, worToken);
    Response putResponse =
        invocationBuilder.put(Entity.entity(object, MediaType.APPLICATION_JSON_TYPE));
    if (putResponse.getStatus() != Response.Status.OK.getStatusCode()) {
      Logger.error(
          "PUT call to "
              + url
              + "/"
              + resourcePath
              + " returned status of "
              + putResponse.getStatus());
      return null;
    }

    if (responseClass != null
        && putResponse.hasEntity()
        && putResponse.getStatus() == Response.Status.OK.getStatusCode())
      return putResponse.readEntity(responseClass);
    return null;
  }
 @SuppressWarnings("unchecked")
 private <E> E put(final E entity, String atURI, String withSessionKey) {
   String thePath = atURI;
   String queryParams = "";
   WebTarget resource = resource();
   if (thePath.contains("?")) {
     String[] pathParts = thePath.split("\\?");
     thePath = pathParts[0];
     queryParams = pathParts[1];
   }
   Invocation.Builder resourcePutter =
       applyQueryParameters(queryParams, resource.path(thePath))
           .request(MediaType.APPLICATION_JSON);
   if (isDefined(withSessionKey)) {
     resourcePutter = resourcePutter.header(HTTP_SESSIONKEY, withSessionKey);
   }
   Class<E> c = (Class<E>) entity.getClass();
   return resourcePutter.put(Entity.entity(entity, MediaType.APPLICATION_JSON), c);
 }
示例#3
0
  public <T> T put(String url, String resourcePath, Object object, Class<T> responseClass) {
    WebTarget target = client.target("https://" + url).path(resourcePath);
    Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
    Response putResponse =
        invocationBuilder.put(Entity.entity(object, MediaType.APPLICATION_JSON_TYPE));
    if (putResponse.getStatus() != Response.Status.OK.getStatusCode()) {
      Logger.error(
          "PUT call to "
              + url
              + "/"
              + resourcePath
              + " returned status of "
              + putResponse.getStatus());
      return null;
    }

    if (responseClass != null
        && putResponse.hasEntity()
        && putResponse.getStatus() == Response.Status.OK.getStatusCode())
      return putResponse.readEntity(responseClass);
    return null;
  }
  private Response getAPIResponse(
      String path,
      String method,
      List<Pair> queryParams,
      Object body,
      byte[] binaryBody,
      Map<String, String> headerParams,
      Map<String, Object> formParams,
      String accept,
      String contentType,
      String[] authNames)
      throws ApiException {

    if (body != null && binaryBody != null) {
      throw new ApiException(500, "either body or binaryBody must be null");
    }

    Client client = getClient();
    Response response = null;

    StringBuilder b = new StringBuilder();
    b.append("?");
    if (queryParams != null) {
      for (Pair queryParam : queryParams) {
        if (!queryParam.getName().isEmpty()) {
          b.append(escapeString(queryParam.getName()));
          b.append("=");
          b.append(escapeString(queryParam.getValue()));
          b.append("&");
        }
      }
    }

    String querystring = b.substring(0, b.length() - 1);

    WebTarget target = client.target(basePath + path + querystring);

    if (debugging) {
      target.register(new LoggingFilter());
    }

    if (authentication != null) {
      authentication.setFilter(target);
    }

    Invocation.Builder builder;
    if (accept != null) {
      builder = target.request(accept);
    } else {
      builder = target.request();
    }

    for (Map.Entry<String, String> entry : headerParams.entrySet()) {
      builder = builder.header(entry.getKey(), entry.getValue());
    }
    for (Map.Entry<String, String> entry : defaultHeaderMap.entrySet()) {
      if (!headerParams.containsKey(entry.getKey())) {
        builder = builder.header(entry.getKey(), entry.getValue());
      }
    }

    if ("GET".equals(method)) {
      response = builder.get();
    } else if ("POST".equals(method)) {
      response = builder.post(Entity.entity(body, contentType));
    } else if ("PUT".equals(method)) {
      if (body != null) {
        response = builder.put(Entity.entity(body, contentType));
      } else {
        response = builder.put(Entity.text(""));
      }
    } else if ("DELETE".equals(method)) {
      response = builder.delete();
    } else {
      throw new ApiException(500, "unknown method type " + method);
    }

    return response;
  }
示例#5
0
 public Response put(String url, String resourcePath, Object object) {
   WebTarget target = client.target("https://" + url).path(resourcePath);
   Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
   return invocationBuilder.put(Entity.entity(object, MediaType.APPLICATION_JSON_TYPE));
 }