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.openo.nfvo.resmanagement.common.util.response;
19 import java.util.HashMap;
20 import java.util.Iterator;
22 import java.util.Map.Entry;
24 import javax.servlet.http.HttpServletRequest;
26 import net.sf.json.JSONObject;
30 * Response utility class.<br>
35 * @version NFVO 0.5 Sep 10, 2016
37 public final class ResponseUtil {
39 private ResponseUtil() {
43 * Roa request common return function, default return code 200 <br/>
46 * The request return code
49 * @return JSONObject The response for http request
52 public static JSONObject genHttpResponse(int retCode, String msg) {
53 return genHttpResponse(null, createCodeMap(-1, retCode), msg, null);
57 * Roa request common return function, default return code 200<br/>
60 * The request return code
64 * Other request info of this request
65 * @return JSONObject The response for http request
68 public static JSONObject genHttpResponse(int retCode, String msg, Map<String, Object> map) {
69 return genHttpResponse(null, createCodeMap(-1, retCode), msg, map);
73 * Roa request common return method <br/>
76 * The http request context
77 * @param httpStatusCode
78 * The http response code
80 * The http request return code
82 * The message of request
83 * @return JSONObject The response for http request
86 public static JSONObject genHttpResponse(HttpServletRequest context, int httpStatusCode, int retCode, String msg) {
87 return genHttpResponse(context, createCodeMap(httpStatusCode, retCode), msg, null);
92 * Roa request common return method.<br>
94 * @param context, The http request context
96 * @param msg, The message of request
97 * @param map, Other message of request
101 public static JSONObject genHttpResponse(HttpServletRequest context, Map<String, Integer> codeMap, String msg,
102 Map<String, Object> map) {
103 JSONObject object = new JSONObject();
105 object.put("msg", msg);
107 Iterator<Entry<String, Object>> ite = map.entrySet().iterator();
109 Map.Entry<String, Object> entry = ite.next();
110 object.put(entry.getKey(), entry.getValue().toString());
117 * Create code map to maintenance the relationship between return code and
118 * http status code <br/>
120 * @param httpStatusCode
121 * The http response code
123 * The http request return code
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);