2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.ws
.client
;
18 import java
.io
.IOException
;
20 import javax
.xml
.transform
.Source
;
22 import org
.springframework
.ws
.client
.WebServiceClientException
;
23 import org
.springframework
.ws
.client
.WebServiceIOException
;
24 import org
.springframework
.ws
.client
.support
.interceptor
.ClientInterceptor
;
25 import org
.springframework
.ws
.context
.MessageContext
;
26 import org
.springframework
.xml
.validation
.XmlValidator
;
27 import org
.xml
.sax
.SAXParseException
;
29 import org
.apache
.commons
.logging
.Log
;
30 import org
.apache
.commons
.logging
.LogFactory
;
32 public class ValidatingClientInterceptor
implements ClientInterceptor
{
33 private final static Log log
= LogFactory
34 .getLog(ValidatingClientInterceptor
.class);
36 private Boolean validateRequest
= true;
37 private Boolean validateResponse
= false;
38 private XmlValidator validator
= null;
40 public boolean handleFault(MessageContext messageContext
)
41 throws WebServiceClientException
{
45 public boolean handleRequest(MessageContext messageContext
)
46 throws WebServiceClientException
{
47 if (validateRequest
) {
48 if (messageContext
.getRequest() == null)
51 Source source
= messageContext
.getRequest().getPayloadSource();
53 return validate(source
);
54 } catch (IOException e
) {
55 throw new WebServiceIOException("Cannot validate request", e
);
62 public boolean handleResponse(MessageContext messageContext
)
63 throws WebServiceClientException
{
64 if (validateResponse
) {
65 if (messageContext
.getResponse() == null)
68 Source source
= messageContext
.getResponse().getPayloadSource();
70 return validate(source
);
71 } catch (IOException e
) {
72 throw new WebServiceIOException("Cannot validate response", e
);
79 protected boolean validate(Source source
) throws IOException
{
80 SAXParseException
[] exceptions
= validator
.validate(source
);
81 if (exceptions
.length
!= 0) {
82 for (SAXParseException ex
: exceptions
) {
83 log
.error(ex
.getMessage());
91 public void setValidateRequest(Boolean validateRequest
) {
92 this.validateRequest
= validateRequest
;
95 public void setValidateResponse(Boolean validateResponse
) {
96 this.validateResponse
= validateResponse
;
99 public void setValidator(XmlValidator validator
) {
100 this.validator
= validator
;