]> git.argeo.org Git - gpl/argeo-slc.git/blob - ValidatingClientInterceptor.java
84c4b193c0880707cf02cafd7bdd1f7e39e3368a
[gpl/argeo-slc.git] / ValidatingClientInterceptor.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.slc.ws.client;
17
18 import java.io.IOException;
19
20 import javax.xml.transform.Source;
21
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;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 public class ValidatingClientInterceptor implements ClientInterceptor {
33 private final static Log log = LogFactory
34 .getLog(ValidatingClientInterceptor.class);
35
36 private Boolean validateRequest = true;
37 private Boolean validateResponse = false;
38 private XmlValidator validator = null;
39
40 public boolean handleFault(MessageContext messageContext)
41 throws WebServiceClientException {
42 return true;
43 }
44
45 public boolean handleRequest(MessageContext messageContext)
46 throws WebServiceClientException {
47 if (validateRequest) {
48 if (messageContext.getRequest() == null)
49 return true;
50
51 Source source = messageContext.getRequest().getPayloadSource();
52 try {
53 return validate(source);
54 } catch (IOException e) {
55 throw new WebServiceIOException("Cannot validate request", e);
56 }
57 } else {
58 return true;
59 }
60 }
61
62 public boolean handleResponse(MessageContext messageContext)
63 throws WebServiceClientException {
64 if (validateResponse) {
65 if (messageContext.getResponse() == null)
66 return true;
67
68 Source source = messageContext.getResponse().getPayloadSource();
69 try {
70 return validate(source);
71 } catch (IOException e) {
72 throw new WebServiceIOException("Cannot validate response", e);
73 }
74 } else {
75 return true;
76 }
77 }
78
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());
84 }
85 return false;
86 } else {
87 return true;
88 }
89 }
90
91 public void setValidateRequest(Boolean validateRequest) {
92 this.validateRequest = validateRequest;
93 }
94
95 public void setValidateResponse(Boolean validateResponse) {
96 this.validateResponse = validateResponse;
97 }
98
99 public void setValidator(XmlValidator validator) {
100 this.validator = validator;
101 }
102
103 }