Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1abed532310e6d01510124039e0184a4276b6b93 (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
/*******************************************************************************
 * Copyright (c) 2013 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.rest.client.internal;

import java.net.URI;
import java.util.Map;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import com.google.inject.Inject;
import com.sun.jersey.api.client.AsyncWebResource;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.client.apache.ApacheHttpClient;
import com.sun.jersey.client.apache.config.ApacheHttpClientConfig;
import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;

/**
 * @author Roberto E. Escobar
 */
public class StandadloneWebClientProvider implements WebClientProvider {

   private Client client;
   private final String proxyAddress;

   @Inject
   public StandadloneWebClientProvider(@OseeHttpProxyAddress String proxyAddress) {
      this.proxyAddress = proxyAddress;
   }

   @Override
   public WebResource createResource(URI uri) {
      Client client = createClient(uri);
      return client.resource(uri);
   }

   @Override
   public AsyncWebResource createAsyncResource(URI uri) {
      Client client = createClient(uri);
      return client.asyncResource(uri);
   }

   private Client createClient(URI uri) {
      if (client == null) {
         DefaultApacheHttpClientConfig clientConfig = new DefaultApacheHttpClientConfig();
         Map<String, Object> properties = clientConfig.getProperties();

         properties.put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
         if (Strings.isValid(proxyAddress)) {
            properties.put(ApacheHttpClientConfig.PROPERTY_PROXY_URI, proxyAddress);
         }
         client = ApacheHttpClient.create(clientConfig);
      }
      return client;
   }

}

Back to the top