Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,40 @@
*/
public class HeaderConstants {

/**
* @deprecated Use {@link org.apache.hc.core5.http.Method}
*/
@Deprecated
public static final String GET_METHOD = "GET";
/**
* @deprecated Use {@link org.apache.hc.core5.http.Method}
*/
@Deprecated
public static final String HEAD_METHOD = "HEAD";
/**
* @deprecated Use {@link org.apache.hc.core5.http.Method}
*/
@Deprecated
public static final String OPTIONS_METHOD = "OPTIONS";
/**
* @deprecated Use {@link org.apache.hc.core5.http.Method}
*/
@Deprecated
public static final String PUT_METHOD = "PUT";
/**
* @deprecated Use {@link org.apache.hc.core5.http.Method}
*/
@Deprecated
public static final String DELETE_METHOD = "DELETE";
/**
* @deprecated Use {@link org.apache.hc.core5.http.Method}
*/
@Deprecated
public static final String TRACE_METHOD = "TRACE";
/**
* @deprecated Use {@link org.apache.hc.core5.http.Method}
*/
@Deprecated
public static final String POST_METHOD = "POST";

/**
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.hc.client5.http.impl.cache;

import org.apache.hc.client5.http.cache.HttpCacheEntry;

class CacheHit {

final String rootKey;
final String variantKey;
final HttpCacheEntry entry;

public CacheHit(final String rootKey, final String variantKey, final HttpCacheEntry entry) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this class be fully immutable? Also, shouldn't rootKey and entry be null-safe?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturobernalg The class is immutable. It is also internal so I forwent getters and null checks for its instance variables.

this.rootKey = rootKey;
this.variantKey = variantKey;
this.entry = entry;
}

public CacheHit(final String rootKey, final HttpCacheEntry entry) {
this(rootKey, null, entry);
}

public String getEntryKey() {
return variantKey != null ? variantKey : rootKey;
}

@Override
public String toString() {
return "CacheHit{" +
"rootKey='" + rootKey + '\'' +
", variantKey='" + variantKey + '\'' +
", entry=" + entry +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@

import java.net.URI;

import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.utils.URIUtils;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.Method;

class CacheInvalidatorBase {

Expand All @@ -43,11 +43,11 @@ static boolean shouldInvalidateHeadCacheEntry(final HttpRequest req, final HttpC
}

static boolean requestIsGet(final HttpRequest req) {
return req.getMethod().equals((HeaderConstants.GET_METHOD));
return Method.GET.isSame(req.getMethod());
}

static boolean isAHeadCacheEntry(final HttpCacheEntry parentCacheEntry) {
return parentCacheEntry != null && parentCacheEntry.getRequestMethod().equals(HeaderConstants.HEAD_METHOD);
return parentCacheEntry != null && Method.HEAD.isSame(parentCacheEntry.getRequestMethod());
}

static boolean isSameHost(final URI requestURI, final URI targetURI) {
Expand All @@ -60,7 +60,7 @@ static boolean requestShouldNotBeCached(final HttpRequest req) {
}

static boolean notGetOrHeadRequest(final String method) {
return !(HeaderConstants.GET_METHOD.equals(method) || HeaderConstants.HEAD_METHOD.equals(method));
return !(Method.GET.isSame(method) || Method.HEAD.isSame(method));
}

private static URI getLocationURI(final URI requestUri, final HttpResponse response, final String headerName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,26 @@
*/
package org.apache.hc.client5.http.impl.cache;

import org.apache.hc.client5.http.cache.HttpCacheEntry;

/** Records a set of information describing a cached variant. */
class Variant {

private final String cacheKey;
private final HttpCacheEntry entry;

public Variant(final String cacheKey, final HttpCacheEntry entry) {
this.cacheKey = cacheKey;
this.entry = entry;
}
/**
* Represents a full match or a partial match (variant options)
* result of a cache lookup operation.
*/
class CacheMatch {

public String getCacheKey() {
return cacheKey;
}
final CacheHit hit;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ok2c

Shouldn't this class be fully immutable?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arturobernalg It is. All its instance variables are final and immutable. There are no access methods but that does not make the class mutable.

final CacheHit root;

public HttpCacheEntry getEntry() {
return entry;
CacheMatch(final CacheHit hit, final CacheHit root) {
this.hit = hit;
this.root = root;
}

@Override
public String toString() {
return cacheKey;
return "CacheLookupResult{" +
"hit=" + hit +
", root=" + root +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
*/
package org.apache.hc.client5.http.impl.cache;

import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.http.ProtocolVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -57,7 +57,7 @@ public boolean isServableFromCache(final RequestCacheControl cacheControl, final
return false;
}

if (!method.equals(HeaderConstants.GET_METHOD) && !method.equals(HeaderConstants.HEAD_METHOD)) {
if (!Method.GET.isSame(method) && !Method.HEAD.isSame(method)) {
if (LOG.isDebugEnabled()) {
LOG.debug("{} request is not serveable from cache", method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.time.Instant;

import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.cache.Resource;
import org.apache.hc.client5.http.cache.ResourceIOException;
Expand All @@ -41,6 +40,7 @@
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.util.TimeValue;

Expand All @@ -64,7 +64,7 @@ class CachedHttpResponseGenerator {
* @return {@link SimpleHttpResponse} constructed response
*/
SimpleHttpResponse generateResponse(final HttpRequest request, final HttpCacheEntry entry) throws ResourceIOException {
final Instant now =Instant.now();
final Instant now = Instant.now();
final SimpleHttpResponse response = new SimpleHttpResponse(entry.getStatus());
response.setVersion(HttpVersion.DEFAULT);

Expand Down Expand Up @@ -160,7 +160,7 @@ private void generateContentLength(final HttpResponse response, final byte[] bod
}

private boolean responseShouldContainEntity(final HttpRequest request, final HttpCacheEntry cacheEntry) {
return request.getMethod().equals(HeaderConstants.GET_METHOD) && cacheEntry.getResource() != null;
return Method.GET.isSame(request.getMethod()) && cacheEntry.getResource() != null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import java.time.Instant;
import java.util.Iterator;

import org.apache.hc.client5.http.cache.HeaderConstants;
import org.apache.hc.client5.http.cache.HttpCacheEntry;
import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HeaderElement;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.Method;
import org.apache.hc.core5.http.message.MessageSupport;
import org.apache.hc.core5.util.TimeValue;
import org.slf4j.Logger;
Expand Down Expand Up @@ -176,7 +176,11 @@ public boolean canCachedResponseBeUsed(final RequestCacheControl requestCacheCon
}

private boolean isGet(final HttpRequest request) {
return request.getMethod().equals(HeaderConstants.GET_METHOD);
return Method.GET.isSame(request.getMethod());
}

private boolean isHead(final HttpRequest request) {
return Method.HEAD.isSame(request.getMethod());
}

private boolean entryIsNotA204Response(final HttpCacheEntry entry) {
Expand All @@ -201,17 +205,18 @@ public boolean isConditional(final HttpRequest request) {
}

/**
* Determines whether the given request is a {@link HeaderConstants#GET_METHOD} request and the associated cache entry was created by a
* {@link HeaderConstants#HEAD_METHOD} request.
* Determines whether the given request is a {@link org.apache.hc.core5.http.Method#GET} request and the
* associated cache entry was created by a {@link org.apache.hc.core5.http.Method#HEAD} request.
*
* @param request The {@link HttpRequest} to check if it is a {@link HeaderConstants#GET_METHOD} request.
* @param entry The {@link HttpCacheEntry} to check if it was created by a {@link HeaderConstants#HEAD_METHOD} request.
* @return true if the request is a {@link HeaderConstants#GET_METHOD} request and the cache entry was created by a
* {@link HeaderConstants#HEAD_METHOD} request, otherwise {@code false}.
* @param request The {@link HttpRequest} to check if it is a {@link org.apache.hc.core5.http.Method#GET} request.
* @param entry The {@link HttpCacheEntry} to check if it was created by
* a {@link org.apache.hc.core5.http.Method#HEAD} request.
* @return true if the request is a {@link org.apache.hc.core5.http.Method#GET} request and the cache entry was
* created by a {@link org.apache.hc.core5.http.Method#HEAD} request, otherwise {@code false}.
* @since 5.3
*/
public boolean isGetRequestWithHeadCacheEntry(final HttpRequest request, final HttpCacheEntry entry) {
return isGet(request) && HeaderConstants.HEAD_METHOD.equalsIgnoreCase(entry.getRequestMethod());
return isGet(request) && Method.HEAD.isSame(entry.getRequestMethod());
}


Expand Down
Loading