2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client;
23 import java.util.Optional;
24 import java.util.concurrent.Callable;
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;
31 import org.onap.so.logger.MsoLogger;
33 public class RestRequest implements Callable<Response> {
35 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL, RestRequest.class);
37 private final RestClient client;
38 private final String method;
39 private final Object entity;
41 public RestRequest(RestClient client, String method, Object entity) {
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)) {
58 response = this.client.getBuilder().accept(this.client.getAccept()).delete();
61 response = this.client.getBuilder().header("X-HTTP-Method-Override", "DELETE").accept(this.client.getAccept())
62 .post(Entity.entity(entity, this.client.getContentType()));
64 } else if ("PUT".equals(method)) {
65 response = this.client.getBuilder().accept(this.client.getAccept()).put(Entity.entity(entity, this.client.getContentType()));
67 response = Response.serverError().entity(method + " not valid").build();
70 Optional<ResponseExceptionMapper> mapper = this.client.addResponseExceptionMapper();
71 if (mapper.isPresent()) {
73 mapper.get().map(response);
74 } catch (NotFoundException e) {
75 if (this.client.props.mapNotFoundToEmpty() && "GET".equals(method)) {
76 msoLogger.debug("RestClient recieved not found on URL: " + this.client.getWebTarget().getUri());