use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method getMetadata.
/**
*
* @param locale The client's Locale.
* @param name The metadata element to get.
* @return JSON metadata element.
* @throws JsonException if name is not a recognized metadata element.
* @deprecated since 4.5.2
*/
@Deprecated
public static JsonNode getMetadata(Locale locale, String name) throws JsonException {
String metadata = Metadata.getBySystemName(name);
ObjectNode root;
if (metadata != null) {
root = mapper.createObjectNode();
root.put(TYPE, METADATA);
ObjectNode data = root.putObject(DATA);
data.put(NAME, name);
data.put(VALUE, Metadata.getBySystemName(name));
} else {
log.error("Unable to get metadata [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", METADATA, name));
}
return root;
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method getLight.
/*
* deprecated in favor of the implementations of the {@code do*} methods in
* {@link jmri.server.json.JsonHttpService}.
* @deprecated since 4.5.1
*/
@Deprecated
public static JsonNode getLight(Locale locale, String name) throws JsonException {
try {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, LIGHT);
ObjectNode data = root.putObject(DATA);
Light light = InstanceManager.lightManagerInstance().getLight(name);
data.put(NAME, light.getSystemName());
data.put(USERNAME, light.getUserName());
data.put(COMMENT, light.getComment());
switch(light.getState()) {
case Light.OFF:
data.put(STATE, OFF);
break;
case Light.ON:
data.put(STATE, ON);
break;
default:
data.put(STATE, UNKNOWN);
break;
}
return root;
} catch (NullPointerException e) {
log.error("Unable to get light [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", LIGHT, name));
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonUtil method getMemory.
/*
* deprecated in favor of the implementations of the {@code do*} methods in
* {@link jmri.server.json.JsonHttpService}.
* @deprecated since 4.5.1
*/
@Deprecated
public static JsonNode getMemory(Locale locale, String name) throws JsonException {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, MEMORY);
ObjectNode data = root.putObject(DATA);
Memory memory = InstanceManager.memoryManagerInstance().getMemory(name);
try {
data.put(NAME, memory.getSystemName());
data.put(USERNAME, memory.getUserName());
data.put(COMMENT, memory.getComment());
if (memory.getValue() == null) {
data.putNull(VALUE);
} else {
data.put(VALUE, memory.getValue().toString());
}
} catch (NullPointerException e) {
log.error("Unable to get memory [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", MEMORY, name));
}
return root;
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonSignalMastServer method parseRequest.
public void parseRequest(Locale locale, JsonNode data) throws JmriException, IOException, JsonException {
String name = data.path(NAME).asText();
String state = data.path(STATE).asText();
if ("".equals(state)) {
//if not passed, retrieve current and respond
SignalMast sm = InstanceManager.getDefault(jmri.SignalMastManager.class).getSignalMast(name);
try {
state = sm.getAspect();
if (state == null) {
//if null, set state to "Unknown"
state = ASPECT_UNKNOWN;
}
if ((sm.getHeld()) && (sm.getAppearanceMap().getSpecificAppearance(jmri.SignalAppearanceMap.HELD) != null)) {
state = ASPECT_HELD;
} else if ((!sm.getLit()) && (sm.getAppearanceMap().getSpecificAppearance(jmri.SignalAppearanceMap.DARK) != null)) {
state = ASPECT_DARK;
}
this.sendStatus(name, state);
} catch (IOException ex) {
this.sendErrorStatus(name);
} catch (NullPointerException e) {
log.error("Unable to get signalMast [{}].", name);
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", SIGNAL_MAST, name));
}
} else {
//else set the aspect to the state passed in
this.setSignalMastAspect(name, state);
}
this.addSignalMastToList(name);
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonThrottle method getThrottle.
/**
* Creates a new JsonThrottle or returns an existing one if the request is
* for an existing throttle.
*
* data can contain either a string {@link jmri.server.json.JSON#ID} node
* containing the ID of a {@link jmri.jmrit.roster.RosterEntry} or an
* integer {@link jmri.server.json.JSON#ADDRESS} node. If data contains an
* ADDRESS, the ID node is ignored. The ADDRESS may be accompanied by a
* boolean {@link jmri.server.json.JSON#IS_LONG_ADDRESS} node specifying the
* type of address, if IS_LONG_ADDRESS is not specified, the inverse of {@link jmri.ThrottleManager#canBeShortAddress(int)
* } is used as the "best guess" of the address length.
*
* @param throttleId The client's identity token for this throttle
* @param data JSON object containing either an ADDRESS or an ID
* @param server The server requesting this throttle on behalf of a
* client
* @return The throttle
* @throws jmri.server.json.JsonException if unable to get the requested
* {@link jmri.Throttle}
*/
public static JsonThrottle getThrottle(String throttleId, JsonNode data, JsonThrottleSocketService server) throws JsonException {
DccLocoAddress address = null;
Locale locale = server.getConnection().getLocale();
JsonThrottleManager manager = JsonThrottleManager.getDefault();
if (!data.path(ADDRESS).isMissingNode()) {
if (manager.canBeLongAddress(data.path(ADDRESS).asInt()) || manager.canBeShortAddress(data.path(ADDRESS).asInt())) {
address = new DccLocoAddress(data.path(ADDRESS).asInt(), data.path(IS_LONG_ADDRESS).asBoolean(!manager.canBeShortAddress(data.path(ADDRESS).asInt())));
} else {
log.warn("Address \"{}\" is not a valid address.", data.path(ADDRESS).asInt());
// NOI18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, Bundle.getMessage(locale, "ErrorThrottleInvalidAddress", data.path(ADDRESS).asInt()));
}
} else if (!data.path(ID).isMissingNode()) {
try {
address = Roster.getDefault().getEntryForId(data.path(ID).asText()).getDccLocoAddress();
} catch (NullPointerException ex) {
log.warn("Roster entry \"{}\" does not exist.", data.path(ID).asText());
// NOI18N
throw new JsonException(HttpServletResponse.SC_NOT_FOUND, Bundle.getMessage(locale, "ErrorThrottleRosterEntry", data.path(ID).asText()));
}
} else {
log.warn("No address specified");
// NOI18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, Bundle.getMessage(locale, "ErrorThrottleNoAddress"));
}
if (manager.containsKey(address)) {
JsonThrottle throttle = manager.get(address);
manager.put(throttle, server);
throttle.sendMessage(server.getConnection().getObjectMapper().createObjectNode().put(CLIENTS, manager.getServers(throttle).size()));
return throttle;
} else {
JsonThrottle throttle = new JsonThrottle(address, server);
if (!manager.requestThrottle(address, throttle)) {
log.error("Unable to get throttle for \"{}\".", address);
throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(server.getConnection().getLocale(), "ErrorThrottleUnableToGetThrottle", address));
}
manager.put(address, throttle);
manager.put(throttle, server);
manager.attachListener(address, throttle);
return throttle;
}
}
Aggregations