2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.impl;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import fj.data.Either;
26 import org.apache.commons.io.IOUtils;
27 import org.apache.http.HttpStatus;
28 import org.eclipse.jgit.util.Base64;
29 import org.openecomp.sdc.be.dao.api.ResourceUploadStatus;
30 import org.openecomp.sdc.be.info.ArtifactAccessInfo;
31 import org.openecomp.sdc.be.info.ArtifactAccessList;
32 import org.openecomp.sdc.be.info.ServletJsonResponse;
33 import org.openecomp.sdc.be.resources.data.ESArtifactData;
34 import org.openecomp.sdc.common.api.Constants;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import javax.ws.rs.WebApplicationException;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.StreamingOutput;
42 import java.io.ByteArrayInputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.util.ArrayList;
46 import java.util.List;
48 public class DownloadArtifactLogic {
50 private static final Logger log = LoggerFactory.getLogger(DownloadArtifactLogic.class);
52 private Gson gson = new GsonBuilder().setPrettyPrinting().create();
54 public Response downloadArtifact(final String artifactName, Either<? extends ESArtifactData, ResourceUploadStatus> getArtifactStatus, String artifactId) {
55 Response response = null;
57 if (getArtifactStatus.isRight()) {
58 log.debug("Could not find artifact for with id: {}", artifactId);
59 ResourceUploadStatus status = getArtifactStatus.right().value();
60 if (status == ResourceUploadStatus.COMPONENT_NOT_EXIST)
61 response = Response.status(HttpStatus.SC_NO_CONTENT).build();
63 response = Response.status(HttpStatus.SC_NOT_FOUND).build();
67 // convert artifact to inputstream
69 ESArtifactData artifactData = getArtifactStatus.left().value();
70 byte[] artifactPayload = artifactData.getDataAsArray();
72 log.debug("payload is encoded. perform decode");
73 byte[] decodedPayload = Base64.decode(new String(artifactPayload));
74 final InputStream artifactStream = new ByteArrayInputStream(decodedPayload);
75 log.debug("found artifact for with id: {}", artifactId);
77 // outputstream for response
78 StreamingOutput stream = output -> {
80 IOUtils.copy(artifactStream, output);
81 } catch (IOException e) {
82 log.debug("failed to copy artifact payload into response");
83 throw new WebApplicationException(e);
86 return Response.ok(stream).type(MediaType.APPLICATION_OCTET_STREAM_TYPE)
87 .header("content-disposition", "attachment; filename = " + artifactName)
93 public List<ArtifactAccessInfo> convertArtifactList(List<? extends ESArtifactData> artifactsList, String servletPath) {
95 List<ArtifactAccessInfo> artifactAccessList = new ArrayList<ArtifactAccessInfo>();
96 for (ESArtifactData artifact : artifactsList) {
97 ArtifactAccessInfo accessInfo = new ArtifactAccessInfo(artifact, servletPath);
98 artifactAccessList.add(accessInfo);
100 return artifactAccessList;
103 public Response createArtifactListResponse(final String serviceName, Either<List<ESArtifactData>, ResourceUploadStatus> getArtifactsStatus/*
104 * List < ? extends IResourceData> artifactsList
105 */, String servletPath) {
107 List<ArtifactAccessInfo> artifactAccessInfos;
108 if (getArtifactsStatus.isRight()) {
109 // if there are no artifacts - return No-Content
110 ResourceUploadStatus status = getArtifactsStatus.right().value();
111 if (status == ResourceUploadStatus.COMPONENT_NOT_EXIST || status == ResourceUploadStatus.SERVICE_NOT_EXIST) {
112 log.debug("resource {} does not exist", serviceName);
113 response = Response.status(HttpStatus.SC_NOT_FOUND).entity("[]").build();
116 log.debug("No content was found for {}", serviceName);
117 response = Response.status(HttpStatus.SC_NO_CONTENT).entity("[]").build();
121 List<? extends ESArtifactData> artifactsList = getArtifactsStatus.left().value();
122 log.debug("{} artifacts were found for {}", artifactsList.size(), serviceName);
123 artifactAccessInfos = convertArtifactList(artifactsList, servletPath);
125 String artifactDataJson = gson.toJson(new ArtifactAccessList(artifactAccessInfos));
126 response = Response.status(HttpStatus.SC_OK).entity(artifactDataJson).type(MediaType.APPLICATION_JSON_TYPE).build();
132 public Response buildResponse(int status, String errorMessage) {
134 ServletJsonResponse jsonResponse = new ServletJsonResponse();
135 jsonResponse.setDescription(errorMessage);
136 jsonResponse.setSource(Constants.CATALOG_BE);
138 Response response = Response.status(status).entity(jsonResponse).build();