2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.ws
.client
;
19 import java
.io
.IOException
;
21 import javax
.xml
.transform
.Source
;
23 import org
.springframework
.ws
.client
.WebServiceClientException
;
24 import org
.springframework
.ws
.client
.WebServiceIOException
;
25 import org
.springframework
.ws
.client
.support
.interceptor
.ClientInterceptor
;
26 import org
.springframework
.ws
.context
.MessageContext
;
27 import org
.springframework
.xml
.validation
.XmlValidator
;
28 import org
.xml
.sax
.SAXParseException
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
33 public class ValidatingClientInterceptor
implements ClientInterceptor
{
34 private final static Log log
= LogFactory
35 .getLog(ValidatingClientInterceptor
.class);
37 private Boolean validateRequest
= true;
38 private Boolean validateResponse
= false;
39 private XmlValidator validator
= null;
41 public boolean handleFault(MessageContext messageContext
)
42 throws WebServiceClientException
{
46 public boolean handleRequest(MessageContext messageContext
)
47 throws WebServiceClientException
{
48 if (validateRequest
) {
49 if (messageContext
.getRequest() == null)
52 Source source
= messageContext
.getRequest().getPayloadSource();
54 return validate(source
);
55 } catch (IOException e
) {
56 throw new WebServiceIOException("Cannot validate request", e
);
63 public boolean handleResponse(MessageContext messageContext
)
64 throws WebServiceClientException
{
65 if (validateResponse
) {
66 if (messageContext
.getResponse() == null)
69 Source source
= messageContext
.getResponse().getPayloadSource();
71 return validate(source
);
72 } catch (IOException e
) {
73 throw new WebServiceIOException("Cannot validate response", e
);
80 protected boolean validate(Source source
) throws IOException
{
81 SAXParseException
[] exceptions
= validator
.validate(source
);
82 if (exceptions
.length
!= 0) {
83 for (SAXParseException ex
: exceptions
) {
84 log
.error(ex
.getMessage());
92 public void setValidateRequest(Boolean validateRequest
) {
93 this.validateRequest
= validateRequest
;
96 public void setValidateResponse(Boolean validateResponse
) {
97 this.validateResponse
= validateResponse
;
100 public void setValidator(XmlValidator validator
) {
101 this.validator
= validator
;