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.HeaderParam;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
42 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
43 import org.openecomp.sdc.be.config.BeEcompErrorManager;
44 import org.openecomp.sdc.be.dao.api.ActionStatus;
45 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
46 import org.openecomp.sdc.be.servlets.BeGenericServlet;
47 import org.openecomp.sdc.common.api.Constants;
48 import org.openecomp.sdc.common.config.EcompErrorName;
49 import org.openecomp.sdc.common.datastructure.AuditingFieldsKeysEnum;
50 import org.openecomp.sdc.exception.ResponseFormat;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 import com.jcabi.aspects.Loggable;
56 import fj.data.Either;
57 import io.swagger.annotations.Api;
58 import io.swagger.annotations.ApiOperation;
59 import io.swagger.annotations.ApiParam;
60 import io.swagger.annotations.ApiResponse;
61 import io.swagger.annotations.ApiResponses;
64 * This Servlet serves external users to download artifacts.
70 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
72 @Api(value = "Distribution Catalog Servlet", description = "This Servlet serves external users to download artifacts.")
74 public class DistributionCatalogServlet extends BeGenericServlet {
76 private static Logger log = LoggerFactory.getLogger(DistributionCatalogServlet.class.getName());
78 // *******************************************************
79 // Download (GET) artifacts
80 // **********************************************************/
85 * @param instanceIdHeader
87 * @param authorization
89 * @param serviceVersion
94 @Path("/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}")
95 @Consumes(MediaType.APPLICATION_JSON)
96 @Produces(MediaType.APPLICATION_OCTET_STREAM)
97 @ApiOperation(value = "Download service artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
98 @ApiResponses(value = {
99 @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
100 @ApiResponse(code = 400, message = "Missing “X-ECOMP-InstanceID” HTTP header - POL5001"),
101 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
102 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
103 @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
104 @ApiResponse(code = 404, message = "Specified Service Version is not found - SVC4504"),
105 @ApiResponse(code = 404, message = "Specified artifact is not found - SVC4505"),
106 @ApiResponse(code = 405, message = "Method Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
107 @ApiResponse(code = 500, message = "The GET request failed either due to internal SDC problem or Cambria Service failure. ECOMP Component should continue the attempts to get the needed information - POL5000")})
108 public Response downloadServiceArtifact(@Context final HttpServletRequest request,
109 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
110 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
111 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
112 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
113 @PathParam("serviceName") final String serviceName,
114 @PathParam("serviceVersion") final String serviceVersion,
115 @PathParam("artifactName") final String artifactName) {
117 Response response = null;
118 String requestURI = request.getRequestURI();
119 AuditingActionEnum auditingActionEnum = AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD;
120 EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<AuditingFieldsKeysEnum, Object>(AuditingFieldsKeysEnum.class);
121 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
122 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, requestURI);
124 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
125 log.debug("Missing X-ECOMP-InstanceID header");
126 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
127 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
128 return buildErrorResponse(responseFormat);
132 ServletContext context = request.getSession().getServletContext();
133 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
134 Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName);
135 if (downloadRsrcArtifactEither.isRight()) {
136 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
137 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
138 response = buildErrorResponse(responseFormat);
140 byte[] value = downloadRsrcArtifactEither.left().value();
141 InputStream is = new ByteArrayInputStream(value);
143 Map<String, String> headers = new HashMap<>();
144 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
145 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
146 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
147 response = buildOkResponse(responseFormat, is, headers);
151 } catch (Exception e) {
152 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "download Murano package artifact for service - external API");
153 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download Murano package artifact for service - external API");
154 log.debug("download artifact failed with exception", e);
155 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
163 * @param instanceIdHeader
165 * @param authorization
167 * @param serviceVersion
168 * @param resourceName
169 * @param resourceVersion
170 * @param artifactName
174 @Path("/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}")
175 @Consumes(MediaType.APPLICATION_JSON)
176 @Produces(MediaType.APPLICATION_OCTET_STREAM)
177 @ApiOperation(value = "Download resource artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
178 @ApiResponses(value = {
179 @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
180 @ApiResponse(code = 400, message = "Missing “X-ECOMP-InstanceID” HTTP header - POL5001"),
181 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
182 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
183 @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
184 @ApiResponse(code = 404, message = "Specified Resource Instance is not found - SVC4526"),
185 @ApiResponse(code = 404, message = "Specified Service Version is not found - SVC4504"),
186 @ApiResponse(code = 404, message = "Specified artifact is not found - SVC4505"),
187 @ApiResponse(code = 405, message = "Method Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
188 @ApiResponse(code = 500, message = "The GET request failed either due to internal SDC problem or Cambria Service failure. ECOMP Component should continue the attempts to get the needed information - POL5000")})
189 public Response downloadResourceArtifact(@Context final HttpServletRequest request,
190 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
191 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
192 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
193 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
194 @PathParam("serviceName") final String serviceName,
195 @PathParam("serviceVersion") final String serviceVersion,
196 @PathParam("resourceName") final String resourceName,
197 @PathParam("resourceVersion") final String resourceVersion,
198 @PathParam("artifactName") final String artifactName) {
200 Response response = null;
201 String requestURI = request.getRequestURI();
202 AuditingActionEnum auditingActionEnum = AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD;
203 EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<AuditingFieldsKeysEnum, Object>(AuditingFieldsKeysEnum.class);
204 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
205 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, requestURI);
207 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
208 log.debug("Missing X-ECOMP-InstanceID header");
209 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
210 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
211 return buildErrorResponse(responseFormat);
215 ServletContext context = request.getSession().getServletContext();
216 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
217 Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName, resourceVersion, artifactName);
218 if (downloadRsrcArtifactEither.isRight()) {
219 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
220 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
221 response = buildErrorResponse(responseFormat);
223 byte[] value = downloadRsrcArtifactEither.left().value();
224 // Returning 64-encoded as it was received during upload
225 InputStream is = new ByteArrayInputStream(value);
226 Map<String, String> headers = new HashMap<>();
227 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
228 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
229 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
230 response = buildOkResponse(responseFormat, is, headers);
234 } catch (Exception e) {
235 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "download interface artifact for resource - external API");
236 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API");
237 log.debug("download artifact failed with exception", e);
238 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
246 * @param instanceIdHeader
248 * @param authorization
250 * @param serviceVersion
251 * @param resourceInstanceName
252 * @param artifactName
256 @Path("/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}")
257 @Consumes(MediaType.APPLICATION_JSON)
258 @Produces(MediaType.APPLICATION_OCTET_STREAM)
259 @ApiOperation(value = "Download resource instance artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
260 @ApiResponses(value = {
261 @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
262 @ApiResponse(code = 400, message = "Missing “X-ECOMP-InstanceID” HTTP header - POL5001"),
263 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
264 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
265 @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
266 @ApiResponse(code = 404, message = "Specified Resource Instance is not found - SVC4526"),
267 @ApiResponse(code = 404, message = "Specified Service Version is not found - SVC4504"),
268 @ApiResponse(code = 404, message = "Specified artifact is not found - SVC4505"),
269 @ApiResponse(code = 405, message = "Method Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
270 @ApiResponse(code = 500, message = "The GET request failed either due to internal SDC problem or Cambria Service failure. ECOMP Component should continue the attempts to get the needed information - POL5000")})
271 public Response downloadResourceInstanceArtifact(@Context final HttpServletRequest request,
272 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
273 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
274 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
275 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
276 @PathParam("serviceName") final String serviceName,
277 @PathParam("serviceVersion") final String serviceVersion,
278 @PathParam("resourceInstanceName") final String resourceInstanceName,
279 @PathParam("artifactName") final String artifactName) {
281 Response response = null;
282 String requestURI = request.getRequestURI();
283 AuditingActionEnum auditingActionEnum = AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD;
284 EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<>(AuditingFieldsKeysEnum.class);
285 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
286 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, requestURI);
288 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
289 log.debug("Missing X-ECOMP-InstanceID header");
290 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
291 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
292 return buildErrorResponse(responseFormat);
296 ServletContext context = request.getSession().getServletContext();
297 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
298 Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName, artifactName);
299 if (downloadRsrcArtifactEither.isRight()) {
300 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
301 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
302 response = buildErrorResponse(responseFormat);
304 byte[] value = downloadRsrcArtifactEither.left().value();
305 // Returning 64-encoded as it was received during upload
306 InputStream is = new ByteArrayInputStream(value);
307 Map<String, String> headers = new HashMap<>();
308 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
309 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
310 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
311 response = buildOkResponse(responseFormat, is, headers);
315 } catch (Exception e) {
316 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "download interface artifact for resource - external API");
317 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API");
318 log.debug("download artifact failed with exception", e);
319 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));