blob: 0a3abad411b0f4d0aa7fadb50f6f4190dab1f77b [file] [log] [blame]
slowr60d4d102017-08-16 18:33:58 -07001/*
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08002 * Copyright 2017-present Open Networking Foundation
slowr60d4d102017-08-16 18:33:58 -07003 *
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
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080017package org.onosproject.xran.impl.rest;
slowr60d4d102017-08-16 18:33:58 -070018
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
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080034 public static Response getResponse(ObjectMapper mapper, int status, String title, String detail) {
slowr60d4d102017-08-16 18:33:58 -070035 ObjectNode rootNode = mapper.createObjectNode();
36
37 switch (status) {
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080038 case 200: {
slowrc86750e2017-08-22 17:26:47 -070039 ArrayNode data = rootNode.putArray("data");
40 ObjectNode addObject = data.addObject();
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080041 addObject.put("status", status);
slowrc86750e2017-08-22 17:26:47 -070042 addObject.put("title", title);
43 addObject.put("detail", detail);
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080044 return Response.status(status)
slowrc86750e2017-08-22 17:26:47 -070045 .entity(rootNode.toString())
46 .build();
47 }
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080048 case 400:
49 case 501:
50 case 408:
51 case 500:
52 case 404: {
slowr60d4d102017-08-16 18:33:58 -070053 ArrayNode errors = rootNode.putArray("errors");
54 ObjectNode addObject = errors.addObject();
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080055 addObject.put("status", status);
slowr60d4d102017-08-16 18:33:58 -070056 addObject.put("title", title);
57 addObject.put("detail", detail);
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080058 return Response.status(status)
slowr60d4d102017-08-16 18:33:58 -070059 .entity(rootNode.toString())
60 .build();
61 }
62 default:
63 return Response.noContent().build();
64 }
65 }
66
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080067 public static Response getResponse(ObjectMapper mapper, int status, JsonNode node) {
slowr60d4d102017-08-16 18:33:58 -070068 ObjectNode rootNode = mapper.createObjectNode();
69
70 switch (status) {
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080071 case 200:
72 case 400:
73 case 501:
74 case 408:
75 case 500:
76 case 404: {
slowr60d4d102017-08-16 18:33:58 -070077 ArrayNode data = rootNode.putArray("data");
78 data.add(node);
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -080079 return Response.status(status)
slowr60d4d102017-08-16 18:33:58 -070080 .entity(rootNode.toString())
81 .build();
82 }
83 default:
84 return Response.noContent().build();
85 }
86 }
slowr60d4d102017-08-16 18:33:58 -070087}