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.HashMap;
28 import javax.inject.Singleton;
29 import javax.servlet.ServletContext;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.HeaderParam;
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.model.DistributionData;
45 import org.openecomp.sdc.be.servlets.BeGenericServlet;
46 import org.openecomp.sdc.common.api.Constants;
47 import org.openecomp.sdc.exception.ResponseFormat;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import com.jcabi.aspects.Loggable;
53 import fj.data.Either;
54 import io.swagger.annotations.Api;
55 import io.swagger.annotations.ApiOperation;
56 import io.swagger.annotations.ApiParam;
57 import io.swagger.annotations.ApiResponse;
58 import io.swagger.annotations.ApiResponses;
61 * This Servlet serves external users to download artifacts.
67 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
69 @Api(value = "Distribution Catalog Servlet", description = "This Servlet serves external users to download artifacts.")
71 public class DistributionCatalogServlet extends BeGenericServlet {
73 private static final Logger log = LoggerFactory.getLogger(DistributionCatalogServlet.class);
75 private HttpServletRequest request;
77 // *******************************************************
78 // Download (GET) artifacts
79 // **********************************************************/
83 * @param instanceIdHeader
85 * @param authorization
87 * @param serviceVersion
92 @Path("/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}")
93 @Consumes(MediaType.APPLICATION_JSON)
94 @Produces(MediaType.APPLICATION_OCTET_STREAM)
95 @ApiOperation(value = "Download service artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
96 @ApiResponses(value = {
97 @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
98 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
99 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
100 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
101 @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
102 @ApiResponse(code = 404, message = "Specified Service Version is not found - SVC4504"),
103 @ApiResponse(code = 404, message = "Specified artifact is not found - SVC4505"),
104 @ApiResponse(code = 405, message = "Method Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
105 @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")})
106 public Response downloadServiceArtifact(
107 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
108 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
109 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
110 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
111 @PathParam("serviceName") final String serviceName,
112 @PathParam("serviceVersion") final String serviceVersion,
113 @PathParam("artifactName") final String artifactName) {
115 Response response = null;
116 String requestURI = request.getRequestURI();
117 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
118 log.debug("Missing X-ECOMP-InstanceID header");
119 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
120 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
121 return buildErrorResponse(responseFormat);
125 ServletContext context = request.getSession().getServletContext();
126 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
127 Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName);
128 if (downloadRsrcArtifactEither.isRight()) {
129 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
130 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
131 response = buildErrorResponse(responseFormat);
133 byte[] value = downloadRsrcArtifactEither.left().value();
134 InputStream is = new ByteArrayInputStream(value);
136 Map<String, String> headers = new HashMap<>();
137 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
138 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
139 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
140 response = buildOkResponse(responseFormat, is, headers);
144 } catch (Exception e) {
145 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download Murano package artifact for service - external API");
146 log.debug("download artifact failed with exception", e);
147 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
154 * @param instanceIdHeader
156 * @param authorization
158 * @param serviceVersion
159 * @param resourceName
160 * @param resourceVersion
161 * @param artifactName
165 @Path("/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}")
166 @Consumes(MediaType.APPLICATION_JSON)
167 @Produces(MediaType.APPLICATION_OCTET_STREAM)
168 @ApiOperation(value = "Download resource artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
169 @ApiResponses(value = {
170 @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
171 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
172 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
173 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
174 @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
175 @ApiResponse(code = 404, message = "Specified Resource Instance is not found - SVC4526"),
176 @ApiResponse(code = 404, message = "Specified Service Version is not found - SVC4504"),
177 @ApiResponse(code = 404, message = "Specified artifact is not found - SVC4505"),
178 @ApiResponse(code = 405, message = "Method Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
179 @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")})
180 public Response downloadResourceArtifact(
181 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
182 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
183 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
184 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
185 @PathParam("serviceName") final String serviceName,
186 @PathParam("serviceVersion") final String serviceVersion,
187 @PathParam("resourceName") final String resourceName,
188 @PathParam("resourceVersion") final String resourceVersion,
189 @PathParam("artifactName") final String artifactName) {
191 Response response = null;
192 String requestURI = request.getRequestURI();
194 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
195 log.debug("Missing X-ECOMP-InstanceID header");
196 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
197 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
198 return buildErrorResponse(responseFormat);
202 ServletContext context = request.getSession().getServletContext();
203 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
204 Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName, resourceVersion, artifactName);
205 if (downloadRsrcArtifactEither.isRight()) {
206 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
207 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
208 response = buildErrorResponse(responseFormat);
210 byte[] value = downloadRsrcArtifactEither.left().value();
211 // Returning 64-encoded as it was received during upload
212 InputStream is = new ByteArrayInputStream(value);
213 Map<String, String> headers = new HashMap<>();
214 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
215 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
216 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
217 response = buildOkResponse(responseFormat, is, headers);
221 } catch (Exception e) {
222 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API");
223 log.debug("download artifact failed with exception", e);
224 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
231 * @param instanceIdHeader
233 * @param authorization
235 * @param serviceVersion
236 * @param resourceInstanceName
237 * @param artifactName
241 @Path("/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}")
242 @Consumes(MediaType.APPLICATION_JSON)
243 @Produces(MediaType.APPLICATION_OCTET_STREAM)
244 @ApiOperation(value = "Download resource instance artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
245 @ApiResponses(value = {
246 @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
247 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
248 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
249 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
250 @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
251 @ApiResponse(code = 404, message = "Specified Resource Instance is not found - SVC4526"),
252 @ApiResponse(code = 404, message = "Specified Service Version is not found - SVC4504"),
253 @ApiResponse(code = 404, message = "Specified artifact is not found - SVC4505"),
254 @ApiResponse(code = 405, message = "Method Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
255 @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")})
256 public Response downloadResourceInstanceArtifactByName(
257 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
258 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
259 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
260 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
261 @PathParam("serviceName") final String serviceName,
262 @PathParam("serviceVersion") final String serviceVersion,
263 @PathParam("resourceInstanceName") final String resourceInstanceName,
264 @PathParam("artifactName") final String artifactName) {
266 Response response = null;
267 String requestURI = request.getRequestURI();
269 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
270 log.debug("Missing X-ECOMP-InstanceID header");
271 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
272 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
273 return buildErrorResponse(responseFormat);
277 ServletContext context = request.getSession().getServletContext();
278 ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
279 Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName, artifactName);
280 if (downloadRsrcArtifactEither.isRight()) {
281 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
282 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
283 response = buildErrorResponse(responseFormat);
285 byte[] value = downloadRsrcArtifactEither.left().value();
286 // Returning 64-encoded as it was received during upload
287 InputStream is = new ByteArrayInputStream(value);
288 Map<String, String> headers = new HashMap<>();
289 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
290 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
291 getComponentsUtils().auditDistributionDownload(responseFormat, new DistributionData(instanceIdHeader, requestURI));
292 response = buildOkResponse(responseFormat, is, headers);
296 } catch (Exception e) {
297 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API");
298 log.debug("download artifact failed with exception", e);
299 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));