aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/impl/DownloadArtifactLogic.java
diff options
context:
space:
mode:
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/impl/DownloadArtifactLogic.java')
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/impl/DownloadArtifactLogic.java227
1 files changed, 104 insertions, 123 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/DownloadArtifactLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/DownloadArtifactLogic.java
index ddc5ecabe7..b268554ee3 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/DownloadArtifactLogic.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/DownloadArtifactLogic.java
@@ -20,143 +20,124 @@
package org.openecomp.sdc.be.impl;
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.StreamingOutput;
-
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import fj.data.Either;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.eclipse.jgit.util.Base64;
-import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.dao.api.ResourceUploadStatus;
import org.openecomp.sdc.be.info.ArtifactAccessInfo;
import org.openecomp.sdc.be.info.ArtifactAccessList;
import org.openecomp.sdc.be.info.ServletJsonResponse;
import org.openecomp.sdc.be.resources.data.ESArtifactData;
import org.openecomp.sdc.common.api.Constants;
-import org.openecomp.sdc.common.config.EcompErrorName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-
-import fj.data.Either;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.StreamingOutput;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
public class DownloadArtifactLogic {
- private static Logger log = LoggerFactory.getLogger(DownloadArtifactLogic.class.getName());
-
- private Gson gson = new GsonBuilder().setPrettyPrinting().create();
-
- public Response downloadArtifact(final String resourceName, final String resourceVersion, final String artifactName, Either<? extends ESArtifactData, ResourceUploadStatus> getArtifactStatus, String artifactId) {
- Response response = null;
-
- if (getArtifactStatus.isRight()) {
- log.debug("Could not find artifact for with id: {}", artifactId);
- ResourceUploadStatus status = getArtifactStatus.right().value();
- if (status == status.COMPONENT_NOT_EXIST)
- response = Response.status(HttpStatus.SC_NO_CONTENT).build();
- else
- response = Response.status(HttpStatus.SC_NOT_FOUND).build();
-
- return response;
- }
- // convert artifact to inputstream
- else {
- ESArtifactData artifactData = getArtifactStatus.left().value();
- byte[] artifactPayload = artifactData.getDataAsArray();
-
- String payloadStr = new String(artifactPayload);
- byte[] decodedPayload = artifactPayload;
- log.debug("payload is encoded. perform decode");
- decodedPayload = Base64.decode(new String(artifactPayload));
- final InputStream artifactStream = new ByteArrayInputStream(decodedPayload);
- log.debug("found artifact for with id: {}", artifactId);
- try {
-
- // outputstream for response
- StreamingOutput stream = new StreamingOutput() {
- public void write(OutputStream output) throws IOException, WebApplicationException {
- try {
- IOUtils.copy(artifactStream, output);
- } catch (Exception e) {
- throw new WebApplicationException(e);
- }
- }
- };
- artifactStream.close();
- return Response.ok(stream).type(MediaType.APPLICATION_OCTET_STREAM_TYPE).header("content-disposition", "attachment; filename = " + artifactName)
- // .header(Constants.MD5_HEADER, new
- // String(artifactData.getArtifactChecksum()))
- .build();
-
- } catch (IOException e) {
- BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeSystemError, "Failed to stream artifact data on the response");
- BeEcompErrorManager.getInstance().logBeSystemError("Failed to stream artifact data on the response");
- log.debug("Failed to stream artifact data on the response: {}", e.getMessage(), e);
- response = buildResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Failed to stream artifact data on the response");
- return response;
- }
- }
- }
-
- public List<ArtifactAccessInfo> convertArtifactList(List<? extends ESArtifactData> artifactsList, String servletPath) {
-
- List<ArtifactAccessInfo> artifactAccessList = new ArrayList<ArtifactAccessInfo>();
- for (ESArtifactData artifact : artifactsList) {
- ArtifactAccessInfo accessInfo = new ArtifactAccessInfo(artifact, servletPath);
- artifactAccessList.add(accessInfo);
- }
- return artifactAccessList;
- }
-
- public Response createArtifactListResponse(final String serviceName, Either<List<ESArtifactData>, ResourceUploadStatus> getArtifactsStatus/*
- * List < ? extends IResourceData> artifactsList
- */, String servletPath) {
- Response response;
- List<ArtifactAccessInfo> artifactAccessInfos;
- if (getArtifactsStatus.isRight()) {
- // if there are no artifacts - return No-Content
- ResourceUploadStatus status = getArtifactsStatus.right().value();
- if (status == ResourceUploadStatus.COMPONENT_NOT_EXIST || status == ResourceUploadStatus.SERVICE_NOT_EXIST) {
- log.debug("resource {} does not exist", serviceName);
- response = Response.status(HttpStatus.SC_NOT_FOUND).entity("[]").build();
-
- } else {
- log.debug("No content was found for {}", serviceName);
- response = Response.status(HttpStatus.SC_NO_CONTENT).entity("[]").build();
- }
- return response;
- } else {
- List<? extends ESArtifactData> artifactsList = getArtifactsStatus.left().value();
- log.debug("{} artifacts were found for {}", artifactsList.size(), serviceName);
- artifactAccessInfos = convertArtifactList(artifactsList, servletPath);
-
- String artifactDataJson = gson.toJson(new ArtifactAccessList(artifactAccessInfos));
- response = Response.status(HttpStatus.SC_OK).entity(artifactDataJson).type(MediaType.APPLICATION_JSON_TYPE).build();
-
- return response;
- }
- }
-
- public Response buildResponse(int status, String errorMessage) {
-
- ServletJsonResponse jsonResponse = new ServletJsonResponse();
- jsonResponse.setDescription(errorMessage);
- jsonResponse.setSource(Constants.CATALOG_BE);
-
- Response response = Response.status(status).entity(jsonResponse).build();
-
- return response;
- }
+ private static final Logger log = LoggerFactory.getLogger(DownloadArtifactLogic.class);
+
+ private Gson gson = new GsonBuilder().setPrettyPrinting().create();
+
+ public Response downloadArtifact(final String artifactName, Either<? extends ESArtifactData, ResourceUploadStatus> getArtifactStatus, String artifactId) {
+ Response response = null;
+
+ if (getArtifactStatus.isRight()) {
+ log.debug("Could not find artifact for with id: {}", artifactId);
+ ResourceUploadStatus status = getArtifactStatus.right().value();
+ if (status == ResourceUploadStatus.COMPONENT_NOT_EXIST)
+ response = Response.status(HttpStatus.SC_NO_CONTENT).build();
+ else
+ response = Response.status(HttpStatus.SC_NOT_FOUND).build();
+
+ return response;
+ }
+ // convert artifact to inputstream
+ else {
+ ESArtifactData artifactData = getArtifactStatus.left().value();
+ byte[] artifactPayload = artifactData.getDataAsArray();
+
+ log.debug("payload is encoded. perform decode");
+ byte[] decodedPayload = Base64.decode(new String(artifactPayload));
+ final InputStream artifactStream = new ByteArrayInputStream(decodedPayload);
+ log.debug("found artifact for with id: {}", artifactId);
+
+ // outputstream for response
+ StreamingOutput stream = output -> {
+ try {
+ IOUtils.copy(artifactStream, output);
+ } catch (IOException e) {
+ log.debug("failed to copy artifact payload into response");
+ throw new WebApplicationException(e);
+ }
+ };
+ return Response.ok(stream).type(MediaType.APPLICATION_OCTET_STREAM_TYPE)
+ .header("content-disposition", "attachment; filename = " + artifactName)
+ .build();
+
+ }
+ }
+
+ public List<ArtifactAccessInfo> convertArtifactList(List<? extends ESArtifactData> artifactsList, String servletPath) {
+
+ List<ArtifactAccessInfo> artifactAccessList = new ArrayList<ArtifactAccessInfo>();
+ for (ESArtifactData artifact : artifactsList) {
+ ArtifactAccessInfo accessInfo = new ArtifactAccessInfo(artifact, servletPath);
+ artifactAccessList.add(accessInfo);
+ }
+ return artifactAccessList;
+ }
+
+ public Response createArtifactListResponse(final String serviceName, Either<List<ESArtifactData>, ResourceUploadStatus> getArtifactsStatus/*
+ * List < ? extends IResourceData> artifactsList
+ */, String servletPath) {
+ Response response;
+ List<ArtifactAccessInfo> artifactAccessInfos;
+ if (getArtifactsStatus.isRight()) {
+ // if there are no artifacts - return No-Content
+ ResourceUploadStatus status = getArtifactsStatus.right().value();
+ if (status == ResourceUploadStatus.COMPONENT_NOT_EXIST || status == ResourceUploadStatus.SERVICE_NOT_EXIST) {
+ log.debug("resource {} does not exist", serviceName);
+ response = Response.status(HttpStatus.SC_NOT_FOUND).entity("[]").build();
+
+ } else {
+ log.debug("No content was found for {}", serviceName);
+ response = Response.status(HttpStatus.SC_NO_CONTENT).entity("[]").build();
+ }
+ return response;
+ } else {
+ List<? extends ESArtifactData> artifactsList = getArtifactsStatus.left().value();
+ log.debug("{} artifacts were found for {}", artifactsList.size(), serviceName);
+ artifactAccessInfos = convertArtifactList(artifactsList, servletPath);
+
+ String artifactDataJson = gson.toJson(new ArtifactAccessList(artifactAccessInfos));
+ response = Response.status(HttpStatus.SC_OK).entity(artifactDataJson).type(MediaType.APPLICATION_JSON_TYPE).build();
+
+ return response;
+ }
+ }
+
+ public Response buildResponse(int status, String errorMessage) {
+
+ ServletJsonResponse jsonResponse = new ServletJsonResponse();
+ jsonResponse.setDescription(errorMessage);
+ jsonResponse.setSource(Constants.CATALOG_BE);
+
+ Response response = Response.status(status).entity(jsonResponse).build();
+
+ return response;
+ }
}