28ee1b01b7c104d6931c6a18bb64b8ed1aeba9fc
[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.res.common.util.response;
18
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Map.Entry;
23
24 import javax.servlet.http.HttpServletRequest;
25
26 import net.sf.json.JSONObject;
27
28 /**
29  *
30  * Response utility class.<br>
31  * <p>
32  * </p>
33  *
34  * @author
35  * @version     NFVO 0.5  Sep 10, 2016
36  */
37 public final class ResponseUtil {
38
39     private ResponseUtil() {
40     }
41
42     /**
43      * Roa request common return function, default return code 200 <br/>
44      *
45      * @param retCode
46      *         The request return code
47      * @param msg
48      *         The request message
49      * @return JSONObject The response for http request
50      * @since NFVO 0.5
51      */
52     public static JSONObject genHttpResponse(int retCode, String msg) {
53         return genHttpResponse(null, createCodeMap(-1, retCode), msg, null);
54     }
55
56     /**
57      * Roa request common return function, default return code 200<br/>
58      *
59      * @param retCode
60      *         The request return code
61      * @param msg
62      *         The request message
63      * @param map
64      *         Other request info of this request
65      * @return JSONObject The response for http request
66      * @since NFVO 0.5
67      */
68     public static JSONObject genHttpResponse(int retCode, String msg, Map<String, Object> map) {
69         return genHttpResponse(null, createCodeMap(-1, retCode), msg, map);
70     }
71
72     /**
73      * Roa request common return method <br/>
74      *
75      * @param context
76      *         The http request context
77      * @param httpStatusCode
78      *         The http response code
79      * @param retCode
80      *         The http request return code
81      * @param msg
82      *         The message of request
83      * @return JSONObject The response for http request
84      * @since NFVO 0.5
85      */
86     public static JSONObject genHttpResponse(HttpServletRequest context, int httpStatusCode, int retCode, String msg) {
87         return genHttpResponse(context, createCodeMap(httpStatusCode, retCode), msg, null);
88     }
89
90     /**
91      *
92      * Roa request common return method.<br>
93      *
94      * @param context, The http request context
95      * @param codeMap
96      * @param msg, The message of request
97      * @param map, Other message of request
98      * @return
99      * @since  NFVO 0.5
100      */
101     public static JSONObject genHttpResponse(HttpServletRequest context, Map<String, Integer> codeMap, String msg,
102             Map<String, Object> map) {
103         JSONObject object = new JSONObject();
104
105         object.put("msg", msg);
106         if(null != map) {
107             Iterator<Entry<String, Object>> ite = map.entrySet().iterator();
108             if(ite.hasNext()) {
109                 Map.Entry<String, Object> entry = ite.next();
110                 object.put(entry.getKey(), entry.getValue().toString());
111             }
112         }
113         return object;
114     }
115
116     /**
117      * Create code map to maintenance the relationship between return code and
118      * http status code <br/>
119      *
120      * @param httpStatusCode
121      *         The http response code
122      * @param retCode
123      *         The http request return code
124      * @return Map
125      * @since NFVO 0.5
126      */
127     private static Map<String, Integer> createCodeMap(int httpStatusCode, int retCode) {
128         Map<String, Integer> codeMap = new HashMap<>(5);
129         codeMap.put("httpStatusCode", httpStatusCode);
130         codeMap.put("retCode", retCode);
131         return codeMap;
132     }
133 }