Replaced all tabs with spaces in java and pom.xml
[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 import javax.ws.rs.HttpMethod;
28 import javax.ws.rs.NotFoundException;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.core.Response;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class RestRequest implements Callable<Response> {
35
36     private static final Logger logger = LoggerFactory.getLogger(RestRequest.class);
37
38     private final RestClient client;
39     private final String method;
40     private final Object entity;
41
42     public RestRequest(RestClient client, String method, Object entity) {
43         this.client = client;
44         this.method = method;
45         this.entity = entity;
46     }
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())
55                     .post(Entity.entity(entity, this.client.getContentType()));
56         } else if ("PATCH".equals(method)) {
57             response = this.client.getBuilder().header("X-HTTP-Method-Override", "PATCH")
58                     .accept(this.client.getAccept()).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())
69                     .put(Entity.entity(entity, this.client.getContentType()));
70         } else {
71             response = Response.serverError().entity(method + " not valid").build();
72         }
73
74         Optional<ResponseExceptionMapper> mapper = this.client.addResponseExceptionMapper();
75         if (mapper.isPresent()) {
76             try {
77                 mapper.get().map(response);
78             } catch (NotFoundException e) {
79                 if (this.client.props.mapNotFoundToEmpty() && "GET".equals(method)) {
80                     logger.debug("RestClient recieved not found on URL: {}", this.client.getWebTarget().getUri());
81                     return response;
82                 } else {
83                     throw e;
84                 }
85             }
86         }
87
88         return response;
89     }
90
91 }