001: package de.schlund.pfixcore.webservice.fault;
002:
003: import java.util.Properties;
004:
005: import javax.servlet.http.HttpServletRequest;
006: import javax.servlet.http.HttpServletResponse;
007: import javax.servlet.http.HttpSession;
008:
009: import org.apache.log4j.Logger;
010:
011: import de.schlund.pfixcore.webservice.ServiceRequest;
012: import de.schlund.pfixcore.webservice.ServiceResponse;
013: import de.schlund.pfixxml.PfixServletRequest;
014: import de.schlund.pfixxml.PfixServletRequestImpl;
015: import de.schlund.pfixxml.config.XMLPropertiesUtil;
016: import de.schlund.pfixxml.exceptionprocessor.ExceptionConfig;
017: import de.schlund.pfixxml.exceptionprocessor.ExceptionProcessor;
018: import de.schlund.pfixxml.resources.FileResource;
019: import de.schlund.pfixxml.resources.ResourceUtil;
020:
021: public class ExceptionProcessorAdapter extends FaultHandler {
022:
023: final static Logger LOG = Logger
024: .getLogger(ExceptionProcessorAdapter.class);
025:
026: private final static String PARAM_CONFIG = "config";
027: private final static String PROP_EXPROC = "exception.java.lang.Throwable.processor";
028:
029: ExceptionProcessor exProc;
030: Properties exProcProps;
031: ExceptionConfig exConf;
032:
033: public ExceptionProcessorAdapter() {
034:
035: }
036:
037: @Override
038: public void init() {
039: String config = getParam(PARAM_CONFIG);
040: if (config == null)
041: throw new IllegalArgumentException("Parameter '"
042: + PARAM_CONFIG + "' is missing.");
043: FileResource configFile = ResourceUtil
044: .getFileResourceFromDocroot(config);
045: exProcProps = new Properties();
046: try {
047: XMLPropertiesUtil.loadPropertiesFromXMLFile(configFile,
048: exProcProps);
049: } catch (Exception x) {
050: throw new RuntimeException("Can't load properties from "
051: + configFile, x);
052: }
053: String procName = exProcProps.getProperty(PROP_EXPROC);
054: if (procName != null) {
055: try {
056: Class<?> clazz = Class.forName(procName);
057: exProc = (ExceptionProcessor) clazz.newInstance();
058: exConf = new ExceptionConfig();
059: exConf.setPage("webservice");
060: exConf.setProcessor(exProc);
061: exConf.setType("java.lang.Throwable");
062: } catch (Exception x) {
063: throw new RuntimeException(
064: "Can't instantiate ExceptionProcessor.", x);
065: }
066: } else
067: LOG
068: .warn("No ExceptionProcessor for java.lang.Throwable found in properties!");
069: }
070:
071: @Override
072: public void handleFault(Fault fault) {
073: if (isNotificationError(fault)) {
074: if (!(fault.getThrowable() instanceof Exception)) {
075: LOG.error("Throwable isn't Exception instance: "
076: + fault.getThrowable().getClass().getName());
077: return;
078: }
079: Exception ex = (Exception) fault.getThrowable();
080: ServiceRequest srvReq = fault.getRequest();
081: ServiceResponse srvRes = fault.getResponse();
082: if (srvReq.getUnderlyingRequest() instanceof HttpServletRequest) {
083: HttpServletRequest req = (HttpServletRequest) srvReq
084: .getUnderlyingRequest();
085: HttpServletResponse res = (HttpServletResponse) srvRes
086: .getUnderlyingResponse();
087: PfixServletRequest pfixReq = new PfixServletRequestImpl(
088: req, new Properties());
089: HttpSession session = req.getSession(false);
090: if (session != null) {
091: try {
092: exProc.processException(ex, exConf, pfixReq,
093: session.getServletContext(), req, res,
094: exProcProps);
095: } catch (Exception x) {
096: LOG.error("Can't process exception.", x);
097: }
098: }
099: }
100: }
101: if (isInternalServerError(fault))
102: fault.setThrowable(new InternalServerError());
103: }
104:
105: public boolean isInternalServerError(Fault fault) {
106: Throwable t = fault.getThrowable();
107: if (t != null && (t instanceof Error))
108: return true;
109: return false;
110: }
111:
112: public boolean isNotificationError(Fault fault) {
113: return true;
114: }
115:
116: }
|