Sort command line output. Closes #10

Updated copyright

Code cleanup
This commit is contained in:
Erik C. Thauvin 2024-06-07 14:12:11 -07:00
parent 8c79c0e17a
commit 053dcba26b
Signed by: erik
GPG key ID: 776702A6A2DA330E
17 changed files with 100 additions and 85 deletions

2
.idea/misc.xml generated
View file

@ -1,9 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<pattern value="net.thauvin.erik.httpstatus.HttpStatusBuild" />
<pattern value="net.thauvin.erik.httpstatus.HttpStatusBuild" method="pmd" />
<pattern value="net.thauvin.erik.httpstatus.HttpStatusBuild" method="jacoco" />
<pattern value="net.thauvin.erik.httpstatus.taglibs.ReasonTag" method="setCode" />
<pattern value="net.thauvin.erik.httpstatus.HttpStatusBuild" method="pmdCli" />
</component>
<component name="PDMPlugin">
<option name="customRuleSets">

View file

@ -2,7 +2,7 @@
[![License (3-Clause BSD)](https://img.shields.io/badge/license-BSD%203--Clause-blue.svg?style=flat-square)](http://opensource.org/licenses/BSD-3-Clause)
[![Java](https://img.shields.io/badge/java-17%2B-blue)](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html)
[![bld](https://img.shields.io/badge/1.8.0-FA9052?label=bld&labelColor=2392FF)](https://rife2.com/bld)
[![bld](https://img.shields.io/badge/1.9.1-FA9052?label=bld&labelColor=2392FF)](https://rife2.com/bld)
[![Release](https://img.shields.io/github/release/ethauvin/httpstatus.svg)](https://github.com/ethauvin/httpstatus/releases/latest)
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/net.thauvin.erik.httpstatus/httpstatus?label=sanpshot&server=https%3A%2F%2Foss.sonatype.org)](https://oss.sonatype.org/content/repositories/snapshots/net/thauvin/erik/httpstatus/httpstatus/)
[![Maven Central](https://img.shields.io/maven-central/v/net.thauvin.erik.httpstatus/httpstatus.svg?color=blue)](https://central.sonatype.com/artifact/net.thauvin.erik.httpstatus/httpstatus)
@ -17,11 +17,11 @@ A simple [JSP](http://www.oracle.com/technetwork/java/javaee/jsp/index.html) Tag
## Table of Contents
- [Examples](#examples)
- Usage
- [Usage](#usage)
- [Gradle](#gradle)
- [bld](#bld)
- [Maven](#maven)
- JSP Tags
- [JSP Tags](#jsp-tags)
- [hs:cause](#hscause)
- [hs:code](#hscode)
- [hs:message](#hsmessage)
@ -111,7 +111,7 @@ The `<hs:cause/>` tag displays the cause of current HTTP status code, if any. A
Optional attributes are:
| Attribute | Description |
|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `default` | The fallback value to output, if no cause is |
| `escapeXml` | Converts &lt;, &gt;, &amp;, ', " to their corresponding [entity codes](http://dev.w3.org/html5/html-author/charref). Value is `true` by default. |
@ -134,7 +134,7 @@ The `<hs:message/>` tag displays the current error message, if any. A shorthand
Optional attributes are:
| Attribute | Description |
|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `default` | The fallback value to output, if no error message is available. |
| `escapeXml` | Converts &lt;, &gt;, &amp;, ', " to their corresponding [entity codes](http://dev.w3.org/html5/html-author/charref). Value is `true` by default. |
@ -143,7 +143,7 @@ Optional attributes are:
The `<hs:reason/>` tag displays the reason for an HTTP status code, if any. Optional attributes are:
| Attribute | Description |
|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `default` | The fallback value to output, if no reason is available. |
| `code` | The HTTP status error code. If not specified the current status code is used. |
| `escapeXml` | Converts &lt;, &gt;, &amp;, ', " to their corresponding [entity codes](http://dev.w3.org/html5/html-author/charref). Value is `true` by default. |
@ -191,7 +191,7 @@ public class ExampleServlet extends HttpServlet {
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. |
@ -206,11 +206,16 @@ The `StatusCode` bean methods are:
The reasons are defined in a [ResourceBundle](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ResourceBundle.html) properties as follows:
| Status Code | Reason |
|-------------|------------------------------------------------------------|
| ----------- | ---------------------------------------------------------- |
| `100` | Continue |
| `101` | Switching Protocols |
| `102` | Processing |
| `103` | Early Hints |
| `110` | Response is Stale |
| `111` | Revalidation Failed |
| `112` | Disconnected Operation |
| `113` | Heuristic Expiration |
| `199` | Miscellaneous Warning |
| `200` | OK |
| `201` | Created |
| `202` | Accepted |
@ -220,15 +225,17 @@ The reasons are defined in a [ResourceBundle](https://docs.oracle.com/en/java/ja
| `206` | Partial Content |
| `207` | Multi-Status |
| `208` | Already Reported |
| `214` | Transformation Applied |
| `218` | This is fine |
| `226` | IM Used |
| `299` | Miscellaneous Persistent Warning |
| `300` | Multiple Choices |
| `301` | Moved Permanently |
| `302` | Found/Moved Temporarily |
| `303` | See Other |
| `304` | Not Modified |
| `305` | Use Proxy |
| `306` | Switch Proxy |
| `306` | Unused |
| `307` | Temporary Redirect |
| `308` | Permanent Redirect |
| `400` | Bad Request |
@ -244,18 +251,19 @@ The reasons are defined in a [ResourceBundle](https://docs.oracle.com/en/java/ja
| `410` | Gone |
| `411` | Length Required |
| `412` | Precondition Failed |
| `413` | Request Entity/Payload Too Large |
| `414` | Request-URI Too Long |
| `413` | Payload Too Large |
| `414` | URI Too Long |
| `415` | Unsupported Media Type |
| `416` | Requested Range Not Satisfiable |
| `416` | 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 |
| `422` | Unprocessable Content |
| `423` | Locked |
| `424` | Failed Dependency |
| `425` | Too Early |
| `426` | Upgrade Required |
| `428` | Precondition Required |
| `429` | Too Many Requests |
@ -270,7 +278,7 @@ The reasons are defined in a [ResourceBundle](https://docs.oracle.com/en/java/ja
| `463` | X-Forwarded-For Header with More than 30 IP Addresses |
| `494` | Request Header Too Large |
| `495` | SSL Certificate Error |
| `496` | No SSL Certificate |
| `496` | SSL Certificate Required |
| `497` | HTTP Request Sent to HTTPS Port |
| `498` | Token Expired/Invalid |
| `499` | Client Closed Request |
@ -288,7 +296,7 @@ The reasons are defined in a [ResourceBundle](https://docs.oracle.com/en/java/ja
| `511` | Network Authentication Required |
| `520` | Unknown Error |
| `521` | Web Server Is Down |
| `522` | Origin Connection Time-out |
| `522` | Connection Timed Out |
| `523` | Origin Is Unreachable |
| `524` | A Timeout Occurred |
| `525` | SSL Handshake Failed |
@ -296,8 +304,11 @@ The reasons are defined in a [ResourceBundle](https://docs.oracle.com/en/java/ja
| `527` | Railgun Error |
| `529` | Site is overloaded |
| `530` | Site is frozen |
| `540` | Temporarily Disabled |
| `561` | Unauthorized |
| `598` | Network Read Timeout Error |
| `599` | Network Connect Timeout Error |
| `783` | Unexpected Token |
## Command Line Usage
@ -317,6 +328,11 @@ $ java -jar httpstatus-1.1.0.jar
101: Switching Protocols
102: Processing
103: Early Hints
110: Response is Stale
111: Revalidation Failed
112: Disconnected Operation
113: Heuristic Expiration
199: Miscellaneous Warning
200: OK
201: Created
202: Accepted
@ -332,7 +348,7 @@ $ java -jar httpstatus-1.1.0.jar 2xx
201: Created
202: Accepted
203: Non-Authoritative Information
...
```
## Contributing

View file

@ -1,7 +1,7 @@
/*
* HttpStatusBuild.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -140,6 +140,11 @@ public class HttpStatusBuild extends Project {
pmdOp.includeLineNumber(false).execute();
}
private void pomRoot() throws FileUtilsErrorException {
PomBuilder.generateInto(publishOperation().info(), dependencies(),
Path.of(workDirectory.getPath(), "pom.xml").toFile());
}
@Override
public void publish() throws Exception {
super.publish();
@ -151,9 +156,4 @@ public class HttpStatusBuild extends Project {
super.publishLocal();
pomRoot();
}
private void pomRoot() throws FileUtilsErrorException {
PomBuilder.generateInto(publishOperation().info(), dependencies(),
Path.of(workDirectory.getPath(), "pom.xml").toFile());
}
}

View file

@ -1,7 +1,7 @@
/*
* Reasons.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -34,6 +34,7 @@ package net.thauvin.erik.httpstatus;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
/**
@ -56,8 +57,8 @@ public final class Reasons {
// Initializes the reason phrases map.
static {
final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_BASENAME);
for (final String key : bundle.keySet()) {
var bundle = ResourceBundle.getBundle(BUNDLE_BASENAME);
for (var key : bundle.keySet()) {
REASON_PHRASES.put(key, bundle.getString(key));
}
}
@ -75,7 +76,7 @@ public final class Reasons {
* @param statusCode The status code.
* @return The reason phrase, or <code>null</code>.
*/
public static String getReasonPhrase(final int statusCode) {
public static String getReasonPhrase(int statusCode) {
return getReasonPhrase(Integer.toString(statusCode));
}
@ -85,7 +86,7 @@ public final class Reasons {
* @param statusCode The status code.
* @return The reason phrase, or <code>null</code>.
*/
public static String getReasonPhrase(final String statusCode) {
public static String getReasonPhrase(String statusCode) {
return REASON_PHRASES.get(statusCode);
}
@ -95,29 +96,26 @@ public final class Reasons {
* @param args The status code(s) or response class(es), prints all if none.
*/
@SuppressWarnings("PMD.SystemPrintln")
public static void main(final String... args) {
public static void main(String... args) {
var keys = new TreeSet<>(REASON_PHRASES.keySet());
if (args.length >= 1) {
for (final String key : args) {
for (var key : args) {
if (key.endsWith("xx")) {
var responseClass = key.charAt(0);
REASON_PHRASES.forEach((k, v) -> {
keys.forEach((k) -> {
if (k.charAt(0) == responseClass) {
System.out.println(k + ": " + v);
System.out.println(k + ": " + REASON_PHRASES.get(k));
}
});
} else {
final String value = REASON_PHRASES.get(key);
var value = REASON_PHRASES.get(key);
if (value != null) {
System.out.println(key + ": " + value);
}
}
}
} else {
REASON_PHRASES.forEach((k, v) -> {
if (v != null) {
System.out.println(k + ": " + v);
}
});
keys.forEach((k) -> System.out.println(k + ": " + REASON_PHRASES.get(k)));
System.out.println("Total: " + REASON_PHRASES.size());
}
}

View file

@ -1,7 +1,7 @@
/*
* StatusCode.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -58,7 +58,7 @@ public class StatusCode implements Serializable {
*
* @param code The status code.
*/
public StatusCode(final int code) {
public StatusCode(int code) {
this.code = code;
}
@ -147,7 +147,7 @@ public class StatusCode implements Serializable {
*
* @param code The HTTP status code.
*/
public void setCode(final int code) {
public void setCode(int code) {
this.code = code;
}
}

View file

@ -1,7 +1,7 @@
/*
* Utils.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -59,11 +59,11 @@ public final class Utils {
* @param value The string value to convert.
* @return The converted string value.
*/
public static String escapeXml(final String value) {
final StringBuilder escaped = new StringBuilder();
public static String escapeXml(String value) {
var escaped = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
final char c = value.charAt(i);
for (var i = 0; i < value.length(); i++) {
var c = value.charAt(i);
switch (c) {
case '<' -> escaped.append("&lt;");
case '>' -> escaped.append("&gt;");
@ -86,7 +86,7 @@ public final class Utils {
* @param xml The {@link #escapeXml(String) xml} flag.
* @throws IOException If an I/O error occurs.
*/
public static void outWrite(final Writer out, final String value, final String defaultValue, final boolean xml)
public static void outWrite(Writer out, String value, String defaultValue, boolean xml)
throws IOException {
if (value != null) {
out.write(xml ? escapeXml(value) : value);

View file

@ -1,7 +1,7 @@
/*
* CauseTag.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,10 +32,11 @@
package net.thauvin.erik.httpstatus.taglibs;
import jakarta.servlet.jsp.PageContext;
import java.io.IOException;
import net.thauvin.erik.httpstatus.Utils;
import jakarta.servlet.jsp.JspWriter;
import jakarta.servlet.jsp.PageContext;
import net.thauvin.erik.httpstatus.Utils;
import java.io.IOException;
/**
@ -51,12 +52,10 @@ public class CauseTag extends XmlSupport {
*/
@Override
public void doTag() throws IOException {
final PageContext pageContext = (PageContext) getJspContext();
final JspWriter out = pageContext.getOut();
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
final Throwable cause = pageContext.getErrorData().getThrowable().getCause();
Throwable cause = pageContext.getErrorData().getThrowable().getCause();
Utils.outWrite(out, getCause(cause), defaultValue, escapeXml);
}

View file

@ -1,7 +1,7 @@
/*
* CodeTag.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -51,8 +51,8 @@ public class CodeTag extends SimpleTagSupport {
*/
@Override
public void doTag() throws IOException {
final PageContext pageContext = (PageContext) getJspContext();
final JspWriter out = pageContext.getOut();
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
out.write(String.valueOf(pageContext.getErrorData().getStatusCode()));
}

View file

@ -1,7 +1,7 @@
/*
* MessageTag.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -51,10 +51,10 @@ public class MessageTag extends XmlSupport {
*/
@Override
public void doTag() throws IOException {
final PageContext pageContext = (PageContext) getJspContext();
final JspWriter out = pageContext.getOut();
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
final String message = (String) pageContext.getRequest().getAttribute(
String message = (String) pageContext.getRequest().getAttribute(
jakarta.servlet.RequestDispatcher.ERROR_MESSAGE);
Utils.outWrite(out, message, defaultValue, escapeXml);

View file

@ -1,7 +1,7 @@
/*
* ReasonTag.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -55,8 +55,8 @@ public class ReasonTag extends XmlSupport {
*/
@Override
public void doTag() {
final PageContext pageContext = (PageContext) getJspContext();
final JspWriter out = pageContext.getOut();
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();
try {
if (statusCode > -1) {
@ -75,7 +75,7 @@ public class ReasonTag extends XmlSupport {
*
* @param statusCode The status code.
*/
public void setCode(final int statusCode) {
public void setCode(int statusCode) {
this.statusCode = statusCode;
}
}

View file

@ -1,7 +1,7 @@
/*
* XmlSupport.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -58,7 +58,7 @@ public abstract class XmlSupport extends SimpleTagSupport {
* @param defaultValue The default value.
*/
@SuppressWarnings("unused")
public void setDefault(final String defaultValue) {
public void setDefault(String defaultValue) {
this.defaultValue = defaultValue;
}
@ -68,7 +68,7 @@ public abstract class XmlSupport extends SimpleTagSupport {
* @param escapeXml <code>true</code> or <code>false</code>
*/
@SuppressWarnings("unused")
public void setEscapeXml(final boolean escapeXml) {
public void setEscapeXml(boolean escapeXml) {
this.escapeXml = escapeXml;
}
}

View file

@ -2,7 +2,7 @@
<!--
~ httpstatus.tld
~
~ Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
~ Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
~ All rights reserved.
~
~ Redistribution and use in source and binary forms, with or without
@ -37,7 +37,7 @@
<description>HttpStatus JSP Tag Library</description>
<display-name>HttpStatus JSP Tags</display-name>
<tlib-version>1.1.0</tlib-version>
<tlib-version>1.1.1</tlib-version>
<short-name>hs</short-name>
<uri>http://erik.thauvin.net/taglibs/httpstatus</uri>

View file

@ -1,7 +1,7 @@
/*
* CauseTagTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without

View file

@ -1,7 +1,7 @@
/*
* ReasonsMainTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without

View file

@ -1,7 +1,7 @@
/*
* ReasonsTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -48,8 +48,8 @@ import static org.assertj.core.api.Assertions.assertThat;
class ReasonsTest {
@Test
void testGetReasonPhrase() {
final ResourceBundle bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
for (final String key : bundle.keySet()) {
var bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
for (var key : bundle.keySet()) {
assertThat(Reasons.getReasonPhrase(key)).as("getReasonPhrase(" + key + ')').isEqualTo(bundle.getString(key));
assertThat(Reasons.getReasonPhrase(Integer.parseInt(key)))
.as("getReasonPhrase(int: " + key + ')').isEqualTo(bundle.getString(key));

View file

@ -1,7 +1,7 @@
/*
* StatusCodeTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -47,10 +47,10 @@ import static org.assertj.core.api.Assertions.assertThat;
class StatusCodeTest {
@Test
void testStatusCode() {
final ResourceBundle bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
StatusCode statusCode = new StatusCode();
for (final String key : bundle.keySet()) {
final int code = Integer.parseInt(key);
var bundle = ResourceBundle.getBundle(Reasons.BUNDLE_BASENAME);
var statusCode = new StatusCode();
for (var key : bundle.keySet()) {
int code = Integer.parseInt(key);
statusCode.setCode(code);
assertThat(statusCode.getCode()).as("is not " + code).isEqualTo(code);
assertThat(statusCode.isInfo()).as(code + " is info").isEqualTo(code >= 100 && code < 200);
@ -65,8 +65,8 @@ class StatusCodeTest {
.isEqualTo(Reasons.getReasonPhrase(code));
}
final int[] unknowns = {0, 99, 600};
for (final int code : unknowns) {
int[] unknowns = {0, 99, 600};
for (var code : unknowns) {
statusCode.setCode(code);
assertThat(statusCode.getCode()).as("is not " + code).isEqualTo(code);
assertThat(statusCode.isInfo()).as(code + " is info").isFalse();

View file

@ -1,7 +1,7 @@
/*
* UtilsTest.java
*
* Copyright 2015-2023 Erik C. Thauvin (erik@thauvin.net)
* Copyright 2015-2024 Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -59,7 +59,7 @@ class UtilsTest {
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@Test
void testOutWrite() throws IOException {
try (StringWriter sw = new StringWriter()) {
try (var sw = new StringWriter()) {
Utils.outWrite(sw, null, "default", false);
assertThat(sw.toString()).isEqualTo("default").as("outWrite(default)");