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
7 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
17 package com.woorea.openstack.connector;
19 import java.util.List;
21 import java.util.logging.Logger;
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;
30 import org.glassfish.jersey.filter.LoggingFilter;
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;
38 public class JaxRs20Connector implements OpenStackClientConnector {
40 protected Client client = OpenStack.CLIENT;
41 private LoggingFilter logger = new LoggingFilter(Logger.getLogger("os"), 10000);
44 public <T> OpenStackResponse request(OpenStackRequest<T> request) {
45 WebTarget target = client.target(request.endpoint()).path(request.path());
47 for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
48 for (Object o : entry.getValue()) {
49 target = target.queryParam(entry.getKey(), o);
52 target.register(logger);
53 Invocation.Builder invocation = target.request();
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));
60 invocation.header(h.getKey(), sb);
63 Entity<?> entity = (request.entity() == null) ? null :
64 Entity.entity(request.entity().getEntity(), request.entity().getContentType());
68 return new JaxRs20Response(invocation.method(request.method().name(), entity));
70 if(HttpMethod.PUT == request.method()) {
71 return new JaxRs20Response(invocation.method(request.method().name(), Entity.entity("", MediaType.APPLICATION_JSON)));
73 return new JaxRs20Response(invocation.method(request.method().name()));
76 } catch (ClientErrorException e) {
77 throw new OpenStackResponseException(e.getResponse()
78 .getStatusInfo().toString(), e.getResponse().getStatus());