update the pom version for the security issue fix
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / VnfmJsonUtil.java
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.vnfm.svnfm.vnfmadapter.common;
18 import java.io.IOException;
19 import java.io.InputStream;
20
21 import javax.servlet.http.HttpServletRequest;
22
23 import org.apache.commons.io.IOUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
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;
31
32 /**
33  * VNFM JSON utils.</br>
34  *
35  * @author
36  * @version     VFC 1.0  Sep 10, 2016
37  */
38 public final class VnfmJsonUtil {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(VnfmJsonUtil.class);
41
42     private VnfmJsonUtil(){
43         //private constructor
44     }
45
46     /**
47      * Get the JSON string from input http context.
48      * <br/>
49      *
50      * @param vnfReq HttpServletRequest
51      * @return
52      * @since  VFC 1.0
53      */
54     @SuppressWarnings("unchecked")
55     public static <T> T getJsonFromContexts(HttpServletRequest vnfReq) {
56         try {
57             InputStream vnfInput = vnfReq.getInputStream();
58             String vnfJsonStr = IOUtils.toString(vnfInput);
59             JSONTokener vnfJsonTokener = new JSONTokener(vnfJsonStr);
60
61             if(vnfJsonTokener.nextClean() == Character.codePointAt("{", 0)) {
62                 return (T)JSONObject.fromObject(vnfJsonStr);
63             }
64
65             vnfJsonTokener.back();
66
67             if(vnfJsonTokener.nextClean() == Character.codePointAt("[", 0)) {
68                 return (T)JSONArray.fromObject(vnfJsonStr);
69             }
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);
74         }
75
76         return null;
77     }
78 }