java.net.http.HttpHeaders Java Examples

The following examples show how to use java.net.http.HttpHeaders. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From Java-Coding-Problems with MIT License 6 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {

        HttpClient client = HttpClient.newHttpClient();

        HttpRequest request = HttpRequest.newBuilder()
                .headers("Content-Type", "application/json")
                .header("Referer", "https://reqres.in/")
                .setHeader("X-Auth", "authtoken")
                .uri(URI.create("https://reqres.in/api/users/2"))
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        HttpHeaders allHeaders = response.headers();        
        System.out.println("All headers: " + allHeaders);                       
        
        List<String> allValuesOfCacheControl = response.headers().allValues("Cache-Control");                        
        System.out.println("All values of Cache-Control: " + allValuesOfCacheControl);                       
        
        Optional<String> firstValueOfCacheControl = response.headers().firstValue("Cache-Control");
        System.out.println("First value of Cache-Control: " + firstValueOfCacheControl);                       
    }
 
Example #2
Source File: AbstractRequester.java    From catnip with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void updateBucket(@Nonnull final Route route, @Nonnull final HttpHeaders headers, final long retryAfter,
                            final long timeDifference) {
    final Optional<Long> rateLimitReset = headers.firstValue("X-RateLimit-Reset")
            .map(s -> Long.parseLong(s.replace(".", "")));
    final OptionalLong rateLimitRemaining = headers.firstValueAsLong("X-RateLimit-Remaining");
    final OptionalLong rateLimitLimit = headers.firstValueAsLong("X-RateLimit-Limit");
    final Optional<Long> rateLimitResetAfter = headers.firstValue("X-RateLimit-Reset-After")
            .map(s -> Long.parseLong(s.replace(".", "")));
    
    catnip.logAdapter().trace(
            "Updating headers for {} ({}): remaining = {}, limit = {}, reset = {}, retryAfter = {}, timeDifference = {}",
            route, route.ratelimitKey(), rateLimitRemaining.orElse(-1L), rateLimitLimit.orElse(-1L),
            rateLimitReset.orElse(-1L), retryAfter, timeDifference
    );
    
    if(retryAfter > 0) {
        rateLimiter.updateRemaining(route, 0);
        if(catnip.options().restRatelimitsWithoutClockSync() && rateLimitResetAfter.isPresent()) {
            rateLimiter.updateReset(route, System.currentTimeMillis() + timeDifference
                    + rateLimitResetAfter.get());
        } else {
            rateLimiter.updateReset(route, retryAfter);
        }
    }
    
    rateLimitReset.ifPresent(aLong -> rateLimiter.updateReset(route, aLong + timeDifference));
    rateLimitLimit.ifPresent(aLong -> rateLimiter.updateLimit(route, Math.toIntExact(aLong)));
    rateLimitRemaining.ifPresent(aLong -> rateLimiter.updateRemaining(route, Math.toIntExact(aLong)));
    
    rateLimiter.updateDone(route);
}
 
Example #3
Source File: JavaRequestProcessor.java    From milkman with MIT License 4 votes vote down vote up
@Override
public HttpHeaders headers() {
	return HttpHeaders.of(Collections.emptyMap(), (s1,s2) -> true);
}
 
Example #4
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) {
    super(message, throwable);
    this.code = code;
    this.responseHeaders = responseHeaders;
    this.responseBody = responseBody;
}
 
Example #5
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(String message, int code, HttpHeaders responseHeaders, String responseBody) {
    this(message, (Throwable) null, code, responseHeaders, responseBody);
}
 
Example #6
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(String message, Throwable throwable, int code, HttpHeaders responseHeaders) {
    this(message, throwable, code, responseHeaders, null);
}
 
Example #7
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(int code, HttpHeaders responseHeaders, String responseBody) {
    this((String) null, (Throwable) null, code, responseHeaders, responseBody);
}
 
Example #8
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(int code, String message, HttpHeaders responseHeaders, String responseBody) {
    this(code, message);
    this.responseHeaders = responseHeaders;
    this.responseBody = responseBody;
}
 
Example #9
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) {
    super(message, throwable);
    this.code = code;
    this.responseHeaders = responseHeaders;
    this.responseBody = responseBody;
}
 
Example #10
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(String message, int code, HttpHeaders responseHeaders, String responseBody) {
    this(message, (Throwable) null, code, responseHeaders, responseBody);
}
 
Example #11
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(String message, Throwable throwable, int code, HttpHeaders responseHeaders) {
    this(message, throwable, code, responseHeaders, null);
}
 
Example #12
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(int code, HttpHeaders responseHeaders, String responseBody) {
    this((String) null, (Throwable) null, code, responseHeaders, responseBody);
}
 
Example #13
Source File: ApiException.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public ApiException(int code, String message, HttpHeaders responseHeaders, String responseBody) {
    this(code, message);
    this.responseHeaders = responseHeaders;
    this.responseBody = responseBody;
}
 
Example #14
Source File: SendGridMailerTest.java    From alf.io with GNU General Public License v3.0 4 votes vote down vote up
private HttpResponse<Object> createMockHttpResponse() {
    return new HttpResponse<>() {
        @Override
        public int statusCode() {
            return 200;
        }

        @Override
        public HttpRequest request() {
            return null;
        }

        @Override
        public Optional<HttpResponse<Object>> previousResponse() {
            return Optional.empty();
        }

        @Override
        public HttpHeaders headers() {
            return null;
        }

        @Override
        public Object body() {
            return null;
        }

        @Override
        public Optional<SSLSession> sslSession() {
            return Optional.empty();
        }

        @Override
        public URI uri() {
            return null;
        }

        @Override
        public HttpClient.Version version() {
            return null;
        }
    };
}
 
Example #15
Source File: ApiException.java    From openapi-generator with Apache License 2.0 2 votes vote down vote up
/**
 * Get the HTTP response headers.
 *
 * @return Headers as an HttpHeaders object
 */
public HttpHeaders getResponseHeaders() {
    return responseHeaders;
}
 
Example #16
Source File: ApiException.java    From openapi-generator with Apache License 2.0 2 votes vote down vote up
/**
 * Get the HTTP response headers.
 *
 * @return Headers as an HttpHeaders object
 */
public HttpHeaders getResponseHeaders() {
    return responseHeaders;
}