001package jmri.server.json.logixngicon; 002 003import com.fasterxml.jackson.databind.JsonNode; 004import com.fasterxml.jackson.databind.ObjectMapper; 005import com.fasterxml.jackson.databind.node.ObjectNode; 006 007import javax.servlet.http.HttpServletResponse; 008 009import jmri.jmrit.display.LogixNGIcon; 010import jmri.server.json.JsonException; 011import jmri.server.json.JsonHttpService; 012import jmri.server.json.JsonRequest; 013import static jmri.server.json.logixngicon.JsonLogixNGIconServiceFactory.LOGIXNG_ICON; 014 015 016/** 017 * Provide JSON HTTP services for managing {@link jmri.jmrit.display.LogixNGIcon}s. 018 * 019 * @author Randall Wood Copyright 2016, 2018 020 * @author Daniel Bergqvist (C) 2023 021 */ 022public class JsonLogixNGIconHttpService extends JsonHttpService { 023 024 public JsonLogixNGIconHttpService(ObjectMapper mapper) { 025 super(mapper); 026 } 027 028 @Override 029 public JsonNode doGet(String type, String name, JsonNode data, JsonRequest request) throws JsonException { 030 throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "GetNotAllowed", type), request.id); 031 } 032 033 /** 034 * Respond to an HTTP POST request for the requested LogixNGIcon. 035 * <p> 036 * This method throws a 404 Not Found error if the named LogixNGIcon does not 037 * exist. 038 * 039 * @param type {@link jmri.server.json.logixngicon.JsonLogixNGIconServiceFactory#LOGIXNG_ICON} 040 * @param name the name of the requested LogixNGIcon 041 * @param data JSON data set of attributes of the requested LogixNGIcon to be 042 * updated 043 * @param request the JSON request 044 * @return an empty JSON logixngicon message. 045 */ 046 @Override 047 public JsonNode doPost(String type, String name, JsonNode data, JsonRequest request) throws JsonException { 048 int identity = data.path("identity").asInt(-1); 049 if (identity != -1) { 050 LogixNGIcon logixNGIcon = LogixNGIcon.IDENTITY_MANAGER.getLogixNGIcon(identity); 051 if (logixNGIcon == null) { 052 throw new JsonException(404, Bundle.getMessage(request.locale, JsonException.ERROR_NOT_FOUND, type, name), 053 request.id); 054 } 055 logixNGIcon.executeLogixNG(); 056 } else { 057 throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, Bundle.getMessage(request.locale, "InvalidIdentity", identity), request.id); 058 } 059 ObjectNode node = mapper.createObjectNode(); 060 return message(LOGIXNG_ICON, node, request.id); 061 } 062 063 @Override 064 public JsonNode doPut(String type, String name, JsonNode data, JsonRequest request) throws JsonException { 065 throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "PutNotAllowed", type), request.id); 066 } 067 068 @Override 069 public JsonNode doSchema(String type, boolean server, JsonRequest request) throws JsonException { 070 switch (type) { 071 case LOGIXNG_ICON: 072 return doSchema(type, 073 server, 074 "jmri/server/json/logixngicon/logixngicon-server.json", 075 "jmri/server/json/logixngicon/logixngicon-client.json", 076 request.id); 077 default: 078 throw new JsonException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, Bundle.getMessage(request.locale, JsonException.ERROR_UNKNOWN_TYPE, type), request.id); 079 } 080 } 081 082 @Override 083 public void doDelete(String type, String name, JsonNode data, JsonRequest request) throws JsonException { 084 throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "DeleteNotAllowed", type), request.id); 085 } 086 087 @Override 088 public JsonNode doGetList(String type, JsonNode data, JsonRequest request) throws JsonException { 089 throw new JsonException(HttpServletResponse.SC_METHOD_NOT_ALLOWED, Bundle.getMessage(request.locale, "GetListNotAllowed", type), request.id); 090 } 091 092// private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(JsonLogixNGIconHttpService.class); 093}