예제 #1
0
  @Override
  public Suggestions getSuggestions(
      final RequestInfo requestInfo,
      final String selection,
      final String dependent,
      @Nullable final ResponseAction responseAction) {
    try {
      final URL url = getUrl(requestInfo, selection, dependent);

      Invocation.Builder requestBuilder =
          client
              .target(url.toURI())
              .request()
              .accept(MediaType.APPLICATION_XML, MediaType.TEXT_XML, MediaType.APPLICATION_JSON);

      if (requestInfo.getAuthentication() != null) {
        requestBuilder =
            requestBuilder
                .property(
                    HttpAuthenticationFeature.HTTP_AUTHENTICATION_USERNAME,
                    requestInfo.getAuthentication().getUsername())
                .property(
                    HttpAuthenticationFeature.HTTP_AUTHENTICATION_PASSWORD,
                    requestInfo.getAuthentication().getPassword());
      }

      if (responseAction == null) {
        // response does not require transformation
        return requestBuilder.get(Suggestions.class);
      } else {
        // we need to transform the custom XML or JSON response into Suggestions format
        final Response response = requestBuilder.get();
        try (final InputStream is = response.readEntity(InputStream.class)) {
          final MediaType mediaType = response.getMediaType();
          final Path transformation = responseAction.getTransformation();
          if (mediaType != null && mediaType.isCompatible(MediaType.APPLICATION_XML_TYPE)
              || mediaType.isCompatible(MediaType.TEXT_XML_TYPE)) {
            // custom XML response
            LOGGER.debug("Transforming XML response from: {} using: {}", url, transformation);
            return transformXmlResponse(is, transformation);
          } else if (mediaType != null && mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
            LOGGER.debug("Transforming JSON response from: {} using: {}", url, transformation);
            // custom JSON response
            return transformJsonResponse(is, transformation);
          } else {
            LOGGER.error(
                "Response from {} has unsupported Content-Type: {}",
                url,
                mediaType); // TODO(AR) maybe something more visible to the user
            return new Suggestions();
          }
        }
      }
    } catch (final URISyntaxException | IOException | TransformationException e) {
      LOGGER.error(e.getMessage(), e); // TODO(AR) maybe something more visible to the user
      return new Suggestions();
    }
  }
 /**
  * Register a request customizer for a single request.
  *
  * <p>A registered customizer will be used to customize the underlying Async HTTP Client request
  * builder.
  *
  * <p>Invoking this method on an instance that is not configured to use Grizzly Async HTTP Client
  * connector does not have any effect.
  *
  * @param builder JAX-RS request invocation builder.
  * @param customizer request customizer to be registered.
  * @return updated Jersey client config with the Grizzly {@link
  *     org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider.RequestCustomizer}
  *     attached.
  */
 public static Invocation.Builder register(
     Invocation.Builder builder, RequestCustomizer customizer) {
   return builder.property(REQUEST_CUSTOMIZER, customizer);
 }
예제 #3
0
 @Override
 public Invocation property(String name, Object value) {
   invBuilder.property(name, value);
   return this;
 }