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