2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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.onap.aai.serialization.queryformats.utils;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
26 import javax.ws.rs.core.MultivaluedMap;
28 import org.onap.aai.serialization.queryformats.exceptions.QueryParamInjectionException;
29 import org.onap.aai.serialization.queryformats.params.*;
31 public class QueryParamInjector {
33 // TODO reimplement this using reflections.
34 private static final Class<?>[] PARAM_CLASSES =
35 new Class[] {AsTree.class, Depth.class, EndTs.class, NodesOnly.class, StartTs.class};
37 private static class Helper {
38 private static final QueryParamInjector INSTANCE = new QueryParamInjector();
41 public static QueryParamInjector getInstance() {
42 return Helper.INSTANCE;
45 public <T> T injectParams(T obj, MultivaluedMap<String, String> params) throws QueryParamInjectionException {
47 for (Class<?> item : PARAM_CLASSES) {
48 if (item.isAssignableFrom(obj.getClass())) {
49 String name = item.getAnnotation(Inject.class).name();
51 if (params.containsKey(name)) {
52 String value = params.getFirst(name);
54 for (Method method : item.getMethods()) {
55 if (method.isAnnotationPresent(Setter.class)) {
56 Class<?>[] args = method.getParameterTypes();
57 if (args.length == 1) {
58 Object o = args[0].getConstructor(String.class).newInstance(value);
59 method.invoke(obj, o);
68 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
69 | NoSuchMethodException | SecurityException e) {
70 throw new QueryParamInjectionException("issue with query params", e);