6f0d71cabff0aee88314a899a3cc0f1444e52100
[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.NotFoundException;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.core.Response;
31
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class RestRequest implements Callable<Response> {
36
37         private static final Logger logger = LoggerFactory.getLogger(RestRequest.class);
38
39         private final RestClient client;
40         private final String method;
41         private final Object entity;
42         
43         public RestRequest(RestClient client, String method, Object entity) {
44                 this.client = client;
45                 this.method = method;
46                 this.entity = entity;
47         }
48         @Override
49         public Response call() throws Exception {
50                 final Response response;
51                 if ("GET".equals(method)) {
52                         response = this.client.getBuilder().accept(this.client.getAccept()).get();
53                 } else if ("POST".equals(method)) {
54                         response = this.client.getBuilder().accept(this.client.getAccept()).post(Entity.entity(entity, this.client.getContentType()));
55                 } else if ("PATCH".equals(method)) {
56                         response = this.client.getBuilder().header("X-HTTP-Method-Override", "PATCH").accept(this.client.getAccept())
57                                         .post(Entity.entity(entity, this.client.getMergeContentType()));
58                 } else if ("DELETE".equals(method)) {
59                         if (entity == null) {
60                                 response = this.client.getBuilder().accept(this.client.getAccept()).delete();
61
62                         } else {
63                                 response = this.client.getBuilder().header("X-HTTP-Method-Override", "DELETE").accept(this.client.getAccept())
64                                                 .post(Entity.entity(entity, this.client.getContentType()));
65                         }
66                 } else if ("PUT".equals(method)) {
67                         response = this.client.getBuilder().accept(this.client.getAccept()).put(Entity.entity(entity, this.client.getContentType()));
68                 } else {
69                         response = Response.serverError().entity(method + " not valid").build();
70                 }
71                 
72                 Optional<ResponseExceptionMapper> mapper = this.client.addResponseExceptionMapper();
73                 if (mapper.isPresent()) {
74                         try {
75                                 mapper.get().map(response);
76                         } catch (NotFoundException e) {
77                                 if (this.client.props.mapNotFoundToEmpty() && "GET".equals(method)) {
78                                         logger.debug("RestClient recieved not found on URL: {}", this.client.getWebTarget().getUri());
79                                         return response;
80                                 } else {
81                                         throw e;
82                                 }
83                         }
84                 }
85
86                 return response;
87         }
88
89 }