Search in sources :

Example 71 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonSignalHeadHttpService method doPost.

@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
    SignalHead signalHead = InstanceManager.getDefault(jmri.SignalHeadManager.class).getSignalHead(name);
    this.postNamedBean(signalHead, data, name, type, locale);
    if (signalHead != null) {
        if (data.path(STATE).isIntegralNumber()) {
            int state = data.path(STATE).asInt();
            if (state == SignalHead.HELD) {
                signalHead.setHeld(true);
            } else {
                boolean isValid = false;
                for (int validState : signalHead.getValidStates()) {
                    if (state == validState) {
                        isValid = true;
                        // TODO: completely insulate JSON state from SignalHead state
                        if (signalHead.getHeld())
                            signalHead.setHeld(false);
                        signalHead.setAppearance(state);
                        break;
                    }
                }
                if (!isValid) {
                    throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", SIGNAL_HEAD, state));
                }
            }
        }
    }
    return this.doGet(type, name, locale);
}
Also used : JsonException(jmri.server.json.JsonException) SignalHeadManager(jmri.SignalHeadManager) SignalHead(jmri.SignalHead)

Example 72 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonSensorHttpService method doPost.

@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
    Sensor sensor = InstanceManager.getDefault(SensorManager.class).getSensor(name);
    if (sensor == null) {
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SENSOR, name));
    }
    if (data.path(JSON.USERNAME).isTextual()) {
        sensor.setUserName(data.path(JSON.USERNAME).asText());
    }
    if (data.path(JSON.INVERTED).isBoolean()) {
        sensor.setInverted(data.path(JSON.INVERTED).asBoolean());
    }
    if (data.path(JSON.COMMENT).isTextual()) {
        sensor.setComment(data.path(JSON.COMMENT).asText());
    }
    int state = data.path(JSON.STATE).asInt(JSON.UNKNOWN);
    try {
        switch(state) {
            case JSON.ACTIVE:
                sensor.setKnownState(Sensor.ACTIVE);
                break;
            case JSON.INACTIVE:
                sensor.setKnownState(Sensor.INACTIVE);
                break;
            case JSON.INCONSISTENT:
            case JSON.UNKNOWN:
                // silently ignore
                break;
            default:
                throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", SENSOR, state));
        }
    } catch (JmriException ex) {
        throw new JsonException(500, ex);
    }
    return this.doGet(type, name, locale);
}
Also used : JsonException(jmri.server.json.JsonException) SensorManager(jmri.SensorManager) JmriException(jmri.JmriException) Sensor(jmri.Sensor)

Example 73 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonReporterHttpService method doGet.

@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
    ObjectNode root = mapper.createObjectNode();
    root.put(TYPE, REPORTER);
    ObjectNode data = root.putObject(DATA);
    Reporter reporter = InstanceManager.getDefault(ReporterManager.class).getReporter(name);
    if (reporter == null) {
        throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", REPORTER, name));
    }
    data.put(NAME, reporter.getSystemName());
    data.put(USERNAME, reporter.getUserName());
    data.put(STATE, reporter.getState());
    data.put(COMMENT, reporter.getComment());
    if (reporter.getCurrentReport() != null) {
        data.put(REPORT, reporter.getCurrentReport().toString());
    } else {
        data.putNull(REPORT);
    }
    if (reporter.getLastReport() != null) {
        data.put(LAST_REPORT, reporter.getLastReport().toString());
    } else {
        data.putNull(LAST_REPORT);
    }
    return root;
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ReporterManager(jmri.ReporterManager) Reporter(jmri.Reporter)

Example 74 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonServlet method doPut.

@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType(UTF8_APPLICATION_JSON);
    // NOI18N
    response.setHeader("Connection", "Keep-Alive");
    ServletUtil.getInstance().setNonCachingHeaders(response);
    // NOI18N
    String[] rest = request.getPathInfo().split("/");
    String type = (rest.length > 1) ? rest[1] : null;
    String name = (rest.length > 2) ? rest[2] : null;
    JsonNode data;
    JsonNode reply = null;
    try {
        if (request.getContentType().contains(APPLICATION_JSON)) {
            data = this.mapper.readTree(request.getReader());
            if (!data.path(DATA).isMissingNode()) {
                data = data.path(DATA);
            }
        } else {
            // need to I18N
            throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "PUT request must be a JSON object");
        }
        if (type != null) {
            // for historical reasons, set the name to POWER on a power request
            if (type.equals(POWER)) {
                name = POWER;
            } else if (name == null) {
                name = data.path(NAME).asText();
            }
            if (name != null) {
                if (this.services.get(type) != null) {
                    ArrayNode array = this.mapper.createArrayNode();
                    JsonException exception = null;
                    try {
                        for (JsonHttpService service : this.services.get(type)) {
                            array.add(service.doPut(type, name, data, request.getLocale()));
                        }
                    } catch (JsonException ex) {
                        exception = ex;
                    }
                    switch(array.size()) {
                        case 0:
                            if (exception != null) {
                                throw exception;
                            }
                            reply = array;
                            break;
                        case 1:
                            reply = array.get(0);
                            break;
                        default:
                            reply = array;
                            break;
                    }
                }
                if (reply == null) {
                    // need to I18N
                    throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, type + " is not a creatable type");
                }
            } else {
                log.warn("Type {} unknown.", type);
                throw new JsonException(HttpServletResponse.SC_NOT_FOUND, Bundle.getMessage(request.getLocale(), "ErrorUnknownType", type));
            }
        } else {
            log.warn("Type not specified.");
            // Need to I18N
            throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "Type must be specified.");
        }
    } catch (JsonException ex) {
        reply = ex.getJsonMessage();
    }
    // use HTTP error codes when possible
    int code = reply.path(DATA).path(CODE).asInt(HttpServletResponse.SC_OK);
    if (code == HttpServletResponse.SC_OK) {
        response.getWriter().write(this.mapper.writeValueAsString(reply));
    } else {
        this.sendError(response, code, this.mapper.writeValueAsString(reply));
    }
}
Also used : JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonHttpService(jmri.server.json.JsonHttpService)

Example 75 with JsonException

use of jmri.server.json.JsonException in project JMRI by JMRI.

the class JsonServlet method doPost.

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType(UTF8_APPLICATION_JSON);
    // NOI18N
    response.setHeader("Connection", "Keep-Alive");
    ServletUtil.getInstance().setNonCachingHeaders(response);
    // NOI18N
    String[] rest = request.getPathInfo().split("/");
    String type = (rest.length > 1) ? rest[1] : null;
    String name = (rest.length > 2) ? rest[2] : null;
    JsonNode data;
    JsonNode reply = null;
    try {
        if (request.getContentType().contains(APPLICATION_JSON)) {
            data = this.mapper.readTree(request.getReader());
            if (!data.path(DATA).isMissingNode()) {
                data = data.path(DATA);
            }
        } else {
            data = this.mapper.createObjectNode();
            if (request.getParameter(STATE) != null) {
                ((ObjectNode) data).put(STATE, Integer.parseInt(request.getParameter(STATE)));
            } else if (request.getParameter(LOCATION) != null) {
                ((ObjectNode) data).put(LOCATION, request.getParameter(LOCATION));
            } else if (request.getParameter(VALUE) != null) {
                // values other than Strings should be sent in a JSON object
                ((ObjectNode) data).put(VALUE, request.getParameter(VALUE));
            }
        }
        if (type != null) {
            // for historical reasons, set the name to POWER on a power request
            if (type.equals(POWER)) {
                name = POWER;
            } else if (name == null) {
                name = data.path(NAME).asText();
            }
            log.debug("POST operation for {}/{} with {}", type, name, data);
            if (name != null) {
                if (this.services.get(type) != null) {
                    log.debug("Using data: {}", data);
                    ArrayNode array = this.mapper.createArrayNode();
                    JsonException exception = null;
                    try {
                        for (JsonHttpService service : this.services.get(type)) {
                            array.add(service.doPost(type, name, data, request.getLocale()));
                        }
                    } catch (JsonException ex) {
                        exception = ex;
                    }
                    switch(array.size()) {
                        case 0:
                            if (exception != null) {
                                throw exception;
                            }
                            reply = array;
                            break;
                        case 1:
                            reply = array.get(0);
                            break;
                        default:
                            reply = array;
                            break;
                    }
                }
                if (reply == null) {
                    log.warn("Type {} unknown.", type);
                    throw new JsonException(HttpServletResponse.SC_NOT_FOUND, Bundle.getMessage(request.getLocale(), "ErrorUnknownType", type));
                }
            } else {
                log.error("Name must be defined.");
                // Need to I18N
                throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "Name must be defined.");
            }
        } else {
            log.warn("Type not specified.");
            // Need to I18N
            throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "Type must be specified.");
        }
    } catch (JsonException ex) {
        reply = ex.getJsonMessage();
    }
    // use HTTP error codes when possible
    int code = reply.path(DATA).path(CODE).asInt(HttpServletResponse.SC_OK);
    if (code == HttpServletResponse.SC_OK) {
        response.getWriter().write(this.mapper.writeValueAsString(reply));
    } else {
        this.sendError(response, code, this.mapper.writeValueAsString(reply));
    }
}
Also used : JsonException(jmri.server.json.JsonException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonHttpService(jmri.server.json.JsonHttpService)

Aggregations

JsonException (jmri.server.json.JsonException)112 JsonNode (com.fasterxml.jackson.databind.JsonNode)65 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)42 Test (org.junit.Test)31 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)25 JmriException (jmri.JmriException)22 IOException (java.io.IOException)21 JsonMockConnection (jmri.server.json.JsonMockConnection)18 Sensor (jmri.Sensor)13 SensorManager (jmri.SensorManager)13 Locale (java.util.Locale)10 Route (jmri.Route)9 RouteManager (jmri.RouteManager)9 Turnout (jmri.Turnout)9 SignalMast (jmri.SignalMast)8 TurnoutManager (jmri.TurnoutManager)8 Light (jmri.Light)7 ReporterManager (jmri.ReporterManager)7 SignalHead (jmri.SignalHead)7 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)6