Beispiel #1
0
  /**
   * Create the new Jetty client connector.
   *
   * @param jaxrsClient JAX-RS client instance, for which the connector is created.
   * @param config client configuration.
   */
  JettyConnector(final Client jaxrsClient, final Configuration config) {

    final SSLContext sslContext = getSslContext(jaxrsClient, config);
    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setSslContext(sslContext);
    this.client = new HttpClient(sslContextFactory);

    final Object connectTimeout = config.getProperties().get(ClientProperties.CONNECT_TIMEOUT);
    if (connectTimeout != null
        && connectTimeout instanceof Integer
        && (Integer) connectTimeout > 0) {
      client.setConnectTimeout((Integer) connectTimeout);
    }
    final Object threadPoolSize =
        config.getProperties().get(ClientProperties.ASYNC_THREADPOOL_SIZE);
    if (threadPoolSize != null
        && threadPoolSize instanceof Integer
        && (Integer) threadPoolSize > 0) {
      final String name = HttpClient.class.getSimpleName() + "@" + hashCode();
      final QueuedThreadPool threadPool = new QueuedThreadPool((Integer) threadPoolSize);
      threadPool.setName(name);
      client.setExecutor(threadPool);
    }
    Boolean disableCookies =
        (Boolean) config.getProperties().get(JettyClientProperties.DISABLE_COOKIES);
    disableCookies = (disableCookies != null) ? disableCookies : false;

    final AuthenticationStore auth = client.getAuthenticationStore();
    final Object basicAuthProvider =
        config.getProperty(JettyClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION);
    if (basicAuthProvider != null && (basicAuthProvider instanceof BasicAuthentication)) {
      auth.addAuthentication((BasicAuthentication) basicAuthProvider);
    }

    final Object proxyUri = config.getProperties().get(ClientProperties.PROXY_URI);
    if (proxyUri != null) {
      final URI u = getProxyUri(proxyUri);
      final ProxyConfiguration proxyConfig = client.getProxyConfiguration();
      proxyConfig.getProxies().add(new HttpProxy(u.getHost(), u.getPort()));
    }

    if (disableCookies) {
      client.setCookieStore(new HttpCookieStore.Empty());
    }

    try {
      client.start();
    } catch (final Exception e) {
      throw new ProcessingException("Failed to start the client.", e);
    }
    this.cookieStore = client.getCookieStore();
  }
Beispiel #2
0
  private SSLContext getSslContext(final Client client, final Configuration config) {
    final SslConfigurator sslConfigurator =
        PropertiesHelper.getValue(
            config.getProperties(), JettyClientProperties.SSL_CONFIG, SslConfigurator.class, null);

    return sslConfigurator != null ? sslConfigurator.createSSLContext() : client.getSslContext();
  }
  @Override
  public boolean configure(final FeatureContext context) {
    final Configuration config = context.getConfiguration();

    if (!config.isRegistered(MustacheTemplateProcessor.class)) {
      // Template Processor.
      context.register(MustacheTemplateProcessor.class);

      // MvcFeature.
      if (!config.isRegistered(MvcFeature.class)) {
        context.register(MvcFeature.class);
      }

      return true;
    }
    return false;
  }
 @Override
 public ResteasyClientBuilder withConfig(Configuration config) {
   providerFactory = new ResteasyProviderFactory();
   providerFactory.setProperties(config.getProperties());
   for (Class clazz : config.getClasses()) {
     Map<Class<?>, Integer> contracts = config.getContracts(clazz);
     try {
       register(clazz, contracts);
     } catch (RuntimeException e) {
       throw new RuntimeException("failed on registering class: " + clazz.getName(), e);
     }
   }
   for (Object obj : config.getInstances()) {
     Map<Class<?>, Integer> contracts = config.getContracts(obj.getClass());
     register(obj, contracts);
   }
   return this;
 }
Beispiel #5
0
    @Override
    public Builder request() {
      checkClosed();

      initTargetClientIfNeeded();

      ClientProviderFactory pf =
          ClientProviderFactory.getInstance(WebClient.getConfig(targetClient).getEndpoint());
      List<Object> providers = new LinkedList<Object>();
      Configuration cfg = configImpl.getConfiguration();
      for (Object p : cfg.getInstances()) {
        if (!(p instanceof Feature)) {
          Map<Class<?>, Integer> contracts = cfg.getContracts(p.getClass());
          if (contracts == null || contracts.isEmpty()) {
            providers.add(p);
          } else {
            providers.add(new FilterProviderInfo<Object>(p, pf.getBus(), contracts));
          }
        }
      }

      pf.setUserProviders(providers);
      WebClient.getConfig(targetClient)
          .getRequestContext()
          .putAll(getConfiguration().getProperties());
      WebClient.getConfig(targetClient)
          .getRequestContext()
          .put(Client.class.getName(), ClientImpl.this);
      WebClient.getConfig(targetClient)
          .getRequestContext()
          .put(Configuration.class.getName(), getConfiguration());
      // TLS
      TLSClientParameters tlsParams = secConfig.getTlsClientParams();
      if (tlsParams.getSSLSocketFactory() != null || tlsParams.getTrustManagers() != null) {
        WebClient.getConfig(targetClient).getHttpConduit().setTlsClientParameters(tlsParams);
      }

      // start building the invocation
      return new InvocationBuilderImpl(WebClient.fromClient(targetClient));
    }
  private Map<String, Object> getConfigProperties(
      final Configuration config, final Set<String> propertyNames) {
    final Map<String, Object> properties = Maps.newHashMap();

    for (final String propertyName : propertyNames) {
      final Object property = config.getProperty(propertyName);
      if (property != null) {
        properties.put(propertyName, property);
      }
    }

    return properties;
  }
    @Override
    public void writeTo(
        final String t,
        final Class<?> type,
        final Type genericType,
        final Annotation[] annotations,
        final MediaType mediaType,
        final MultivaluedMap<String, Object> httpHeaders,
        final OutputStream entityStream)
        throws IOException, WebApplicationException {
      // Underlying stream should not be closed and Jersey is preventing from closing it.
      entityStream.close();

      httpHeaders.putSingle(
          HEADER_NAME,
          config.getRuntimeType().equals(RuntimeType.SERVER)
              ? HEADER_VALUE_SERVER
              : HEADER_VALUE_CLIENT);

      entityStream.write(t.getBytes());
    }
 @PostConstruct
 private void init() {
   final String paramName =
       (String) configuration.getProperty(SelectableEntityFilteringFeature.QUERY_PARAM_NAME);
   SELECTABLE_PARAM_NAME = paramName != null ? paramName : SELECTABLE_PARAM_NAME;
 }