This commit is contained in:
Erik C. Thauvin 2021-06-01 03:03:55 -07:00
parent 95f4c9714e
commit e77c8595cb
6 changed files with 60 additions and 58 deletions

102
README.md
View file

@ -98,6 +98,58 @@ Attribute | Description
`default` | The fallback value to output, if no reason is available. `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. `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" %>
<jsp:useBean id="statusCode" class="net.thauvin.erik.httpstatus.StatusCode"/>
<c:set target="${statusCode}" property="code"><hs:code/></c:set>
<c:choose>
<c:when test="${statusCode.isClientError()}">
An error occurred on your side. (<hs:reason/>)
</c:when>
<c:otherwise>
An error occurred on our side. (<hs:message/>)
</c:otherwise>
</c:choose>
```
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: 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 Status Code | Reason
@ -194,56 +246,6 @@ Status Code | Reason
`598` | Network Read Timeout Error `598` | Network Read Timeout Error
`599` | Network Connect 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" %>
<jsp:useBean id="statusCode" class="net.thauvin.erik.httpstatus.StatusCode"/>
<c:set target="${statusCode}" property="code"><hs:code/></c:set>
<c:choose>
<c:when test="${statusCode.isClientError()}">
An error occurred on your side. (<hs:reason/>)
</c:when>
<c:otherwise>
An error occurred on our side. (<hs:message/>)
</c:otherwise>
</c:choose>
```
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 ## Command Line Usage
You can query the reason phrase for status codes as follows: You can query the reason phrase for status codes as follows:

View file

@ -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: <code>Interal Server Error</code>)
* *
* @return <code>true</code> if the status code is a client error, <code>false</code> otherwise. * @return <code>true</code> if the status code is a client error, <code>false</code> otherwise.
*/ */

View file

@ -45,7 +45,7 @@ import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
/** /**
* The <code>TestMain</code> class. * Main Class Tests.
* *
* @author <a href="https://erik.thauvin.net/" target="_blank">Erik C. Thauvin</a> * @author <a href="https://erik.thauvin.net/" target="_blank">Erik C. Thauvin</a>
* @created 2019-05-06 * @created 2019-05-06

View file

@ -39,7 +39,7 @@ import java.util.ResourceBundle;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
/** /**
* The <code>ReasonsTest</code> class. * Reasons Tests.
* *
* @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a> * @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a>
* @created 2015-12-03 * @created 2015-12-03

View file

@ -48,12 +48,12 @@ import static org.testng.Assert.assertTrue;
public class StatusCodeTest { public class StatusCodeTest {
@Test @Test
void testStatusCode() { void testStatusCode() {
final StatusCode statusCode = new StatusCode(100); StatusCode statusCode = new StatusCode();
statusCode.setCode(100);
assertEquals(statusCode.getCode(), 100, "100 is 100"); assertEquals(statusCode.getCode(), 100, "100 is 100");
assertTrue(statusCode.isInfo(), "100 is informational"); assertTrue(statusCode.isInfo(), "100 is informational");
statusCode.setCode(200); statusCode = new StatusCode(200);
assertEquals(statusCode.getCode(), 200, "200 is 200"); assertEquals(statusCode.getCode(), 200, "200 is 200");
assertTrue(statusCode.isSuccess(), "200 is OK"); assertTrue(statusCode.isSuccess(), "200 is OK");

View file

@ -41,7 +41,7 @@ import java.io.StringWriter;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
/** /**
* The <code>UtilsTest</code> class. * Utils Tests.
* *
* @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a> * @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a>
* @created 2015-12-03 * @created 2015-12-03