2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.dal.rest;
25 import java.security.SecureRandom;
27 import org.onap.aai.cl.api.Logger;
28 import org.onap.aai.cl.eelf.LoggerFactory;
29 import org.onap.aai.sparky.logging.AaiUiMsgs;
31 import com.sun.jersey.api.client.Client;
32 import com.sun.jersey.api.client.ClientResponse;
33 import com.sun.jersey.api.client.WebResource;
34 import com.sun.jersey.api.client.WebResource.Builder;
37 * The Class RestfulDataAccessor.
42 public class RestfulDataAccessor implements RestDataProvider {
44 protected SecureRandom txnIdGenerator;
46 protected RestClientBuilder clientBuilder;
48 private static final Logger LOG =
49 LoggerFactory.getInstance().getLogger(RestfulDataAccessor.class);
51 private boolean resourceNotFoundErrorsSurpressed;
53 public static final String APPLICATION_JSON = "application/json";
54 public static final String APPLICATION_MERGE_PATCH_JSON = "application/merge-patch+json";
55 public static final String APPLICATION_X_WWW_FORM_URL_ENCODED =
56 "application/x-www-form-urlencoded";
60 * Instantiates a new restful data accessor.
62 * @param clientBuilder the client builder
64 public RestfulDataAccessor(RestClientBuilder clientBuilder) {
65 this.clientBuilder = clientBuilder;
66 txnIdGenerator = new SecureRandom();
67 resourceNotFoundErrorsSurpressed = false;
71 * Populate operation result.
73 * @param response the response
74 * @param opResult the op result
76 protected void populateOperationResult(ClientResponse response, OperationResult opResult) {
78 if (response == null) {
79 opResult.setResult(500, "Client response was null");
83 int statusCode = response.getStatus();
84 String payload = response.getEntity(String.class);
86 opResult.setResult(statusCode, payload);
94 * org.openecomp.sparky.dal.rest.RestDataProvider#doRestfulOperation(org.openecomp.sparky.dal.rest
95 * .HttpMethod, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
98 public OperationResult doRestfulOperation(HttpMethod method, String url, String payload,
99 String payloadType, String acceptContentType) {
101 ClientResponse clientResponse = null;
103 long startTimeInMs = System.currentTimeMillis();
104 Client client = null;
105 Builder builder = null;
108 * else cache miss / cache disabled (default operation)
111 OperationResult operationResult = new OperationResult();
112 operationResult.setRequestLink(url);
116 client = clientBuilder.getClient();
120 builder = setClientDefaults(client, url, null, acceptContentType);
121 clientResponse = builder.get(ClientResponse.class);
126 builder = setClientDefaults(client, url, payloadType, acceptContentType);
127 clientResponse = builder.put(ClientResponse.class, payload);
132 builder = setClientDefaults(client, url, payloadType, acceptContentType);
133 clientResponse = builder.post(ClientResponse.class, payload);
138 builder = setClientDefaults(client, url, null, acceptContentType);
139 clientResponse = builder.delete(ClientResponse.class);
144 builder = setClientDefaults(client, url, payloadType, acceptContentType);
145 builder = builder.header("X-HTTP-Method-Override", "PATCH");
146 clientResponse = builder.post(ClientResponse.class, payload);
151 builder = setClientDefaults(client, url, null, acceptContentType);
152 clientResponse = builder.head();
158 operationResult.setResult(500, "Unhandled HTTP Method operation = " + method);
159 return operationResult;
164 } catch (Exception ex) {
165 LOG.error(AaiUiMsgs.RESTFULL_OP_ERROR_VERBOSE, url, ex.getLocalizedMessage());
166 operationResult.setResult(500,
167 String.format("Error retrieving link = '%s' from restful endpoint due to error = '%s'",
168 url, ex.getLocalizedMessage()));
169 return operationResult;
172 populateOperationResult(clientResponse, operationResult);
174 if (operationResult.getResultCode() != 404
175 || (operationResult.getResultCode() == 404 && !isResourceNotFoundErrorsSurpressed())) {
176 LOG.info(AaiUiMsgs.RESTFULL_OP_COMPLETE, method.toString(),
177 String.valueOf(System.currentTimeMillis() - startTimeInMs), url,
178 String.valueOf(operationResult.getResultCode()));
181 return operationResult;
185 public boolean isResourceNotFoundErrorsSurpressed() {
186 return resourceNotFoundErrorsSurpressed;
189 public void setResourceNotFoundErrorsSurpressed(boolean resourceNotFoundErrorsSurpressed) {
190 this.resourceNotFoundErrorsSurpressed = resourceNotFoundErrorsSurpressed;
196 * @see org.openecomp.sparky.dal.rest.RestDataProvider#doGet(java.lang.String, java.lang.String)
199 public OperationResult doGet(String url, String acceptContentType) {
200 return doRestfulOperation(HttpMethod.GET, url, null, null, acceptContentType);
206 * @see org.openecomp.sparky.dal.rest.RestDataProvider#doDelete(java.lang.String,
210 public OperationResult doDelete(String url, String acceptContentType) {
211 return doRestfulOperation(HttpMethod.DELETE, url, null, null, acceptContentType);
217 * @see org.openecomp.sparky.dal.rest.RestDataProvider#doPost(java.lang.String, java.lang.String,
221 public OperationResult doPost(String url, String jsonPayload, String acceptContentType) {
222 return doRestfulOperation(HttpMethod.POST, url, jsonPayload, APPLICATION_JSON,
229 * @see org.openecomp.sparky.dal.rest.RestDataProvider#doPut(java.lang.String, java.lang.String,
233 public OperationResult doPut(String url, String jsonPayload, String acceptContentType) {
234 return doRestfulOperation(HttpMethod.PUT, url, jsonPayload, APPLICATION_JSON,
241 * @see org.openecomp.sparky.dal.rest.RestDataProvider#doPatch(java.lang.String, java.lang.String,
245 public OperationResult doPatch(String url, String jsonPayload, String acceptContentType) {
246 return doRestfulOperation(HttpMethod.PATCH, url, jsonPayload, APPLICATION_MERGE_PATCH_JSON,
253 * @see org.openecomp.sparky.dal.rest.RestDataProvider#doHead(java.lang.String, java.lang.String)
256 public OperationResult doHead(String url, String acceptContentType) {
257 return doRestfulOperation(HttpMethod.HEAD, url, null, null, acceptContentType);
261 * Sets the client defaults.
263 * @param client the client
265 * @param payloadContentType the payload content type
266 * @param acceptContentType the accept content type
267 * @return the builder
269 protected Builder setClientDefaults(Client client, String url, String payloadContentType,
270 String acceptContentType) {
271 WebResource resource = client.resource(url);
272 Builder builder = null;
273 builder = resource.accept(acceptContentType);
275 if (payloadContentType != null) {
276 builder = builder.header("Content-Type", payloadContentType);
285 * @see org.openecomp.sparky.dal.rest.RestDataProvider#shutdown()
288 public void shutdown() {
295 * @see org.openecomp.sparky.dal.rest.RestDataProvider#clearCache()
298 public void clearCache() {