2 * Copyright 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.
17 package org.onap.vfc.nfvo.resmanagement.common.util.request;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22 import java.net.URLEncoder;
23 import java.util.Enumeration;
24 import java.util.HashMap;
27 import javax.servlet.http.HttpServletRequest;
29 import org.apache.commons.io.IOUtils;
30 import org.apache.logging.log4j.LogManager;
31 import org.apache.logging.log4j.Logger;
32 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulClientConst;
33 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulParametes;
34 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
36 import net.sf.json.JSONException;
37 import net.sf.json.JSONObject;
42 * Utility used for request
46 * @version VFC 1.0 2016-3-17
48 public final class RequestUtil {
50 private static final Logger LOGGER = LogManager.getLogger(RequestUtil.class);
59 private RequestUtil() {
63 * Get context string from http context
67 * @return the needed string in http context
69 public static String getStringRequestBody(HttpServletRequest context) {
71 InputStream input = context.getInputStream();
72 return IOUtils.toString(input);
73 } catch(IOException e) {
74 LOGGER.error("function=getStringRequestBody, get httpservletrequest body exception: {}", e);
80 * Get json parameter from http context
86 public static JSONObject getJsonRequestBody(HttpServletRequest context) {
88 String bodyStr = getStringRequestBody(context);
89 return JSONObject.fromObject(bodyStr);
90 } catch(JSONException e) {
91 LOGGER.error("function=getJsonRequestBody, maybe request param is not a jsonobject exception: {}", e);
97 * Get the body of all request in json format<br/>
101 * @return JSONObject The body of all request in json format
104 public static JSONObject getAllJsonRequestBody(HttpServletRequest context) {
105 JSONObject requestBody = getJsonRequestBody(context);
106 if(null == requestBody) {
107 LOGGER.error("get httpservletrequest body exception");
108 requestBody = new JSONObject();
110 LOGGER.warn("function=getAllJsonRequestBody; msg=get request data is:[{}]", requestBody.toString());
111 requestBody.put("header", getContextHeader(context));
116 * Get the context header<br/>
120 * @return Map context header
123 @SuppressWarnings("unchecked")
124 private static Map<String, String> getContextHeader(HttpServletRequest context) {
125 Map<String, String> header = new HashMap<>();
126 Enumeration<String> headerNames = context.getHeaderNames();
127 while(headerNames.hasMoreElements()) {
128 String headerName = headerNames.nextElement();
129 String value = context.getHeader(headerName);
130 header.put(headerName, value);
135 public static String encodeParams(final RestfulParametes restParametes) throws ServiceException {
136 final Map<String, String> parm = restParametes.getParamMap();
138 boolean bHasParma = false;
139 final StringBuilder builder = new StringBuilder();
141 for(final String key : parm.keySet()) {
142 value = parm.get(key);
148 str = String.format("&%s=%s", URLEncoder.encode(key, RestfulClientConst.ENCODING),
149 URLEncoder.encode(value, RestfulClientConst.ENCODING));
152 str = String.format("%s=%s", URLEncoder.encode(key, RestfulClientConst.ENCODING),
153 URLEncoder.encode(value, RestfulClientConst.ENCODING));
157 } catch(final UnsupportedEncodingException ex) {
158 LOGGER.error("unsupported encoding: ", ex);
159 throw new ServiceException("Broken VM does not support UTF-8");
161 return builder.toString();
164 public static Map<String, String> getAAIHeaderMap() {
165 HashMap<String, String> headerMap = new HashMap<>();
166 headerMap.put("X-TransactionId", "9999");
167 headerMap.put("X-FromAppId", "jimmy");
168 headerMap.put("Real-Time", "true");
169 headerMap.put("Authorization", "Basic QUFJOkFBSQ==");
170 headerMap.put("Accept", "application/json");
171 headerMap.put("Content-Type", "application/json");