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.vnfm.svnfm.vnfmadapter.common;
18 import java.io.IOException;
19 import java.io.InputStream;
21 import javax.servlet.http.HttpServletRequest;
23 import org.apache.commons.io.IOUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import net.sf.json.JSONArray;
28 import net.sf.json.JSONException;
29 import net.sf.json.JSONObject;
30 import net.sf.json.util.JSONTokener;
33 * VNFM JSON utils.</br>
36 * @version NFVO 0.5 Sep 10, 2016
38 public final class VnfmJsonUtil {
40 private static final Logger LOGGER = LoggerFactory.getLogger(VnfmJsonUtil.class);
42 private VnfmJsonUtil(){
47 * Get the JSON string from input http context.
50 * @param vnfReq HttpServletRequest
54 @SuppressWarnings("unchecked")
55 public static <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
57 InputStream vnfInput = vnfReq.getInputStream();
58 String vnfJsonStr = IOUtils.toString(vnfInput);
59 JSONTokener vnfJsonTokener = new JSONTokener(vnfJsonStr);
61 if(vnfJsonTokener.nextClean() == Character.codePointAt("{", 0)) {
62 return (T)JSONObject.fromObject(vnfJsonStr);
65 vnfJsonTokener.back();
67 if(vnfJsonTokener.nextClean() == Character.codePointAt("[", 0)) {
68 return (T)JSONArray.fromObject(vnfJsonStr);
70 } catch(IOException e) {
71 LOGGER.error("function=getJsonFromContext, msg=IOException occurs, e={}.", e);
72 } catch(JSONException e) {
73 LOGGER.error("function=getJsonFromContext, msg=JSONException occurs, e={}.", e);