Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fd10ab3a80784774d88e5692e5faa609cce66237 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*******************************************************************************
 * Copyright (c) 2018 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/
package org.eclipse.scout.rt.rest.jersey;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.eclipse.scout.rt.platform.ApplicationScoped;
import org.eclipse.scout.rt.platform.BEANS;
import org.eclipse.scout.rt.rest.client.AbstractRestClientHelper;
import org.eclipse.scout.rt.rest.client.proxy.ErrorDoRestClientExceptionTransformer;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;

@ApplicationScoped
public class JerseyTestRestClientHelper extends AbstractRestClientHelper {

  @Override
  public String getBaseUri() {
    return "http://localhost:" + BEANS.get(JerseyTestApplication.class).getPort();
  }

  /**
   * Returns the raw, un-proxied {@link Client}.
   */
  public Client rawClient() {
    return internalClient();
  }

  @Override
  protected RuntimeException transformException(RuntimeException e, Response response) {
    return BEANS.get(ErrorDoRestClientExceptionTransformer.class).transform(e, response);
  }

  @Override
  protected void configureClientBuilder(ClientBuilder clientBuilder) {
    super.configureClientBuilder(clientBuilder);
    clientBuilder.property(ApacheClientProperties.CONNECTION_MANAGER, createTestingConnectionManager());
  }

  /**
   * Creates a {@link PoolingHttpClientConnectionManager} that manages exactly one connection. This limitation helps
   * finding resource leaks (i.e. leased connections that are never put back to the pool).
   */
  protected HttpClientConnectionManager createTestingConnectionManager() {
    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
        RegistryBuilder.<ConnectionSocketFactory> create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .build());
    connectionManager.setValidateAfterInactivity(1);
    connectionManager.setMaxTotal(1);
    connectionManager.setDefaultMaxPerRoute(1);
    return connectionManager;
  }
}

Back to the top