Merge "Add proxy support"
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / distribution / servlet / DistributionCatalogServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.distribution.servlet;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.InputStream;
25 import java.util.EnumMap;
26 import java.util.HashMap;
27 import java.util.Map;
28
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;
41
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;
53
54 import com.jcabi.aspects.Loggable;
55
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;
62
63 /**
64  * This Servlet serves external users to download artifacts.
65  * 
66  * @author tgitelman
67  *
68  */
69
70 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
71 @Path("/v1/catalog")
72 @Api(value = "Distribution Catalog Servlet", description = "This Servlet serves external users to download artifacts.")
73 @Singleton
74 public class DistributionCatalogServlet extends BeGenericServlet {
75
76         private static Logger log = LoggerFactory.getLogger(DistributionCatalogServlet.class.getName());
77         @Context
78         private HttpServletRequest request;
79
80         // *******************************************************
81         // Download (GET) artifacts
82         // **********************************************************/
83         /**
84          * 
85          * @param requestId
86          * @param instanceIdHeader
87          * @param accept
88          * @param authorization
89          * @param serviceName
90          * @param serviceVersion
91          * @param artifactName
92          * @return
93          */
94         @GET
95         @Path("/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}")
96         @Consumes(MediaType.APPLICATION_JSON)
97         @Produces(MediaType.APPLICATION_OCTET_STREAM)
98         @ApiOperation(value = "Download service artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
99         @ApiResponses(value = {
100                         @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
101                         @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
102                         @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
103                         @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
104                         @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
105                         @ApiResponse(code = 404, message = "Specified Service Version is  not  found - SVC4504"),
106                         @ApiResponse(code = 404, message = "Specified artifact is  not found - SVC4505"),
107                         @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
108                         @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")})
109         public Response downloadServiceArtifact(
110                         @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
111                         @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
112                         @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
113                         @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
114                         @PathParam("serviceName") final String serviceName, 
115                         @PathParam("serviceVersion") final String serviceVersion, 
116                         @PathParam("artifactName") final String artifactName) {
117                 
118                 Response response = null;
119                 String requestURI = request.getRequestURI();
120                 AuditingActionEnum auditingActionEnum = AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD;
121                 EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<AuditingFieldsKeysEnum, Object>(AuditingFieldsKeysEnum.class);
122                 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
123                 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, requestURI);
124
125                 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
126                         log.debug("Missing X-ECOMP-InstanceID header");
127                         ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
128                         getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
129                         return buildErrorResponse(responseFormat);
130                 }
131
132                 try {
133                         ServletContext context = request.getSession().getServletContext();
134                         ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
135                         Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName);
136                         if (downloadRsrcArtifactEither.isRight()) {
137                                 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
138                                 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
139                                 response = buildErrorResponse(responseFormat);
140                         } else {
141                                 byte[] value = downloadRsrcArtifactEither.left().value();
142                                 InputStream is = new ByteArrayInputStream(value);
143
144                                 Map<String, String> headers = new HashMap<>();
145                                 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
146                                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
147                                 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
148                                 response = buildOkResponse(responseFormat, is, headers);
149                         }
150                         return response;
151
152                 } catch (Exception e) {
153                         BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "download Murano package artifact for service - external API");
154                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download Murano package artifact for service - external API");
155                         log.debug("download artifact failed with exception", e);
156                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
157                 }
158         }
159         
160         /**
161          * 
162          * @param requestId
163          * @param instanceIdHeader
164          * @param accept
165          * @param authorization
166          * @param serviceName
167          * @param serviceVersion
168          * @param resourceName
169          * @param resourceVersion
170          * @param artifactName
171          * @return
172          */
173         @GET
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(
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) {
199                 
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);
206
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);
212                 }
213
214                 try {
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);
222                         } else {
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);
231                         }
232                         return response;
233
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));
239                 }
240         }
241
242         /**
243          * 
244          * @param requestId
245          * @param instanceIdHeader
246          * @param accept
247          * @param authorization
248          * @param serviceName
249          * @param serviceVersion
250          * @param resourceInstanceName
251          * @param artifactName
252          * @return
253          */
254         @GET
255         @Path("/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}")
256         @Consumes(MediaType.APPLICATION_JSON)
257         @Produces(MediaType.APPLICATION_OCTET_STREAM)
258         @ApiOperation(value = "Download resource instance artifact", httpMethod = "GET", notes = "Returns downloaded artifact", response = String.class)
259         @ApiResponses(value = {
260                         @ApiResponse(code = 200, message = "The artifact is found and streamed.", response = String.class),
261                         @ApiResponse(code = 400, message = "Missing  'X-ECOMP-InstanceID'  HTTP header - POL5001"),
262                         @ApiResponse(code = 401, message = "ECOMP component  should authenticate itself  and  to  re-send  again  HTTP  request  with its Basic  Authentication credentials - POL5002"),
263                         @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
264                         @ApiResponse(code = 404, message = "Specified Service is not found - SVC4503"),
265                         @ApiResponse(code = 404, message = "Specified Resource Instance  is not found - SVC4526"),
266                         @ApiResponse(code = 404, message = "Specified Service Version is  not  found - SVC4504"),
267                         @ApiResponse(code = 404, message = "Specified artifact is  not found - SVC4505"),
268                         @ApiResponse(code = 405, message = "Method  Not Allowed: Invalid HTTP method type used (PUT,DELETE,POST will be rejected) - POL4050"),
269                         @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")})
270         public Response downloadResourceInstanceArtifact(
271                         @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
272                         @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
273                         @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
274                         @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
275                         @PathParam("serviceName") final String serviceName, 
276                         @PathParam("serviceVersion") final String serviceVersion, 
277                         @PathParam("resourceInstanceName") final String resourceInstanceName,
278                         @PathParam("artifactName") final String artifactName) {
279                 
280                 Response response = null;
281                 String requestURI = request.getRequestURI();
282                 AuditingActionEnum auditingActionEnum = AuditingActionEnum.DISTRIBUTION_ARTIFACT_DOWNLOAD;
283                 EnumMap<AuditingFieldsKeysEnum, Object> additionalParam = new EnumMap<>(AuditingFieldsKeysEnum.class);
284                 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_CONSUMER_ID, instanceIdHeader);
285                 additionalParam.put(AuditingFieldsKeysEnum.AUDIT_DISTRIBUTION_RESOURCE_URL, requestURI);
286
287                 if (instanceIdHeader == null || instanceIdHeader.isEmpty()) {
288                         log.debug("Missing X-ECOMP-InstanceID header");
289                         ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
290                         getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
291                         return buildErrorResponse(responseFormat);
292                 }
293
294                 try {
295                         ServletContext context = request.getSession().getServletContext();
296                         ArtifactsBusinessLogic artifactsLogic = getArtifactBL(context);
297                         Either<byte[], ResponseFormat> downloadRsrcArtifactEither = artifactsLogic.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName, artifactName);
298                         if (downloadRsrcArtifactEither.isRight()) {
299                                 ResponseFormat responseFormat = downloadRsrcArtifactEither.right().value();
300                                 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
301                                 response = buildErrorResponse(responseFormat);
302                         } else {
303                                 byte[] value = downloadRsrcArtifactEither.left().value();
304                                 // Returning 64-encoded as it was received during upload
305                                 InputStream is = new ByteArrayInputStream(value);
306                                 Map<String, String> headers = new HashMap<>();
307                                 headers.put(Constants.CONTENT_DISPOSITION_HEADER, getContentDispositionValue(artifactName));
308                                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
309                                 getComponentsUtils().auditDistributionDownload(responseFormat, auditingActionEnum, additionalParam);
310                                 response = buildOkResponse(responseFormat, is, headers);
311                         }
312                         return response;
313
314                 } catch (Exception e) {
315                         BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "download interface artifact for resource - external API");
316                         BeEcompErrorManager.getInstance().logBeRestApiGeneralError("download interface artifact for resource - external API");
317                         log.debug("download artifact failed with exception", e);
318                         return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
319                 }
320         }
321 }