2 * Copyright 2016-2017 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.
17 package org.onap.vfc.nfvo.resmanagement.common.util;
19 import java.lang.invoke.MethodHandles;
20 import java.lang.invoke.MethodType;
21 import java.util.HashMap;
24 import javax.servlet.http.HttpServletResponse;
26 import org.apache.commons.lang.StringUtils;
27 import org.onap.vfc.nfvo.resmanagement.common.ResourceUtil;
28 import org.onap.vfc.nfvo.resmanagement.common.constant.Constant;
29 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
30 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.Restful;
31 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulAsyncCallback;
32 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulFactory;
33 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulOptions;
34 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulParametes;
35 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulResponse;
36 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import net.sf.json.JSONArray;
41 import net.sf.json.JSONException;
42 import net.sf.json.JSONObject;
45 * Restful Utility Class.<br>
50 * @version VFC 1.0 Sep 10, 2016
52 public class RestfulUtil {
54 public static final String TYPE_GET = "get";
56 public static final String TYPE_PUT = "put";
58 public static final String TYPE_POST = "post";
60 public static final String TYPE_DEL = "delete";
62 public static final String CONTENT_TYPE = "Content-type";
64 public static final String APPLICATION = "application/json";
66 public static final String NO_RESULT_EXCEPTION =
67 "org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result";
69 private static final Logger LOGGER = LoggerFactory.getLogger(RestfulUtil.class);
71 private static final Restful REST_CLIENT = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
73 private RestfulUtil() {
77 * Get response object.<br>
84 public static JSONObject getResponseObj(String url, String type) {
85 return getResponseObj(url, new RestfulParametes(), type);
89 * Get response object.<br>
97 public static JSONObject getResponseObj(String url, RestfulParametes parametes, String type) {
99 String content = RestfulUtil.getResponseContent(url, parametes, null, type);
100 LOGGER.error("function=getResponseObj, content : {}", content);
101 if(StringUtils.isEmpty(content)) {
104 return JSONObject.fromObject(content);
105 } catch(JSONException e) {
106 LOGGER.error("function=getResponseObj, exception : {}", e);
112 * Get response content.<br>
115 * @param restParametes
120 public static String getResponseContent(String url, RestfulParametes restParametes, String type) {
121 return getResponseContent(url, restParametes, null, type);
125 * Get response map.<br>
128 * @param restParametes
134 public static Map<String, Object> getResponseMap(String url, RestfulParametes restParametes, RestfulOptions opt,
136 RestfulResponse response = restfulResponse(url, restParametes, opt, type);
137 return getResponseMap(response);
141 * Get response content map.<br>
148 public static Map<String, Object> getResponseContentMap(String url, String type) {
149 RestfulResponse response = restfulResponse(url, new RestfulParametes(), null, type);
150 return getResponseMap(response);
153 private static Map<String, Object> getResponseMap(RestfulResponse response) {
154 Map<String, Object> resMap = new HashMap<>(10);
155 if(null != response) {
156 resMap.put(Constant.RESPONSE_CONTENT, response.getResponseContent());
157 resMap.put(Constant.STATUS_CODE, response.getStatus());
163 * Get response content.<br>
166 * @param restParametes
172 public static String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
174 String responseContent = null;
175 RestfulResponse rsp = restfulResponse(url, restParametes, opt, type);
177 int httpStatus = rsp.getStatus();
178 LOGGER.warn("function=getResponseContent, get response httpStatusCode : {} ", httpStatus);
179 if(httpStatus < HttpServletResponse.SC_BAD_REQUEST && httpStatus > 0) {
180 responseContent = rsp.getResponseContent();
181 LOGGER.warn("function=getResponseContent, get response data success!responseContent={}",
185 return responseContent;
189 * Get restful response.<br>
192 * @param restParametes
197 public static RestfulResponse getRestfulResponse(String url, RestfulParametes restParametes, String type) {
198 return restfulResponse(url, restParametes, null, type);
201 private static RestfulResponse restfulResponse(String url, RestfulParametes restParametes, RestfulOptions opt,
203 RestfulResponse rsp = new RestfulResponse();
206 if(REST_CLIENT != null) {
207 if(TYPE_GET.equals(type)) {
208 rsp = REST_CLIENT.get(url, restParametes, opt);
209 } else if(TYPE_POST.equals(type)) {
210 rsp = REST_CLIENT.post(url, restParametes, opt);
211 } else if(TYPE_PUT.equals(type)) {
212 rsp = REST_CLIENT.put(url, restParametes, opt);
213 } else if(TYPE_DEL.equals(type)) {
214 rsp = REST_CLIENT.delete(url, restParametes, opt);
217 } catch(ServiceException e) {
218 LOGGER.error("function=restfulResponse, get restful response catch exception {} ", e);
220 LOGGER.warn("function=restfulResponse, response status is {} ", rsp.getStatus());
225 * encapsulate the java reflect exception.<br>
227 * @param methodName, Restful's method.
228 * @param objects, method param array.
232 public static RestfulResponse getRestRes(String methodName, Object... objects) {
234 if(objects == null || REST_CLIENT == null) {
238 Class<?>[] classes = new Class[objects.length];
239 for(int i = 0; i < objects.length; i++) {
240 classes[i] = objects[i].getClass();
242 if(methodName.startsWith("async")) {
243 classes[classes.length - 1] = RestfulAsyncCallback.class;
246 Class<?> rtType = methodName.startsWith("async") ? void.class : RestfulResponse.class;
247 MethodType mt = MethodType.methodType(rtType, classes);
248 Object result = MethodHandles.lookup().findVirtual(REST_CLIENT.getClass(), methodName, mt)
249 .bindTo(REST_CLIENT).invokeWithArguments(objects);
251 return (RestfulResponse)result;
253 LOGGER.warn("function=getRestRes, msg: invoke Restful async {} method which return type is Void.",
256 } catch(ReflectiveOperationException e) {
257 LOGGER.error("function=getRestRes, msg=error occurs, e={}.", e);
258 } catch(Throwable e) {// NOSONAR
259 LOGGER.error("function=getRestRes, msg=Throwable, e={}.", e);
261 throw (ServiceException)new ServiceException().initCause(e.getCause());
262 } catch(ServiceException se) {
263 LOGGER.error("function=getRestRes, msg=ServiceException occurs, e={}.", se);
272 * @param restParametes
275 * @throws ServiceException
278 public static JSONArray getResponseRes(RestfulParametes restParametes, String url) throws ServiceException {
279 String result = getResponseContent(url, restParametes, RestfulUtil.TYPE_GET);
280 if(null == result || result.isEmpty()) {
281 LOGGER.error("result from url:" + url + " result:" + result);
282 throw new ServiceException(
283 ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
286 JSONArray rsArray = null;
288 JSONObject rsJson = JSONObject.fromObject(result);
289 rsArray = rsJson.getJSONArray(ParamConstant.PARAM_DATA);
290 } catch(JSONException e) {
291 LOGGER.error("getResources error:" + e);
292 throw new ServiceException(
293 ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
301 * @param restParametes
305 * @throws ServiceException
308 public static JSONArray getResponseRes(RestfulParametes restParametes, String url, String iResName)
309 throws ServiceException {
310 String result = getResponseContent(url, restParametes, RestfulUtil.TYPE_GET);
311 if(null == result || result.isEmpty()) {
312 LOGGER.error("result from url:" + url + " result:" + result);
313 throw new ServiceException(
314 ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
317 JSONArray rsArray = null;
319 JSONObject rsJson = JSONObject.fromObject(result);
320 rsArray = rsJson.getJSONArray(iResName);
321 String vimId = rsJson.getString(ParamConstant.PARAM_VIMID);
322 String vimName = rsJson.getString(ParamConstant.PARAM_VIMNAME);
323 for(int i = 0; i < rsArray.size(); i++) {
324 JSONObject jsonObj = rsArray.getJSONObject(i);
325 jsonObj.put(ParamConstant.PARAM_VIMID, vimId);
326 jsonObj.put(ParamConstant.PARAM_VIMNAME, vimName);
328 } catch(JSONException e) {
329 LOGGER.error("getResources error:" + e);
330 throw new ServiceException(
331 ResourceUtil.getMessage(NO_RESULT_EXCEPTION));
344 public static RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
345 String url = paramsMap.get("url");
346 String methodType = paramsMap.get("methodType");
348 RestfulResponse rsp = null;
349 Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
352 RestfulParametes restfulParametes = new RestfulParametes();
353 Map<String, String> headerMap = new HashMap<>(3);
354 headerMap.put(CONTENT_TYPE, APPLICATION);
355 restfulParametes.setHeaderMap(headerMap);
356 restfulParametes.setRawData(params);
359 if(TYPE_GET.equalsIgnoreCase(methodType)) {
360 rsp = rest.get(url, restfulParametes);
361 } else if(TYPE_POST.equalsIgnoreCase(methodType)) {
362 rsp = rest.post(url, restfulParametes);
363 } else if(TYPE_PUT.equalsIgnoreCase(methodType)) {
364 rsp = rest.put(url, restfulParametes);
365 } else if(TYPE_DEL.equalsIgnoreCase(methodType)) {
366 rsp = rest.delete(url, restfulParametes);
369 } catch(ServiceException e) {
370 LOGGER.error("function=getRemoteResponse, get restful response catch exception {}", e);