001package jmri.server.json.message;
002
003import com.fasterxml.jackson.databind.JsonNode;
004import com.fasterxml.jackson.databind.ObjectMapper;
005import com.fasterxml.jackson.databind.node.ArrayNode;
006import java.io.IOException;
007import javax.servlet.http.HttpServletResponse;
008import jmri.server.json.JSON;
009import jmri.server.json.JsonException;
010import jmri.server.json.JsonHttpService;
011import jmri.server.json.JsonRequest;
012
013/**
014 *
015 * @author Randall Wood Copyright 2018
016 */
017public class JsonMessageHttpService extends JsonHttpService {
018
019    public JsonMessageHttpService(ObjectMapper mapper) {
020        super(mapper);
021    }
022
023    @Override
024    public JsonNode doGet(String type, String name, JsonNode data, JsonRequest request) throws JsonException {
025        throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "GetNotAllowed", type), request.id);
026    }
027
028    @Override
029    public JsonNode doPost(String type, String name, JsonNode data, JsonRequest request) throws JsonException {
030        throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "PostNotAllowed", type), request.id);
031    }
032
033    @Override
034    public ArrayNode doGetList(String type, JsonNode data, JsonRequest request) throws JsonException {
035        throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, Bundle.getMessage(request.locale, "UnlistableService", type), request.id);
036    }
037
038    @Override
039    public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException {
040        switch (type) {
041            case JsonMessage.CLIENT:
042                return doSchema(type,
043                        server,
044                        "jmri/server/json/message/client-server.json",
045                        "jmri/server/json/message/client-client.json",
046                        request.id);
047            case JsonMessage.MESSAGE:
048                if (server) {
049                    try {
050                        return doSchema(type, server,
051                                this.mapper.readTree(this.getClass().getClassLoader().getResource("jmri/server/json/message/message-server.json")), request.id);
052                    } catch (IOException ex) {
053                        throw new JsonException(500, ex, request.id);
054                    }
055                } else {
056                    throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, Bundle.getMessage(request.locale, "NotAClientType", type), request.id);
057                }
058            case JSON.HELLO:
059                return doSchema(type,
060                        server,
061                        "jmri/server/json/util/hello-server.json",
062                        "jmri/server/json/util/hello-client.json",
063                        request.id);
064            default:
065                throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id);
066        }
067    }
068
069}