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