From b0b9e5677ea51430946eddfdcc0f2d6b3f4d7995 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 10 Jul 2012 09:39:01 +0000 Subject: [PATCH] SWS-784 - Add URLPathEndpointMapping Feature --- .../endpoint/mapping/UriEndpointMapping.java | 49 ++++++++++++++----- .../mapping/UriEndpointMappingTest.java | 23 +++++++-- 2 files changed, 57 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/org/springframework/ws/server/endpoint/mapping/UriEndpointMapping.java b/core/src/main/java/org/springframework/ws/server/endpoint/mapping/UriEndpointMapping.java index e69a144b..7df065f7 100644 --- a/core/src/main/java/org/springframework/ws/server/endpoint/mapping/UriEndpointMapping.java +++ b/core/src/main/java/org/springframework/ws/server/endpoint/mapping/UriEndpointMapping.java @@ -1,11 +1,11 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2012 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 + * 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, @@ -26,29 +26,48 @@ import org.springframework.ws.transport.context.TransportContext; import org.springframework.ws.transport.context.TransportContextHolder; /** - * Implementation of the EndpointMapping interface to map from the full request URI to endpoint beans. - * Supports both mapping to bean instances and mapping to bean names: the latter is required for prototype handlers. + * Implementation of the {@code EndpointMapping} interface to map from the full request URI or request URI path to + * endpoint beans. Supports both mapping to bean instances and mapping to bean names: the latter is required for + * prototype handlers. *

- * The endpointMap property is suitable for populating the endpoint map with bean references, e.g. via the - * map element in XML bean definitions. + * When the {@link #setUsePath(boolean) usePath} property is enabled, the mapping will be based on the URI path rather + * than the full URI. *

- * Mappings to bean names can be set via the mappings property, in a form accepted by the - * java.util.Properties class, like as follows: + * The {@code endpointMap} property is suitable for populating the endpoint map with bean references, e.g. via the map + * element in XML bean definitions. + *

+ * Mappings to bean names can be set via the {@code mappings} property, in a form accepted by the {@code + * java.util.Properties} class, like as follows: *

  * http://example.com:8080/services/bookFlight=bookFlightEndpoint
  * jms://exampleQueue=getFlightsEndpoint
  * 
- * The syntax is URI=ENDPOINT_BEAN_NAME. + * or, when the {@code usePath} property is enabled: + *
+ * /services/bookFlight=bookFlightEndpoint
+ * 
+ * The syntax is [URI|PATH]=ENDPOINT_BEAN_NAME. *

* This endpoint mapping does not read from the request message, and therefore is more suitable for message factories - * which directly read from the transport request (such as the {@link AxiomSoapMessageFactory} with the - * payloadCaching disabled). However, this endpoint mapping obviously is transport specific. + * which directly read from the transport request (such as the {@link AxiomSoapMessageFactory} with the {@code + * payloadCaching} disabled). However, this endpoint mapping obviously is transport specific. * * @author Arjen Poutsma * @since 1.5.0 */ public class UriEndpointMapping extends AbstractMapBasedEndpointMapping { + private boolean usePath = false; + + /** + * Indicates whether the path should be used instead of the full URI. Default is {@code false}. + * + * @since 2.1.1 + */ + public void setUsePath(boolean usePath) { + this.usePath = usePath; + } + @Override protected boolean validateLookupKey(String key) { try { @@ -66,7 +85,13 @@ public class UriEndpointMapping extends AbstractMapBasedEndpointMapping { if (transportContext != null) { WebServiceConnection connection = transportContext.getConnection(); if (connection != null) { - return connection.getUri().toString(); + URI connectionUri = connection.getUri(); + if (usePath) { + return connectionUri.getPath(); + } + else { + return connectionUri.toString(); + } } } return null; diff --git a/core/src/test/java/org/springframework/ws/server/endpoint/mapping/UriEndpointMappingTest.java b/core/src/test/java/org/springframework/ws/server/endpoint/mapping/UriEndpointMappingTest.java index 4ad54c27..b9c20422 100644 --- a/core/src/test/java/org/springframework/ws/server/endpoint/mapping/UriEndpointMappingTest.java +++ b/core/src/test/java/org/springframework/ws/server/endpoint/mapping/UriEndpointMappingTest.java @@ -1,11 +1,11 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2012 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 + * 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, @@ -50,7 +50,7 @@ public class UriEndpointMappingTest { } @Test - public void testGetLookupKeyForMessage() throws Exception { + public void getLookupKeyForMessage() throws Exception { WebServiceConnection connectionMock = createMock(WebServiceConnection.class); TransportContextHolder.setTransportContext(new DefaultTransportContext(connectionMock)); @@ -64,6 +64,23 @@ public class UriEndpointMappingTest { verify(connectionMock); } + @Test + public void getLookupKeyForMessagePath() throws Exception { + mapping.setUsePath(true); + + WebServiceConnection connectionMock = createMock(WebServiceConnection.class); + TransportContextHolder.setTransportContext(new DefaultTransportContext(connectionMock)); + + URI uri = new URI("http://example.com/foo/bar"); + expect(connectionMock.getUri()).andReturn(uri); + + replay(connectionMock); + + Assert.assertEquals("Invalid lookup key", "/foo/bar", mapping.getLookupKeyForMessage(context)); + + verify(connectionMock); + } + @Test public void testValidateLookupKey() throws Exception { Assert.assertTrue("URI not valid", mapping.validateLookupKey("http://example.com/services"));