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.distribution.servlet;
23 import java.io.ByteArrayInputStream;
24 import java.io.InputStream;
25 import java.util.EnumMap;
26 import java.util.HashMap;
29 import javax.inject.Singleton;
30 import javax.servlet.ServletContext;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
41 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
42 import org.openecomp.sdc.be.config.BeEcompErrorManager;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
45 import org.openecomp.sdc.be.servlets.BeGenericServlet;
46 import org.openecomp.sdc.common.api.Constants;
47 import org.openecomp.sdc.common.config.EcompErrorName;
48 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
49 import org.openecomp.sdc.exception.ResponseFormat;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 import com.jcabi.aspects.Loggable;
54 import com.wordnik.swagger.annotations.Api;
55 import com.wordnik.swagger.annotations.ApiOperation;
56 import com.wordnik.swagger.annotations.ApiResponse;
57 import com.wordnik.swagger.annotations.ApiResponses;
59 import fj.data.Either;
62 * This Servlet serves external users to download artifacts.
68 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
70 @Api(value = "Distribution Catalog Servlet", description = "This Servlet serves external users to download artifacts.")
72 public class DistributionCatalogServlet extends BeGenericServlet {
74 private static Logger log = LoggerFactory.getLogger(DistributionCatalogServlet.class.getName());
76 // *******************************************************
77 // Download (GET) artifacts
78 // **********************************************************/
81 @Path("/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}")
82 @Consumes(MediaType.APPLICATION_JSON)
83 @Produces(MediaType.APPLICATION_OCTET_STREAM)
84 @ApiOperation(value = "Download service artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = Response.class)
85 @ApiResponses(value = { @ApiResponse(code = 200, message = "Artifact downloaded"), @ApiResponse(code = 401, message = "Authorization required"), @ApiResponse(code = 403, message = "Restricted operation"),
86 @ApiResponse(code = 404, message = "Artifact not found") })
87 public Response downloadServiceArtifact(@PathParam("serviceName") final String serviceName, @PathParam("serviceVersion") final String serviceVersion, @PathParam("artifactName") final String artifactName,
88 @Context final HttpServletRequest request) {
89 Response response = null;
90 String instanceIdHeader = request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER);
91 String requestURI = request.getRequestURI();
92 AuditingActionEnum auditingActionEnum = AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD;
93 EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<AuditingFieldsKeysEnum, Object>(AuditingFieldsKeysEnum.class);
94 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
95 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, requestURI);
97 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
98 log.debug("Missing X-ECOMP-InstanceID header");
99 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
100 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
101 return buildErrorResponse(responseFormat);
105 ServletContext context = request.getSession().getServletContext();
106 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
107 Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName);
108 if (downloadRsrcArtifactEither.isRight()) {
109 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
110 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
111 response = buildErrorResponse(responseFormat);
113 byte[] value = downloadRsrcArtifactEither.left().value();
114 InputStream is = new ByteArrayInputStream(value);
116 Map<String, String> headers = new HashMap<>();
117 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
118 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
119 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
120 response = buildOkResponse(responseFormat, is, headers);
124 } catch (Exception e) {
125 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "download Murano package artifact for service - external API");
126 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download Murano package artifact for service - external API");
127 log.debug("download artifact failed with exception", e);
128 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
133 @Path("/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}")
134 @Consumes(MediaType.APPLICATION_JSON)
135 @Produces(MediaType.APPLICATION_OCTET_STREAM)
136 @ApiOperation(value = "Download resource artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = Response.class)
137 @ApiResponses(value = { @ApiResponse(code = 200, message = "Artifact downloaded"), @ApiResponse(code = 401, message = "Authorization required"), @ApiResponse(code = 403, message = "Restricted operation"),
138 @ApiResponse(code = 404, message = "Artifact not found") })
139 public Response downloadResourceArtifact(@PathParam("serviceName") final String serviceName, @PathParam("serviceVersion") final String serviceVersion, @PathParam("resourceName") final String resourceName,
140 @PathParam("resourceVersion") final String resourceVersion, @PathParam("artifactName") final String artifactName, @Context final HttpServletRequest request) {
141 Response response = null;
142 String instanceIdHeader = request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER);
143 String requestURI = request.getRequestURI();
144 AuditingActionEnum auditingActionEnum = AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD;
145 EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<AuditingFieldsKeysEnum, Object>(AuditingFieldsKeysEnum.class);
146 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
147 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, requestURI);
149 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
150 log.debug("Missing X-ECOMP-InstanceID header");
151 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
152 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
153 return buildErrorResponse(responseFormat);
157 ServletContext context = request.getSession().getServletContext();
158 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
159 Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName, resourceVersion, artifactName);
160 if (downloadRsrcArtifactEither.isRight()) {
161 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
162 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
163 response = buildErrorResponse(responseFormat);
165 byte[] value = downloadRsrcArtifactEither.left().value();
166 // Returning 64-encoded as it was received during upload
167 InputStream is = new ByteArrayInputStream(value);
168 Map<String, String> headers = new HashMap<>();
169 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
170 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
171 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
172 response = buildOkResponse(responseFormat, is, headers);
176 } catch (Exception e) {
177 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "download interface artifact for resource - external API");
178 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API");
179 log.debug("download artifact failed with exception", e);
180 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
184 // --------------------------------
187 @Path("/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}")
188 @Consumes(MediaType.APPLICATION_JSON)
189 @Produces(MediaType.APPLICATION_OCTET_STREAM)
190 @ApiOperation(value = "Download resource artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = Response.class)
191 @ApiResponses(value = { @ApiResponse(code = 200, message = "Artifact downloaded"), @ApiResponse(code = 401, message = "Authorization required"), @ApiResponse(code = 403, message = "Restricted operation"),
192 @ApiResponse(code = 404, message = "Artifact not found") })
193 public Response downloadResourceInstanceArtifact(@PathParam("serviceName") final String serviceName, @PathParam("serviceVersion") final String serviceVersion, @PathParam("resourceInstanceName") final String resourceInstanceName,
194 @PathParam("artifactName") final String artifactName, @Context final HttpServletRequest request) {
195 Response response = null;
196 String instanceIdHeader = request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER);
197 String requestURI = request.getRequestURI();
198 AuditingActionEnum auditingActionEnum = AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD;
199 EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<AuditingFieldsKeysEnum, Object>(AuditingFieldsKeysEnum.class);
200 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
201 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, requestURI);
203 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
204 log.debug("Missing X-ECOMP-InstanceID header");
205 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
206 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
207 return buildErrorResponse(responseFormat);
211 ServletContext context = request.getSession().getServletContext();
212 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
213 Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName, artifactName);
214 if (downloadRsrcArtifactEither.isRight()) {
215 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
216 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
217 response = buildErrorResponse(responseFormat);
219 byte[] value = downloadRsrcArtifactEither.left().value();
220 // Returning 64-encoded as it was received during upload
221 InputStream is = new ByteArrayInputStream(value);
222 Map<String, String> headers = new HashMap<>();
223 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
224 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
225 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
226 response = buildOkResponse(responseFormat, is, headers);
230 } catch (Exception e) {
231 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "download interface artifact for resource - external API");
232 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API");
233 log.debug("download artifact failed with exception", e);
234 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
238 // --------------------------------