2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.serialization.queryformats.utils;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
28 import javax.ws.rs.core.MultivaluedMap;
30 import org.onap.aai.serialization.queryformats.exceptions.QueryParamInjectionException;
31 import org.onap.aai.serialization.queryformats.params.Inject;
32 import org.onap.aai.serialization.queryformats.params.Setter;
33 import org.reflections.Reflections;
35 public class QueryParamInjector {
37 private final Set<Class<?>> results;
40 private QueryParamInjector () {
41 Reflections reflections = new Reflections("org.onap.aai.serialization.queryformats.params");
42 results = reflections.getTypesAnnotatedWith(Inject.class);
45 private static class Helper {
46 private static final QueryParamInjector INSTANCE = new QueryParamInjector();
49 public static QueryParamInjector getInstance() {
50 return Helper.INSTANCE;
53 public <T> T injectParams(T obj, MultivaluedMap<String, String> params) throws QueryParamInjectionException{
55 for (Class<?> item : results) {
56 if (item.isAssignableFrom(obj.getClass())) {
57 String name = item.getAnnotation(Inject.class).name();
59 if (params.containsKey(name)) {
60 String value = params.getFirst(name);
62 for (Method method : item.getMethods()) {
63 if (method.isAnnotationPresent(Setter.class)) {
64 Class<?>[] args = method.getParameterTypes();
65 if (args.length == 1) {
66 Object o = args[0].getConstructor(String.class).newInstance(value);
67 method.invoke(obj, o);
76 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
77 | InvocationTargetException | NoSuchMethodException
78 | SecurityException e) {
79 throw new QueryParamInjectionException("issue with query params", e);