b769f0a7ab6293f896893fd3f72bff0ca899fb5d
[vfc/nfvo/wfengine.git] / CommonLibrary / rest-client / src / main / java / org / openo / baseservice / roa / util / clientsdk / RestClientUtil.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.baseservice.roa.util.clientsdk;
17
18 import org.openo.baseservice.remoteservice.exception.ServiceException;
19 import org.openo.baseservice.roa.util.restclient.Restful;
20 import org.openo.baseservice.roa.util.restclient.RestfulAsyncCallback;
21 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
22 import org.openo.baseservice.roa.util.restclient.RestfulResponse;
23
24 import java.util.HashSet;
25 import java.util.Set;
26
27 /**
28  * Rest Client Tools.
29  * <br/>
30  * <p>
31  * </p>
32  * 
33  * @author
34  * @version SDNO 0.5 28-May-2016
35  */
36 public class RestClientUtil {
37
38     private static Set<Class<?>> ret = new HashSet<>();
39
40     static {
41         ret.add(Boolean.class);
42         ret.add(Character.class);
43         ret.add(Byte.class);
44         ret.add(Short.class);
45         ret.add(Integer.class);
46         ret.add(Long.class);
47         ret.add(Float.class);
48         ret.add(Double.class);
49         ret.add(Void.class);
50         ret.add(String.class);
51     }
52
53     private RestClientUtil() {
54
55     }
56
57     /**
58      * Processing HTTP requests.
59      * <br/>
60      * 
61      * @param method method name.
62      * @param path request path.
63      * @param parameters parameters.
64      * @param restFull ReST request instance
65      * @return The ReST response.
66      * @throws ServiceException Exception information.
67      * @since SDNO 0.5
68      */
69     public static RestfulResponse invokeMethod(final String method, final String path,
70             final RestfulParametes parameters, final Restful restFull) throws ServiceException {
71         RestfulResponse response;
72         if("get".equalsIgnoreCase(method)) {
73             response = restFull.get(path, parameters);
74         } else if("put".equalsIgnoreCase(method)) {
75             response = restFull.put(path, parameters);
76         } else if("post".equalsIgnoreCase(method)) {
77             response = restFull.post(path, parameters);
78         } else if("delete".equalsIgnoreCase(method)) {
79             response = restFull.delete(path, parameters);
80         } else if("patch".equalsIgnoreCase(method)) {
81             response = restFull.patch(path, parameters);
82         } else {
83             throw new ServiceException("NotSuppertMethod", 400);
84         }
85         return response;
86     }
87
88     /**
89      * An asynchronous HTTP request.
90      * <br/>
91      * 
92      * @param method http method.
93      * @param path request path.
94      * @param parameters request parameters.
95      * @param restFull restFull instance.
96      * @param callback callback function.
97      * @throws ServiceException in case error.
98      * @since SDNO 0.5
99      */
100     public static void invokeAsyncMethod(final String method, final String path, final RestfulParametes parameters,
101             final Restful restFull, final RestfulAsyncCallback callback) throws ServiceException {
102         if("get".equalsIgnoreCase(method)) {
103             restFull.asyncGet(path, parameters, callback);
104         } else if("put".equalsIgnoreCase(method)) {
105             restFull.asyncPut(path, parameters, callback);
106         } else if("post".equalsIgnoreCase(method)) {
107             restFull.asyncPost(path, parameters, callback);
108         } else if("delete".equalsIgnoreCase(method)) {
109             restFull.asyncDelete(path, parameters, callback);
110         } else if("patch".equalsIgnoreCase(method)) {
111             restFull.asyncPatch(path, parameters, callback);
112         } else {
113             throw new ServiceException("NotSuppertMethod", 400);
114         }
115     }
116
117     /**
118      * Determine whether a class is a native.<br/>
119      * 
120      * @param clazz: class type.
121      * @return whether primitive or not.
122      * @since SDNO 0.5
123      */
124     public static boolean isPrimitive(final Class<?> clazz) {
125         if(clazz.isPrimitive()) {
126             return true;
127         }
128         if(ret.contains(clazz)) {
129             return true;
130         }
131         return false;
132     }
133 }