blob: 5e2f1dd995083ee84f720d9e9372cae4f7e44d4c [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) {
slowrc86750e2017-08-22 17:26:47 -070032 case OK: {
33 ArrayNode data = rootNode.putArray("data");
34 ObjectNode addObject = data.addObject();
35 addObject.put("status", status.status);
36 addObject.put("title", title);
37 addObject.put("detail", detail);
38 return Response.status(status.status)
39 .entity(rootNode.toString())
40 .build();
41 }
slowr60d4d102017-08-16 18:33:58 -070042 case BAD_REQUEST:
43 case NOT_IMPLEMENTED:
44 case REQUEST_TIMEOUT:
45 case INTERNAL_SERVER_ERROR:
46 case NOT_FOUND: {
47 ArrayNode errors = rootNode.putArray("errors");
48 ObjectNode addObject = errors.addObject();
49 addObject.put("status", status.status);
50 addObject.put("title", title);
51 addObject.put("detail", detail);
52 return Response.status(status.status)
53 .entity(rootNode.toString())
54 .build();
55 }
56 default:
57 return Response.noContent().build();
58 }
59 }
60
61 public static Response getResponse(ObjectMapper mapper, statusCode status, JsonNode node) {
62 ObjectNode rootNode = mapper.createObjectNode();
63
64 switch (status) {
65 case OK:
66 case BAD_REQUEST:
67 case NOT_IMPLEMENTED:
68 case REQUEST_TIMEOUT:
69 case INTERNAL_SERVER_ERROR:
70 case NOT_FOUND: {
71 ArrayNode data = rootNode.putArray("data");
72 data.add(node);
73 return Response.status(status.status)
74 .entity(rootNode.toString())
75 .build();
76 }
77 default:
78 return Response.noContent().build();
79 }
80 }
81
82 public enum statusCode {
83 OK(200),
84 BAD_REQUEST(400),
85 NOT_FOUND(404),
86 REQUEST_TIMEOUT(408),
87 INTERNAL_SERVER_ERROR(500),
88 NOT_IMPLEMENTED(501);
89
90 public int status;
91
92 statusCode(int status) {
93 this.status = status;
94 }
95 }
96}