slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.xran.rest; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 23 | |
| 24 | import javax.ws.rs.core.Response; |
| 25 | |
| 26 | public 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 | } |