SEC-2230: Added Cache Control support
This commit is contained in:
+71
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.security.web.headers;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.security.web.util.RequestMatcher;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Delegates to the provided {@link HeaderWriter} when
|
||||
* {@link RequestMatcher#matches(HttpServletRequest)} returns true.
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @since 3.2
|
||||
*/
|
||||
public class DelegatingRequestMatcherHeaderWriter implements HeaderWriter {
|
||||
private final RequestMatcher requestMatcher;
|
||||
|
||||
private final HeaderWriter delegateHeaderWriter;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
*
|
||||
* @param requestMatcher
|
||||
* the {@link RequestMatcher} to use. If returns true, the
|
||||
* delegateHeaderWriter will be invoked.
|
||||
* @param delegateHeaderWriter
|
||||
* the {@link HeaderWriter} to invoke if the
|
||||
* {@link RequestMatcher} returns true.
|
||||
*/
|
||||
public DelegatingRequestMatcherHeaderWriter(RequestMatcher requestMatcher,
|
||||
HeaderWriter delegateHeaderWriter) {
|
||||
Assert.notNull(requestMatcher, "requestMatcher cannot be null");
|
||||
Assert.notNull(delegateHeaderWriter, "delegateHeaderWriter cannot be null");
|
||||
this.requestMatcher = requestMatcher;
|
||||
this.delegateHeaderWriter = delegateHeaderWriter;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.security.web.headers.HeaderWriter#writeHeaders(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
||||
*/
|
||||
@Override
|
||||
public void writeHeaders(HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
if(requestMatcher.matches(request)) {
|
||||
delegateHeaderWriter.writeHeaders(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getName()+ " [requestMatcher="
|
||||
+ requestMatcher + ", delegateHeaderWriter="
|
||||
+ delegateHeaderWriter + "]";
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import org.springframework.util.Assert;
|
||||
/**
|
||||
* Represents a Header to be added to the {@link HttpServletResponse}
|
||||
*/
|
||||
final class Header {
|
||||
public final class Header {
|
||||
|
||||
private final String headerName;
|
||||
private final List<String> headerValues;
|
||||
|
||||
+26
-4
@@ -1,30 +1,52 @@
|
||||
package org.springframework.security.web.headers;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@code HeaderWriter} implementation which writes the same {@code Header} instance.
|
||||
*
|
||||
* @author Marten Deinum
|
||||
* @author Rob Winch
|
||||
* @since 3.2
|
||||
*/
|
||||
public class StaticHeadersWriter implements HeaderWriter {
|
||||
|
||||
private final Header header;
|
||||
private final List<Header> headers;
|
||||
|
||||
/**
|
||||
* Creates a new instance
|
||||
* @param headers the {@link Header} instances to use
|
||||
*/
|
||||
public StaticHeadersWriter(List<Header> headers) {
|
||||
Assert.notEmpty(headers,"headers cannot be null or empty");
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with a single header
|
||||
* @param headerName the name of the header
|
||||
* @param headerValues the values for the header
|
||||
*/
|
||||
public StaticHeadersWriter(String headerName, String... headerValues) {
|
||||
header = new Header(headerName, headerValues);
|
||||
this(Collections.singletonList(new Header(headerName, headerValues)));
|
||||
}
|
||||
|
||||
public void writeHeaders(HttpServletRequest request, HttpServletResponse response) {
|
||||
for(String value : header.getValues()) {
|
||||
response.addHeader(header.getName(), value);
|
||||
for(Header header : headers) {
|
||||
for(String value : header.getValues()) {
|
||||
response.addHeader(header.getName(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getName() + " [headers=" + headers + "]";
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.security.web.headers;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.security.web.util.RequestMatcher;
|
||||
|
||||
/**
|
||||
* @author Rob Winch
|
||||
*
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DelegatingRequestMatcherHeaderWriterTests {
|
||||
@Mock
|
||||
private RequestMatcher matcher;
|
||||
|
||||
@Mock
|
||||
private HeaderWriter delegate;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
private DelegatingRequestMatcherHeaderWriter headerWriter;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
request = new MockHttpServletRequest();
|
||||
response = new MockHttpServletResponse();
|
||||
headerWriter = new DelegatingRequestMatcherHeaderWriter(matcher, delegate);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorNullRequestMatcher() {
|
||||
new DelegatingRequestMatcherHeaderWriter(null, delegate);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorNullDelegate() {
|
||||
new DelegatingRequestMatcherHeaderWriter(matcher, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeHeadersOnMatch() {
|
||||
when(matcher.matches(request)).thenReturn(true);
|
||||
|
||||
headerWriter.writeHeaders(request, response);
|
||||
|
||||
verify(delegate).writeHeaders(request, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeHeadersOnNoMatch() {
|
||||
when(matcher.matches(request)).thenReturn(false);
|
||||
|
||||
headerWriter.writeHeaders(request, response);
|
||||
|
||||
verify(delegate, times(0)).writeHeaders(request, response);
|
||||
}
|
||||
}
|
||||
+24
@@ -18,6 +18,7 @@ package org.springframework.security.web.headers;
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -41,6 +42,16 @@ public class StaticHeaderWriterTests {
|
||||
response = new MockHttpServletResponse();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorNullHeaders() {
|
||||
new StaticHeadersWriter(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorEmptyHeaders() {
|
||||
new StaticHeadersWriter(Collections.<Header>emptyList());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void constructorNullHeaderName() {
|
||||
new StaticHeadersWriter(null, "value1");
|
||||
@@ -65,4 +76,17 @@ public class StaticHeaderWriterTests {
|
||||
factory.writeHeaders(request, response);
|
||||
assertThat(response.getHeaderValues(headerName)).isEqualTo(Arrays.asList(headerValue));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeHeadersMulti() {
|
||||
Header pragma = new Header("Pragma","no-cache");
|
||||
Header cacheControl= new Header("Cache-Control","no-cache","no-store","must-revalidate");
|
||||
StaticHeadersWriter factory = new StaticHeadersWriter(Arrays.asList(pragma, cacheControl));
|
||||
|
||||
factory.writeHeaders(request, response);
|
||||
|
||||
assertThat(response.getHeaderNames().size()).isEqualTo(2);
|
||||
assertThat(response.getHeaderValues(pragma.getName())).isEqualTo(pragma.getValues());
|
||||
assertThat(response.getHeaderValues(cacheControl.getName())).isEqualTo(cacheControl.getValues());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user