001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.jaxws;
021:
022: import java.util.Iterator;
023: import java.util.Set;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.soap.Detail;
027: import javax.xml.soap.SOAPBody;
028: import javax.xml.soap.SOAPElement;
029: import javax.xml.soap.SOAPException;
030: import javax.xml.soap.SOAPFault;
031: import javax.xml.soap.SOAPMessage;
032: import javax.xml.ws.handler.MessageContext;
033: import javax.xml.ws.handler.soap.SOAPHandler;
034: import javax.xml.ws.handler.soap.SOAPMessageContext;
035:
036: import org.apache.log4j.Logger;
037:
038: import de.schlund.pfixcore.webservice.ServiceCallContext;
039: import de.schlund.pfixcore.webservice.ServiceRequest;
040: import de.schlund.pfixcore.webservice.ServiceResponse;
041: import de.schlund.pfixcore.webservice.config.Configuration;
042: import de.schlund.pfixcore.webservice.config.ServiceConfig;
043: import de.schlund.pfixcore.webservice.fault.Fault;
044: import de.schlund.pfixcore.webservice.fault.FaultHandler;
045: import de.schlund.pfixcore.workflow.Context;
046:
047: /**
048: * Handles exceptions by calling the configured FaultHandler and
049: * and removing error details (stacktraces) from the response.
050: *
051: * @author mleidig@schlund.de
052: */
053: public class ErrorHandler implements SOAPHandler<SOAPMessageContext> {
054:
055: private final static Logger LOG = Logger
056: .getLogger(ErrorHandler.class.getName());
057:
058: /**
059: * Do nothing.
060: */
061: public boolean handleMessage(SOAPMessageContext ctx) {
062: return true;
063: }
064:
065: /**
066: * Process fault and remove error details (stacktraces) from response.
067: */
068: public boolean handleFault(SOAPMessageContext ctx) {
069: boolean isOutbound = (Boolean) ctx
070: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
071: if (isOutbound) {
072: Throwable error = processFault(ctx);
073: if (error == null)
074: error = new Exception("No error information available.");
075: SOAPMessage msg = ctx.getMessage();
076: try {
077: SOAPBody body = msg.getSOAPBody();
078: if (body != null) {
079: SOAPFault fault = body.getFault();
080: if (fault != null) {
081: fault.setFaultString(error.getMessage());
082: Detail detail = fault.getDetail();
083: if (detail != null) {
084: Iterator<?> it = detail.getChildElements();
085: while (it.hasNext()) {
086: SOAPElement elem = (SOAPElement) it
087: .next();
088: elem.setAttribute("class", error
089: .getClass().getName());
090: elem.removeContents();
091: elem.removeAttribute("note");
092: }
093: }
094: }
095: }
096: } catch (SOAPException x) {
097: LOG
098: .error(
099: "Error while removing error details from SOAPFault",
100: x);
101: }
102: }
103: return true;
104: }
105:
106: /**
107: * Get Throwable from context and process it by FaultHandler.
108: */
109: private Throwable processFault(SOAPMessageContext ctx) {
110: ServiceCallContext callContext = ServiceCallContext
111: .getCurrentContext();
112: JAXWSContext jaxwsContext = JAXWSContext.getCurrentContext();
113: if (callContext != null && jaxwsContext != null) {
114: Throwable error = jaxwsContext.getThrowable();
115: String serviceName = callContext.getServiceRequest()
116: .getServiceName();
117: Configuration config = callContext.getServiceRuntime()
118: .getConfiguration();
119: ServiceConfig serviceConfig = config
120: .getServiceConfig(serviceName);
121: FaultHandler faultHandler = serviceConfig.getFaultHandler();
122: if (faultHandler != null) {
123: try {
124: Context context = callContext.getContext();
125: ServiceRequest srvReq = callContext
126: .getServiceRequest();
127: ServiceResponse srvRes = callContext
128: .getServiceResponse();
129: Fault fault = new Fault(serviceName, srvReq,
130: srvRes, null, context);
131: fault.setThrowable(error);
132: faultHandler.handleFault(fault);
133: error = fault.getThrowable();
134: } catch (Exception x) {
135: LOG.error("Error while processing fault.", x);
136: }
137: }
138: return error;
139: }
140: LOG.warn("Can't get Throwable from webservice call context.");
141: return null;
142: }
143:
144: public void close(MessageContext arg0) {
145: }
146:
147: public Set<QName> getHeaders() {
148: return null;
149: }
150:
151: }
|