Skip to main content
summaryrefslogtreecommitdiffstats
blob: 30b9bfcbc2c255f0780554834e34b619c983130f (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
/*******************************************************************************
 * 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.jaxrs.client.internal;

import java.net.URI;
import java.util.Map;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.jaxrs.ErrorResponse;
import org.eclipse.osee.jaxrs.client.JaxRsClient;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
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.DefaultApacheHttpClientConfig;

/**
 * @author Roberto E. Escobar
 */
public class JaxRsClientImpl implements JaxRsClient {

   private Client client;

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

   @Override
   public RuntimeException handleException(UniformInterfaceException ex) {
      String message = null;
      try {
         ClientResponse response = ex.getResponse();
         ErrorResponse error = response.getEntity(ErrorResponse.class);
         message = error != null ? error.toString() : "Error message not available.";
      } catch (Throwable th) {
         message = String.format("Error Response object not available - [%s]", th.getLocalizedMessage());
      }
      return new OseeCoreException(ex, message);
   }

   protected void configure(URI uri, Map<String, Object> properties) {
      properties.put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
   }

   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);
         configure(uri, properties);
         client = ApacheHttpClient.create(clientConfig);
      }
      return client;
   }

}

Back to the top