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.openecomp.sdc.common.log.wrappers.Logger;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.StreamingOutput;
41 import java.io.ByteArrayInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.util.ArrayList;
45 import java.util.List;
47 public class DownloadArtifactLogic {
49 private static final Logger log = Logger.getLogger(DownloadArtifactLogic.class);
51 private Gson gson = new GsonBuilder().setPrettyPrinting().create();
53 public Response downloadArtifact(final String artifactName, Either<? extends ESArtifactData, ResourceUploadStatus> getArtifactStatus, String artifactId) {
54 Response response = null;
56 if (getArtifactStatus.isRight()) {
57 log.debug("Could not find artifact for with id: {}", artifactId);
58 ResourceUploadStatus status = getArtifactStatus.right().value();
59 if (status == ResourceUploadStatus.COMPONENT_NOT_EXIST)
60 response = Response.status(HttpStatus.SC_NO_CONTENT).build();
62 response = Response.status(HttpStatus.SC_NOT_FOUND).build();
66 // convert artifact to inputstream
68 ESArtifactData artifactData = getArtifactStatus.left().value();
69 byte[] artifactPayload = artifactData.getDataAsArray();
71 log.debug("payload is encoded. perform decode");
72 byte[] decodedPayload = Base64.decode(new String(artifactPayload));
73 final InputStream artifactStream = new ByteArrayInputStream(decodedPayload);
74 log.debug("found artifact for with id: {}", artifactId);
76 // outputstream for response
77 StreamingOutput stream = output -> {
79 IOUtils.copy(artifactStream, output);
80 } catch (IOException e) {
81 log.debug("failed to copy artifact payload into response");
82 throw new WebApplicationException(e);
85 return Response.ok(stream).type(MediaType.APPLICATION_OCTET_STREAM_TYPE)
86 .header("content-disposition", "attachment; filename = " + artifactName)
92 public List<ArtifactAccessInfo> convertArtifactList(List<? extends ESArtifactData> artifactsList, String servletPath) {
94 List<ArtifactAccessInfo> artifactAccessList = new ArrayList<>();
95 for (ESArtifactData artifact : artifactsList) {
96 ArtifactAccessInfo accessInfo = new ArtifactAccessInfo(servletPath);
97 artifactAccessList.add(accessInfo);
99 return artifactAccessList;
102 public Response createArtifactListResponse(final String serviceName, Either<List<ESArtifactData>, ResourceUploadStatus> getArtifactsStatus/*
103 * List < ? extends IResourceData> artifactsList
104 */, String servletPath) {
106 List<ArtifactAccessInfo> artifactAccessInfos;
107 if (getArtifactsStatus.isRight()) {
108 // if there are no artifacts - return No-Content
109 ResourceUploadStatus status = getArtifactsStatus.right().value();
110 if (status == ResourceUploadStatus.COMPONENT_NOT_EXIST || status == ResourceUploadStatus.SERVICE_NOT_EXIST) {
111 log.debug("resource {} does not exist", serviceName);
112 response = Response.status(HttpStatus.SC_NOT_FOUND).entity("[]").build();
115 log.debug("No content was found for {}", serviceName);
116 response = Response.status(HttpStatus.SC_NO_CONTENT).entity("[]").build();
120 List<? extends ESArtifactData> artifactsList = getArtifactsStatus.left().value();
121 log.debug("{} artifacts were found for {}", artifactsList.size(), serviceName);
122 artifactAccessInfos = convertArtifactList(artifactsList, servletPath);
124 String artifactDataJson = gson.toJson(new ArtifactAccessList(artifactAccessInfos));
125 response = Response.status(HttpStatus.SC_OK).entity(artifactDataJson).type(MediaType.APPLICATION_JSON_TYPE).build();
131 public Response buildResponse(int status, String errorMessage) {
133 ServletJsonResponse jsonResponse = new ServletJsonResponse();
134 jsonResponse.setDescription(errorMessage);
135 jsonResponse.setSource(Constants.CATALOG_BE);
137 return Response.status(status).entity(jsonResponse).build();