blob: f037ceee2aabb990423a832b0c72574e89c61f73 [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
slowr577f3222017-08-28 10:49:08 -070026/**
27 * Various types of responses.
28 */
29public final class ResponseHelper {
slowr60d4d102017-08-16 18:33:58 -070030
slowr577f3222017-08-28 10:49:08 -070031 private ResponseHelper() {
32 }
33
34 public static Response getResponse(ObjectMapper mapper, StatusCode status, String title, String detail) {
slowr60d4d102017-08-16 18:33:58 -070035 ObjectNode rootNode = mapper.createObjectNode();
36
37 switch (status) {
slowrc86750e2017-08-22 17:26:47 -070038 case OK: {
39 ArrayNode data = rootNode.putArray("data");
40 ObjectNode addObject = data.addObject();
41 addObject.put("status", status.status);
42 addObject.put("title", title);
43 addObject.put("detail", detail);
44 return Response.status(status.status)
45 .entity(rootNode.toString())
46 .build();
47 }
slowr60d4d102017-08-16 18:33:58 -070048 case BAD_REQUEST:
49 case NOT_IMPLEMENTED:
50 case REQUEST_TIMEOUT:
51 case INTERNAL_SERVER_ERROR:
52 case NOT_FOUND: {
53 ArrayNode errors = rootNode.putArray("errors");
54 ObjectNode addObject = errors.addObject();
55 addObject.put("status", status.status);
56 addObject.put("title", title);
57 addObject.put("detail", detail);
58 return Response.status(status.status)
59 .entity(rootNode.toString())
60 .build();
61 }
62 default:
63 return Response.noContent().build();
64 }
65 }
66
slowr577f3222017-08-28 10:49:08 -070067 public static Response getResponse(ObjectMapper mapper, StatusCode status, JsonNode node) {
slowr60d4d102017-08-16 18:33:58 -070068 ObjectNode rootNode = mapper.createObjectNode();
69
70 switch (status) {
71 case OK:
72 case BAD_REQUEST:
73 case NOT_IMPLEMENTED:
74 case REQUEST_TIMEOUT:
75 case INTERNAL_SERVER_ERROR:
76 case NOT_FOUND: {
77 ArrayNode data = rootNode.putArray("data");
78 data.add(node);
79 return Response.status(status.status)
80 .entity(rootNode.toString())
81 .build();
82 }
83 default:
84 return Response.noContent().build();
85 }
86 }
87
slowr577f3222017-08-28 10:49:08 -070088 public enum StatusCode {
slowr60d4d102017-08-16 18:33:58 -070089 OK(200),
90 BAD_REQUEST(400),
91 NOT_FOUND(404),
92 REQUEST_TIMEOUT(408),
93 INTERNAL_SERVER_ERROR(500),
94 NOT_IMPLEMENTED(501);
95
96 public int status;
97
slowr577f3222017-08-28 10:49:08 -070098 StatusCode(int status) {
slowr60d4d102017-08-16 18:33:58 -070099 this.status = status;
100 }
101 }
102}