8ae59b118e0a8a5c3e343dc17560c4cf4aed8264
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 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
17 package org.onap.vfc.nfvo.resmanagement.common.util.request;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.Enumeration;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import javax.servlet.http.HttpServletRequest;
26
27 import org.apache.commons.io.IOUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
33
34 /**
35  * <br/>
36  * <p>
37  * Utility used for request
38  * </p>
39  *
40  * @author
41  * @version NFVO 0.5 2016-3-17
42  */
43 public final class RequestUtil {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(RequestUtil.class);
46
47     /**
48      * Constructor<br/>
49      * <p>
50      * </p>
51      *
52      * @since NFVO 0.5
53      */
54     private RequestUtil() {
55     }
56
57     /**
58      * Get context string from http context
59      *
60      * @param context
61      *         http context
62      * @return the needed string in http context
63      */
64     public static String getStringRequestBody(HttpServletRequest context) {
65         try {
66             InputStream input = context.getInputStream();
67             return IOUtils.toString(input);
68         } catch(IOException e) {
69             LOGGER.error("function=getStringRequestBody, get httpservletrequest body exception: {}", e);
70             return null;
71         }
72     }
73
74     /**
75      * Get json parameter from http context
76      *
77      * @param context
78      *         http context
79      * @return JSONObject
80      */
81     public static JSONObject getJsonRequestBody(HttpServletRequest context) {
82         try {
83             String bodyStr = getStringRequestBody(context);
84             return JSONObject.fromObject(bodyStr);
85         } catch(JSONException e) {
86             LOGGER.error("function=getJsonRequestBody, maybe request param is not a jsonobject exception: {}", e);
87             return null;
88         }
89     }
90
91     /**
92      * Get the body of all request in json format<br/>
93      *
94      * @param context
95      *         The http context
96      * @return JSONObject The body of all request in json format
97      * @since NFVO 0.5
98      */
99     public static JSONObject getAllJsonRequestBody(HttpServletRequest context) {
100         JSONObject requestBody = getJsonRequestBody(context);
101         if(null == requestBody) {
102             LOGGER.error("get httpservletrequest body exception");
103             requestBody = new JSONObject();
104         }
105         LOGGER.warn("function=getAllJsonRequestBody; msg=get request data is:[{}]", requestBody.toString());
106         requestBody.put("header", getContextHeader(context));
107         return requestBody;
108     }
109
110     /**
111      * Get the context header<br/>
112      *
113      * @param context
114      *         The http context
115      * @return Map context header
116      * @since NFVO 0.5
117      */
118     @SuppressWarnings("unchecked")
119     private static Map<String, String> getContextHeader(HttpServletRequest context) {
120         Map<String, String> header = new HashMap<String, String>();
121         Enumeration<String> headerNames = context.getHeaderNames();
122         while(headerNames.hasMoreElements()) {
123             String headerName = headerNames.nextElement();
124             String value = context.getHeader(headerName);
125             header.put(headerName, value);
126         }
127         return header;
128     }
129 }