Initial commit.
This commit is contained in:
commit
ad982eff1b
14 changed files with 1047 additions and 0 deletions
75
src/main/java/net/thauvin/erik/httpstatus/Reasons.java
Normal file
75
src/main/java/net/thauvin/erik/httpstatus/Reasons.java
Normal file
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* HttpStatus.java
|
||||
*
|
||||
* Copyright (c) 2015 Erik C. Thauvin (http://erik.thauvin.net/)
|
||||
* All rights reserved.
|
||||
*/
|
||||
package net.thauvin.erik.httpstatus;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* The <code>Reasons</code> class.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2015-12-02
|
||||
* @since 1.0
|
||||
*/
|
||||
public class Reasons
|
||||
{
|
||||
/**
|
||||
* The reason phrases map.
|
||||
*/
|
||||
private static final Map<String, String> REASON_PHRASES = new TreeMap<String, String>();
|
||||
|
||||
/**
|
||||
* Gets the reason phrase for the specified status code.
|
||||
*
|
||||
* @param statusCode The status code.
|
||||
*
|
||||
* @return The reason phrase, or <code>null</code>.
|
||||
*/
|
||||
public static String getReasonPhrase(int statusCode)
|
||||
{
|
||||
return getReasonPhrase(Integer.toString(statusCode));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the reason phrase for the specified status code.
|
||||
*
|
||||
* @param statusCode The status code.
|
||||
*
|
||||
* @return The reason phrase, or <code>null</code>.
|
||||
*/
|
||||
public static String getReasonPhrase(String statusCode)
|
||||
{
|
||||
return REASON_PHRASES.get(statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the status codes and reason phrases.
|
||||
*
|
||||
* @param args The command line arguments.
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
for (final Map.Entry<String, String> entry : REASON_PHRASES.entrySet())
|
||||
{
|
||||
System.out.println(entry.getKey() + '=' + entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the reason phrases map.
|
||||
*/
|
||||
static
|
||||
{
|
||||
final ResourceBundle bundle = ResourceBundle.getBundle("reasons", java.util.Locale.getDefault());
|
||||
for (final String key : bundle.keySet())
|
||||
{
|
||||
REASON_PHRASES.put(key, bundle.getString(key));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* ReasonTag.java
|
||||
*
|
||||
* Copyright (c) 2015 Erik C. Thauvin (http://erik.thauvin.net/)
|
||||
* All rights reserved.
|
||||
*/
|
||||
package net.thauvin.erik.httpstatus.taglibs;
|
||||
|
||||
import net.thauvin.erik.httpstatus.Reasons;
|
||||
|
||||
import javax.servlet.jsp.JspException;
|
||||
import javax.servlet.jsp.JspWriter;
|
||||
import javax.servlet.jsp.PageContext;
|
||||
import javax.servlet.jsp.tagext.SimpleTagSupport;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* The <code>ReasonTag</code> class.
|
||||
*
|
||||
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
|
||||
* @created 2015-12-02
|
||||
* @since 1.0
|
||||
*/
|
||||
public class ReasonTag extends SimpleTagSupport
|
||||
{
|
||||
private int statusCode;
|
||||
private String defaultValue;
|
||||
|
||||
@Override
|
||||
public void doTag()
|
||||
throws JspException
|
||||
{
|
||||
final PageContext pageContext = (PageContext) getJspContext();
|
||||
final JspWriter out = pageContext.getOut();
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
if (statusCode > 0)
|
||||
{
|
||||
out.write(Reasons.getReasonPhrase(statusCode));
|
||||
}
|
||||
else
|
||||
{
|
||||
out.write(Reasons.getReasonPhrase(pageContext.getErrorData().getStatusCode()));
|
||||
}
|
||||
}
|
||||
catch(NullPointerException npe)
|
||||
{
|
||||
if (defaultValue != null)
|
||||
{
|
||||
out.write(defaultValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
out.write("");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the status code.
|
||||
*
|
||||
* @param statusCode The status code.
|
||||
*/
|
||||
public void setStatusCode(int statusCode)
|
||||
{
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default value.
|
||||
*
|
||||
* @param defaultValue The default value.
|
||||
*/
|
||||
public void setDefault(String defaultValue)
|
||||
{
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
}
|
38
src/main/resources/META-INF/httpstatus.tld
Normal file
38
src/main/resources/META-INF/httpstatus.tld
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
|
||||
version="2.1">
|
||||
|
||||
<description>HttpStatus JSP Tag Library</description>
|
||||
<display-name>HttpStatus JSP Tags</display-name>
|
||||
<tlib-version>1.0</tlib-version>
|
||||
<short-name>hs</short-name>
|
||||
<uri>http://erik.thauvin.net/taglibs/httpstatus</uri>
|
||||
|
||||
<tag>
|
||||
<description>
|
||||
Returns the Reason Phrase for the current (or specified) HTTP Status Error Code.
|
||||
</description>
|
||||
<name>reason</name>
|
||||
<tag-class>net.thauvin.erik.httpstatus.taglibs.ReasonTag</tag-class>
|
||||
<body-content>empty</body-content>
|
||||
<attribute>
|
||||
<description>
|
||||
Default value if the resulting reason is null.
|
||||
</description>
|
||||
<name>default</name>
|
||||
<required>false</required>
|
||||
<rtexprvalue>true</rtexprvalue>
|
||||
</attribute>
|
||||
<attribute>
|
||||
<description>
|
||||
HTTP Status Error Code to be looked up.
|
||||
</description>
|
||||
<name>statusCode</name>
|
||||
<required>false</required>
|
||||
<rtexprvalue>true</rtexprvalue>
|
||||
</attribute>
|
||||
</tag>
|
||||
|
||||
</taglib>
|
48
src/main/resources/reasons.properties
Normal file
48
src/main/resources/reasons.properties
Normal file
|
@ -0,0 +1,48 @@
|
|||
100=Continue
|
||||
101=Switching Protocols
|
||||
102=Processing
|
||||
200=OK
|
||||
201=Created
|
||||
202=Accepted
|
||||
203=Non-Authoritative Information
|
||||
204=No Content
|
||||
205=Reset Content
|
||||
206=Partial Content
|
||||
207=Multi-Status
|
||||
300=Multiple Choices
|
||||
301=Moved Permanently
|
||||
302=Moved Temporarily
|
||||
303=See Other
|
||||
304=Not Modified
|
||||
305=Use Proxy
|
||||
307=Temporary Redirect
|
||||
400=Bad Request
|
||||
401=Unauthorized
|
||||
402=Payment Required
|
||||
403=Forbidden
|
||||
404=Not Found
|
||||
405=Method Not Allowed
|
||||
406=Not Acceptable
|
||||
407=Proxy Authentication Required
|
||||
408=Request Timeout
|
||||
409=Conflict
|
||||
410=Gone
|
||||
411=Length Required
|
||||
412=Precondition Failed
|
||||
413=Request Entity Too Large
|
||||
414=Request-URI Too Long
|
||||
415=Unsupported Media Type
|
||||
416=Requested Range Not Satisfiable
|
||||
417=Expectation Failed
|
||||
419=Insufficient Space on Resource
|
||||
420=Method Failure
|
||||
422=Unprocessable Entity
|
||||
423=Locked
|
||||
424=Failed Dependency
|
||||
500=Internal Server Error
|
||||
501=Not Implemented
|
||||
502=Bad Gateway
|
||||
503=Service Unavailable
|
||||
504=Gateway Timeout
|
||||
505=HTTP Version Not Supported
|
||||
507=Insufficient Storage
|
Loading…
Add table
Add a link
Reference in a new issue