Minor cleanup
This commit is contained in:
parent
b218313ed3
commit
b34363fccf
2 changed files with 20 additions and 14 deletions
|
@ -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.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.ShortenUrl](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.ShortenUrl) | Shortens 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 URL |
|
||||
| [rife.render.Uptime](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Uptime) | Renders the server uptime in various customizable formats |
|
||||
|
||||
|
||||
|
|
|
@ -35,6 +35,8 @@ import java.util.concurrent.TimeUnit;
|
|||
* Collection of utility-type methods commonly used by the renderers.
|
||||
*/
|
||||
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() {
|
||||
// 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,
|
||||
StringUtils.encodeUrl(src.trim())))
|
||||
.openConnection();
|
||||
connection.setRequestProperty(
|
||||
"User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0"
|
||||
);
|
||||
var responseCode = connection.getResponseCode();
|
||||
if (responseCode >= 200 && responseCode <= 399) {
|
||||
connection.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
|
||||
if (validResponseCode(connection.getResponseCode())) {
|
||||
try (var inputStream = connection.getInputStream()) {
|
||||
svg = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
@ -256,11 +255,8 @@ public final class RenderUtils {
|
|||
var connection = (HttpURLConnection) new URL(
|
||||
String.format("https://is.gd/create.php?format=simple&url=%s", StringUtils.encodeUrl(url.trim())))
|
||||
.openConnection();
|
||||
connection.setRequestProperty(
|
||||
"User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0"
|
||||
);
|
||||
var responseCode = connection.getResponseCode();
|
||||
if (responseCode >= 200 && responseCode <= 399) {
|
||||
connection.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
|
||||
if (validResponseCode(connection.getResponseCode())) {
|
||||
try (var inputStream = connection.getInputStream()) {
|
||||
shorten = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
@ -418,4 +414,14 @@ public final class RenderUtils {
|
|||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue