2 * ============LICENSE_START=======================================================
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
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.servlets;
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;
32 import java.util.stream.Collectors;
33 import javax.inject.Inject;
34 import javax.servlet.ServletContext;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.ws.rs.Consumes;
37 import javax.ws.rs.POST;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.Context;
41 import javax.ws.rs.core.MediaType;
42 import javax.ws.rs.core.Response;
43 import org.apache.commons.lang3.tuple.ImmutablePair;
44 import org.onap.sdc.gab.model.GABQuery;
45 import org.onap.sdc.gab.model.GABQuery.GABQueryType;
46 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
48 import org.openecomp.sdc.be.components.impl.GenericArtifactBrowserBusinessLogic;
49 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.info.GenericArtifactQueryInfo;
52 import org.openecomp.sdc.be.user.UserBusinessLogic;
53 import org.openecomp.sdc.common.log.wrappers.Logger;
54 import org.openecomp.sdc.exception.ResponseFormat;
55 import org.owasp.esapi.ESAPI;
56 import org.springframework.stereotype.Controller;
58 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
59 @Path("/v1/catalog/gab")
60 @Consumes(MediaType.APPLICATION_JSON)
61 @Produces(MediaType.APPLICATION_JSON)
62 @Api(value = "Generic Artifact Browser")
64 public class GenericArtifactBrowserServlet extends BeGenericServlet {
66 private static final Logger LOGGER = Logger.getLogger(GenericArtifactBrowserServlet.class);
67 private final GenericArtifactBrowserBusinessLogic gabLogic;
68 private final ArtifactsBusinessLogic artifactsBusinessLogic;
71 public GenericArtifactBrowserServlet(UserBusinessLogic userBusinessLogic,
72 ComponentsUtils componentsUtils,
73 ArtifactsBusinessLogic artifactsBusinessLogic,
74 GenericArtifactBrowserBusinessLogic gabLogic) {
75 super(userBusinessLogic, componentsUtils);
76 this.artifactsBusinessLogic = artifactsBusinessLogic;
77 this.gabLogic = gabLogic;
82 @ApiOperation(value = "Search json paths inside the yaml", httpMethod = "POST", notes = "Returns found entries of json paths", response = Response.class)
83 @ApiResponses(value = {
84 @ApiResponse(code = 200, message = "Returned yaml entries"),
85 @ApiResponse(code = 400, message = "Invalid content / Missing content")})
86 public Response searchFor(
87 @ApiParam(value = "Generic Artifact search model", required = true) GenericArtifactQueryInfo query,
88 @Context final HttpServletRequest request) {
90 Either<ImmutablePair<String, byte[]>, ResponseFormat> immutablePairResponseFormatEither = artifactsBusinessLogic
91 .downloadArtifact(ESAPI.encoder().canonicalize(query.getParentId()), ESAPI.encoder().canonicalize(query.getArtifactUniqueId()));
92 if (immutablePairResponseFormatEither.isLeft()){
93 GABQuery gabQuery = prepareGabQuery(query, immutablePairResponseFormatEither);
94 return buildOkResponse(gabLogic.searchFor(gabQuery));
96 throw new IOException(immutablePairResponseFormatEither.right().value().getFormattedMessage());
98 } catch (IOException e) {
99 LOGGER.error("Cannot search for a given queries in the yaml file", e);
100 return buildGeneralErrorResponse();
104 private GABQuery prepareGabQuery(GenericArtifactQueryInfo query,
105 Either<ImmutablePair<String, byte[]>, ResponseFormat> immutablePairResponseFormatEither) {
106 byte[] content = immutablePairResponseFormatEither.left().value().getRight();
107 Set<String> queryFields = query.getFields().stream().map(ESAPI.encoder()::canonicalize).collect(Collectors.toSet());
108 return new GABQuery(queryFields, new String(content), GABQueryType.CONTENT);