blob: 826adc145ab592913377e541fbe9e01e9d992867 [file] [log] [blame]
slowr60d4d102017-08-16 18:33:58 -07001/*
2 * Copyright 2015-present Open Networking Foundation
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
17package org.onosproject.xran.rest;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23
24import javax.ws.rs.core.Response;
25
26public class ResponseHelper {
27
28 public static Response getResponse(ObjectMapper mapper, statusCode status, String title, String detail) {
29 ObjectNode rootNode = mapper.createObjectNode();
30
31 switch (status) {
32 case OK:
33 case BAD_REQUEST:
34 case NOT_IMPLEMENTED:
35 case REQUEST_TIMEOUT:
36 case INTERNAL_SERVER_ERROR:
37 case NOT_FOUND: {
38 ArrayNode errors = rootNode.putArray("errors");
39 ObjectNode addObject = errors.addObject();
40 addObject.put("status", status.status);
41 addObject.put("title", title);
42 addObject.put("detail", detail);
43 return Response.status(status.status)
44 .entity(rootNode.toString())
45 .build();
46 }
47 default:
48 return Response.noContent().build();
49 }
50 }
51
52 public static Response getResponse(ObjectMapper mapper, statusCode status, JsonNode node) {
53 ObjectNode rootNode = mapper.createObjectNode();
54
55 switch (status) {
56 case OK:
57 case BAD_REQUEST:
58 case NOT_IMPLEMENTED:
59 case REQUEST_TIMEOUT:
60 case INTERNAL_SERVER_ERROR:
61 case NOT_FOUND: {
62 ArrayNode data = rootNode.putArray("data");
63 data.add(node);
64 return Response.status(status.status)
65 .entity(rootNode.toString())
66 .build();
67 }
68 default:
69 return Response.noContent().build();
70 }
71 }
72
73 public enum statusCode {
74 OK(200),
75 BAD_REQUEST(400),
76 NOT_FOUND(404),
77 REQUEST_TIMEOUT(408),
78 INTERNAL_SERVER_ERROR(500),
79 NOT_IMPLEMENTED(501);
80
81 public int status;
82
83 statusCode(int status) {
84 this.status = status;
85 }
86 }
87}