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