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