ea00a86ad9d6de7cc09c352a2ab570c1e574a338
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / GenericArtifactBrowserServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiParam;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30 import java.io.IOException;
31 import java.util.Set;
32 import java.util.stream.Collectors;
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.Consumes;
36 import javax.ws.rs.POST;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import org.apache.commons.lang3.tuple.ImmutablePair;
43 import org.onap.sdc.gab.model.GABQuery;
44 import org.onap.sdc.gab.model.GABQuery.GABQueryType;
45 import org.openecomp.sdc.be.info.GenericArtifactQueryInfo;
46 import org.openecomp.sdc.common.log.wrappers.Logger;
47 import org.openecomp.sdc.exception.ResponseFormat;
48 import org.owasp.esapi.ESAPI;
49 import org.springframework.stereotype.Controller;
50
51 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
52 @Path("/v1/catalog/gab")
53 @Consumes(MediaType.APPLICATION_JSON)
54 @Produces(MediaType.APPLICATION_JSON)
55 @Api(value = "Generic Artifact Browser")
56 @Controller
57 public class GenericArtifactBrowserServlet extends BeGenericServlet {
58
59     private static final Logger LOGGER = Logger.getLogger(GenericArtifactBrowserServlet.class);
60
61     @POST
62     @Path("/searchFor")
63     @ApiOperation(value = "Search json paths inside the yaml", httpMethod = "POST", notes = "Returns found entries of json paths", response = Response.class)
64     @ApiResponses(value = {
65         @ApiResponse(code = 200, message = "Returned yaml entries"),
66         @ApiResponse(code = 400, message = "Invalid content / Missing content")})
67     public Response searchFor(
68         @ApiParam(value = "Generic Artifact search model", required = true) GenericArtifactQueryInfo query,
69         @Context final HttpServletRequest request) {
70         try {
71             ServletContext context = request.getSession().getServletContext();
72             Either<ImmutablePair<String, byte[]>, ResponseFormat> immutablePairResponseFormatEither = getArtifactBL(context)
73                 .downloadArtifact(ESAPI.encoder().canonicalize(query.getParentId()), ESAPI.encoder().canonicalize(query.getArtifactUniqueId()));
74             if (immutablePairResponseFormatEither.isLeft()){
75                 GABQuery gabQuery = prepareGabQuery(query, immutablePairResponseFormatEither);
76                 return buildOkResponse(getGenericArtifactBrowserBL(context).searchFor(gabQuery));
77             }else{
78                 throw new IOException(immutablePairResponseFormatEither.right().value().getFormattedMessage());
79             }
80         } catch (IOException e) {
81             LOGGER.error("Cannot search for a given queries in the yaml file", e);
82             return buildGeneralErrorResponse();
83         }
84     }
85
86     private GABQuery prepareGabQuery(GenericArtifactQueryInfo query,
87         Either<ImmutablePair<String, byte[]>, ResponseFormat> immutablePairResponseFormatEither) {
88         byte[] content = immutablePairResponseFormatEither.left().value().getRight();
89         Set<String> queryFields = query.getFields().stream().map(ESAPI.encoder()::canonicalize).collect(Collectors.toSet());
90         return new GABQuery(queryFields, new String(content), GABQueryType.CONTENT);
91     }
92
93 }