3864f2e5cc97c5f979b4f7ffd492456e349862ae
[so.git] / common / src / main / java / org / onap / so / client / RestRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.client;
24
25 import java.util.Optional;
26 import java.util.concurrent.Callable;
27
28 import javax.ws.rs.HttpMethod;
29 import javax.ws.rs.NotFoundException;
30 import javax.ws.rs.client.Entity;
31 import javax.ws.rs.core.Response;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class RestRequest implements Callable<Response> {
37
38         private static final Logger logger = LoggerFactory.getLogger(RestRequest.class);
39
40         private final RestClient client;
41         private final String method;
42         private final Object entity;
43         
44         public RestRequest(RestClient client, String method, Object entity) {
45                 this.client = client;
46                 this.method = method;
47                 this.entity = entity;
48         }
49         @Override
50         public Response call() throws Exception {
51                 final Response response;
52                 if ("GET".equals(method)) {
53                         response = this.client.getBuilder().accept(this.client.getAccept()).get();
54                 } else if ("POST".equals(method)) {
55                         response = this.client.getBuilder().accept(this.client.getAccept()).post(Entity.entity(entity, this.client.getContentType()));
56                 } else if ("PATCH".equals(method)) {
57                         response = this.client.getBuilder().header("X-HTTP-Method-Override", "PATCH").accept(this.client.getAccept())
58                                         .post(Entity.entity(entity, this.client.getMergeContentType()));
59                 } else if ("DELETE".equals(method)) {
60                         if (entity == null) {
61                                 response = this.client.getBuilder().accept(this.client.getAccept()).delete();
62
63                         } else {
64                                 response = this.client.getBuilder().accept(this.client.getAccept())
65                                                 .build(HttpMethod.DELETE, Entity.entity(entity, this.client.getContentType())).invoke();
66                         }
67                 } else if ("PUT".equals(method)) {
68                         response = this.client.getBuilder().accept(this.client.getAccept()).put(Entity.entity(entity, this.client.getContentType()));
69                 } else {
70                         response = Response.serverError().entity(method + " not valid").build();
71                 }
72                 
73                 Optional<ResponseExceptionMapper> mapper = this.client.addResponseExceptionMapper();
74                 if (mapper.isPresent()) {
75                         try {
76                                 mapper.get().map(response);
77                         } catch (NotFoundException e) {
78                                 if (this.client.props.mapNotFoundToEmpty() && "GET".equals(method)) {
79                                         logger.debug("RestClient recieved not found on URL: {}", this.client.getWebTarget().getUri());
80                                         return response;
81                                 } else {
82                                         throw e;
83                                 }
84                         }
85                 }
86
87                 return response;
88         }
89
90 }