diff options
author | Jakub Dudycz <jakub.dudycz@nokia.com> | 2018-02-20 15:56:58 +0100 |
---|---|---|
committer | Patrick Brady <pb071s@att.com> | 2018-02-22 17:20:11 +0000 |
commit | d8e87fc3b407e6cb15b8ad235ae548f323ce82ce (patch) | |
tree | 06620ca3781d0e1cb2fa42690b60146bdede9a92 /appc-inbound/appc-design-services | |
parent | f78405fa2d752fe29b1f86e527d741b75b388fcb (diff) |
ArtifactHandlerClient sonar fixes
Change-Id: Ic43b1e4748c753e696ffff1deb89f2b73a8cb9ce
Issue-ID: APPC-650
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
Diffstat (limited to 'appc-inbound/appc-design-services')
2 files changed, 103 insertions, 95 deletions
diff --git a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerClient.java b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerClient.java index 0d7357a4a..87b45eab2 100644 --- a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerClient.java +++ b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerClient.java @@ -24,162 +24,144 @@ package org.onap.appc.design.services.util; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.config.DefaultClientConfig; +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; +import com.sun.jersey.client.urlconnection.HTTPSProperties; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; -import java.security.SecureRandom; import java.util.HashMap; +import java.util.Map; import java.util.Properties; - import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.ws.rs.HttpMethod; import javax.ws.rs.core.MediaType; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; - -public class ArtifactHandlerClient { +public class ArtifactHandlerClient { private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerClient.class); private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR"; - Properties props = new Properties(); - public ArtifactHandlerClient() throws Exception { + private Properties props = new Properties(); + + public ArtifactHandlerClient() throws IOException { String propDir = System.getenv(SDNC_CONFIG_DIR_VAR); - if (propDir == null) - throw new Exception(" Cannot find Property file -" + SDNC_CONFIG_DIR_VAR); + if (propDir == null) { + throw new IOException(" Cannot find Property file -" + SDNC_CONFIG_DIR_VAR); + } String propFile = propDir + "/" + DesignServiceConstants.DESIGN_SERVICE_PROPERTIES; InputStream propStream = new FileInputStream(propFile); - try{ + try { props.load(propStream); - } - catch (Exception e){ - throw new Exception("Could not load properties file " + propFile, e); - } - finally{ - try{ + } catch (Exception e) { + throw new IOException("Could not load properties file " + propFile, e); + } finally { + try { propStream.close(); - } - catch (Exception e){ + } catch (Exception e) { log.warn("Could not close FileInputStream", e); } - } + } } - - public String createArtifactData(String payload, String requestID) throws JsonProcessingException, IOException { - + + public String createArtifactData(String payload, String requestID) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); JsonNode payloadObject = objectMapper.readTree(payload); - - ObjectNode json = objectMapper.createObjectNode(); - - String artifact_name = payloadObject.get(DesignServiceConstants.ARTIFACT_NAME).textValue(); - String artifact_version = payloadObject.get(DesignServiceConstants.ARTIFACT_VERSOIN).textValue(); - String artifact_contents = payloadObject.get(DesignServiceConstants.ARTIFACT_CONTENTS).textValue(); - - ObjectNode requestInfo = objectMapper.createObjectNode(); + ObjectNode json = objectMapper.createObjectNode(); + String artifactName = payloadObject.get(DesignServiceConstants.ARTIFACT_NAME).textValue(); + String artifactVersion = payloadObject.get(DesignServiceConstants.ARTIFACT_VERSOIN).textValue(); + String artifactContents = payloadObject.get(DesignServiceConstants.ARTIFACT_CONTENTS).textValue(); + + ObjectNode requestInfo = objectMapper.createObjectNode(); requestInfo.put(DesignServiceConstants.REQUETS_ID, requestID); requestInfo.put(DesignServiceConstants.REQUEST_ACTION, "StoreSdcDocumentRequest"); requestInfo.put(DesignServiceConstants.SOURCE, DesignServiceConstants.DESIGN_TOOL); - - String random = getRandom(); - - ObjectNode docParams = objectMapper.createObjectNode(); - docParams.put(DesignServiceConstants.ARTIFACT_VERSOIN, artifact_version); - docParams.put(DesignServiceConstants.ARTIFACT_NAME, artifact_name); - docParams.put(DesignServiceConstants.ARTIFACT_CONTENTS, artifact_contents); + ObjectNode docParams = objectMapper.createObjectNode(); + docParams.put(DesignServiceConstants.ARTIFACT_VERSOIN, artifactVersion); + docParams.put(DesignServiceConstants.ARTIFACT_NAME, artifactName); + docParams.put(DesignServiceConstants.ARTIFACT_CONTENTS, artifactContents); - json.put(DesignServiceConstants.REQUEST_INFORMATION, requestInfo); json.put(DesignServiceConstants.DOCUMENT_PARAMETERS, docParams); - log.info("Final data =" + json.toString()); + log.info("Final data =" + json.toString()); return String.format("{\"input\": %s}", json.toString()); } - public HashMap<String, String> execute(String payload, String rpc) throws Exception{ - log.info("Configuring Rest Operation for Payload " + payload + " RPC : " + rpc ); - HashMap<String, String> outputMessage = new HashMap<String, String>(); + public Map<String, String> execute(String payload, String rpc) throws IOException { + log.info("Configuring Rest Operation for Payload " + payload + " RPC : " + rpc); + Map<String, String> outputMessage = new HashMap<>(); Client client = null; - WebResource webResource = null; + WebResource webResource; ClientResponse clientResponse = null; EncryptionTool et = EncryptionTool.getInstance(); - String responseDataType=MediaType.APPLICATION_JSON; - String requestDataType=MediaType.APPLICATION_JSON; + String responseDataType = MediaType.APPLICATION_JSON; + String requestDataType = MediaType.APPLICATION_JSON; - try{ + try { DefaultClientConfig defaultClientConfig = new DefaultClientConfig(); System.setProperty("jsse.enableSNIExtension", "false"); - SSLContext sslContext = null; + SSLContext sslContext; SecureRestClientTrustManager secureRestClientTrustManager = new SecureRestClientTrustManager(); sslContext = SSLContext.getInstance("SSL"); - sslContext.init(null, new javax.net.ssl.TrustManager[] { secureRestClientTrustManager }, null); - defaultClientConfig.getProperties().put( - com.sun.jersey.client.urlconnection.HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, - new com.sun.jersey.client.urlconnection.HTTPSProperties(getHostnameVerifier(), sslContext)); + sslContext.init(null, new javax.net.ssl.TrustManager[]{secureRestClientTrustManager}, null); + + defaultClientConfig + .getProperties() + .put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, new HTTPSProperties(getHostnameVerifier(), sslContext)); + client = Client.create(defaultClientConfig); - String password = et.decrypt(props.getProperty("appc.upload.pass")); - client.addFilter(new HTTPBasicAuthFilter(props.getProperty("appc.upload.user"),password)); + String password = et.decrypt(props.getProperty("appc.upload.pass")); + client.addFilter(new HTTPBasicAuthFilter(props.getProperty("appc.upload.user"), password)); webResource = client.resource(new URI(props.getProperty("appc.upload.provider.url"))); webResource.setProperty("Content-Type", "application/json;charset=UTF-8"); log.info("Starting Rest Operation....."); - if(HttpMethod.GET.equalsIgnoreCase(rpc)){ + if (HttpMethod.GET.equalsIgnoreCase(rpc)) { clientResponse = webResource.accept(responseDataType).get(ClientResponse.class); - }else if(HttpMethod.POST.equalsIgnoreCase(rpc)){ + } else if (HttpMethod.POST.equalsIgnoreCase(rpc)) { clientResponse = webResource.type(requestDataType).post(ClientResponse.class, payload); - }else if(HttpMethod.PUT.equalsIgnoreCase(rpc)){ - clientResponse = webResource.type(requestDataType).put(ClientResponse.class,payload); - }else if(HttpMethod.DELETE.equalsIgnoreCase(rpc)){ + } else if (HttpMethod.PUT.equalsIgnoreCase(rpc)) { + clientResponse = webResource.type(requestDataType).put(ClientResponse.class, payload); + } else if (HttpMethod.DELETE.equalsIgnoreCase(rpc)) { clientResponse = webResource.delete(ClientResponse.class); } - - if(!(clientResponse.getStatus() == 200)) - throw new Exception("HTTP error code : " + clientResponse.getStatus()); - - + validateClientResponse(clientResponse); log.info("Completed Rest Operation....."); - }catch (Exception e) { - e.printStackTrace(); - log.debug("failed in RESTCONT Action with falut message :"+e.getMessage()); - throw new Exception("Error While Sending Rest Request" + e.getMessage()); - } - finally { + } catch (Exception e) { + log.debug("failed in RESTCONT Action", e); + throw new IOException("Error While Sending Rest Request" + e.getMessage()); + } finally { // clean up. - webResource = null; - if(client != null){ + if (client != null) { client.destroy(); - client = null; } } - return outputMessage; } - private String getRandom() { - SecureRandom random = new SecureRandom(); - int num = random.nextInt(100000); - String formatted = String.format("%05d", num); - return formatted; + + private void validateClientResponse(ClientResponse clientResponse) throws ArtifactHandlerInternalException { + if (clientResponse == null) { + throw new ArtifactHandlerInternalException("Failed to create client response"); + } + if (clientResponse.getStatus() != 200) { + throw new ArtifactHandlerInternalException("HTTP error code : " + clientResponse.getStatus()); + } } - + private HostnameVerifier getHostnameVerifier() { - return new HostnameVerifier() { - @Override - public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) { - return true; - } - }; + return (hostname, sslSession) -> true; } } diff --git a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerInternalException.java b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerInternalException.java new file mode 100644 index 000000000..493bcc258 --- /dev/null +++ b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/util/ArtifactHandlerInternalException.java @@ -0,0 +1,26 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.design.services.util; + +class ArtifactHandlerInternalException extends Exception { + + ArtifactHandlerInternalException(String message) { + super(message); + } +} |