use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonLightHttpService method doPost.
@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
Light light = InstanceManager.lightManagerInstance().getLight(name);
if (light == null) {
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", LIGHT, name));
}
if (data.path(USERNAME).isTextual()) {
light.setUserName(data.path(USERNAME).asText());
}
if (data.path(COMMENT).isTextual()) {
light.setComment(data.path(COMMENT).asText());
}
int state = data.path(STATE).asInt(UNKNOWN);
switch(state) {
case ON:
light.setState(Light.ON);
break;
case OFF:
light.setState(Light.OFF);
break;
case UNKNOWN:
// leave state alone in this case
break;
default:
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", LIGHT, state));
}
return this.doGet(type, name, locale);
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonRouteHttpService method doPost.
/**
* Respond to an HTTP POST request for the requested route.
* <p>
* This method throws a 404 Not Found error if the named route does not
* exist.
* <p>
* <strong>Note:</strong> attempting to set a state of
* {@link jmri.server.json.JSON#INACTIVE} or
* {@link jmri.server.json.JSON#UNKNOWN} has no effect. Setting a state of
* {@link jmri.server.json.JSON#TOGGLE} has the same effect as setting a
* state of {@link jmri.server.json.JSON#ACTIVE}. Any other states throw a
* 400 Invalid Request error.
*
* @param type one of
* {@link jmri.server.json.route.JsonRouteServiceFactory#ROUTE}
* or
* {@link jmri.server.json.route.JsonRouteServiceFactory#ROUTES}
* @param name the name of the requested route.
* @param data JSON data set of attributes of the requested route to be
* updated.
* @param locale the requesting client's Locale.
* @return a JSON description of the requested route. Since a route changes
* state on a separete thread, this may return a route in the state
* prior to this call, the target state, or an intermediate state.
*/
@Override
public JsonNode doPost(String type, String name, JsonNode data, Locale locale) throws JsonException {
Route route = InstanceManager.getDefault(RouteManager.class).getRoute(name);
if (route == null) {
throw new JsonException(404, Bundle.getMessage(locale, "ErrorObject", ROUTE, name));
}
if (data.path(JSON.USERNAME).isTextual()) {
route.setUserName(data.path(JSON.USERNAME).asText());
}
if (data.path(JSON.COMMENT).isTextual()) {
route.setComment(data.path(JSON.COMMENT).asText());
}
int state = data.path(JSON.STATE).asInt(JSON.UNKNOWN);
switch(state) {
case JSON.ACTIVE:
case JSON.TOGGLE:
route.setRoute();
break;
case JSON.INACTIVE:
case JSON.UNKNOWN:
// leave state alone in this case
break;
default:
// NOI18N
throw new JsonException(400, Bundle.getMessage(locale, "ErrorUnknownState", ROUTE, state));
}
return this.doGet(type, name, locale);
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonRosterHttpService method getRosterGroup.
public JsonNode getRosterGroup(Locale locale, String name) throws JsonException {
if (name.equals(Roster.ALLENTRIES) || Roster.getDefault().getRosterGroupList().contains(name)) {
int size = Roster.getDefault().getEntriesInGroup(name).size();
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, JsonRoster.ROSTER_GROUP);
ObjectNode data = root.putObject(DATA);
data.put(NAME, name.isEmpty() ? Roster.AllEntries(locale) : name);
data.put(LENGTH, size);
return root;
} else {
throw new JsonException(HttpServletResponse.SC_NOT_FOUND, Bundle.getMessage(locale, "ErrorNotFound", JsonRoster.ROSTER_GROUP, name));
}
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonPowerHttpService method doGet.
@Override
public JsonNode doGet(String type, String name, Locale locale) throws JsonException {
ObjectNode root = mapper.createObjectNode();
root.put(TYPE, POWER);
ObjectNode data = root.putObject(DATA);
try {
PowerManager manager = InstanceManager.getDefault(PowerManager.class);
if (name != null && !name.isEmpty()) {
for (PowerManager pm : InstanceManager.getList(PowerManager.class)) {
if (pm.getUserName().equals(name)) {
manager = pm;
data.put(NAME, name);
}
}
}
switch(manager.getPower()) {
case PowerManager.OFF:
data.put(STATE, OFF);
break;
case PowerManager.ON:
data.put(STATE, ON);
break;
default:
data.put(STATE, UNKNOWN);
break;
}
data.put(DEFAULT, false);
if (manager.equals(InstanceManager.getDefault(PowerManager.class))) {
data.put(DEFAULT, true);
}
} catch (JmriException e) {
log.error("Unable to get Power state.", e);
throw new JsonException(500, Bundle.getMessage(locale, "ErrorPower"));
} catch (NullPointerException e) {
// No PowerManager is defined; just report it as UNKNOWN
data.put(STATE, UNKNOWN);
}
return root;
}
use of jmri.server.json.JsonException in project JMRI by JMRI.
the class JsonServlet method doGet.
/**
* Handle HTTP get requests for JSON data. Examples:
* <ul>
* <li>/json/sensor/IS22 (return data for sensor with system name
* "IS22")</li>
* <li>/json/sensors (returns a list of all sensors known to JMRI)</li>
* </ul>
* sample responses:
* <ul>
* <li>{"type":"sensor","data":{"name":"IS22","userName":"FarEast","comment":null,"inverted":false,"state":4}}</li>
* <li>[{"type":"sensor","data":{"name":"IS22","userName":"FarEast","comment":null,"inverted":false,"state":4}}]</li>
* </ul>
* Note that data will vary for each type.
*
* @param request an HttpServletRequest object that contains the request
* the client has made of the servlet
* @param response an HttpServletResponse object that contains the response
* the servlet sends to the client
* @throws java.io.IOException if an input or output error is detected when
* the servlet handles the GET request
*/
@Override
protected void doGet(final HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
// NOI18N
response.setHeader("Connection", "Keep-Alive");
if (request.getAttribute("result") != null) {
JsonNode result = (JsonNode) request.getAttribute("result");
// use HTTP error codes when possible
int code = result.path(DATA).path(CODE).asInt(HttpServletResponse.SC_OK);
if (code == HttpServletResponse.SC_OK) {
response.getWriter().write(this.mapper.writeValueAsString(result));
} else {
this.sendError(response, code, this.mapper.writeValueAsString(result));
}
return;
}
// NOI18N
String[] rest = request.getPathInfo().split("/");
String type = (rest.length > 1) ? rest[1] : null;
if (type != null) {
response.setContentType(UTF8_APPLICATION_JSON);
ServletUtil.getInstance().setNonCachingHeaders(response);
final String name = (rest.length > 2) ? URLDecoder.decode(rest[2], StandardCharsets.UTF_8.name()) : null;
ObjectNode parameters = this.mapper.createObjectNode();
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
parameters.put(entry.getKey(), URLDecoder.decode(entry.getValue()[0], "UTF-8"));
}
JsonNode reply = null;
try {
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.addAll(service.doGetList(type, 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 {
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.doGet(type, name, 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("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND, Bundle.getMessage(request.getLocale(), "ErrorUnknownType", type));
}
}
} 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));
}
} else {
// NOI18N
response.setContentType(ServletUtil.UTF8_TEXT_HTML);
response.getWriter().print(String.format(request.getLocale(), FileUtil.readURL(FileUtil.findURL(Bundle.getMessage(request.getLocale(), "Json.html"))), String.format(request.getLocale(), Bundle.getMessage(request.getLocale(), "HtmlTitle"), ServletUtil.getInstance().getRailroadName(false), Bundle.getMessage(request.getLocale(), "JsonTitle")), ServletUtil.getInstance().getNavBar(request.getLocale(), request.getContextPath()), ServletUtil.getInstance().getRailroadName(false), ServletUtil.getInstance().getFooter(request.getLocale(), request.getContextPath())));
}
}
Aggregations