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