コード例 #1
0
 @Override
 public String getStringResponse(
     String root, String path, Mappable params, Map<String, String> headers) {
   try {
     int idx = path.indexOf(configuration.getVersion());
     if (idx > -1) path = path.substring(idx + configuration.getVersion().length());
     MultivaluedMap<String, String> queryParams =
         params == null ? new MultivaluedMapImpl() : params.toMap();
     WebResource resource =
         getClient()
             .resource(root)
             .path(configuration.getVersion())
             .path(path)
             .queryParams(queryParams);
     WebResource.Builder builder = resource.getRequestBuilder();
     if (headers != null) {
       for (String key : headers.keySet()) {
         builder = resource.header(key, headers.get(key));
       }
     }
     String response =
         builder
             .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XHTML_XML)
             .get(String.class);
     logger.fine(response);
     return response;
   } catch (BaseSpaceException bs) {
     throw bs;
   } catch (Throwable t) {
     throw new RuntimeException(t);
   }
 }
コード例 #2
0
 @SuppressWarnings("unchecked")
 protected <T extends ApiResponse<?, ?>> T postInternal(
     Class<? extends ApiResponse<?, ?>> clazz,
     String root,
     String path,
     Map<String, String> headers,
     Object data,
     String mediaType) {
   try {
     WebResource webResource =
         getClient().resource(root).path(configuration.getVersion()).path(path);
     WebResource.Builder builder = webResource.getRequestBuilder();
     builder = builder.header("Content-Type", mediaType);
     if (headers != null) {
       for (String key : headers.keySet()) {
         builder = builder.header(key, headers.get(key));
       }
     }
     logger.fine(data.toString());
     String response = builder.accept(mediaType).post(String.class, data);
     logger.fine(response);
     return (T) TypeHelper.INSTANCE.getObjectMapper().readValue(response, clazz);
   } catch (BaseSpaceException bs) {
     throw bs;
   } catch (Throwable t) {
     t.printStackTrace();
     throw new RuntimeException(t);
   }
 }
コード例 #3
0
  /*
   * Send DELETE request to KittyHawk server, and handle redirect/cookies
   *
   * @param resource webResource
   *
   * @param param parameters for delete
   *
   * @throws VNXeException
   */
  public ClientResponse deleteRequest(Object param) throws VNXeException {
    _logger.debug("delete data: " + _url);
    ClientResponse response = sendDeleteRequest(param);

    Status statusCode = response.getClientResponseStatus();
    if (statusCode == ClientResponse.Status.OK
        || statusCode == ClientResponse.Status.ACCEPTED
        || statusCode == ClientResponse.Status.NO_CONTENT) {
      return response;
    } else if (response.getClientResponseStatus() == ClientResponse.Status.UNAUTHORIZED) {
      authenticate();
      response = sendDeleteRequest(param);
      statusCode = response.getClientResponseStatus();
      if (statusCode == ClientResponse.Status.OK
          || statusCode == ClientResponse.Status.ACCEPTED
          || statusCode == ClientResponse.Status.NO_CONTENT) {
        return response;
      }
    }
    int redirectTimes = 1;
    // handle redirect
    while (response.getClientResponseStatus() == ClientResponse.Status.FOUND
        && redirectTimes < VNXeConstants.REDIRECT_MAX) {

      String code = response.getClientResponseStatus().toString();
      String data = response.getEntity(String.class);
      _logger.debug("Returned code: {} returned data: ", code, data);
      WebResource resource = handelRedirect(response);
      if (resource != null) {
        response =
            buildRequest(resource.getRequestBuilder()).entity(param).delete(ClientResponse.class);
        redirectTimes++;
      } else {
        // could not find the redirect url, return
        _logger.error(
            String.format(
                "The post request to: %s failed with: %s %s",
                _url,
                response.getClientResponseStatus().toString(),
                response.getEntity(String.class)));
        throw VNXeException.exceptions.unexpectedDataError(
            "Got redirect status code, but could not get redirected URL");
      }
    }
    if (redirectTimes >= VNXeConstants.REDIRECT_MAX) {
      _logger.error("redirected too many times for the request {} ", _url);
      throw VNXeException.exceptions.unexpectedDataError(
          "Redirected too many times while sending the request for " + _url);
    }

    checkResponse(response, DELETE_REQUEST);

    return response;
  }
コード例 #4
0
ファイル: RestClient.java プロジェクト: JavaAmtho/MRM-PUB2.0
  protected Builder getResource(String url, Map<String, String> headerParameters) {
    WebResource webResource = getClient().resource(url);
    if (headerParameters != null) {
      Set<String> keySet = headerParameters.keySet();

      Builder requestBuilder = webResource.getRequestBuilder();
      for (String key : keySet) {
        requestBuilder.header(key, headerParameters.get(key));
      }
      return requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE);
    }
    return webResource.accept(MediaType.APPLICATION_JSON_TYPE);
  }
コード例 #5
0
 protected Builder addNecessaryHeaders(URI uri, WebResource resource) {
   Map<String, Object> headers = cachedHeaders.get(uri.toString());
   Builder builder = resource.getRequestBuilder();
   if (headers != null) {
     final Object etag = headers.get(HttpHeaders.ETAG);
     if (etag != null) {
       builder.header(HttpHeaders.IF_MATCH, etag);
     }
     final Object lastModified = headers.get(HttpHeaders.LAST_MODIFIED);
     if (lastModified != null) {
       builder.header(HttpHeaders.IF_UNMODIFIED_SINCE, lastModified);
     }
   }
   return builder;
 }
コード例 #6
0
ファイル: RestClient.java プロジェクト: JavaAmtho/MRM-PUB2.0
  @Override
  public ClientResponse post(String url, Map<String, String> headerParameters, String input) {

    WebResource webResource = getClient().resource(url);
    if (headerParameters != null) {
      Set<String> keySet = headerParameters.keySet();

      Builder requestBuilder = webResource.getRequestBuilder();
      for (String key : keySet) {
        requestBuilder.header(key, headerParameters.get(key));
      }
    }

    ClientResponse response =
        webResource.type("application/json").post(ClientResponse.class, input);
    return response;
  }
コード例 #7
0
 protected void deleteInternal(String root, String path, Map<String, String> headers) {
   try {
     WebResource webResource =
         getClient().resource(root).path(configuration.getVersion()).path(path);
     WebResource.Builder builder = webResource.getRequestBuilder();
     if (headers != null) {
       for (String key : headers.keySet()) {
         builder = builder.header(key, headers.get(key));
       }
     }
     String response = builder.delete(String.class);
     logger.fine(response);
   } catch (BaseSpaceException bs) {
     throw bs;
   } catch (Throwable t) {
     t.printStackTrace();
     throw new RuntimeException(t);
   }
 }
コード例 #8
0
ファイル: ALMConnection.java プロジェクト: Jfeurich/almapi
 /**
  * TODO Documentation
  *
  * @param url the rest service URL to request
  * @return the resulting {@link ClientResponse}. If the request fails (HTTP response code NOT 200)
  *     <code>null</code> will be returned.
  */
 private ClientResponse request(final String url) {
   // Set the cookies
   if (loginCookies == null) {
     // TODO throw a real exception here
     throw new RuntimeException("Please login first");
   }
   // build the request
   final DefaultClientConfig cc = new DefaultClientConfig();
   final Client client = Client.create(cc);
   final WebResource resource = client.resource(url);
   final Builder builder = resource.getRequestBuilder();
   builder.accept(MediaType.APPLICATION_XML);
   for (Cookie cookie : loginCookies.getCookies()) {
     builder.cookie(cookie);
   }
   final ClientResponse response = builder.get(ClientResponse.class);
   if (response.getStatus() != 200) {
     LOG.warning("Request failed! HTTP response code: " + response.getStatus());
   }
   return response;
 }
コード例 #9
0
 @Override
 public HttpResponse get(final URI uri, final Optional<Map<String, String>> additionalHeaders) {
   ClientResponse clientResponse = null;
   try {
     final WebResource webResource = queueProxyClient.resource(uri);
     WebResource.Builder builder = webResource.getRequestBuilder();
     if (additionalHeaders.isPresent()) {
       for (final Map.Entry<String, String> entry : additionalHeaders.get().entrySet()) {
         builder = builder.header(entry.getKey(), entry.getValue());
       }
     }
     clientResponse = builder.get(ClientResponse.class);
     return new HttpResponse(clientResponse.getStatus(), clientResponse.getEntity(String.class));
   } catch (ClientHandlerException ex) {
     throw new HttpClientException("Jersey client throwing exception.", ex);
   } finally {
     if (clientResponse != null) {
       clientResponse.close();
     }
   }
 }
コード例 #10
0
  @SuppressWarnings("unchecked")
  @Override
  public <T extends ApiResponse<?, ?>> T putFile(
      Class<? extends ApiResponse<?, ?>> clazz,
      String path,
      Map<String, String> headers,
      InputStream file) {
    try {
      WebResource resource =
          getClient()
              .resource(
                  UriBuilder.fromUri(getConfiguration().getApiRootUri())
                      .path(getConfiguration().getVersion())
                      .path(path)
                      .build());

      WebResource.Builder builder = resource.getRequestBuilder();
      if (headers != null) {
        for (String key : headers.keySet()) {
          builder = builder.header(key, headers.get(key));
        }
      }
      builder =
          builder
              .type(MediaType.APPLICATION_OCTET_STREAM)
              .accept(MediaType.APPLICATION_JSON)
              .entity(file);

      String response = builder.put(String.class);
      logger.fine(response);
      return (T) TypeHelper.INSTANCE.getObjectMapper().readValue(response, clazz);
    } catch (BaseSpaceException bs) {
      throw bs;
    } catch (RuntimeException t) {
      throw t;
    } catch (Throwable t) {
      throw new RuntimeException(t);
    }
  }
コード例 #11
0
 @Override
 protected Builder setResourceHeaders(WebResource resource) {
   return resource.getRequestBuilder();
 }
コード例 #12
0
  /*
   * Send GET request to KittyHawk server, and handle redirect/cookies
   */
  private ClientResponse sendGetRequest(WebResource resource) throws VNXeException {
    _logger.info("getting data: {} ", _url);
    if (_client.isUnity() == true) {
      setFields();
    }
    ClientResponse response =
        buildRequest(addQueryParameters(buildResource(resource)).getRequestBuilder())
            .get(ClientResponse.class);
    Status statusCode = response.getClientResponseStatus();
    _logger.info(response.getStatus() + ":" + response.toString());
    if (statusCode == ClientResponse.Status.OK) {
      String emcCsrfToken = response.getHeaders().getFirst(EMC_CSRF_HEADER);
      if (emcCsrfToken != null) {
        saveEmcCsrfToken(emcCsrfToken);
      }

      saveClientCookies();
      return response;
    } else if (response.getClientResponseStatus() == ClientResponse.Status.UNAUTHORIZED) {
      authenticate();
      response =
          buildRequest(addQueryParameters(buildResource(resource)).getRequestBuilder())
              .get(ClientResponse.class);
      ;
    }
    int redirectTimes = 1;
    while (response.getClientResponseStatus() == ClientResponse.Status.FOUND
        && redirectTimes < VNXeConstants.REDIRECT_MAX) {

      String code = response.getClientResponseStatus().toString();
      String data = response.getEntity(String.class);
      _logger.debug("Returned code: {}, returned data:", code, data);
      WebResource newResource = handelRedirect(response);
      if (newResource != null) {
        response = buildRequest(newResource.getRequestBuilder()).get(ClientResponse.class);
        redirectTimes++;
      } else {
        // could not find the redirect url, return
        _logger.error(
            String.format(
                "The post request to: %s failed with: %s %s",
                _url,
                response.getClientResponseStatus().toString(),
                response.getEntity(String.class)));
        throw VNXeException.exceptions.unexpectedDataError(
            "Got redirect status code, but could not get redirected URL");
      }
    }
    if (redirectTimes >= VNXeConstants.REDIRECT_MAX) {
      _logger.error("redirected too many times for the request {}", _url);
      throw VNXeException.exceptions.unexpectedDataError(
          "Redirected too many times while sending the request for " + _url);
    }

    checkResponse(response, GET_REQUEST);

    List<NewCookie> cookies = response.getCookies();
    if (cookies != null && !cookies.isEmpty()) {
      _requestCookies.addAll(cookies);
    }
    saveClientCookies();

    String emcCsrfToken = response.getHeaders().getFirst(EMC_CSRF_HEADER);
    if (emcCsrfToken != null) {
      saveEmcCsrfToken(emcCsrfToken);
    }
    return response;
  }
コード例 #13
0
  /*
   * Send POST request to KittyHawk server, and handle redirect/cookies
   *
   * @param resource webResource
   *
   * @param ParamBase parameters for post
   *
   * @throws VNXeException
   */
  public ClientResponse postRequest(ParamBase param) throws VNXeException {
    _logger.debug("post data: " + _url);
    ObjectMapper mapper = new ObjectMapper();
    String parmString = null;
    if (param != null) {
      try {
        parmString = mapper.writeValueAsString(param);
        _logger.debug("Content of the post: {}", parmString);
      } catch (JsonGenerationException e) {
        _logger.error("Post request param is not valid. ", e);
        throw VNXeException.exceptions.vnxeCommandFailed("Post request param is not valid.", e);
      } catch (JsonMappingException e) {
        _logger.error("Post request param is not valid. ", e);
        throw VNXeException.exceptions.vnxeCommandFailed("Post request param is not valid.", e);
      } catch (IOException e) {
        _logger.error("Post request param is not valid. ", e);
        throw VNXeException.exceptions.vnxeCommandFailed("Post request param is not valid.", e);
      }
    }
    ClientResponse response =
        buildRequest(addQueryParameters(buildResource(_resource)).getRequestBuilder())
            .entity(parmString)
            .post(ClientResponse.class);
    Status statusCode = response.getClientResponseStatus();
    if (statusCode == ClientResponse.Status.CREATED
        || statusCode == ClientResponse.Status.ACCEPTED
        || statusCode == ClientResponse.Status.OK
        || statusCode == ClientResponse.Status.NO_CONTENT) {
      return response;
    } else if (statusCode == ClientResponse.Status.UNAUTHORIZED) {
      authenticate();
      response =
          buildRequest(addQueryParameters(buildResource(_resource)).getRequestBuilder())
              .entity(parmString)
              .post(ClientResponse.class);
      ;
      statusCode = response.getClientResponseStatus();

      if (statusCode == ClientResponse.Status.OK
          || statusCode == ClientResponse.Status.ACCEPTED
          || statusCode == ClientResponse.Status.NO_CONTENT
          || statusCode == ClientResponse.Status.CREATED) {
        return response;
      }
    }
    int redirectTimes = 1;
    // handle redirect
    while (response.getClientResponseStatus() == ClientResponse.Status.FOUND
        && redirectTimes < VNXeConstants.REDIRECT_MAX) {

      String code = response.getClientResponseStatus().toString();
      String data = response.getEntity(String.class);
      _logger.debug("Returned code: {}, returned data {}", code, data);
      WebResource newResource = handelRedirect(response);
      if (newResource != null) {
        response =
            buildRequest(newResource.getRequestBuilder())
                .entity(parmString)
                .post(ClientResponse.class);
        redirectTimes++;
      } else {
        // could not find the redirect url, return
        _logger.error(
            String.format(
                "The post request to: %s failed with: %s %s",
                _url,
                response.getClientResponseStatus().toString(),
                response.getEntity(String.class)));
        throw VNXeException.exceptions.unexpectedDataError(
            "Got redirect status code, but could not get redirected URL");
      }
    }
    if (redirectTimes >= VNXeConstants.REDIRECT_MAX) {
      _logger.error("redirected too many times for the request {}", _url);
      throw VNXeException.exceptions.unexpectedDataError(
          "Redirected too many times while sending the request for " + _url);
    }
    checkResponse(response, POST_REQUEST);
    return response;
  }