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.pfixxml.exceptionprocessor.util;
021:
022: import java.util.ArrayList;
023: import java.util.Enumeration;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027:
028: import javax.servlet.http.HttpSession;
029:
030: import org.apache.log4j.Logger;
031:
032: import de.schlund.pfixxml.PfixServletRequest;
033: import de.schlund.pfixxml.serverutil.SessionAdmin;
034: import de.schlund.pfixxml.serverutil.SessionInfoStruct;
035:
036: /**
037: * @author jh
038: *
039: * To change the template for this generated type comment go to
040: * Window - Preferences - Java - Code Generation - Code and Comments
041: */
042: public class ExceptionDataValueHelper {
043: private final static Logger LOG = Logger
044: .getLogger(ExceptionDataValueHelper.class);
045:
046: /**
047: * @param exception
048: * @param pfixReq
049: * @return
050: */
051: public static ExceptionDataValue createExceptionDataValue(
052: Throwable exception, PfixServletRequest pfixReq) {
053: ExceptionDataValue exdata = new ExceptionDataValue();
054: exdata.setThrowable(exception);
055: exdata.setScheme(pfixReq.getScheme());
056: exdata.setServername(pfixReq.getServerName());
057: exdata.setPort(pfixReq.getServerPort());
058: exdata.setUri(pfixReq.getRequestURI());
059: final HttpSession session = pfixReq.getSession(false);
060: final String id = session.getId();
061: exdata.setSessionid(id);
062: exdata.setServlet(pfixReq.getServletName());
063: String pagename = pfixReq.getPageName();
064: if (pagename == null) {
065: exdata.setPage("null");
066: } else {
067: exdata.setPage(pagename);
068: }
069: exdata.setQuery(pfixReq.getQueryString());
070:
071: HashMap<String, String> keysnvalues = new HashMap<String, String>();
072: String[] param_names = pfixReq.getRequestParamNames();
073: for (int i = 0; i < param_names.length; i++) {
074: keysnvalues.put(param_names[i], pfixReq.getRequestParam(
075: param_names[i]).getValue());
076: }
077: exdata.setRequestParams(keysnvalues);
078:
079: SessionAdmin sessadmin = SessionAdmin.getInstance();
080: SessionInfoStruct info = sessadmin.getInfo(id);
081: ArrayList<String> steps = new ArrayList<String>();
082: if (info != null) {
083: LinkedList<SessionInfoStruct.TrailElement> trail = info
084: .getTraillog();
085: if (trail != null && trail.size() > 0) {
086: for (Iterator<SessionInfoStruct.TrailElement> j = trail
087: .listIterator(); j.hasNext();) {
088: SessionInfoStruct.TrailElement step = j.next();
089: steps.add("[" + step.getCounter() + "] "
090: + step.getStylesheetname() + " ["
091: + step.getServletname() + "]");
092: }
093: }
094: }
095: exdata.setLastSteps(steps);
096:
097: HashMap<String, String> sessdata = new HashMap<String, String>();
098: Enumeration<?> enm = session.getAttributeNames();
099: while (enm.hasMoreElements()) {
100: String key = (String) enm.nextElement();
101: Object value = session.getAttribute(key);
102: String strvalue = null;
103: try {
104: strvalue = value.toString();
105: } catch (Exception e) {
106: // Catch all exceptions here. If an exception occurs in context.toString
107: // we definitly want the exception-info to be generated.
108: LOG.error("Exception while dumping session!", e);
109: strvalue = e.getMessage() == null ? e.toString() : e
110: .getMessage();
111: }
112: sessdata.put(key, strvalue);
113: }
114:
115: exdata.setSessionKeysAndValues(sessdata);
116: return exdata;
117: }
118:
119: }
|