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