11 changed files with 323 additions and 39 deletions
@ -0,0 +1,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 + "]"; |
||||
} |
||||
} |
||||
@ -1,30 +1,52 @@
@@ -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 + "]"; |
||||
} |
||||
} |
||||
@ -0,0 +1,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); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue