Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / impl / DownloadArtifactLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.impl;
22
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;
37
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;
47
48 public class DownloadArtifactLogic {
49
50     private static final Logger log = LoggerFactory.getLogger(DownloadArtifactLogic.class);
51
52     private Gson gson = new GsonBuilder().setPrettyPrinting().create();
53
54     public Response downloadArtifact(final String artifactName, Either<? extends ESArtifactData, ResourceUploadStatus> getArtifactStatus, String artifactId) {
55         Response response = null;
56
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();
62             else
63                 response = Response.status(HttpStatus.SC_NOT_FOUND).build();
64
65             return response;
66         }
67         // convert artifact to inputstream
68         else {
69             ESArtifactData artifactData = getArtifactStatus.left().value();
70             byte[] artifactPayload = artifactData.getDataAsArray();
71
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);
76
77             // outputstream for response
78             StreamingOutput stream = output -> {
79                 try {
80                     IOUtils.copy(artifactStream, output);
81                 } catch (IOException e) {
82                     log.debug("failed to copy artifact payload into response");
83                     throw new WebApplicationException(e);
84                 }
85             };
86             return Response.ok(stream).type(MediaType.APPLICATION_OCTET_STREAM_TYPE)
87                     .header("content-disposition", "attachment; filename = " + artifactName)
88                     .build();
89
90         }
91     }
92
93     public List<ArtifactAccessInfo> convertArtifactList(List<? extends ESArtifactData> artifactsList, String servletPath) {
94
95         List<ArtifactAccessInfo> artifactAccessList = new ArrayList<ArtifactAccessInfo>();
96         for (ESArtifactData artifact : artifactsList) {
97             ArtifactAccessInfo accessInfo = new ArtifactAccessInfo(artifact, servletPath);
98             artifactAccessList.add(accessInfo);
99         }
100         return artifactAccessList;
101     }
102
103     public Response createArtifactListResponse(final String serviceName, Either<List<ESArtifactData>, ResourceUploadStatus> getArtifactsStatus/*
104                                                                                                                                                  * List < ? extends IResourceData> artifactsList
105                                                                                                                                                  */, String servletPath) {
106         Response response;
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();
114
115             } else {
116                 log.debug("No content was found for {}", serviceName);
117                 response = Response.status(HttpStatus.SC_NO_CONTENT).entity("[]").build();
118             }
119             return response;
120         } else {
121             List<? extends ESArtifactData> artifactsList = getArtifactsStatus.left().value();
122             log.debug("{} artifacts were found for {}", artifactsList.size(), serviceName);
123             artifactAccessInfos = convertArtifactList(artifactsList, servletPath);
124
125             String artifactDataJson = gson.toJson(new ArtifactAccessList(artifactAccessInfos));
126             response = Response.status(HttpStatus.SC_OK).entity(artifactDataJson).type(MediaType.APPLICATION_JSON_TYPE).build();
127
128             return response;
129         }
130     }
131
132     public Response buildResponse(int status, String errorMessage) {
133
134         ServletJsonResponse jsonResponse = new ServletJsonResponse();
135         jsonResponse.setDescription(errorMessage);
136         jsonResponse.setSource(Constants.CATALOG_BE);
137
138         Response response = Response.status(status).entity(jsonResponse).build();
139
140         return response;
141     }
142
143 }