2 * Copyright (c) 2016, Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openo.baseservice.roa.util.clientsdk;
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;
24 import java.util.HashSet;
34 * @version SDNO 0.5 28-May-2016
36 public class RestClientUtil {
38 private static Set<Class<?>> ret = new HashSet<>();
41 ret.add(Boolean.class);
42 ret.add(Character.class);
45 ret.add(Integer.class);
48 ret.add(Double.class);
50 ret.add(String.class);
53 private RestClientUtil() {
58 * Processing HTTP requests.
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.
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);
83 throw new ServiceException("NotSuppertMethod", 400);
89 * An asynchronous HTTP request.
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.
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);
113 throw new ServiceException("NotSuppertMethod", 400);
118 * Determine whether a class is a native.<br/>
120 * @param clazz: class type.
121 * @return whether primitive or not.
124 public static boolean isPrimitive(final Class<?> clazz) {
125 if(clazz.isPrimitive()) {
128 if(ret.contains(clazz)) {