Skip to content

Commit 5a51be4

Browse files
committed
SWS-277
1 parent a200d48 commit 5a51be4

File tree

16 files changed

+300
-11
lines changed

16 files changed

+300
-11
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2008 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.server.endpoint.mapping;
18+
19+
import java.net.URI;
20+
import java.net.URISyntaxException;
21+
22+
import org.springframework.ws.context.MessageContext;
23+
import org.springframework.ws.soap.axiom.AxiomSoapMessageFactory;
24+
import org.springframework.ws.transport.WebServiceConnection;
25+
import org.springframework.ws.transport.context.TransportContext;
26+
import org.springframework.ws.transport.context.TransportContextHolder;
27+
28+
/**
29+
* Implementation of the <code>EndpointMapping</code> interface to map from the full request URI to endpoint beans.
30+
* Supports both mapping to bean instances and mapping to bean names: the latter is required for prototype handlers.
31+
* <p/>
32+
* The <code>endpointMap</code> property is suitable for populating the endpoint map with bean references, e.g. via the
33+
* map element in XML bean definitions.
34+
* <p/>
35+
* Mappings to bean names can be set via the <code>mappings</code> property, in a form accepted by the
36+
* <code>java.util.Properties</code> class, like as follows:
37+
* <pre>
38+
* http://example.com:8080/services/bookFlight=bookFlightEndpoint
39+
* jms://exampleQueue=getFlightsEndpoint
40+
* </pre>
41+
* The syntax is SOAP_ACTION=ENDPOINT_BEAN_NAME.
42+
* <p/>
43+
* This endpoint mapping does not read from the request message, and therefore is more suitable for message factories
44+
* which directly read from the transport request (such as the {@link AxiomSoapMessageFactory} with the
45+
* <code>payloadCaching</code> disabled). However, this endpoint mapping obviously is transport specific.
46+
*
47+
* @author Arjen Poutsma
48+
* @since 1.5.0
49+
*/
50+
public class UriEndpointMapping extends AbstractMapBasedEndpointMapping {
51+
52+
protected boolean validateLookupKey(String key) {
53+
try {
54+
new URI(key);
55+
return true;
56+
}
57+
catch (URISyntaxException e) {
58+
return false;
59+
}
60+
}
61+
62+
protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
63+
TransportContext transportContext = TransportContextHolder.getTransportContext();
64+
if (transportContext != null) {
65+
WebServiceConnection connection = transportContext.getConnection();
66+
if (connection != null) {
67+
return connection.getUri().toString();
68+
}
69+
}
70+
return null;
71+
}
72+
}

core/src/main/java/org/springframework/ws/transport/WebServiceConnection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.net.URI;
21+
import java.net.URISyntaxException;
2122

2223
import org.springframework.ws.WebServiceMessage;
2324
import org.springframework.ws.WebServiceMessageFactory;
@@ -52,6 +53,9 @@ public interface WebServiceConnection {
5253
*/
5354
WebServiceMessage receive(WebServiceMessageFactory messageFactory) throws IOException;
5455

56+
/** Returns the URI for this connection. */
57+
URI getUri() throws URISyntaxException;
58+
5559
/**
5660
* Indicates whether this connection has an error. Typically, error detection is done by inspecting connection error
5761
* codes, etc.

core/src/main/java/org/springframework/ws/transport/http/CommonsHttpConnection.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.io.OutputStream;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
2325
import java.util.Arrays;
2426
import java.util.Iterator;
2527

2628
import org.apache.commons.httpclient.Header;
2729
import org.apache.commons.httpclient.HttpClient;
30+
import org.apache.commons.httpclient.URIException;
2831
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
2932
import org.apache.commons.httpclient.methods.PostMethod;
3033

@@ -63,6 +66,18 @@ public void onClose() throws IOException {
6366
}
6467

6568
/*
69+
* URI
70+
*/
71+
72+
public URI getUri() throws URISyntaxException {
73+
try {
74+
return new URI(postMethod.getURI().toString());
75+
}
76+
catch (URIException ex) {
77+
throw new URISyntaxException("", ex.getMessage());
78+
}
79+
}
80+
/*
6681
* Sending request
6782
*/
6883

core/src/main/java/org/springframework/ws/transport/http/HttpServletConnection.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
22+
import java.net.URI;
23+
import java.net.URISyntaxException;
2224
import java.util.Iterator;
2325
import javax.servlet.http.HttpServletRequest;
2426
import javax.servlet.http.HttpServletResponse;
@@ -82,9 +84,19 @@ public String getErrorMessage() throws IOException {
8284
}
8385

8486
/*
85-
* Receiving request
87+
* URI
8688
*/
8789

90+
public URI getUri() throws URISyntaxException {
91+
return new URI(httpServletRequest.getScheme(), null, httpServletRequest.getServerName(),
92+
httpServletRequest.getServerPort(), httpServletRequest.getRequestURI(),
93+
httpServletRequest.getQueryString(), null);
94+
}
95+
96+
/*
97+
* Receiving request
98+
*/
99+
88100
protected Iterator getRequestHeaderNames() throws IOException {
89101
return new EnumerationIterator(getHttpServletRequest().getHeaderNames());
90102
}

core/src/main/java/org/springframework/ws/transport/http/HttpUrlConnection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
2222
import java.net.HttpURLConnection;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
2325
import java.util.ArrayList;
2426
import java.util.Collections;
2527
import java.util.Iterator;
@@ -59,6 +61,14 @@ public void onClose() {
5961
connection.disconnect();
6062
}
6163

64+
/*
65+
* URI
66+
*/
67+
68+
public URI getUri() throws URISyntaxException {
69+
return connection.getURL().toURI();
70+
}
71+
6272
/*
6373
* Sending request
6474
*/

core/src/main/java/org/springframework/ws/transport/support/WebServiceMessageReceiverObjectSupport.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.ws.transport.support;
1818

19+
import java.net.URISyntaxException;
20+
1921
import org.apache.commons.logging.Log;
2022
import org.apache.commons.logging.LogFactory;
2123

@@ -76,6 +78,7 @@ public void afterPropertiesSet() throws Exception {
7678
*/
7779
protected final void handleConnection(WebServiceConnection connection, WebServiceMessageReceiver receiver)
7880
throws Exception {
81+
logUri(connection);
7982
TransportContext previousTransportContext = TransportContextHolder.getTransportContext();
8083
TransportContextHolder.setTransportContext(new DefaultTransportContext(connection));
8184

@@ -105,4 +108,15 @@ protected final void handleConnection(WebServiceConnection connection, WebServic
105108
}
106109
}
107110

111+
private void logUri(WebServiceConnection connection) {
112+
if (logger.isDebugEnabled()) {
113+
try {
114+
logger.debug("Regestering incoming connection [" + connection.getUri() + "]");
115+
}
116+
catch (URISyntaxException e) {
117+
// ignore
118+
}
119+
}
120+
}
121+
108122
}

core/src/test/java/org/springframework/ws/client/core/WebServiceTemplateTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected void setUp() throws Exception {
5252
template.setMessageFactory(messageFactory);
5353
connectionControl = MockControl.createStrictControl(FaultAwareWebServiceConnection.class);
5454
connectionMock = (FaultAwareWebServiceConnection) connectionControl.getMock();
55+
connectionControl.expectAndDefaultReturn(connectionMock.getUri(), new URI("mock"));
5556
final URI expectedUri = new URI("http://www.springframework.org/spring-ws");
5657
template.setMessageSender(new WebServiceMessageSender() {
5758

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright ${YEAR} the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.server.endpoint.mapping;
18+
19+
import java.net.URI;
20+
21+
import junit.framework.TestCase;
22+
import org.easymock.MockControl;
23+
24+
import org.springframework.ws.MockWebServiceMessageFactory;
25+
import org.springframework.ws.context.DefaultMessageContext;
26+
import org.springframework.ws.context.MessageContext;
27+
import org.springframework.ws.transport.WebServiceConnection;
28+
import org.springframework.ws.transport.context.DefaultTransportContext;
29+
import org.springframework.ws.transport.context.TransportContextHolder;
30+
31+
public class UriEndpointMappingTest extends TestCase {
32+
33+
private UriEndpointMapping mapping;
34+
35+
private MessageContext context;
36+
37+
protected void setUp() throws Exception {
38+
mapping = new UriEndpointMapping();
39+
context = new DefaultMessageContext(new MockWebServiceMessageFactory());
40+
}
41+
42+
public void testGetLookupKeyForMessage() throws Exception {
43+
MockControl control = MockControl.createControl(WebServiceConnection.class);
44+
WebServiceConnection connectionMock = (WebServiceConnection) control.getMock();
45+
TransportContextHolder.setTransportContext(new DefaultTransportContext(connectionMock));
46+
47+
URI uri = new URI("jms://exampleQueue");
48+
control.expectAndReturn(connectionMock.getUri(), uri);
49+
control.replay();
50+
51+
assertEquals("Invalid lookup key", uri.toString(), mapping.getLookupKeyForMessage(context));
52+
53+
control.verify();
54+
TransportContextHolder.setTransportContext(null);
55+
}
56+
57+
public void testValidateLookupKey() throws Exception {
58+
assertTrue("URI not valid", mapping.validateLookupKey("http://example.com/services"));
59+
assertFalse("URI not valid", mapping.validateLookupKey("some string"));
60+
}
61+
}

core/src/test/java/org/springframework/ws/transport/support/WebServiceMessageReceiverObjectSupportTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package org.springframework.ws.transport.support;
1818

19+
import java.net.URI;
20+
1921
import junit.framework.TestCase;
2022
import org.easymock.MockControl;
23+
2124
import org.springframework.ws.MockWebServiceMessage;
2225
import org.springframework.ws.MockWebServiceMessageFactory;
2326
import org.springframework.ws.context.MessageContext;
@@ -46,6 +49,7 @@ protected void setUp() throws Exception {
4649
}
4750

4851
public void testHandleConnectionResponse() throws Exception {
52+
connectionControl.expectAndReturn(connectionMock.getUri(), new URI("http://example.com"));
4953
connectionControl.expectAndReturn(connectionMock.receive(messageFactory), request);
5054
connectionMock.send(new MockWebServiceMessage());
5155
connectionControl.setMatcher(MockControl.ALWAYS_MATCHER);
@@ -67,6 +71,7 @@ public void receive(MessageContext messageContext) throws Exception {
6771
}
6872

6973
public void testHandleConnectionNoResponse() throws Exception {
74+
connectionControl.expectAndReturn(connectionMock.getUri(), new URI("http://example.com"));
7075
connectionControl.expectAndReturn(connectionMock.receive(messageFactory), request);
7176
connectionMock.close();
7277

support/src/main/java/org/springframework/ws/transport/jms/JmsReceiverConnection.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
24+
import java.net.URI;
25+
import java.net.URISyntaxException;
2426
import java.util.Iterator;
2527
import javax.jms.BytesMessage;
2628
import javax.jms.JMSException;
@@ -98,6 +100,19 @@ public Message getResponseMessage() {
98100
return responseMessage;
99101
}
100102

103+
/*
104+
* URI
105+
*/
106+
107+
public URI getUri() throws URISyntaxException {
108+
try {
109+
return JmsTransportUtils.toUri(requestMessage.getJMSDestination());
110+
}
111+
catch (JMSException ex) {
112+
throw new URISyntaxException("", ex.getMessage());
113+
}
114+
}
115+
101116
/*
102117
* Errors
103118
*/

support/src/main/java/org/springframework/ws/transport/jms/JmsSenderConnection.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.IOException;
2222
import java.io.InputStream;
2323
import java.io.OutputStream;
24+
import java.net.URI;
25+
import java.net.URISyntaxException;
2426
import java.util.Iterator;
2527
import javax.jms.BytesMessage;
2628
import javax.jms.Connection;
@@ -136,6 +138,19 @@ void setMessageType(int messageType) {
136138
this.messageType = messageType;
137139
}
138140

141+
/*
142+
* URI
143+
*/
144+
145+
public URI getUri() throws URISyntaxException {
146+
try {
147+
return JmsTransportUtils.toUri(requestDestination);
148+
}
149+
catch (JMSException ex) {
150+
throw new URISyntaxException("", ex.getMessage());
151+
}
152+
}
153+
139154
/*
140155
* Errors
141156
*/

0 commit comments

Comments
 (0)