cz.jirutka.spring.http.client.cache.internal.CacheControl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-http-client-cache Show documentation
Show all versions of spring-http-client-cache Show documentation
A very simple HTTP cache for the Spring RestTemplate.
The newest version!
/*
* Copyright 2014 Jakub Jirutka .
*
* Licensed 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.
*/
package cz.jirutka.spring.http.client.cache.internal;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.util.Assert;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Represents a HTTP Cache-Control response header and parses it from string.
*
* Note: This class ignores 1#field-name parameter for
* private and no-cache directive and cache extensions.
*
* @see HTTP/1.1 section 14.9
*/
@Data
@NoArgsConstructor
public class CacheControl {
// copied from org.apache.abdera.protocol.util.CacheControlUtil
private static final Pattern PATTERN
= Pattern.compile("\\s*([\\w\\-]+)\\s*(=)?\\s*(\\-?\\d+|\\\"([^\"\\\\]*(\\\\.[^\"\\\\]*)*)+\\\")?\\s*");
/**
* Corresponds to the max-age cache control directive.
* The default value is -1, i.e. not specified.
*
* @see HTTP/1.1 section 14.9.3
*/
private int maxAge = -1;
/**
* Corresponds to the s-maxage cache control directive.
* The default value is -1, i.e. not specified.
*
* @see HTTP/1.1 section 14.9.3
*/
private int sMaxAge = -1;
/**
* Whether the must-revalidate directive is specified.
* The default value is false.
*
* @see HTTP/1.1 section 14.9.4
*/
private boolean isMustRevalidate = false;
/**
* Whether the no-cache directive is specified.
* The default value is false.
*
* @see HTTP/1.1 section 14.9.1
*/
private boolean isNoCache = false;
/**
* Whether the no-store directive is specified.
* The default value is false.
*
* @see HTTP/1.1 section 14.9.2
*/
private boolean isNoStore = false;
/**
* Whether the no-transform directive is specified.
* The default value is false.
*
* @see HTTP/1.1 section 14.9.5
*/
private boolean isNoTransform = false;
/**
* Whether the private directive is specified.
* The default value is false.
*
* @see HTTP/1.1 section 14.9.1
*/
private boolean isPrivate = false;
/**
* Whether the public directive is specified.
* The default value is false.
*
* @see HTTP/1.1 section 14.9.1
*/
private boolean isPublic = false;
/**
* Whether the proxy-revalidate directive is specified.
* The default value is false.
*
* @see HTTP/1.1 section 14.9.4
*/
private boolean isProxyRevalidate = false;
/**
* Creates a new instance of CacheControl by parsing the supplied string.
*
* @param value A value the Cache-Control header.
*/
public static CacheControl valueOf(String value) {
CacheControl cc = new CacheControl();
if (value != null) {
Matcher matcher = PATTERN.matcher(value);
while (matcher.find()) {
switch (matcher.group(1).toLowerCase()) {
case "max-age":
cc.setMaxAge(Integer.parseInt(matcher.group(3))); break;
case "s-maxage":
cc.setSMaxAge(Integer.parseInt(matcher.group(3))); break;
case "must-revalidate":
cc.setMustRevalidate(true); break;
case "no-cache":
cc.setNoCache(true); break;
case "no-store":
cc.setNoStore(true); break;
case "no-transform":
cc.setNoTransform(true); break;
case "private":
cc.setPrivate(true); break;
case "public":
cc.setPublic(true); break;
case "proxy-revalidate":
cc.setProxyRevalidate(true); break;
default: //ignore
}
}
}
return cc;
}
public static CacheControl parseCacheControl(HttpHeaders headers) {
Assert.notNull(headers, "headers must not be null");
return valueOf(headers.getCacheControl());
}
/**
* Returns max-age, or s-maxage according to whether
* considering a shared cache, or a private cache. If shared cache and the
* s-maxage is negative (i.e. not set), then returns
* max-age instead.
*
* @param sharedCache true for a shared cache,
* or false for a private cache
* @return A {@link #maxAge}, or {@link #sMaxAge} according to the given
* sharedCache argument.
*/
public int getMaxAge(boolean sharedCache) {
if (sharedCache) {
return sMaxAge >= 0 ? sMaxAge : maxAge;
} else {
return maxAge;
}
}
}