Fix no content artifact handling 25/1425/3
authorSerban Jora <sj2381@att.com>
Fri, 23 Mar 2018 16:21:37 +0000 (12:21 -0400)
committerSerban Jora <sj2381@att.com>
Fri, 23 Mar 2018 18:43:56 +0000 (14:43 -0400)
Also adds positive outcome logging for artifact content manipulation

Change-Id: Id393796317f40bc3fafdce1119632a6ff776809b
Issue-ID: ACUMOS-276
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/controller/CatalogController.java
gateway/src/main/java/org/acumos/federation/gateway/service/CatalogService.java
gateway/src/main/java/org/acumos/federation/gateway/service/impl/CatalogServiceImpl.java
gateway/src/main/java/org/acumos/federation/gateway/service/impl/CatalogServiceLocalImpl.java

index 4313ab8..13ed28e 100644 (file)
@@ -476,14 +476,17 @@ public class PeerGateway {
                                                }
                                        }
 
-                                       boolean doContent = (SubscriptionScope.Full == SubscriptionScope.forCode(this.sub.getScopeType()));
+                                       boolean doContent = (peerArtifact.getUri() != null) &&
+                                                                                                                       (SubscriptionScope.Full == SubscriptionScope.forCode(this.sub.getScopeType()));
                                        if (doContent) {
+                                               log.info(EELFLoggerDelegate.debugLogger, "Processing content for artifact {}", peerArtifact); 
                                                // TODO: we are trying to access the artifact by its identifier which
                                                // is fine in the common case but the uri specified in the artifact
                                                // data is a more flexible approach.
                                                Resource artifactContent = null;
                                                try {
                                                        artifactContent = fedClient.downloadArtifact(peerArtifact.getArtifactId());
+                                                       log.info(EELFLoggerDelegate.debugLogger, "Received {} bytes of artifact content", artifactContent.contentLength()); 
                                                }
                                                catch (Exception x) {
                                                        log.error(EELFLoggerDelegate.errorLogger, "Failed to retrieve acumos artifact content", x);
@@ -497,6 +500,7 @@ public class PeerGateway {
                                                                                                localArtifact.getName(), /* probably wrong */
                                                                                                localArtifact.getVersion(), "", /* should receive this from peer */
                                                                                                artifactContent.contentLength(), artifactContent.getInputStream());
+                                                               log.info(EELFLoggerDelegate.debugLogger, "Wrote artifact content locally to {}", uploadInfo.getArtifactMvnPath()); 
                                                        }
                                                        catch (Exception x) {
                                                                log.error(EELFLoggerDelegate.errorLogger,
@@ -515,6 +519,7 @@ public class PeerGateway {
                                        if (doUpdate) {
                                                try {
                                                        cdsClient.updateArtifact(localArtifact);
+                                                       log.info(EELFLoggerDelegate.debugLogger, "Local artifact updated with local content reference: {}", localArtifact); 
                                                }
                                                catch (HttpStatusCodeException restx) {
                                                        log.error(EELFLoggerDelegate.errorLogger,
index f7dfaf4..6175a24 100644 (file)
@@ -299,21 +299,26 @@ public class CatalogController extends AbstractController {
        @ResponseBody
        public InputStreamResource downloadSolutionArtifact(HttpServletRequest theHttpRequest,
                        HttpServletResponse theHttpResponse, @PathVariable("artifactId") String theArtifactId) {
-               InputStreamResource inputStreamResource = null;
+               InputStreamResource resource = null;
                try {
-                       inputStreamResource = catalogService.getSolutionRevisionArtifactContent(theArtifactId,
+                       resource = catalogService.getSolutionRevisionArtifactContent(theArtifactId,
                                        new ControllerContext());
-                       theHttpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
-                       theHttpResponse.setHeader("Pragma", "no-cache");
-                       theHttpResponse.setHeader("Expires", "0");
-                       theHttpResponse.setStatus(HttpServletResponse.SC_OK);
+                       if (resource == null) {
+                               theHttpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                       }
+                       else {
+                               theHttpResponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
+                               theHttpResponse.setHeader("Pragma", "no-cache");
+                               theHttpResponse.setHeader("Expires", "0");
+                               theHttpResponse.setStatus(HttpServletResponse.SC_OK);
+                       }
                } 
                catch (Exception x) {
                        theHttpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                        log.error(EELFLoggerDelegate.errorLogger,
                                        "An error occurred while downloading artifact " + theArtifactId, x);
                }
-               return inputStreamResource;
+               return resource;
        }
 
        /** */
@@ -332,13 +337,16 @@ public class CatalogController extends AbstractController {
        
        /** */
        private void encodeArtifact(MLPArtifact theArtifact, HttpServletRequest theRequest) throws URISyntaxException {
-               URI requestUri = new URI(theRequest.getRequestURL().toString());
-               URI artifactUri = API.ARTIFACT_DOWNLOAD
-                                                                                       .buildUri(
-                                                                                               new URI(requestUri.getScheme(), null, requestUri.getHost(),
-                                                                                                                               requestUri.getPort(), null, null, null).toString(),
-                                                                                               theArtifact.getArtifactId());
-               log.debug(EELFLoggerDelegate.debugLogger,       "getSolutionRevisionArtifacts: content uri " + artifactUri);
-               theArtifact.setUri(artifactUri.toString());
+               if (theArtifact.getUri() != null) {
+                       URI requestUri = new URI(theRequest.getRequestURL().toString());
+                       URI artifactUri = API.ARTIFACT_DOWNLOAD
+                                                                                               .buildUri(
+                                                                                                       new URI(requestUri.getScheme(), null, requestUri.getHost(),
+                                                                                                                                       requestUri.getPort(), null, null, null).toString(),
+                                                                                                       theArtifact.getArtifactId());
+                       log.debug(EELFLoggerDelegate.debugLogger,       "getSolutionRevisionArtifacts: encoded content uri " + artifactUri);
+                       theArtifact.setUri(artifactUri.toString());
+               }
        }
+
 }
index fc33ddd..ccf1ff0 100644 (file)
@@ -173,6 +173,7 @@ public interface CatalogService {
         *            the execution context
         * @return resource containing access to the actual artifact content
         */
-       public InputStreamResource getSolutionRevisionArtifactContent(String theArtifactId, ServiceContext theContext);
+       public InputStreamResource getSolutionRevisionArtifactContent(String theArtifactId, ServiceContext theContext)
+                                                                                                                                                                                                                                                                                                                                                                       throws ServiceException;
 
 }
index e627669..6033cb7 100644 (file)
@@ -40,12 +40,8 @@ import org.apache.commons.io.FileUtils;
 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
 import org.acumos.federation.gateway.service.CatalogService;
 import org.acumos.federation.gateway.service.ServiceContext;
+import org.acumos.federation.gateway.service.ServiceException;
 import org.acumos.federation.gateway.util.Utils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.env.Environment;
-import org.springframework.core.io.InputStreamResource;
-import org.springframework.stereotype.Service;
-import org.springframework.context.annotation.Conditional;
 
 import org.acumos.nexus.client.NexusArtifactClient;
 
@@ -57,6 +53,13 @@ import org.acumos.cds.domain.MLPSolution;
 import org.acumos.cds.domain.MLPSolutionRevision;
 import org.acumos.cds.transport.RestPageResponse;
 
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.env.Environment;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.stereotype.Service;
+import org.springframework.context.annotation.Conditional;
+
+
 /**
  * CDS based implementation of the CatalogService.
  *
@@ -137,34 +140,29 @@ public class CatalogServiceImpl extends AbstractServiceImpl implements CatalogSe
                return mlpArtifacts;
        }
 
+       /**
+        * @return a resource containing the content or null if the artifact has no content
+        * @throws ServiceException if failing to retrieve artifact information or retrieve content 
+        */
        @Override
-       public InputStreamResource getSolutionRevisionArtifactContent(String theArtifactId, ServiceContext theContext) {
+       public InputStreamResource getSolutionRevisionArtifactContent(String theArtifactId, ServiceContext theContext) throws ServiceException {
 
                InputStreamResource streamResource = null;
-               ByteArrayOutputStream byteArrayOutputStream = null;
                try {
                        ICommonDataServiceRestClient cdsClient = getClient();
-                       MLPArtifact mlpArtifact = cdsClient.getArtifact(theArtifactId);
-
-                       NexusArtifactClient artifactClient = this.clients.getNexusClient();
-
-                       byteArrayOutputStream = artifactClient.getArtifact(mlpArtifact.getUri());
-                       InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
-
-                       // Plain Old Java. Sprint 3 will use try resource handling
-                       if (inputStream != null) {
-                               streamResource = new InputStreamResource(inputStream
-                               /*
-                                * , some_description
-                                */);
+                       MLPArtifact artifact = cdsClient.getArtifact(theArtifactId);
+
+                       if (artifact.getUri() != null) {
+                               NexusArtifactClient artifactClient = this.clients.getNexusClient();
+                               streamResource = new InputStreamResource(
+                                                                                                       new ByteArrayInputStream(
+                                                                                                               artifactClient.getArtifact(artifact.getUri()).toByteArray()
+                                                                                                       ));
                        }
-                       if (byteArrayOutputStream != null) {
-                               byteArrayOutputStream.close();
-                       }
-
                }
                catch (Exception x) {
-                       log.error(EELFLoggerDelegate.errorLogger, "getSolutionRevisionArtifactiContent", x);
+                       log.error(EELFLoggerDelegate.errorLogger, "Failed to retrieve artifact content for artifact " + theArtifactId, x);
+                       throw new ServiceException("Failed to retrieve artifsact content for artifact " + theArtifactId, x);
                }
                return streamResource;
        }
index cfce00b..c05e6c0 100644 (file)
@@ -50,14 +50,8 @@ import org.apache.commons.io.FileUtils;
 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
 import org.acumos.federation.gateway.service.CatalogService;
 import org.acumos.federation.gateway.service.ServiceContext;
+import org.acumos.federation.gateway.service.ServiceException;
 import org.acumos.federation.gateway.util.Utils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.BeanInitializationException;
-import org.springframework.core.env.Environment;
-import org.springframework.core.io.InputStreamResource;
-import org.springframework.stereotype.Service;
-import org.springframework.context.annotation.Conditional;
-import org.springframework.boot.context.properties.ConfigurationProperties;
 
 import org.acumos.cds.AccessTypeCode;
 import org.acumos.cds.ValidationStatusCode;
@@ -67,6 +61,15 @@ import org.acumos.cds.domain.MLPSolution;
 import org.acumos.cds.domain.MLPSolutionRevision;
 import org.acumos.cds.transport.RestPageResponse;
 
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.BeanInitializationException;
+import org.springframework.core.env.Environment;
+import org.springframework.core.io.InputStreamResource;
+import org.springframework.stereotype.Service;
+import org.springframework.context.annotation.Conditional;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+
 /**
  * 
  *
@@ -180,7 +183,8 @@ public class CatalogServiceLocalImpl extends AbstractServiceLocalImpl implements
        }
 
        @Override
-       public InputStreamResource getSolutionRevisionArtifactContent(String theArtifactId, ServiceContext theContext) {
+       public InputStreamResource getSolutionRevisionArtifactContent(String theArtifactId, ServiceContext theContext) 
+                                                                                                                                                                                                                                                                                                                                                                                               throws ServiceException {
 
                log.debug(EELFLoggerDelegate.debugLogger, "getSolutionRevisionArtifactContent");
                // cumbersome
@@ -192,7 +196,8 @@ public class CatalogServiceLocalImpl extends AbstractServiceLocalImpl implements
                                                        return new InputStreamResource(new URI(artifact.getUri()).toURL().openStream());
                                                } catch (Exception x) {
                                                        log.debug(EELFLoggerDelegate.debugLogger,
-                                                                       "failed to load content from " + artifact.getUri(), x);
+                                                                       "failed to load artifact content from " + artifact.getUri(), x);
+                                                       throw new ServiceException("Failed to retrieve content for artifact " + theArtifactId, x);
                                                }
                                        }
                                }