]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.ws.client/src/main/java/org/argeo/slc/ws/client/WebServiceUtils.java
Improve logging
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.ws.client / src / main / java / org / argeo / slc / ws / client / WebServiceUtils.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.util.Iterator;
20
21 import javax.xml.transform.Source;
22 import javax.xml.transform.dom.DOMSource;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.springframework.ws.client.core.WebServiceTemplate;
27 import org.springframework.ws.soap.SoapFaultDetail;
28 import org.springframework.ws.soap.SoapFaultDetailElement;
29 import org.springframework.ws.soap.client.SoapFaultClientException;
30 import org.springframework.xml.transform.StringResult;
31 import org.w3c.dom.Node;
32
33 public abstract class WebServiceUtils {
34 private final static Log log = LogFactory.getLog(WebServiceUtils.class);
35
36 public static Object marshalSendAndReceiveSafe(WebServiceTemplate template,
37 Object req) {
38 try {
39 Object resp = marshalSendAndReceive(template, req);
40 return resp;
41 } catch (Exception e) {
42 log.error("Cannot send web servicerequest: " + e.getMessage());
43 if (log.isDebugEnabled()) {
44 log.debug("Webservice exception details: ", e);
45 }
46 return null;
47 }
48 }
49
50 public static Object marshalSendAndReceive(WebServiceTemplate template,
51 Object req) {
52 if (log.isTraceEnabled()) {
53 try {
54 StringResult result = new StringResult();
55 template.getMarshaller().marshal(req, result);
56 log.trace("About to send " + result);
57 } catch (Exception e) {
58 log.error("Cannot marshall " + req + " for logging", e);
59 }
60 }
61 Object resp = template.marshalSendAndReceive(req);
62 return resp;
63 }
64
65 public static void manageSoapException(SoapFaultClientException e) {
66 log
67 .error("WS root cause: "
68 + e.getSoapFault().getFaultStringOrReason());
69 if (log.isTraceEnabled()) {
70 StringBuffer stack = new StringBuffer("");
71 SoapFaultDetail detail = e.getSoapFault().getFaultDetail();
72 if (detail != null) {
73 Iterator<SoapFaultDetailElement> it = (Iterator<SoapFaultDetailElement>) detail
74 .getDetailEntries();
75 while (it.hasNext()) {
76 SoapFaultDetailElement elem = it.next();
77 if (elem.getName().getLocalPart().equals("StackElement")) {
78 Source source = elem.getSource();
79 if (source instanceof DOMSource) {
80 Node node = ((DOMSource) source).getNode();
81 // stack.append(node.getTextContent()).append('\n');
82 stack
83 .append(
84 node.getChildNodes().item(0)
85 .getNodeValue()).append(
86 '\n');
87 }
88 }
89 }
90
91 if (stack.length() > 0)
92 log.error("WS root cause stack: " + stack);
93 }
94 }
95 }
96
97 private WebServiceUtils() {
98
99 }
100 }