2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.servlets;
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;
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;
48 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
49 @Path("/v1/catalog/resources/available")
50 public class ResourceArtifactDownloadServlet extends ToscaDaoServlet {
52 private static final Logger log = LoggerFactory.getLogger(ResourceArtifactDownloadServlet.class);
54 private Gson gson = new GsonBuilder().setPrettyPrinting().create();
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) {
62 String url = request.getMethod() + " " + request.getRequestURI();
63 log.debug("Start handle request of {}", url);
64 Response response = null;
66 // get the artifact data
67 String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
69 IResourceUploader resouceUploader = getResourceUploader(request.getSession().getServletContext());
70 if (resouceUploader == null) {
71 return buildResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "");
74 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resouceUploader.getArtifact(artifactId);
76 DownloadArtifactLogic logic = getLogic(request.getSession().getServletContext());
78 return buildResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "");
81 response = logic.downloadArtifact(artifactName, getArtifactStatus, artifactId);
83 log.info("Finish handle request of {} | result = {}", url, response.getStatus());
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));
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) {
100 String url = request.getMethod() + " " + request.getRequestURI();
101 log.debug("Start handle request of {}", url);
103 Response response = null;
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");
112 String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
113 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resourceDao.getArtifact(artifactId);
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);
121 response = Response.status(HttpStatus.SC_NO_CONTENT).build();
122 log.debug("Could not find artifact for with id: {}", artifactId);
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();
132 log.info("Finish handle request of {} | result = {}", url, response.getStatus());
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));
145 public Logger getLogger() {