diff --git a/README.md b/README.md index ee683a2..a8f2c4c 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,58 @@ Attribute | Description `default` | The fallback value to output, if no reason is available. `escapeXml` | Converts <, >, &, ', " to their corresponding [entity codes](http://dev.w3.org/html5/html-author/charref). Value is `true` by default. +## StatusCode Bean + +The `StatusCode` bean can be used to check the class of the status code error. For example, using the JSTL: + +```jsp +<%@ taglib prefix="hs" uri="http://erik.thauvin.net/taglibs/httpstatus" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> + + + + + + An error occurred on your side. () + + + An error occurred on our side. () + + +``` + +or in a Servlet: + +```java +import net.thauvin.erik.httpstatus.StatusCode; + +// --- + +StatusCode statusCode = new StatusCode((Integer) request.getAttribute("javax.servlet.error.status_code")); +if (statusCode.isError()) { + if (statusCode.isServerError()) { + String reason = statusCode.getReason(); + } else { + // ... + } +} +``` + +The `StatusCode` bean methods are: + +Method | Description +----------------- | -------------------------------------------------------------------- +`getReason` | Returns the reason for the status code (eg: `Internal Server Error`) +`isClientError` | Checks if the status code is a client error. +`isError` | Checks if the status code is a server or client error. +`isInfo` | Checks if the status code is informational. +`isRedirect` | Checks if the status code is a redirect. +`isServerError` | Checks if the status code is a server error. +`isSuccess` | Checks if the status code is a success. (`OK`) +`isValid` | Checks if the status code is valid. + +## Reasons + The reasons are defined in a [ResourceBundle](http://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html) properties as follows: Status Code | Reason @@ -194,56 +246,6 @@ Status Code | Reason `598` | Network Read Timeout Error `599` | Network Connect Timeout Error -## StatusCode Bean - -The `StatusCode` bean can be used to check the class of the status code error. For example, using the JSTL: - -```jsp -<%@ taglib prefix="hs" uri="http://erik.thauvin.net/taglibs/httpstatus" %> -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> - - - - - - An error occurred on your side. () - - - An error occurred on our side. () - - -``` - -or in a Servlet: - -```java -import net.thauvin.erik.httpstatus.StatusCode; - -// --- - -final StatusCode statusCode = new StatusCode((Integer) request.getAttribute("javax.servlet.error.status_code")); -if (statusCode.isError()) { - if (statusCode.isServerError()) { - final String reason = statusCode.getReason(); - } else { - // ... - } -} -``` - -The `StatusCode` bean methods are: - -Method | Description ------------------ | ------------------------------------------------------------------ -`getReason` | Returns the reason for the status code (eg: Internal Server Error) -'isClientError' | Checks if the status code is a client error. -`isError` | Checks if the status code is a server or client error. -`isInfo` | Checks if the status code is informational. -`isRedirect` | Checks if the status code is a redirect. -`isServerError' | Checks if the status code is a server error. -`isSuccess` | Checks if the status code is a success. (`OK`) -`isValid` | Checks if the status code is valid. - ## Command Line Usage You can query the reason phrase for status codes as follows: diff --git a/src/main/java/net/thauvin/erik/httpstatus/StatusCode.java b/src/main/java/net/thauvin/erik/httpstatus/StatusCode.java index e6a4df7..a09f7e2 100644 --- a/src/main/java/net/thauvin/erik/httpstatus/StatusCode.java +++ b/src/main/java/net/thauvin/erik/httpstatus/StatusCode.java @@ -76,7 +76,7 @@ public class StatusCode implements Serializable { } /** - * Checks if the status code is a client error. + * Checks if the status code is a client error. (eg: Interal Server Error) * * @return true if the status code is a client error, false otherwise. */ diff --git a/src/test/java/net/thauvin/erik/httpstatus/ReasonsMainTest.java b/src/test/java/net/thauvin/erik/httpstatus/ReasonsMainTest.java index a4175b4..642dfb6 100644 --- a/src/test/java/net/thauvin/erik/httpstatus/ReasonsMainTest.java +++ b/src/test/java/net/thauvin/erik/httpstatus/ReasonsMainTest.java @@ -45,7 +45,7 @@ import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; /** - * The TestMain class. + * Main Class Tests. * * @author Erik C. Thauvin * @created 2019-05-06 diff --git a/src/test/java/net/thauvin/erik/httpstatus/ReasonsTest.java b/src/test/java/net/thauvin/erik/httpstatus/ReasonsTest.java index 7dee1d8..76806c2 100644 --- a/src/test/java/net/thauvin/erik/httpstatus/ReasonsTest.java +++ b/src/test/java/net/thauvin/erik/httpstatus/ReasonsTest.java @@ -39,7 +39,7 @@ import java.util.ResourceBundle; import static org.testng.Assert.assertEquals; /** - * The ReasonsTest class. + * Reasons Tests. * * @author Erik C. Thauvin * @created 2015-12-03 @@ -56,4 +56,4 @@ public class ReasonsTest { } } -} \ No newline at end of file +} diff --git a/src/test/java/net/thauvin/erik/httpstatus/StatusCodeTest.java b/src/test/java/net/thauvin/erik/httpstatus/StatusCodeTest.java index 0ce8357..1bee73a 100644 --- a/src/test/java/net/thauvin/erik/httpstatus/StatusCodeTest.java +++ b/src/test/java/net/thauvin/erik/httpstatus/StatusCodeTest.java @@ -48,12 +48,12 @@ import static org.testng.Assert.assertTrue; public class StatusCodeTest { @Test void testStatusCode() { - final StatusCode statusCode = new StatusCode(100); - + StatusCode statusCode = new StatusCode(); + statusCode.setCode(100); assertEquals(statusCode.getCode(), 100, "100 is 100"); assertTrue(statusCode.isInfo(), "100 is informational"); - statusCode.setCode(200); + statusCode = new StatusCode(200); assertEquals(statusCode.getCode(), 200, "200 is 200"); assertTrue(statusCode.isSuccess(), "200 is OK"); diff --git a/src/test/java/net/thauvin/erik/httpstatus/UtilsTest.java b/src/test/java/net/thauvin/erik/httpstatus/UtilsTest.java index e03ec34..93bb6ff 100644 --- a/src/test/java/net/thauvin/erik/httpstatus/UtilsTest.java +++ b/src/test/java/net/thauvin/erik/httpstatus/UtilsTest.java @@ -41,7 +41,7 @@ import java.io.StringWriter; import static org.testng.Assert.assertEquals; /** - * The UtilsTest class. + * Utils Tests. * * @author Erik C. Thauvin * @created 2015-12-03