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.components.impl;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonSerializationContext;
29 import com.google.gson.JsonSerializer;
30 import java.io.IOException;
31 import java.lang.reflect.Type;
32 import org.onap.sdc.gab.GABService;
33 import org.onap.sdc.gab.GABServiceImpl;
34 import org.onap.sdc.gab.model.GABQuery;
35 import org.onap.sdc.gab.model.GABResult;
36 import org.onap.sdc.gab.model.GABResults;
38 @org.springframework.stereotype.Component
39 public class GenericArtifactBrowserBusinessLogic extends BaseBusinessLogic {
41 private GABService gabService;
43 public GenericArtifactBrowserBusinessLogic() {
44 gabService = new GABServiceImpl();
47 public String searchFor(GABQuery gabQuery) throws IOException {
48 GABResults gabResults = gabService.searchFor(gabQuery);
49 return createGsonForGABResult().toJson(gabResults);
52 private Gson createGsonForGABResult(){
53 return new GsonBuilder().setPrettyPrinting()
54 .registerTypeAdapter(GABResult.class, new GABResultSerializer())
55 .registerTypeAdapter(GABResults.class, new GABResultsSerializer())
59 private class GABResultsSerializer implements JsonSerializer<GABResults> {
61 public JsonElement serialize(GABResults gabResults, Type type,
62 JsonSerializationContext jsonSerializationContext) {
63 JsonObject result = new JsonObject();
64 JsonArray jsonArray = new JsonArray();
65 gabResults.getRows().stream().map(jsonSerializationContext::serialize).forEach(jsonArray::add);
66 result.add("data", jsonArray);
71 private class GABResultSerializer implements JsonSerializer<GABResult> {
73 public JsonElement serialize(GABResult gabResult, Type type, JsonSerializationContext jsonSerializationContext) {
74 JsonObject result = new JsonObject();
75 gabResult.getEntries().forEach(entry -> result.addProperty(entry.getPath(), String.valueOf(entry.getData())));