catalog-be servlets refactoring
[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.components.impl.ComponentInstanceBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
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.ComponentsUtils;
32 import org.openecomp.sdc.be.impl.DownloadArtifactLogic;
33 import org.openecomp.sdc.be.info.ArtifactAccessInfo;
34 import org.openecomp.sdc.be.resources.api.IResourceUploader;
35 import org.openecomp.sdc.be.resources.data.ESArtifactData;
36 import org.openecomp.sdc.be.user.UserBusinessLogic;
37 import org.openecomp.sdc.common.api.Constants;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39
40 import javax.servlet.http.HttpServletRequest;
41 import javax.ws.rs.GET;
42 import javax.ws.rs.Path;
43 import javax.ws.rs.PathParam;
44 import javax.ws.rs.Produces;
45 import javax.ws.rs.core.Context;
46 import javax.ws.rs.core.MediaType;
47 import javax.ws.rs.core.Response;
48
49
50 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
51 @Path("/v1/catalog/resources/available")
52 public class ResourceArtifactDownloadServlet extends ToscaDaoServlet {
53
54     private static final Logger log = Logger.getLogger(ResourceArtifactDownloadServlet.class);
55
56     public ResourceArtifactDownloadServlet(UserBusinessLogic userBusinessLogic,
57         ComponentsUtils componentsUtils,
58         IResourceUploader resourceUploader, DownloadArtifactLogic logic) {
59         super(userBusinessLogic, componentsUtils, resourceUploader, logic);
60     }
61
62     @GET
63     @Path("/{resourceName}/{resourceVersion}/artifacts/{artifactName}")
64     // @Produces(MediaType.APPLICATION_OCTET_STREAM)
65     public Response getResourceArtifactByName(@PathParam("resourceName") final String resourceName, @PathParam("resourceVersion") final String resourceVersion, @PathParam("artifactName") final String artifactName,
66             @Context final HttpServletRequest request) {
67
68         String url = request.getMethod() + " " + request.getRequestURI();
69         log.debug("Start handle request of {}", url);
70         Response response = null;
71         try {
72             // get the artifact data
73             String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
74
75             Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resourceUploader.getArtifact(artifactId);
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
102             String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
103             Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resourceUploader.getArtifact(artifactId);
104
105             if (getArtifactStatus.isRight()) {
106                 ResourceUploadStatus status = getArtifactStatus.right().value();
107                 if (status == ResourceUploadStatus.COMPONENT_NOT_EXIST) {
108                     response = Response.status(HttpStatus.SC_NOT_FOUND).build();
109                     log.debug("Could not find artifact for with id: {}", artifactId);
110                 } else {
111                     response = Response.status(HttpStatus.SC_NO_CONTENT).build();
112                     log.debug("Could not find artifact for with id: {}", artifactId);
113                 }
114                 return response;
115             } else {
116                 ESArtifactData artifactData = getArtifactStatus.left().value();
117                 log.debug("found artifact with id: {}", artifactId);
118                 ArtifactAccessInfo artifactInfo = new ArtifactAccessInfo(artifactData);
119                 String artifactDataJson = gson.toJson(artifactInfo);
120                 response = Response.status(HttpStatus.SC_OK).entity(artifactDataJson).type(MediaType.APPLICATION_JSON_TYPE).build();
121
122                 log.info("Finish handle request of {} | result = {}", url, response.getStatus());
123                 return response;
124             }
125         } catch (Exception e) {
126             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Resource Artifact Metadata");
127             log.debug("getResourceArtifactMetadata failed with exception", e);
128             response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
129             return response;
130         }
131
132     }
133
134     @Override
135     public Logger getLogger() {
136         return log;
137     }
138 }