From 9ff860af4041ff87f0182873b508b936f5c75246 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Tue, 16 Mar 2021 09:56:02 -0700 Subject: [PATCH] Added MessageTag. Closes #4 --- README.md | 15 ++ docs/README.html | 209 ++++++++++++------ .../erik/httpstatus/taglibs/MessageTag.java | 63 ++++++ src/main/resources/META-INF/httpstatus.tld | 27 ++- version.properties | 4 +- 5 files changed, 244 insertions(+), 74 deletions(-) create mode 100644 src/main/java/net/thauvin/erik/httpstatus/taglibs/MessageTag.java diff --git a/README.md b/README.md index 8fa5e6c..78f4b57 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,21 @@ The `` tag displays the current HTTP status code, if any. A shorthand <%= pageContext.getErrorData().getStatusCode() %> ``` +## hs:message + +The `` tag displays the cause of current error message, if any. A shorthand for: + +```jsp +<%= request.getAttribute("javax.servlet.error.message") %> +``` + +Optional attributes are: + +Attribute | Description +----------- | ------------------------------------------------------------------------------------------- +`default` | The fallback value to output, if no error message is available. +`escapeXml` | Converts <, >, &, ', " to their corresponding [entity codes](http://dev.w3.org/html5/html-author/charref). Value is `true` by default. + ## hs:reason The `` tag displays the reason for a HTTP status code, if any. Optional attributes are: diff --git a/docs/README.html b/docs/README.html index aaf1876..e127ca7 100644 --- a/docs/README.html +++ b/docs/README.html @@ -5,13 +5,13 @@ HttpStatus JSP Tag Library - - -

HttpStatus JSP Tag Library

release Maven Central Download
License (3-Clause BSD) Known Vulnerabilities Quality Gate Status
-Build Status Build status CircleCI

+Build Status Build status CircleCI

A simple JSP Tag Library to display the code, reason and/or cause for HTTP status codes in JSP error pages.

For example:

<%@ page isErrorPage="true" %>
@@ -125,6 +122,28 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni
 

hs:code

The <hs:code/> tag displays the current HTTP status code, if any. A shorthand for:

+

hs:message

+

The <hs:message/> tag displays the cause of current error message, if any. A shorthand for:

+ +

Optional attributes are:

+ + + + + + + + + + + + + + + + + +
AttributeDescription
defaultThe fallback value to output, if no error message is available.
escapeXmlConverts <, >, &, ', " to their corresponding entity codes. Value is true by default.

hs:reason

The <hs:reason/> tag displays the reason for a HTTP status code, if any. Optional attributes are:

@@ -211,157 +230,165 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni + + + + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + + @@ -387,20 +414,28 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni + + + + + + + + - + - + - + @@ -463,9 +498,41 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -479,36 +546,36 @@ code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warni

Usage with Gradle or Maven

Include the following in your build.gradle file:

dependencies {
-    compile 'net.thauvin.erik.httpstatus:httpstatus:1.0.4'
+    implementation 'net.thauvin.erik.httpstatus:httpstatus:1.0.5'
 }
 

or as a Maven artifact:

- +

Command Line Usage

You can query the reason phrase for status codes as follows:

- +

If no status code is specified, all will be printed:

- + diff --git a/src/main/java/net/thauvin/erik/httpstatus/taglibs/MessageTag.java b/src/main/java/net/thauvin/erik/httpstatus/taglibs/MessageTag.java new file mode 100644 index 0000000..9e7ae9a --- /dev/null +++ b/src/main/java/net/thauvin/erik/httpstatus/taglibs/MessageTag.java @@ -0,0 +1,63 @@ +/* + * CauseTag.java + * + * Copyright (c) 2015-2021, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.thauvin.erik.httpstatus.taglibs; + +import net.thauvin.erik.httpstatus.Utils; + +import javax.servlet.jsp.JspWriter; +import javax.servlet.jsp.PageContext; +import java.io.IOException; + +/** + * The <hs:message> tag returns the message (if any) for the current error. + * + * @author Erik C. Thauvin + * @created 2021-03-16 + * @since 1.0.5 + */ +public class MessageTag extends XmlSupport { + /** + * {@inheritDoc} + */ + @Override + public void doTag() throws IOException { + final PageContext pageContext = (PageContext) getJspContext(); + @SuppressWarnings("PMD.CloseResource") + final JspWriter out = pageContext.getOut(); + + final String message = (String) pageContext.getRequest().getAttribute( + javax.servlet.RequestDispatcher.ERROR_MESSAGE); + + Utils.outWrite(out, message, defaultValue, escapeXml); + } +} diff --git a/src/main/resources/META-INF/httpstatus.tld b/src/main/resources/META-INF/httpstatus.tld index 2b8bb62..2f3d7c1 100644 --- a/src/main/resources/META-INF/httpstatus.tld +++ b/src/main/resources/META-INF/httpstatus.tld @@ -37,7 +37,7 @@ HttpStatus JSP Tag LibraryHttpStatus JSP Tags - 1.0.4 + 1.0.5hshttp://erik.thauvin.net/taglibs/httpstatus @@ -75,6 +75,31 @@ empty + + + Returns the message (if any) for the current error. + + message + net.thauvin.erik.httpstatus.taglibs.MessageTag + empty + + + Default value if the resulting error message is null. + + default + false + true + + + + Converts <, > ,& ,' ," to their corresponding entity codes. Value is true by default. + + escapeXml + false + true + + + Returns the Reason Phrase for the current (or specified) HTTP Status Error Code. diff --git a/version.properties b/version.properties index f17a9fd..106b8b6 100644 --- a/version.properties +++ b/version.properties @@ -3,6 +3,6 @@ version.buildmeta= version.major=1 version.minor=0 -version.patch=4 +version.patch=5 version.prerelease= -version.semver=1.0.4 +version.semver=1.0.5
Already Reported
218This is fine
226 IM Used
300 Multiple Choices
301 Moved Permanently
302 Found/Moved Temporarily
303 See Other
304 Not Modified
305 Use Proxy
306 Switch Proxy
307 Temporary Redirect
308 Permanent 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/Payload Too Large
414 Request-URI Too Long
415 Unsupported Media Type
416 Requested Range Not Satisfiable
417 Expectation Failed
418 I'm A Teapot
419 Insufficient Space on Resource
420 Method Failure
421 Misdirected Request
422 Unprocessable Entity
423 Locked
424 Failed Dependency
426 Upgrade Required
428 Precondition Required
429 Too Many Requests
430Request Header Fields Too Large
431 Request Header Fields Too LargeUnavailable For Legal Reasons
460Client Closed Connection Before Load Balancer Idle Timeout
463X-Forwarded-For Header with More than 30 IP Addresses
494 Request Header Too Large
495Cert ErrorSSL Certificate Error
496No CertNo SSL Certificate
497HTTP to HTTPSHTTP Request Sent to HTTPS Port
498Unknown Error
521Web Server Is Down
522 Origin Connection Time-out
523Origin Is Unreachable
524A Timeout Occurred
525SSL Handshake Failed
526Invalid SSL Certificate
527Railgun Error
529Site is overloaded
530Site is frozen
598 Network Read Timeout Error