summaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/application-config-rest/application-config-rest-services/pom.xml
blob: fe7f012689c630fddef1bbae105c9a10cb0365a2 (plain)
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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>application-config-rest-services</artifactId>

    <parent>
        <groupId>org.openecomp.sdc</groupId>
        <artifactId>application-config-rest</artifactId>
        <version>1.4.0-SNAPSHOT</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>${ws.rs.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.framework.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>${javax.inject.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openecomp.sdc</groupId>
            <artifactId>openecomp-sdc-application-config-manager</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
             <groupId>org.openecomp.sdc</groupId>
            <artifactId>application-config-rest-types</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openecomp.sdc</groupId>
            <artifactId>openecomp-sdc-common-rest</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- CXF -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>${cxf.version}</version>
        </dependency>
    </dependencies>

    
</project>
m"> * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END============================================ * * ECOMP and OpenECOMP are trademarks * and service marks of AT&T Intellectual Property. * */ package com.woorea.openstack.connector; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.UnknownHostException; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.HttpResponseException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.log4j.Logger; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.woorea.openstack.base.client.OpenStackClientConnector; import com.woorea.openstack.base.client.OpenStackConnectException; import com.woorea.openstack.base.client.OpenStackRequest; import com.woorea.openstack.base.client.OpenStackResponse; import com.woorea.openstack.base.client.OpenStackResponseException; public class HttpClientConnector implements OpenStackClientConnector { public static ObjectMapper DEFAULT_MAPPER; public static ObjectMapper WRAPPED_MAPPER; private static Logger LOGGER = Logger.getLogger(HttpClientConnector.class); static { DEFAULT_MAPPER = new ObjectMapper(); DEFAULT_MAPPER.setSerializationInclusion(Include.NON_NULL); DEFAULT_MAPPER.disable(SerializationFeature.INDENT_OUTPUT); DEFAULT_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); DEFAULT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); WRAPPED_MAPPER = new ObjectMapper(); WRAPPED_MAPPER.setSerializationInclusion(Include.NON_NULL); WRAPPED_MAPPER.disable(SerializationFeature.INDENT_OUTPUT); WRAPPED_MAPPER.enable(SerializationFeature.WRAP_ROOT_VALUE); WRAPPED_MAPPER.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); WRAPPED_MAPPER.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); WRAPPED_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); } protected static <T> ObjectMapper getObjectMapper (Class<T> type) { return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER; } @Override public <T> OpenStackResponse request(OpenStackRequest<T> request) { CloseableHttpClient httpClient = null; //HttpClients.createDefault(); httpClient = HttpClients.custom().setRedirectStrategy(new HttpClientRedirectStrategy()).build(); URI uri = null; // Build the URI with query params try { URIBuilder uriBuilder = new URIBuilder(request.endpoint() + request.path()); for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) { for (Object o : entry.getValue()) { uriBuilder.setParameter(entry.getKey(), String.valueOf(o)); } } uri = uriBuilder.build(); } catch (URISyntaxException e) { throw new HttpClientException (e); } HttpEntity entity = null; if (request.entity() != null) { // Flatten the entity to a Json string try { // Get appropriate mapper, based on existence of a root element in Entity class ObjectMapper mapper = getObjectMapper (request.entity().getEntity().getClass()); String entityJson = mapper.writeValueAsString (request.entity().getEntity()); entity = new StringEntity(entityJson, ContentType.create(request.entity().getContentType())); LOGGER.debug("Openstack query JSON:"+entityJson); LOGGER.debug ("Request JSON Body: " + entityJson.replaceAll("\"password\":\"[^\"]*\"", "\"password\":\"***\"")); } catch (JsonProcessingException e) { throw new HttpClientException ("Json processing error on request entity", e); } catch (IOException e) { throw new HttpClientException ("Json IO error on request entity", e); } } // Determine the HttpRequest class based on the method HttpUriRequest httpRequest; switch (request.method()) { case POST: HttpPost post = new HttpPost(uri); post.setEntity (entity); httpRequest = post; break; case GET: httpRequest = new HttpGet(uri); break; case PUT: HttpPut put = new HttpPut(uri); put.setEntity (entity); httpRequest = put; break; case DELETE: httpRequest = new HttpDelete(uri); break; default: throw new HttpClientException ("Unrecognized HTTP Method: " + request.method()); } for (Entry<String, List<Object>> h : request.headers().entrySet()) { StringBuilder sb = new StringBuilder(); for (Object v : h.getValue()) { sb.append(String.valueOf(v)); } httpRequest.addHeader(h.getKey(), sb.toString()); } LOGGER.debug ("Sending HTTP request: " + httpRequest.toString()); // Get the Response. But don't get the body entity yet, as this response // will be wrapped in an HttpClientResponse. The HttpClientResponse // buffers the body in constructor, so can close the response here. HttpClientResponse httpClientResponse = null; CloseableHttpResponse httpResponse = null; // Catch known HttpClient exceptions, and wrap them in OpenStack Client Exceptions // so calling functions can distinguish. Only RuntimeExceptions are allowed. try { httpResponse = httpClient.execute(httpRequest); LOGGER.debug ("Response status: " + httpResponse.getStatusLine().getStatusCode()); httpClientResponse = new HttpClientResponse (httpResponse); int status = httpResponse.getStatusLine().getStatusCode(); if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED || status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_ACCEPTED) { return httpClientResponse; } } catch (HttpResponseException e) { // What exactly does this mean? It does not appear to get thrown for // non-2XX responses as documented. throw new OpenStackResponseException(e.getMessage(), e.getStatusCode()); } catch (UnknownHostException e) { throw new OpenStackConnectException("Unknown Host: " + e.getMessage()); } catch (IOException e) { // Catch all other IOExceptions and throw as OpenStackConnectException throw new OpenStackConnectException(e.getMessage()); } catch (Exception e) { // Catchall for anything else, must throw as a RuntimeException LOGGER.error ("Unexpected client exception: " +e.getMessage()); throw new RuntimeException("Unexpected client exception", e); } finally { // Have the body. Close the stream if (httpResponse != null) try { httpResponse.close(); } catch (IOException e) { LOGGER.warn("Unable to close HTTP Response: " + e); } } // Get here on an error response (4XX-5XX) throw new OpenStackResponseException(httpResponse.getStatusLine().getReasonPhrase(), httpResponse.getStatusLine().getStatusCode(), httpClientResponse); } }