Updated Branches: refs/heads/master 342bfe1dc -> a977e7835
CAMEL-6424: camel-netty-http added support for basic auth. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a977e783 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a977e783 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a977e783 Branch: refs/heads/master Commit: a977e78359d98bd668683c3e98f0929e49dfa1d1 Parents: 342bfe1 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Jul 16 16:30:22 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Jul 16 16:30:22 2013 +0200 ---------------------------------------------------------------------- .../netty/http/DefaultNettyHttpBinding.java | 17 +++++++++++++++-- .../component/netty/http/NettyHttpConstants.java | 1 + .../http/handlers/HttpServerChannelHandler.java | 6 ++++++ .../NettyHttpBasicAuthConstraintMapperTest.java | 4 ++-- .../netty/http/NettyHttpBasicAuthTest.java | 1 + ...ttyHttpSimpleBasicAuthConstraintMapperTest.java | 4 ++-- .../netty/http/SpringNettyHttpBasicAuthTest.xml | 8 ++++---- 7 files changed, 31 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a977e783/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java index 8e7579a..64f5b58 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/DefaultNettyHttpBinding.java @@ -25,6 +25,7 @@ import java.net.URLDecoder; import java.nio.charset.Charset; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Map; import org.apache.camel.Exchange; @@ -120,7 +121,7 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding { // strip the starting endpoint path so the path is relative to the endpoint uri String path = uri.getPath(); - if (configuration.getPath() != null && configuration.getPath().startsWith(path)) { + if (configuration.getPath() != null && path.startsWith(configuration.getPath())) { path = path.substring(configuration.getPath().length()); } headers.put(Exchange.HTTP_PATH, path); @@ -132,9 +133,21 @@ public class DefaultNettyHttpBinding implements NettyHttpBinding { for (String name : request.getHeaderNames()) { // mapping the content-type - if (name.toLowerCase().equals("content-type")) { + if (name.toLowerCase(Locale.US).equals("content-type")) { name = Exchange.CONTENT_TYPE; } + + if (name.toLowerCase(Locale.US).equals("authorization")) { + String value = request.getHeader(name); + // store a special header that this request was authenticated using HTTP Basic + if (value != null && value.trim().startsWith("Basic")) { + if (headerFilterStrategy != null + && !headerFilterStrategy.applyFilterToExternalHeaders(NettyHttpConstants.HTTP_AUTHENTICATION, "Basic", exchange)) { + NettyHttpHelper.appendHeader(headers, NettyHttpConstants.HTTP_AUTHENTICATION, "Basic"); + } + } + } + // add the headers one by one, and use the header filter strategy List<String> values = request.getHeaders(name); Iterator<?> it = ObjectHelper.createIterator(values); http://git-wip-us.apache.org/repos/asf/camel/blob/a977e783/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java index 073ba78..df31626 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConstants.java @@ -24,6 +24,7 @@ public final class NettyHttpConstants { public static final String CONTENT_TYPE_JAVA_SERIALIZED_OBJECT = "application/x-java-serialized-object"; public static final String CONTENT_TYPE_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; public static final String HTTP_RESPONSE_TEXT = "CamelHttpResponseText"; + public static final String HTTP_AUTHENTICATION = "CamelHttpAuthentication"; private NettyHttpConstants() { } http://git-wip-us.apache.org/repos/asf/camel/blob/a977e783/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java index c2e3731..37c60a1 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java @@ -128,6 +128,12 @@ public class HttpServerChannelHandler extends ServerChannelHandler { URI uri = new URI(request.getUri()); String target = uri.getPath(); + // strip the starting endpoint path so the target is relative to the endpoint uri + String path = consumer.getConfiguration().getPath(); + if (path != null && target.startsWith(path)) { + target = target.substring(path.length()); + } + // is it a restricted resource? String roles; if (security.getSecurityConstraint() != null) { http://git-wip-us.apache.org/repos/asf/camel/blob/a977e783/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthConstraintMapperTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthConstraintMapperTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthConstraintMapperTest.java index 879b4f2..dd46daa 100644 --- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthConstraintMapperTest.java +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthConstraintMapperTest.java @@ -46,8 +46,8 @@ public class NettyHttpBasicAuthConstraintMapperTest extends BaseNettyTest { security.setSecurityAuthenticator(auth); SecurityConstraintMapping matcher = new SecurityConstraintMapping(); - matcher.addInclusion("/foo/*"); - matcher.addExclusion("/foo/public/*"); + matcher.addInclusion("/*"); + matcher.addExclusion("/public/*"); security.setSecurityConstraint(matcher); jndi.bind("mySecurityConfig", security); http://git-wip-us.apache.org/repos/asf/camel/blob/a977e783/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthTest.java index ca328ce..789e66b 100644 --- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthTest.java +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpBasicAuthTest.java @@ -61,6 +61,7 @@ public class NettyHttpBasicAuthTest extends BaseNettyTest { } getMockEndpoint("mock:input").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:input").expectedHeaderReceived(NettyHttpConstants.HTTP_AUTHENTICATION, "Basic"); // username:password is scott:secret String auth = "Basic c2NvdHQ6c2VjcmV0"; http://git-wip-us.apache.org/repos/asf/camel/blob/a977e783/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthConstraintMapperTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthConstraintMapperTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthConstraintMapperTest.java index a241a80..4d06a50 100644 --- a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthConstraintMapperTest.java +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSimpleBasicAuthConstraintMapperTest.java @@ -40,8 +40,8 @@ public class NettyHttpSimpleBasicAuthConstraintMapperTest extends BaseNettyTest JndiRegistry jndi = super.createRegistry(); SecurityConstraintMapping matcher = new SecurityConstraintMapping(); - matcher.addInclusion("/foo/*"); - matcher.addExclusion("/foo/public/*"); + matcher.addInclusion("/*"); + matcher.addExclusion("/public/*"); jndi.bind("myConstraint", matcher); http://git-wip-us.apache.org/repos/asf/camel/blob/a977e783/components/camel-netty-http/src/test/resources/org/apache/camel/component/netty/http/SpringNettyHttpBasicAuthTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/resources/org/apache/camel/component/netty/http/SpringNettyHttpBasicAuthTest.xml b/components/camel-netty-http/src/test/resources/org/apache/camel/component/netty/http/SpringNettyHttpBasicAuthTest.xml index 7f423ce..0a78086 100644 --- a/components/camel-netty-http/src/test/resources/org/apache/camel/component/netty/http/SpringNettyHttpBasicAuthTest.xml +++ b/components/camel-netty-http/src/test/resources/org/apache/camel/component/netty/http/SpringNettyHttpBasicAuthTest.xml @@ -41,15 +41,15 @@ <!-- a * should be used for any role accepted (or even no roles) --> <property name="inclusions"> <map> - <entry key="/foo/*" value="*"/> - <entry key="/foo/admin/*" value="admin"/> - <entry key="/foo/guest/*" value="admin,guest"/> + <entry key="/*" value="*"/> + <entry key="/admin/*" value="admin"/> + <entry key="/guest/*" value="admin,guest"/> </map> </property> <!-- exclusions is used to define public urls, which requires no authentication --> <property name="exclusions"> <set> - <value>/foo/public/*</value> + <value>/public/*</value> </set> </property> </bean>