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