Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / utils / QueryParamInjector.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.serialization.queryformats.utils;
21
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.util.Set;
25
26 import javax.ws.rs.core.MultivaluedMap;
27
28 import org.onap.aai.serialization.queryformats.exceptions.QueryParamInjectionException;
29 import org.onap.aai.serialization.queryformats.params.Inject;
30 import org.onap.aai.serialization.queryformats.params.Setter;
31 import org.reflections.Reflections;
32
33 public class QueryParamInjector {
34
35         private final Set<Class<?>> results;
36         
37         
38         private QueryParamInjector () {
39                 Reflections reflections = new Reflections("org.onap.aai.serialization.queryformats.params");
40                 results = reflections.getTypesAnnotatedWith(Inject.class);
41         }
42         
43         private static class Helper {
44                 private static final QueryParamInjector INSTANCE = new QueryParamInjector();
45         }
46         
47         public static QueryParamInjector getInstance() {
48                 return Helper.INSTANCE;
49         }
50         
51         public <T> T injectParams(T obj, MultivaluedMap<String, String> params) throws QueryParamInjectionException{
52                 try {
53                         for (Class<?> item : results) {
54                                 if (item.isAssignableFrom(obj.getClass())) {
55                                         String name = item.getAnnotation(Inject.class).name();
56                                         
57                                         if (params.containsKey(name)) {
58                                                 String value = params.getFirst(name);
59                                                 
60                                                 for (Method method : item.getMethods()) {
61                                                         if (method.isAnnotationPresent(Setter.class)) {
62                                                                 Class<?>[] args = method.getParameterTypes();
63                                                                 if (args.length == 1) {
64                                                                         Object o = args[0].getConstructor(String.class).newInstance(value);
65                                                                         method.invoke(obj, o);
66                                                                 } else {
67                                                                         method.invoke(obj);
68                                                                 }
69                                                         }
70                                                 }
71                                         }
72                                 }
73                         }
74         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
75                                                                                         | InvocationTargetException | NoSuchMethodException
76                                                                                         | SecurityException e) {
77                 throw new QueryParamInjectionException("issue with query params", e);
78         }
79                 
80                 
81                 return obj;
82         }
83 }