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.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;
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;
50 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
51 @Path("/v1/catalog/resources/available")
52 public class ResourceArtifactDownloadServlet extends ToscaDaoServlet {
54 private static final Logger log = Logger.getLogger(ResourceArtifactDownloadServlet.class);
56 public ResourceArtifactDownloadServlet(UserBusinessLogic userBusinessLogic,
57 ComponentsUtils componentsUtils,
58 IResourceUploader resourceUploader, DownloadArtifactLogic logic) {
59 super(userBusinessLogic, componentsUtils, resourceUploader, logic);
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) {
68 String url = request.getMethod() + " " + request.getRequestURI();
69 log.debug("Start handle request of {}", url);
70 Response response = null;
72 // get the artifact data
73 String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
75 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resourceUploader.getArtifact(artifactId);
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;
102 String artifactId = String.format(Constants.ARTIFACT_ID_FORMAT, resourceName, resourceVersion, artifactName);
103 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = resourceUploader.getArtifact(artifactId);
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);
111 response = Response.status(HttpStatus.SC_NO_CONTENT).build();
112 log.debug("Could not find artifact for with id: {}", artifactId);
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();
122 log.info("Finish handle request of {} | result = {}", url, response.getStatus());
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));
135 public Logger getLogger() {