Skip to content

Commit a0c8adf

Browse files
committed
SWS-188
1 parent 262ff36 commit a0c8adf

File tree

6 files changed

+134
-5
lines changed

6 files changed

+134
-5
lines changed

security/src/main/java/org/springframework/ws/soap/security/AbstractWsSecurityInterceptor.java

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import org.springframework.util.Assert;
2525
import org.springframework.ws.context.MessageContext;
2626
import org.springframework.ws.soap.SoapBody;
27+
import org.springframework.ws.soap.SoapFault;
2728
import org.springframework.ws.soap.SoapHeaderElement;
2829
import org.springframework.ws.soap.SoapMessage;
2930
import org.springframework.ws.soap.server.SoapEndpointInterceptor;
31+
import org.springframework.ws.soap.soap11.Soap11Body;
3032

3133
/**
3234
* Interceptor base class for interceptors that handle WS-Security.
@@ -42,19 +44,25 @@ public abstract class AbstractWsSecurityInterceptor implements SoapEndpointInter
4244
private static final QName WS_SECURITY_NAME =
4345
new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
4446

45-
/** Logger available to subclasses. */
47+
/**
48+
* Logger available to subclasses.
49+
*/
4650
protected final Log logger = LogFactory.getLog(getClass());
4751

4852
private boolean secureResponse = true;
4953

5054
private boolean validateRequest = true;
5155

52-
/** Indicates whether outgoing responsed are to be secured. Defaults to <code>true</code>. */
56+
/**
57+
* Indicates whether outgoing responsed are to be secured. Defaults to <code>true</code>.
58+
*/
5359
public void setSecureResponse(boolean secureResponse) {
5460
this.secureResponse = secureResponse;
5561
}
5662

57-
/** Indicates whether incoming request are to be validated. Defaults to <code>true</code>. */
63+
/**
64+
* Indicates whether incoming request are to be validated. Defaults to <code>true</code>.
65+
*/
5866
public void setValidateRequest(boolean validateRequest) {
5967
this.validateRequest = validateRequest;
6068
}
@@ -70,6 +78,9 @@ public final boolean handleRequest(MessageContext messageContext, Object endpoin
7078
catch (WsSecurityValidationException ex) {
7179
return handleValidationException(ex, messageContext);
7280
}
81+
catch (WsSecurityFaultException ex) {
82+
return handleFaultException(ex, messageContext);
83+
}
7384
}
7485
else {
7586
return true;
@@ -87,13 +98,18 @@ public final boolean handleResponse(MessageContext messageContext, Object endpoi
8798
catch (WsSecuritySecurementException ex) {
8899
return handleSecurementException(ex, messageContext);
89100
}
101+
catch (WsSecurityFaultException ex) {
102+
return handleFaultException(ex, messageContext);
103+
}
90104
}
91105
else {
92106
return true;
93107
}
94108
}
95109

96-
/** Returns <code>true</code>, i.e. faults are not secured. */
110+
/**
111+
* Returns <code>true</code>, i.e. faults are not secured.
112+
*/
97113
public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
98114
return true;
99115
}
@@ -134,6 +150,30 @@ protected boolean handleValidationException(WsSecurityValidationException ex, Me
134150
return false;
135151
}
136152

153+
/**
154+
* Handles a fault exception.Default implementation logs the given exception, and creates a SOAP Fault with the
155+
* properties of the given exception, and returns <code>false</code>.
156+
*
157+
* @param ex the validation exception
158+
* @param messageContext the message context
159+
* @return <code>true</code> to continue processing the message, <code>false</code> (the default) otherwise
160+
*/
161+
protected boolean handleFaultException(WsSecurityFaultException ex, MessageContext messageContext) {
162+
if (logger.isWarnEnabled()) {
163+
logger.warn("Could not handle request: " + ex.getMessage());
164+
}
165+
SoapBody response = ((SoapMessage) messageContext.getResponse()).getSoapBody();
166+
SoapFault fault;
167+
if (response instanceof Soap11Body) {
168+
fault = ((Soap11Body) response).addFault(ex.getFaultCode(), ex.getFaultString(), Locale.ENGLISH);
169+
}
170+
else {
171+
fault = response.addClientOrSenderFault(ex.getFaultString(), Locale.ENGLISH);
172+
}
173+
fault.setFaultActorOrRole(ex.getFaultActor());
174+
return false;
175+
}
176+
137177
/**
138178
* Abstract template method. Subclasses are required to validate the request contained in the given {@link
139179
* SoapMessage}, and replace the original request with the validated version.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.springframework.ws.soap.security;
2+
3+
import javax.xml.namespace.QName;
4+
5+
/**
6+
* Exception indicating that a WS-Security executions should result in a SOAP Fault.
7+
*
8+
* @author Arjen Poutsma
9+
* @since 1.0.1
10+
*/
11+
public abstract class WsSecurityFaultException extends WsSecurityException {
12+
13+
private QName faultCode;
14+
15+
private String faultString;
16+
17+
private String faultActor;
18+
19+
/**
20+
* Construct a new <code>WsSecurityFaultException</code> with the given fault code, string, and actor.
21+
*/
22+
public WsSecurityFaultException(QName faultCode, String faultString, String faultActor) {
23+
super(faultString);
24+
this.faultCode = faultCode;
25+
this.faultString = faultString;
26+
this.faultActor = faultActor;
27+
}
28+
29+
/**
30+
* Returns the fault code for the exception.
31+
*/
32+
public QName getFaultCode() {
33+
return faultCode;
34+
}
35+
36+
/**
37+
* Returns the fault string for the exception.
38+
*/
39+
public String getFaultString() {
40+
return faultString;
41+
}
42+
43+
/**
44+
* Returns the fault actor for the exception.
45+
*/
46+
public String getFaultActor() {
47+
return faultActor;
48+
}
49+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.springframework.ws.soap.security.xwss;
2+
3+
import javax.xml.namespace.QName;
4+
5+
import org.springframework.ws.soap.security.WsSecurityFaultException;
6+
7+
/**
8+
* XWSS-specific version of the {@link WsSecurityFaultException}.
9+
*
10+
* @author Arjen Poutsma
11+
* @since 1.0.1
12+
*/
13+
public class XwsSecurityFaultException extends WsSecurityFaultException {
14+
15+
public XwsSecurityFaultException(QName faultCode, String faultString, String faultActor) {
16+
super(faultCode, faultString, faultActor);
17+
}
18+
}

security/src/main/java/org/springframework/ws/soap/security/xwss/XwsSecurityInterceptor.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.sun.xml.wss.XWSSProcessor;
2525
import com.sun.xml.wss.XWSSProcessorFactory;
2626
import com.sun.xml.wss.XWSSecurityException;
27+
import com.sun.xml.wss.impl.WssSoapFaultException;
2728
import org.springframework.beans.factory.InitializingBean;
2829
import org.springframework.core.io.Resource;
2930
import org.springframework.util.Assert;
@@ -86,7 +87,9 @@ public void setCallbackHandlers(CallbackHandler[] callbackHandler) {
8687
this.callbackHandler = new CallbackHandlerChain(callbackHandler);
8788
}
8889

89-
/** Sets the policy configuration to use for XWSS. Required. */
90+
/**
91+
* Sets the policy configuration to use for XWSS. Required.
92+
*/
9093
public void setPolicyConfiguration(Resource policyConfiguration) {
9194
this.policyConfiguration = policyConfiguration;
9295
}
@@ -130,6 +133,9 @@ protected void secureMessage(SoapMessage soapMessage) throws XwsSecuritySecureme
130133
catch (XWSSecurityException ex) {
131134
throw new XwsSecuritySecurementException(ex.getMessage(), ex);
132135
}
136+
catch (WssSoapFaultException ex) {
137+
throw new XwsSecurityFaultException(ex.getFaultCode(), ex.getFaultString(), ex.getFaultActor());
138+
}
133139
}
134140

135141
/**
@@ -151,5 +157,9 @@ protected void validateMessage(SoapMessage soapMessage) throws WsSecurityValidat
151157
catch (XWSSecurityException ex) {
152158
throw new XwsSecurityValidationException(ex.getMessage(), ex);
153159
}
160+
catch (WssSoapFaultException ex) {
161+
throw new XwsSecurityFaultException(ex.getFaultCode(), ex.getFaultString(), ex.getFaultActor());
162+
}
154163
}
164+
155165
}

security/src/main/java/org/springframework/ws/soap/security/xwss/XwsSecuritySecurementException.java

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

1919
import org.springframework.ws.soap.security.WsSecuritySecurementException;
2020

21+
/**
22+
* XWSS-specific version of the {@link WsSecuritySecurementException}.
23+
*
24+
* @author Arjen Poutsma
25+
* @since 1.0.0
26+
*/
2127
public class XwsSecuritySecurementException extends WsSecuritySecurementException {
2228

2329
public XwsSecuritySecurementException(String msg) {

security/src/main/java/org/springframework/ws/soap/security/xwss/XwsSecurityValidationException.java

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

1919
import org.springframework.ws.soap.security.WsSecurityValidationException;
2020

21+
/**
22+
* XWSS-specific version of the {@link WsSecurityValidationException}.
23+
*
24+
* @author Arjen Poutsma
25+
* @since 1.0.0
26+
*/
2127
public class XwsSecurityValidationException extends WsSecurityValidationException {
2228

2329
public XwsSecurityValidationException(String msg) {

0 commit comments

Comments
 (0)