2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.client;
25 import java.util.Optional;
26 import java.util.concurrent.Callable;
28 import javax.ws.rs.NotFoundException;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.core.Response;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class RestRequest implements Callable<Response> {
37 private static final Logger logger = LoggerFactory.getLogger(RestRequest.class);
39 private final RestClient client;
40 private final String method;
41 private final Object entity;
43 public RestRequest(RestClient client, String method, Object entity) {
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)) {
60 response = this.client.getBuilder().accept(this.client.getAccept()).delete();
63 response = this.client.getBuilder().header("X-HTTP-Method-Override", "DELETE").accept(this.client.getAccept())
64 .post(Entity.entity(entity, this.client.getContentType()));
66 } else if ("PUT".equals(method)) {
67 response = this.client.getBuilder().accept(this.client.getAccept()).put(Entity.entity(entity, this.client.getContentType()));
69 response = Response.serverError().entity(method + " not valid").build();
72 Optional<ResponseExceptionMapper> mapper = this.client.addResponseExceptionMapper();
73 if (mapper.isPresent()) {
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());