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.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;
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;
46 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
47 @Path("/v1/catalog/resources/available")
48 public class ResourceArtifactDownloadServlet extends ToscaDaoServlet {
50 private static final Logger log = Logger.getLogger(ResourceArtifactDownloadServlet.class);
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) {
58 String url = request.getMethod() + " " + request.getRequestURI();
59 log.debug("Start handle request of {}", url);
60 Response response = null;
62 // get the artifact data
63 String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
65 IResourceUploader resouceUploader = getResourceUploader(request.getSession().getServletContext());
66 if (resouceUploader == null) {
67 return buildResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "");
70 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resouceUploader.getArtifact(artifactId);
72 DownloadArtifactLogic logic = getLogic(request.getSession().getServletContext());
74 return buildResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "");
77 response = logic.downloadArtifact(artifactName, getArtifactStatus, artifactId);
79 log.info("Finish handle request of {} | result = {}", url, response.getStatus());
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));
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) {
96 String url = request.getMethod() + " " + request.getRequestURI();
97 log.debug("Start handle request of {}", url);
99 Response response = null;
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");
108 String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
109 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resourceDao.getArtifact(artifactId);
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);
117 response = Response.status(HttpStatus.SC_NO_CONTENT).build();
118 log.debug("Could not find artifact for with id: {}", artifactId);
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();
128 log.info("Finish handle request of {} | result = {}", url, response.getStatus());
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));
141 public Logger getLogger() {