001package jmri.server.json.schema;
002
003import com.fasterxml.jackson.databind.JsonNode;
004import java.io.IOException;
005import javax.servlet.http.HttpServletResponse;
006import jmri.JmriException;
007import jmri.server.json.JSON;
008import jmri.server.json.JsonConnection;
009import jmri.server.json.JsonException;
010import jmri.server.json.JsonRequest;
011import jmri.server.json.JsonSocketService;
012
013/**
014 * JSON Service to provide schema data for the running JSON server.
015 *
016 * @author Randall Wood Copyright 2018
017 */
018public class JsonSchemaSocketService extends JsonSocketService<JsonSchemaHttpService> {
019
020    JsonSchemaSocketService(JsonConnection connection) {
021        super(connection, new JsonSchemaHttpService(connection.getObjectMapper()));
022    }
023
024    @Override
025    public void onMessage(String type, JsonNode data, JsonRequest request) throws IOException, JmriException, JsonException {
026        switch (request.method) {
027            case JSON.DELETE:
028                throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "DeleteNotAllowed", type), request.id);
029            case JSON.POST:
030                throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "PostNotAllowed", type), request.id);
031            case JSON.PUT:
032                throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "PutNotAllowed", type), request.id);
033            case JSON.GET:
034                connection.sendMessage(service.doGet(type, data.path(JSON.NAME).asText(JSON.JSON), data, request), request.id);
035                break;
036            default:
037                throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "MethodNotImplemented", request.method, type), request.id);
038        }
039    }
040
041    @Override
042    public void onList(String type, JsonNode data, JsonRequest request) throws IOException, JmriException, JsonException {
043        connection.sendMessage(service.doGetList(type, data, request), request.id);
044    }
045
046    @Override
047    public void onClose() {
048        // nothing to do
049    }
050
051}