Skip to content

Commit

Permalink
feat: Converge error messages between JSON sockets and servlets
Browse files Browse the repository at this point in the history
  • Loading branch information
rhwood committed Jan 2, 2020
1 parent 07845f0 commit 3a859d7
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 24 deletions.
2 changes: 0 additions & 2 deletions java/src/jmri/web/servlet/json/Bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ JsonTitle=Json Console
# providing relative path to translated Roster.html
# (for example) Roster.html=web/servlet/json/Json_it.html
Json.html=web/servlet/json/Json.html
#Error message passed to client when requesting an object type not handled by the JSON server
ErrorUnknownType=Object Type "{0}" is unknown
2 changes: 0 additions & 2 deletions java/src/jmri/web/servlet/json/Bundle_ca.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ JsonTitle=Consola Json
# providing relative path to translated Roster.html
# (for example) Roster.html=web/servlet/json/Json_it.html
Json.html=web/servlet/json/Json.html
#Error message passed to client when requesting an object type not handled by the JSON server
ErrorUnknownType=Tipus d'Objecte \\"{0}\\" \u00e9s desconegut
2 changes: 0 additions & 2 deletions java/src/jmri/web/servlet/json/Bundle_cs.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ JsonTitle=Json konsole
# providing relative path to translated Roster.html
# (for example) Roster.html=web/servlet/json/Json_it.html
Json.html=web/servlet/json/Json_cs.html
#Error message passed to client when requesting an object type not handled by the JSON server
ErrorUnknownType= Objekt typu \\"{0}\\" je nezn\u00e1m\u00fd
2 changes: 0 additions & 2 deletions java/src/jmri/web/servlet/json/Bundle_da.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ JsonTitle=Json Console
# providing relative path to translated Roster.html
# (for example) Roster.html=web/servlet/json/Json_it.html
Json.html=web/servlet/json/Json.html
#Error message passed to client when requesting an object type not handled by the JSON server
ErrorUnknownType=Object Type \\"{0}\\" is unknown
2 changes: 0 additions & 2 deletions java/src/jmri/web/servlet/json/Bundle_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ JsonTitle=JSON Console
# providing relative path to translated Roster.html
# (for example) Roster.html=web/servlet/json/Json_it.html
Json.html=web/servlet/json/Json_de.html
#Error message passed to client when requesting an object type not handled by the JSON server
ErrorUnknownType=Object Type \\"{0}\\" is unknown
2 changes: 0 additions & 2 deletions java/src/jmri/web/servlet/json/Bundle_nl.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ JsonTitle=JSON Console
# providing relative path to translated Roster.html
# (for example) Roster.html=web/servlet/json/Json_it.html
Json.html=web/servlet/json/Json_nl.html
#Error message passed to client when requesting an object type not handled by the JSON server
ErrorUnknownType=Object Type \\"{0}\\" is unknown
113 changes: 113 additions & 0 deletions java/src/jmri/web/servlet/json/JsonBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package jmri.web.servlet.json;

import java.util.Locale;
import javax.annotation.CheckReturnValue;
import javax.annotation.CheckForNull;
import javax.annotation.ParametersAreNonnullByDefault;

@ParametersAreNonnullByDefault
@CheckReturnValue

@javax.annotation.concurrent.Immutable

/**
* Provides standard access for resource bundles in a package.
* <p>
* Convention is to provide a subclass of this name in each package, working off
* the local resource bundle name.
* <p>
* This instance is named differently than usual since two Bundle classes exist
* for this package: one that inherits from jmri.web.servlet and one that
* inherits from jmri.server.json
*
* @author Bob Jacobsen Copyright (C) 2012
* @since 3.3.1
*/
public class JsonBundle extends jmri.server.json.Bundle {

@CheckForNull
private static final String name = null; // NOI18N

//
// below here is boilerplate to be copied exactly
//
/**
* Provides a translated string for a given key from the package resource
* bundle or parent.
* <p>
* Note that this is intentionally package-local access.
*
* @param key Bundle key to be translated
* @return Internationalized text
*/
static String getMessage(String key) {
return getBundle().handleGetMessage(key);
}

/**
* Provides a translated string for a given key in a given locale from the
* package resource bundle or parent.
* <p>
* Note that this is intentionally package-local access.
*
* @param locale The locale to be used
* @param key Bundle key to be translated
* @return Internationalized text
*/
static String getMessage(Locale locale, String key) {
return getBundle().handleGetMessage(locale, key);
}

/**
* Merges user data with a translated string for a given key from the
* package resource bundle or parent.
* <p>
* Uses the transformation conventions of the Java MessageFormat utility.
* <p>
* Note that this is intentionally package-local access.
*
* @see java.text.MessageFormat
* @param key Bundle key to be translated
* @param subs One or more objects to be inserted into the message
* @return Internationalized text
*/
static String getMessage(String key, Object... subs) {
return getBundle().handleGetMessage(key, subs);
}

/**
* Merges user data with a translated string for a given key in a given
* locale from the package resource bundle or parent.
* <p>
* Uses the transformation conventions of the Java MessageFormat utility.
* <p>
* Note that this is intentionally package-local access.
*
* @see java.text.MessageFormat
* @param locale The locale to be used
* @param key Bundle key to be translated
* @param subs One or more objects to be inserted into the message
* @return Internationalized text
*/
static String getMessage(Locale locale, String key, Object... subs) {
return getBundle().handleGetMessage(locale, key, subs);
}

private final static JsonBundle b = new JsonBundle();

@Override
@CheckForNull
protected String bundleName() {
return name;
}

protected static jmri.Bundle getBundle() {
return b;
}

@Override
protected String retry(Locale locale, String key) {
return super.getBundle().handleGetMessage(locale, key);
}

}
23 changes: 12 additions & 11 deletions java/src/jmri/web/servlet/json/JsonServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ protected void doGet(final HttpServletRequest request, HttpServletResponse respo
}
}
if (reply == null) {
log.warn("Requested type {} unknown.", type);
log.warn("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND,
Bundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
JsonBundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
}
} else {
if (services.get(jsonRequest.version).get(type) != null) {
Expand Down Expand Up @@ -236,7 +236,7 @@ protected void doGet(final HttpServletRequest request, HttpServletResponse respo
if (reply == null) {
log.warn("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND,
Bundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
JsonBundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
}
}
} catch (JsonException ex) {
Expand Down Expand Up @@ -335,14 +335,14 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}
}
if (reply == null) {
log.warn("Requested type {} unknown.", type);
log.warn("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND,
Bundle.getMessage(request.getLocale(), "ErrorUnknownType", type), id);
JsonBundle.getMessage(request.getLocale(), "ErrorUnknownType", type), id);
}
} else {
log.error("Name must be defined.");
// TODO: I18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, "Name must be defined.", id);
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST,
JsonBundle.getMessage(request.getLocale(), "ErrorMissingName"), id);
}
} else {
log.warn("Type not specified.");
Expand Down Expand Up @@ -419,14 +419,15 @@ protected void doPut(HttpServletRequest request, HttpServletResponse response) t
}
}
if (reply == null) {
// not a creatable item
// item cannot be created
// TODO: I18N
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, type + " is not a creatable type",
jsonRequest.id); // need to I18N
jsonRequest.id);
}
} else {
log.warn("Requested type {} unknown.", type);
log.warn("Requested type '{}' unknown.", type);
throw new JsonException(HttpServletResponse.SC_NOT_FOUND,
Bundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
JsonBundle.getMessage(request.getLocale(), "ErrorUnknownType", type), jsonRequest.id);
}
} else {
log.warn("Type not specified.");
Expand Down
46 changes: 46 additions & 0 deletions java/test/jmri/web/servlet/json/JsonBundleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package jmri.web.servlet.json;

import java.util.Locale;
import org.junit.Assert;
import org.junit.Test;

/**
* Tests for the JsonBundle class
*
* @author Bob Jacobsen Copyright (C) 2012
*/
public class JsonBundleTest {

@Test
public void testGoodKeyMessage() {
Assert.assertEquals("Turnout", JsonBundle.getMessage("BeanNameTurnout"));
}

@Test(expected = java.util.MissingResourceException.class)
public void testBadKeyMessage() {
JsonBundle.getMessage("FFFFFTTTTTTT");
}

@Test
public void testGoodKeyMessageArg() {
Assert.assertEquals("Turnout", JsonBundle.getMessage("BeanNameTurnout", new Object[]{}));
Assert.assertEquals("About Test", JsonBundle.getMessage("TitleAbout", "Test"));
}

@Test(expected = java.util.MissingResourceException.class)
public void testBadKeyMessageArg() {
JsonBundle.getMessage("FFFFFTTTTTTT", new Object[]{});
}

@Test
public void testLocaleMessage() {
Assert.assertEquals("Scambio", JsonBundle.getMessage(Locale.ITALY, "BeanNameTurnout"));
}

@Test
public void testLocaleMessageArg() {
Assert.assertEquals("Scambio", JsonBundle.getMessage(Locale.ITALY, "BeanNameTurnout", new Object[]{}));
Assert.assertEquals("Informazioni su Test", JsonBundle.getMessage(Locale.ITALY, "TitleAbout", "Test"));
}

}
1 change: 0 additions & 1 deletion java/test/jmri/web/servlet/json/JsonServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ public void testDoPostPowerV5Parameters() throws IOException, ServletException,
*
* @throws java.io.IOException unexpected failure in test context
* @throws javax.servlet.ServletException unexpected failure in test context
* @throws JmriException unexpected failure in test context
*/
@Test
public void testGetV5noParameters() throws ServletException, IOException {
Expand Down
1 change: 1 addition & 0 deletions java/test/jmri/web/servlet/json/PackageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@RunWith(Suite.class)
@Suite.SuiteClasses({
BundleTest.class,
JsonBundleTest.class,
JsonServletTest.class
})

Expand Down

0 comments on commit 3a859d7

Please sign in to comment.