b1b528f17f75d8aafc7ac8a7696b10d876308a1c
[so/libs.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  * ============LICENSE_END=========================================================
15  */
16
17 package com.woorea.openstack.connector;
18
19 import java.util.List;
20 import java.util.Map;
21 import java.util.logging.Logger;
22
23 import javax.ws.rs.ClientErrorException;
24 import javax.ws.rs.client.Client;
25 import javax.ws.rs.client.Entity;
26 import javax.ws.rs.client.Invocation;
27 import javax.ws.rs.client.WebTarget;
28 import javax.ws.rs.core.MediaType;
29
30 import org.glassfish.jersey.filter.LoggingFilter;
31
32 import com.woorea.openstack.base.client.HttpMethod;
33 import com.woorea.openstack.base.client.OpenStackClientConnector;
34 import com.woorea.openstack.base.client.OpenStackRequest;
35 import com.woorea.openstack.base.client.OpenStackResponse;
36 import com.woorea.openstack.base.client.OpenStackResponseException;
37
38 public class JaxRs20Connector implements OpenStackClientConnector {
39
40     protected Client client = OpenStack.CLIENT;
41     private LoggingFilter logger = new LoggingFilter(Logger.getLogger("os"), 10000);
42
43     @Override
44     public <T> OpenStackResponse request(OpenStackRequest<T> request) {
45         WebTarget target = client.target(request.endpoint()).path(request.path());
46
47         for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
48             for (Object o : entry.getValue()) {
49                 target = target.queryParam(entry.getKey(), o);
50             }
51         }
52         target.register(logger);
53         Invocation.Builder invocation = target.request();
54
55         for(Map.Entry<String, List<Object>> h : request.headers().entrySet()) {
56             StringBuilder sb = new StringBuilder();
57             for(Object v : h.getValue()) {
58                 sb.append(String.valueOf(v));
59             }
60             invocation.header(h.getKey(), sb);
61         }
62
63         Entity<?> entity = (request.entity() == null) ? null :
64                 Entity.entity(request.entity().getEntity(), request.entity().getContentType());
65
66         try {
67             if (entity != null) {
68                 return new JaxRs20Response(invocation.method(request.method().name(), entity));
69             } else {
70                 if(HttpMethod.PUT == request.method()) {
71                     return new JaxRs20Response(invocation.method(request.method().name(), Entity.entity("", MediaType.APPLICATION_JSON)));
72                 } else {
73                     return new JaxRs20Response(invocation.method(request.method().name()));
74                 }
75             }
76         } catch (ClientErrorException e) {
77             throw new OpenStackResponseException(e.getResponse()
78                     .getStatusInfo().toString(), e.getResponse().getStatus());
79         }
80     }
81 }