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 private static final Logger LOGGER = LoggerFactory.getLogger(RestfulUtil.class);
68 private static final Restful REST_CLIENT = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
70 private RestfulUtil() {
74 * Get response object.<br>
81 public static JSONObject getResponseObj(String url, String type) {
82 return getResponseObj(url, new RestfulParametes(), type);
86 * Get response object.<br>
94 public static JSONObject getResponseObj(String url, RestfulParametes parametes, String type) {
96 String content = RestfulUtil.getResponseContent(url, parametes, null, type);
97 LOGGER.error("function=getResponseObj, content : {}", content);
98 if(StringUtils.isEmpty(content)) {
101 return JSONObject.fromObject(content);
102 } catch(JSONException e) {
103 LOGGER.error("function=getResponseObj, exception : {}", e);
109 * Get response content.<br>
112 * @param restParametes
117 public static String getResponseContent(String url, RestfulParametes restParametes, String type) {
118 return getResponseContent(url, restParametes, null, type);
122 * Get response map.<br>
125 * @param restParametes
131 public static Map<String, Object> getResponseMap(String url, RestfulParametes restParametes, RestfulOptions opt,
133 RestfulResponse response = restfulResponse(url, restParametes, opt, type);
134 return getResponseMap(response);
138 * Get response content map.<br>
145 public static Map<String, Object> getResponseContentMap(String url, String type) {
146 RestfulResponse response = restfulResponse(url, new RestfulParametes(), null, type);
147 return getResponseMap(response);
150 private static Map<String, Object> getResponseMap(RestfulResponse response) {
151 Map<String, Object> resMap = new HashMap<>(10);
152 if(null != response) {
153 resMap.put(Constant.RESPONSE_CONTENT, response.getResponseContent());
154 resMap.put(Constant.STATUS_CODE, response.getStatus());
160 * Get response content.<br>
163 * @param restParametes
169 public static String getResponseContent(String url, RestfulParametes restParametes, RestfulOptions opt,
171 String responseContent = null;
172 RestfulResponse rsp = restfulResponse(url, restParametes, opt, type);
174 int httpStatus = rsp.getStatus();
175 LOGGER.warn("function=getResponseContent, get response httpStatusCode : {} ", httpStatus);
176 if(httpStatus < HttpServletResponse.SC_BAD_REQUEST && httpStatus > 0) {
177 responseContent = rsp.getResponseContent();
178 LOGGER.warn("function=getResponseContent, get response data success!responseContent={}",
182 return responseContent;
186 * Get restful response.<br>
189 * @param restParametes
194 public static RestfulResponse getRestfulResponse(String url, RestfulParametes restParametes, String type) {
195 return restfulResponse(url, restParametes, null, type);
198 private static RestfulResponse restfulResponse(String url, RestfulParametes restParametes, RestfulOptions opt,
200 RestfulResponse rsp = new RestfulResponse();
203 if(REST_CLIENT != null) {
204 if(TYPE_GET.equals(type)) {
205 rsp = REST_CLIENT.get(url, restParametes, opt);
206 } else if(TYPE_POST.equals(type)) {
207 rsp = REST_CLIENT.post(url, restParametes, opt);
208 } else if(TYPE_PUT.equals(type)) {
209 rsp = REST_CLIENT.put(url, restParametes, opt);
210 } else if(TYPE_DEL.equals(type)) {
211 rsp = REST_CLIENT.delete(url, restParametes, opt);
214 } catch(ServiceException e) {
215 LOGGER.error("function=restfulResponse, get restful response catch exception {} ", e);
217 LOGGER.warn("function=restfulResponse, response status is {} ", rsp.getStatus());
222 * encapsulate the java reflect exception.<br>
224 * @param methodName, Restful's method.
225 * @param objects, method param array.
229 public static RestfulResponse getRestRes(String methodName, Object... objects) {
231 if(objects == null || REST_CLIENT == null) {
235 Class<?>[] classes = new Class[objects.length];
236 for(int i = 0; i < objects.length; i++) {
237 classes[i] = objects[i].getClass();
239 if(methodName.startsWith("async")) {
240 classes[classes.length - 1] = RestfulAsyncCallback.class;
243 Class<?> rtType = methodName.startsWith("async") ? void.class : RestfulResponse.class;
244 MethodType mt = MethodType.methodType(rtType, classes);
245 Object result = MethodHandles.lookup().findVirtual(REST_CLIENT.getClass(), methodName, mt)
246 .bindTo(REST_CLIENT).invokeWithArguments(objects);
248 return (RestfulResponse)result;
250 LOGGER.warn("function=getRestRes, msg: invoke Restful async {} method which return type is Void.",
253 } catch(ReflectiveOperationException e) {
254 LOGGER.error("function=getRestRes, msg=error occurs, e={}.", e);
255 } catch(Throwable e) {// NOSONAR
256 LOGGER.error("function=getRestRes, msg=Throwable, e={}.", e);
258 throw (ServiceException)new ServiceException().initCause(e.getCause());
259 } catch(ServiceException se) {
260 LOGGER.error("function=getRestRes, msg=ServiceException occurs, e={}.", se);
269 * @param restParametes
272 * @throws ServiceException
275 public static JSONArray getResponseRes(RestfulParametes restParametes, String url) throws ServiceException {
276 String result = getResponseContent(url, restParametes, RestfulUtil.TYPE_GET);
277 if(null == result || result.isEmpty()) {
278 LOGGER.error("result from url:" + url + " result:" + result);
279 throw new ServiceException(
280 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result"));
283 JSONArray rsArray = null;
285 JSONObject rsJson = JSONObject.fromObject(result);
286 rsArray = rsJson.getJSONArray(ParamConstant.PARAM_DATA);
287 } catch(JSONException e) {
288 LOGGER.error("getResources error:" + e);
289 throw new ServiceException(
290 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result"));
298 * @param restParametes
302 * @throws ServiceException
305 public static JSONArray getResponseRes(RestfulParametes restParametes, String url, String iResName)
306 throws ServiceException {
307 String result = getResponseContent(url, restParametes, RestfulUtil.TYPE_GET);
308 if(null == result || result.isEmpty()) {
309 LOGGER.error("result from url:" + url + " result:" + result);
310 throw new ServiceException(
311 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result"));
314 JSONArray rsArray = null;
316 JSONObject rsJson = JSONObject.fromObject(result);
317 rsArray = rsJson.getJSONArray(iResName);
318 String vimId = rsJson.getString(ParamConstant.PARAM_VIMID);
319 String vimName = rsJson.getString(ParamConstant.PARAM_VIMNAME);
320 for(int i = 0; i < rsArray.size(); i++) {
321 JSONObject jsonObj = rsArray.getJSONObject(i);
322 jsonObj.put(ParamConstant.PARAM_VIMID, vimId);
323 jsonObj.put(ParamConstant.PARAM_VIMNAME, vimName);
325 } catch(JSONException e) {
326 LOGGER.error("getResources error:" + e);
327 throw new ServiceException(
328 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.result"));
341 public static RestfulResponse getRemoteResponse(Map<String, String> paramsMap, String params) {
342 String url = paramsMap.get("url");
343 String methodType = paramsMap.get("methodType");
345 RestfulResponse rsp = null;
346 Restful rest = RestfulFactory.getRestInstance(RestfulFactory.PROTO_HTTP);
349 RestfulParametes restfulParametes = new RestfulParametes();
350 Map<String, String> headerMap = new HashMap<>(3);
351 headerMap.put(CONTENT_TYPE, APPLICATION);
352 restfulParametes.setHeaderMap(headerMap);
353 restfulParametes.setRawData(params);
356 if(TYPE_GET.equalsIgnoreCase(methodType)) {
357 rsp = rest.get(url, restfulParametes);
358 } else if(TYPE_POST.equalsIgnoreCase(methodType)) {
359 rsp = rest.post(url, restfulParametes);
360 } else if(TYPE_PUT.equalsIgnoreCase(methodType)) {
361 rsp = rest.put(url, restfulParametes);
362 } else if(TYPE_DEL.equalsIgnoreCase(methodType)) {
363 rsp = rest.delete(url, restfulParametes);
366 } catch(ServiceException e) {
367 LOGGER.error("function=getRemoteResponse, get restful response catch exception {}", e);