Search in sources :

Example 1 with ContentType

use of org.glassfish.grizzly.http.util.ContentType in project Payara by payara.

the class JwtKeyStoreUtils method readKeyFromURL.

private static CacheableString readKeyFromURL(URL keyURL, Duration defaultCacheTTL) throws IOException {
    URLConnection urlConnection = keyURL.openConnection();
    Charset charset = Charset.defaultCharset();
    ContentType contentType = ContentType.newContentType(urlConnection.getContentType());
    if (contentType != null) {
        String charEncoding = contentType.getCharacterEncoding();
        if (charEncoding != null) {
            try {
                if (!Charset.isSupported(charEncoding)) {
                    LOGGER.warning("Charset " + charEncoding + " for remote key not supported, using default charset instead");
                } else {
                    charset = Charset.forName(contentType.getCharacterEncoding());
                }
            } catch (IllegalCharsetNameException ex) {
                LOGGER.severe("Charset " + ex.getCharsetName() + " for remote key not supported, Cause: " + ex.getMessage());
            }
        }
    }
    // There's no guarantee that the response will contain at most one Cache-Control header and at most one max-age
    // directive. Here, we apply the smallest of all max-age directives.
    Duration cacheTTL = urlConnection.getHeaderFields().entrySet().stream().filter(e -> e.getKey() != null && e.getKey().trim().equalsIgnoreCase("Cache-Control")).flatMap(headers -> headers.getValue().stream()).flatMap(headerValue -> Stream.of(headerValue.split(","))).filter(directive -> directive.trim().startsWith("max-age")).map(maxAgeDirective -> {
        String[] keyValue = maxAgeDirective.split("=", 2);
        String maxAge = keyValue[keyValue.length - 1];
        try {
            return Duration.ofSeconds(Long.parseLong(maxAge));
        } catch (NumberFormatException e) {
            return null;
        }
    }).filter(Objects::nonNull).min(Duration::compareTo).orElse(defaultCacheTTL);
    try (InputStream inputStream = urlConnection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset))) {
        String keyContents = reader.lines().collect(Collectors.joining(System.lineSeparator()));
        return CacheableString.from(keyContents, cacheTTL);
    }
}
Also used : URL(java.net.URL) Thread.currentThread(java.lang.Thread.currentThread) JsonValue(javax.json.JsonValue) ByteArrayInputStream(java.io.ByteArrayInputStream) Charset(java.nio.charset.Charset) URLConnection(java.net.URLConnection) Duration(java.time.Duration) Json(javax.json.Json) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) JsonObject(javax.json.JsonObject) JsonReader(javax.json.JsonReader) MalformedURLException(java.net.MalformedURLException) JsonArray(javax.json.JsonArray) IOException(java.io.IOException) Config(org.eclipse.microprofile.config.Config) Logger(java.util.logging.Logger) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Base64(java.util.Base64) ContentType(org.glassfish.grizzly.http.util.ContentType) Stream(java.util.stream.Stream) StringReader(java.io.StringReader) Optional(java.util.Optional) BufferedReader(java.io.BufferedReader) InputStream(java.io.InputStream) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) ContentType(org.glassfish.grizzly.http.util.ContentType) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Objects(java.util.Objects) BufferedReader(java.io.BufferedReader) Charset(java.nio.charset.Charset) Duration(java.time.Duration) URLConnection(java.net.URLConnection)

Aggregations

BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 StringReader (java.io.StringReader)1 Thread.currentThread (java.lang.Thread.currentThread)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Charset (java.nio.charset.Charset)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 Duration (java.time.Duration)1 Base64 (java.util.Base64)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Json (javax.json.Json)1