handle CDS style rest errors 10/410/1
authorSerban Jora <sj2381@att.com>
Tue, 9 Jan 2018 15:34:58 +0000 (10:34 -0500)
committerSerban Jora <sj2381@att.com>
Tue, 9 Jan 2018 15:35:07 +0000 (10:35 -0500)
Change-Id: I2ef7c21e1702cd13acecaf9f89127037398e34fb
Signed-off-by: Serban Jora <sj2381@att.com>
gateway/src/main/java/org/acumos/federation/gateway/adapter/PeerGateway.java
gateway/src/main/java/org/acumos/federation/gateway/util/Errors.java [new file with mode: 0644]

index 94e508d..61f6e15 100644 (file)
@@ -34,6 +34,7 @@ import org.acumos.federation.gateway.config.EELFLoggerDelegate;
 import org.acumos.federation.gateway.event.PeerSubscriptionEvent;
 import org.acumos.federation.gateway.service.impl.Clients;
 import org.acumos.federation.gateway.service.impl.FederationClient;
+import org.acumos.federation.gateway.util.Errors;
 import org.acumos.federation.gateway.util.Utils;
 
 import org.acumos.nexus.client.data.UploadArtifactInfo;
@@ -148,11 +149,7 @@ public class PeerGateway {
 
                        //list with category and subcategory currently used for onap
                        //more dynamic mapping to come: based on solution information it will provide sdc assettype, categoty and subcategoty
-                       ICommonDataServiceRestClient cdsClient =
-                               new CommonDataServiceRestClientImpl(
-                                                       env.getProperty("cdms.client.url"),
-                                                       env.getProperty("cdms.client.username"),
-                                                       env.getProperty("cdms.client.password"));
+                       ICommonDataServiceRestClient cdsClient = PeerGateway.this.clients.getClient();
                        
                        logger.info(EELFLoggerDelegate.debugLogger, "Received peer " + this.peer + " solutions: " + this.solutions);
 
@@ -381,7 +378,7 @@ public class PeerGateway {
                                                peerRevisions.get(peerRevisions.size()-1).getRevisionId());
                        }
                        catch (HttpStatusCodeException restx) {
-                               if (restx.getStatusCode() != HttpStatus.NOT_FOUND) {
+                               if (!Errors.isCDSNotFound(restx)) {
                                        logger.error(EELFLoggerDelegate.debugLogger, "getSolutionRevision CDS call failed. CDS message is " + restx.getResponseBodyAsString(),  restx);
                                        throw restx;
                                }
@@ -418,7 +415,7 @@ public class PeerGateway {
                                                        cdsClient.getArtifact(peerArtifact.getArtifactId());
                                        }
                                        catch (HttpStatusCodeException restx) {
-                                               if (restx.getStatusCode() != HttpStatus.NOT_FOUND) {
+                                               if (!Errors.isCDSNotFound(restx)) {
                                                        logger.error(EELFLoggerDelegate.debugLogger, "getArtifact CDS call failed. CDS message is " + restx.getResponseBodyAsString(),  restx);
                                                        throw restx;
                                                }
diff --git a/gateway/src/main/java/org/acumos/federation/gateway/util/Errors.java b/gateway/src/main/java/org/acumos/federation/gateway/util/Errors.java
new file mode 100644 (file)
index 0000000..649a054
--- /dev/null
@@ -0,0 +1,48 @@
+/*-
+ * ===============LICENSE_START=======================================================
+ * Acumos
+ * ===================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
+ * ===================================================================================
+ * This Acumos software file is distributed by AT&T and Tech Mahindra
+ * 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
+ *  
+ * This file 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.acumos.federation.gateway.util;
+
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.client.HttpStatusCodeException;
+
+
+/** */
+public class Errors {
+
+       private Errors() {
+       }
+       
+
+       /**
+        * CDS provides a 400 error with a particular error message
+        */
+       public static boolean isCDSNotFound(HttpStatusCodeException theError) {
+
+               if (theError.getStatusCode() == HttpStatus.BAD_REQUEST) {
+                       String msg = theError.getResponseBodyAsString();
+                       return (msg != null &&
+                                                       msg.startsWith("No entry for ID"));
+               }
+               return false;
+       }
+
+}