Minor cleanup

This commit is contained in:
Erik C. Thauvin 2023-03-19 03:19:36 -07:00
parent b218313ed3
commit b34363fccf
2 changed files with 20 additions and 14 deletions

View file

@ -38,8 +38,8 @@ This project provides a collection of useful template renderers.
|:--------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------| |:--------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------|
| [rife.render.formatCreditcard](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.FormatCreditCard) | Formats a template credit card number value to the last 4 digits | | [rife.render.formatCreditcard](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.FormatCreditCard) | Formats a template credit card number value to the last 4 digits |
| [rife.render.Normalize](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Normalize) | Normalizes a template value for inclusion in a URL path | | [rife.render.Normalize](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Normalize) | Normalizes a template value for inclusion in a URL path |
| [rife.render.QrCode](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.QrCode) | Generate a SVG QR Code from a template value | | [rife.render.QrCode](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.QrCode) | Generates an SVG QR Code from a template value |
| [rife.render.ShortenUrl](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.ShortenUrl) | Shortens a template value | | [rife.render.ShortenUrl](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.ShortenUrl) | Shortens a template value URL |
| [rife.render.Uptime](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Uptime) | Renders the server uptime in various customizable formats | | [rife.render.Uptime](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Uptime) | Renders the server uptime in various customizable formats |
@ -57,4 +57,4 @@ This project provides a collection of useful template renderers.
## Documentation ## Documentation
Read more in the [full documenation](https://github.com/rife2/rife2-template-renderers/wiki). Read more in the [full documenation](https://github.com/rife2/rife2-template-renderers/wiki).

View file

@ -35,6 +35,8 @@ import java.util.concurrent.TimeUnit;
* Collection of utility-type methods commonly used by the renderers. * Collection of utility-type methods commonly used by the renderers.
*/ */
public final class RenderUtils { public final class RenderUtils {
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0";
private RenderUtils() { private RenderUtils() {
// no-op // no-op
} }
@ -184,11 +186,8 @@ public final class RenderUtils {
String.format("https://api.qrserver.com/v1/create-qr-code/?format=svg&size=%s&data=%s", size, String.format("https://api.qrserver.com/v1/create-qr-code/?format=svg&size=%s&data=%s", size,
StringUtils.encodeUrl(src.trim()))) StringUtils.encodeUrl(src.trim())))
.openConnection(); .openConnection();
connection.setRequestProperty( connection.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
"User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0" if (validResponseCode(connection.getResponseCode())) {
);
var responseCode = connection.getResponseCode();
if (responseCode >= 200 && responseCode <= 399) {
try (var inputStream = connection.getInputStream()) { try (var inputStream = connection.getInputStream()) {
svg = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); svg = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
} }
@ -256,11 +255,8 @@ public final class RenderUtils {
var connection = (HttpURLConnection) new URL( var connection = (HttpURLConnection) new URL(
String.format("https://is.gd/create.php?format=simple&url=%s", StringUtils.encodeUrl(url.trim()))) String.format("https://is.gd/create.php?format=simple&url=%s", StringUtils.encodeUrl(url.trim())))
.openConnection(); .openConnection();
connection.setRequestProperty( connection.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
"User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0" if (validResponseCode(connection.getResponseCode())) {
);
var responseCode = connection.getResponseCode();
if (responseCode >= 200 && responseCode <= 399) {
try (var inputStream = connection.getInputStream()) { try (var inputStream = connection.getInputStream()) {
shorten = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); shorten = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
} }
@ -418,4 +414,14 @@ public final class RenderUtils {
return sb.toString(); return sb.toString();
} }
}
/**
* Checks whether the specified HTTP response code is valid.
*
* @param code the HTTP response code.
* @return {@code true} if the response code is valid.
*/
public static boolean validResponseCode(int code) {
return code >= 200 && code <= 399;
}
}