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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue