Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ResourceArtifactDownloadServlet.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.servlets;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.jcabi.aspects.Loggable;
26 import fj.data.Either;
27 import org.apache.http.HttpStatus;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.dao.api.ResourceUploadStatus;
31 import org.openecomp.sdc.be.impl.DownloadArtifactLogic;
32 import org.openecomp.sdc.be.info.ArtifactAccessInfo;
33 import org.openecomp.sdc.be.resources.api.IResourceUploader;
34 import org.openecomp.sdc.be.resources.data.ESArtifactData;
35 import org.openecomp.sdc.common.api.Constants;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.ws.rs.GET;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.PathParam;
43 import javax.ws.rs.Produces;
44 import javax.ws.rs.core.Context;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47
48 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
49 @Path("/v1/catalog/resources/available")
50 public class ResourceArtifactDownloadServlet extends ToscaDaoServlet {
51
52     private static final Logger log = LoggerFactory.getLogger(ResourceArtifactDownloadServlet.class);
53
54     private Gson gson = new GsonBuilder().setPrettyPrinting().create();
55
56     @GET
57     @Path("/{resourceName}/{resourceVersion}/artifacts/{artifactName}")
58     // @Produces(MediaType.APPLICATION_OCTET_STREAM)
59     public Response getResourceArtifactByName(@PathParam("resourceName") final String resourceName, @PathParam("resourceVersion") final String resourceVersion, @PathParam("artifactName") final String artifactName,
60             @Context final HttpServletRequest request) {
61
62         String url = request.getMethod() + " " + request.getRequestURI();
63         log.debug("Start handle request of {}", url);
64         Response response = null;
65         try {
66             // get the artifact data
67             String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
68
69             IResourceUploader resouceUploader = getResourceUploader(request.getSession().getServletContext());
70             if (resouceUploader == null) {
71                 return buildResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "");
72
73             }
74             Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resouceUploader.getArtifact(artifactId);
75
76             DownloadArtifactLogic logic = getLogic(request.getSession().getServletContext());
77             if (logic == null) {
78                 return buildResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "");
79
80             }
81             response = logic.downloadArtifact(artifactName, getArtifactStatus, artifactId);
82
83             log.info("Finish handle request of {} | result = {}", url, response.getStatus());
84             return response;
85
86         } catch (Exception e) {
87             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Resource Artifact By Name");
88             log.debug("getResourceArtifactByName failed with exception", e);
89             response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
90             return response;
91         }
92     }
93
94     @GET
95     @Path("/{resourceName}/{resourceVersion}/artifacts/{artifactName}/metadata")
96     @Produces(MediaType.APPLICATION_JSON)
97     public Response getResourceArtifactMetadata(@PathParam("resourceName") final String resourceName, @PathParam("resourceVersion") final String resourceVersion, @PathParam("artifactName") final String artifactName,
98             @Context final HttpServletRequest request) {
99
100         String url = request.getMethod() + " " + request.getRequestURI();
101         log.debug("Start handle request of {}", url);
102
103         Response response = null;
104         try {
105             IResourceUploader resourceDao = getResourceUploader(request.getSession().getServletContext());
106             if (resourceDao == null) {
107                 log.error("resource dao cannot be found");
108                 response = buildResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Resource dao cannot be found");
109                 return response;
110             }
111
112             String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
113             Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resourceDao.getArtifact(artifactId);
114
115             if (getArtifactStatus.isRight()) {
116                 ResourceUploadStatus status = getArtifactStatus.right().value();
117                 if (status == ResourceUploadStatus.COMPONENT_NOT_EXIST) {
118                     response = Response.status(HttpStatus.SC_NOT_FOUND).build();
119                     log.debug("Could not find artifact for with id: {}", artifactId);
120                 } else {
121                     response = Response.status(HttpStatus.SC_NO_CONTENT).build();
122                     log.debug("Could not find artifact for with id: {}", artifactId);
123                 }
124                 return response;
125             } else {
126                 ESArtifactData artifactData = getArtifactStatus.left().value();
127                 log.debug("found artifact with id: {}", artifactId);
128                 ArtifactAccessInfo artifactInfo = new ArtifactAccessInfo(artifactData);
129                 String artifactDataJson = gson.toJson(artifactInfo);
130                 response = Response.status(HttpStatus.SC_OK).entity(artifactDataJson).type(MediaType.APPLICATION_JSON_TYPE).build();
131
132                 log.info("Finish handle request of {} | result = {}", url, response.getStatus());
133                 return response;
134             }
135         } catch (Exception e) {
136             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Resource Artifact Metadata");
137             log.debug("getResourceArtifactMetadata failed with exception", e);
138             response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
139             return response;
140         }
141
142     }
143
144     @Override
145     public Logger getLogger() {
146         return log;
147     }
148 }