/*
* Copyright 2016 Huawei Technologies Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.vfc.nfvo.multivimproxy.common.util.request;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulClientConst;
import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulParametes;
import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.ServiceException;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
/**
*
*
* Utility used for request *
* * @author * @version VFC 1.0 2016-3-17 */ public final class RequestUtil { private static final Logger LOGGER = LogManager.getLogger(RequestUtil.class); /** * Constructor*
* * @since VFC 1.0 */ private RequestUtil() { } /** * Get context string from http context * * @param context * http context * @return the needed string in http context */ public static String getStringRequestBody(HttpServletRequest context) { try { InputStream input = context.getInputStream(); return IOUtils.toString(input); } catch(IOException e) { LOGGER.error("function=getStringRequestBody, get httpservletrequest body exception: {}", e); return null; } } /** * Get json parameter from http context * * @param context * http context * @return JSONObject */ public static JSONObject getJsonRequestBody(HttpServletRequest context) { try { String bodyStr = getStringRequestBody(context); return JSONObject.fromObject(bodyStr); } catch(JSONException e) { LOGGER.error("function=getJsonRequestBody, maybe request param is not a jsonobject exception: {}", e); return null; } } /** * Get the body of all request in json format