Clover icon

Magnolia REST Client Module 1.0.1

  1. Project Clover database Fri Dec 5 2014 14:52:06 EST
  2. Package info.magnolia.rest.client

File DefaultRestClient.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
0% of files have more coverage

Code metrics

2
12
4
1
94
43
6
0.5
3
4
1.5
18.2% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
DefaultRestClient 49 12 18.2% 6 18
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2014 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.rest.client;
35   
36    import info.magnolia.rest.client.call.AbstractRestCall;
37   
38    import javax.ws.rs.WebApplicationException;
39    import javax.ws.rs.client.WebTarget;
40   
41    import org.slf4j.Logger;
42    import org.slf4j.LoggerFactory;
43   
44    /**
45    * Default implementation of rest client.
46    *
47    * @param <E>
48    */
 
49    public class DefaultRestClient<E extends Throwable> implements RestClient<E> {
50   
51    private static final Logger log = LoggerFactory.getLogger(DefaultRestClient.class);
52   
53    private RestClientDefinition restClientDefinition;
54    private WebTarget target;
55   
 
56  0 toggle public DefaultRestClient(WebTarget target, RestClientDefinition restClientDefinition) {
57  0 this.target = target;
58  0 this.restClientDefinition = restClientDefinition;
59    }
60   
 
61  0 toggle @Override
62    public <T> T invoke(AbstractRestCall<T> restCall) throws E {
63  0 try {
64  0 return restCall.doCall(target);
65    } catch (WebApplicationException e) {
66  0 return handleRestException(restCall, e);
67    }
68    }
69   
 
70  0 toggle @Override
71    public <T> T invoke(String restCallName) throws E {
72  0 if (getRestClientDefinition().getRestCalls().containsKey(restCallName)) {
73  0 AbstractRestCall<?> restCall = getRestClientDefinition().getRestCalls().get(restCallName);
74  0 return (T) invoke(restCall);
75    }
76  0 log.warn("Unknown rest call with name " + restCallName + ".");
77  0 return null;
78    }
79   
 
80  0 toggle @Override
81    public <T> T handleRestException(AbstractRestCall<T> restCall, WebApplicationException e) throws E {
82  0 log.error("Error while issuing a rest call: Status code: " + e.getResponse().getStatus() + ", Response: " + e.getResponse().readEntity(String.class));
83  0 return null;
84    }
85   
 
86    toggle @Override
87    public RestClientDefinition getRestClientDefinition() {
88    return restClientDefinition;
89    }
90   
 
91    toggle public void setRestClientDefinition(RestClientDefinition restClientDefinition) {
92    this.restClientDefinition = restClientDefinition;
93    }
94    }