X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Frestclient%2FAaiRestClient.java;h=fca517dbca312230b098d1bfc83a6f0add84f0ad;hb=HEAD;hp=40aeacc0c79e9cb916a16d686abd3b0dd38190f4;hpb=598c2469a004c50a1b29882e02e2fab7a8407d8b;p=aai%2Fmodel-loader.git diff --git a/src/main/java/org/onap/aai/modelloader/restclient/AaiRestClient.java b/src/main/java/org/onap/aai/modelloader/restclient/AaiRestClient.java index 40aeacc..fca517d 100644 --- a/src/main/java/org/onap/aai/modelloader/restclient/AaiRestClient.java +++ b/src/main/java/org/onap/aai/modelloader/restclient/AaiRestClient.java @@ -20,64 +20,56 @@ */ package org.onap.aai.modelloader.restclient; -import com.sun.jersey.core.util.MultivaluedMapImpl; // NOSONAR -import java.io.IOException; -import java.io.StringReader; -import java.net.URI; import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.stream.IntStream; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriBuilder; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.onap.aai.modelloader.entity.AaiResourcesObject; import org.onap.aai.modelloader.service.ModelLoaderMsgs; -import org.onap.aai.restclient.client.OperationResult; -import org.onap.aai.restclient.client.RestClient; -import org.onap.aai.restclient.enums.RestAuthenticationMode; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; /** * Wrapper around the standard A&AI Rest Client interface. This currently uses Jersey client 1.x * */ +@Component public class AaiRestClient { + private static Logger logger = LoggerFactory.getInstance().getLogger(AaiRestClient.class.getName()); public static final String HEADER_TRANS_ID = "X-TransactionId"; public static final String HEADER_FROM_APP_ID = "X-FromAppId"; public static final String ML_APP_NAME = "ModelLoader"; private static final String RESOURCE_VERSION_PARAM = "resource-version"; + private final ModelLoaderConfig config; + private final RestTemplate restTemplate; - private static Logger logger = LoggerFactory.getInstance().getLogger(AaiRestClient.class.getName()); - - private ModelLoaderConfig config = null; - - public AaiRestClient(ModelLoaderConfig config) { + public AaiRestClient(ModelLoaderConfig config, RestTemplate restTemplate) { this.config = config; + this.restTemplate = restTemplate; } - /** * Send a GET request to the A&AI for a resource. + * @param * * @param url * @param transId * @param mediaType * @return */ - public OperationResult getResource(String url, String transId, MediaType mediaType) { - return setupClient().get(url, buildHeaders(transId), mediaType); + public ResponseEntity getResource(String url, String transId, MediaType mediaType, Class responseType) { + HttpHeaders headers = defaultHeaders(transId); + headers.setAccept(Collections.singletonList(mediaType)); + HttpEntity entity = new HttpEntity<>(headers); + + return restTemplate.exchange(url, HttpMethod.GET, entity, responseType); } /** @@ -89,27 +81,26 @@ public class AaiRestClient { * @param mediaType - the content type (XML or JSON) * @return operation result */ - public OperationResult putResource(String url, String payload, String transId, MediaType mediaType) { - logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload); - return setupClient().put(url, payload, buildHeaders(transId), mediaType, mediaType); + public ResponseEntity putResource(String url, T payload, String transId, MediaType mediaType, Class responseType) { + logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload.toString()); + HttpHeaders headers = defaultHeaders(transId); + headers.setAccept(Collections.singletonList(mediaType)); + headers.setContentType(mediaType); + HttpEntity entity = new HttpEntity<>(payload, headers); + + return restTemplate.exchange(url, HttpMethod.PUT, entity, responseType); } + public ResponseEntity postResource(String url, T payload, String transId, MediaType mediaType, Class responseType) { + logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload.toString()); + HttpHeaders headers = defaultHeaders(transId); + headers.setAccept(Collections.singletonList(mediaType)); + headers.setContentType(mediaType); + HttpEntity entity = new HttpEntity<>(payload, headers); - /** - * Send a POST request to the A&AI. - * - * @param url - the url - * @param transId - transaction ID - * @param payload - the XML or JSON payload for the request - * @param mimeType - the content type (XML or JSON) - * @return ClientResponse - */ - public OperationResult postResource(String url, String payload, String transId, MediaType mediaType) { - logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_PAYLOAD, payload); - return setupClient().post(url, payload, buildHeaders(transId), mediaType, mediaType); + return restTemplate.exchange(url, HttpMethod.POST, entity, responseType); } - /** * Send a DELETE request to the A&AI. * @@ -118,9 +109,11 @@ public class AaiRestClient { * @param transId - transaction ID * @return ClientResponse */ - public OperationResult deleteResource(String url, String resourceVersion, String transId) { - URI uri = UriBuilder.fromUri(url).queryParam(RESOURCE_VERSION_PARAM, resourceVersion).build(); - return setupClient().delete(uri.toString(), buildHeaders(transId), null); + public ResponseEntity deleteResource(String url, String resourceVersion, String transId) { + HttpHeaders headers = defaultHeaders(transId); + String uri = url + "?" + RESOURCE_VERSION_PARAM + "=" + resourceVersion; + HttpEntity entity = new HttpEntity<>(headers); + return restTemplate.exchange(uri, HttpMethod.DELETE, entity, String.class); } /** @@ -130,86 +123,34 @@ public class AaiRestClient { * @param transId - transaction ID * @return ClientResponse */ - public OperationResult getAndDeleteResource(String url, String transId) { + public ResponseEntity getAndDeleteResource(String url, String transId) { + ResponseEntity response = getResource(url, transId, MediaType.APPLICATION_XML, AaiResourcesObject.class); // First, GET the model - OperationResult getResponse = getResource(url, transId, MediaType.APPLICATION_XML_TYPE); - if (getResponse == null || getResponse.getResultCode() != Response.Status.OK.getStatusCode()) { - return getResponse; + if (response == null || response.getStatusCode() != HttpStatus.OK) { + return response; } - // Delete the model using the resource version in the response - String resVersion = null; - try { - resVersion = getResourceVersion(getResponse); - } catch (Exception e) { - logger.error(ModelLoaderMsgs.AAI_REST_REQUEST_ERROR, "GET", url, e.getLocalizedMessage()); - return null; - } - - return deleteResource(url, resVersion, transId); + return deleteResource(url, response.getBody().getResourceVersion(), transId); } - public boolean useBasicAuth() { + private boolean useBasicAuth() { return config.getAaiAuthenticationUser() != null && config.getAaiAuthenticationPassword() != null; } - private RestClient setupClient() { - RestClient restClient = new RestClient(); - restClient.validateServerHostname(false) - .validateServerCertChain(false) - .connectTimeoutMs(config.getClientConnectTimeoutMs()) - .readTimeoutMs(config.getClientReadTimeoutMs()); - - //Use certs only if SSL is enabled - if (config.useHttpsWithAAI()) - {// @formatter:off - restClient - .clientCertFile(config.getAaiKeyStorePath()) - .clientCertPassword(config.getAaiKeyStorePassword()); - // @formatter:on - } - - if (useBasicAuth()) { - restClient.authenticationMode(RestAuthenticationMode.SSL_BASIC); - restClient.basicAuthUsername(config.getAaiAuthenticationUser()); - restClient.basicAuthPassword(config.getAaiAuthenticationPassword()); - } - - return restClient; - } - /** * Create the HTTP headers required for an A&AI operation (GET/POST/PUT/DELETE) * * @param transId * @return map of headers */ - private Map> buildHeaders(String transId) { - MultivaluedMap headers = new MultivaluedMapImpl(); - headers.put(HEADER_TRANS_ID, Collections.singletonList(transId)); - headers.put(HEADER_FROM_APP_ID, Collections.singletonList(ML_APP_NAME)); + private HttpHeaders defaultHeaders(String transId) { + HttpHeaders headers = new HttpHeaders(); + headers.set(AaiRestClient.HEADER_TRANS_ID, transId); + headers.set(AaiRestClient.HEADER_FROM_APP_ID, AaiRestClient.ML_APP_NAME); + if (useBasicAuth()) { + headers.setBasicAuth(config.getAaiAuthenticationUser(), config.getAaiAuthenticationPassword()); + } return headers; } - - private String getResourceVersion(OperationResult getResponse) - throws ParserConfigurationException, SAXException, IOException { - String respData = getResponse.getResult(); - - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); - DocumentBuilder builder = factory.newDocumentBuilder(); - InputSource is = new InputSource(new StringReader(respData)); - Document doc = builder.parse(is); - - NodeList nodesList = doc.getDocumentElement().getChildNodes(); - - // @formatter:off - return IntStream.range(0, nodesList.getLength()).mapToObj(nodesList::item) - .filter(childNode -> childNode.getNodeName().equals(RESOURCE_VERSION_PARAM)) - .findFirst() - .map(Node::getTextContent) - .orElse(null); - // @formatter:on - } }