001: /* Copyright 2001, 2006 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.channels;
007:
008: import java.util.Locale;
009: import java.util.ResourceBundle;
010:
011: import javax.servlet.http.HttpServletRequest;
012:
013: import org.jasig.portal.ChannelRuntimeData;
014: import org.jasig.portal.ChannelRuntimeProperties;
015: import org.jasig.portal.ChannelStaticData;
016: import org.jasig.portal.IPrivilegedChannel;
017: import org.jasig.portal.PortalControlStructures;
018: import org.jasig.portal.PortalEvent;
019: import org.jasig.portal.PortalException;
020: import org.jasig.portal.i18n.LocaleManager;
021: import org.jasig.portal.utils.DocumentFactory;
022: import org.jasig.portal.utils.XSLT;
023: import org.w3c.dom.Document;
024: import org.w3c.dom.Element;
025: import org.xml.sax.ContentHandler;
026:
027: /**
028: * <p>A channel which displays HTTP request and HTML header info.
029: * This channel implements IPrivilegedChannel rather than
030: * IChannel because it needs access to the HttpServletRequest object.</p>
031: * <p>This channel was partially developed at Columbia University
032: * as an exercise.</p>
033: * @author Ken Weiner, kweiner@unicon.net
034: * @version $Revision: 36688 $
035: */
036: public class CSnoop implements IPrivilegedChannel {
037: private PortalControlStructures pcs;
038: private ChannelRuntimeData runtimeData;
039:
040: private static final String sslLocation = "CSnoop/CSnoop.ssl";
041: private static final String bundleLocation = "/org/jasig/portal/channels/CSnoop/CSnoop";
042:
043: /**
044: * No-argument constructor for CSnoop.
045: */
046: public CSnoop() {
047: this .runtimeData = new ChannelRuntimeData();
048: }
049:
050: /**
051: * Sends portal control structures to the portal, i.e. HttpServletRequest,
052: * HttpServletResponse, UserPreferencesManager, etc.
053: * @param pcs the portal control structures
054: */
055: public void setPortalControlStructures(PortalControlStructures pcs) {
056: this .pcs = pcs;
057: }
058:
059: /**
060: * Returns channel runtime properties.
061: * @return handle to runtime properties
062: */
063: public ChannelRuntimeProperties getRuntimeProperties() {
064: // Channel will always render, so the default values are ok
065: return new ChannelRuntimeProperties();
066: }
067:
068: /**
069: * Processes layout-level events coming from the portal
070: * @param ev a portal layout event
071: */
072: public void receiveEvent(PortalEvent ev) {
073: // no events for this channel
074: }
075:
076: /**
077: * Receive static channel data from the portal
078: * @param sd static channel data
079: */
080: public void setStaticData(ChannelStaticData sd) {
081: }
082:
083: /**
084: * Receives channel runtime data from the portal and processes actions
085: * passed to it. The names of these parameters are entirely up to the channel.
086: * @param rd handle to channel runtime data
087: */
088: public void setRuntimeData(ChannelRuntimeData rd) {
089: this .runtimeData = rd;
090: }
091:
092: /**
093: * Output channel content to the portal
094: * @param out a sax document handler
095: */
096: public void renderXML(ContentHandler out) throws PortalException {
097: HttpServletRequest request = pcs.getHttpServletRequest();
098: Document doc = DocumentFactory.getNewDocument();
099:
100: // <snooper>
101: Element snooperE = doc.createElement("snooper");
102:
103: // <request-info>
104: Element requestInfoE = doc.createElement("request-info");
105:
106: addInfo(requestInfoE, "request-protocol", request.getProtocol());
107: addInfo(requestInfoE, "request-method", request.getMethod());
108: addInfo(requestInfoE, "server-name", request.getServerName());
109: addInfo(requestInfoE, "server-port", String.valueOf(request
110: .getServerPort()));
111: addInfo(requestInfoE, "request-uri", request.getRequestURI());
112: addInfo(requestInfoE, "context-path", request.getContextPath());
113: addInfo(requestInfoE, "servlet-path", request.getServletPath());
114: addInfo(requestInfoE, "query-string", request.getQueryString());
115: addInfo(requestInfoE, "path-info", request.getPathInfo());
116: addInfo(requestInfoE, "path-translated", request
117: .getPathTranslated());
118: addInfo(requestInfoE, "content-length", String.valueOf(request
119: .getContentLength()));
120: addInfo(requestInfoE, "content-type", request.getContentType());
121: addInfo(requestInfoE, "remote-user", request.getRemoteUser());
122: addInfo(requestInfoE, "remote-address", request.getRemoteAddr());
123: addInfo(requestInfoE, "remote-host", request.getRemoteHost());
124: addInfo(requestInfoE, "authorization-scheme", request
125: .getAuthType());
126: addInfo(requestInfoE, "locale", request.getLocale().toString());
127:
128: // <headers>
129: Element headersE = doc.createElement("headers");
130: java.util.Enumeration enum1 = request.getHeaderNames();
131: while (enum1.hasMoreElements()) {
132: String name = (String) enum1.nextElement();
133: String value = request.getHeader(name);
134: Element headerE = doc.createElement("header");
135: headerE.setAttribute("name", name);
136: headerE.appendChild(doc.createTextNode(value));
137: headersE.appendChild(headerE);
138: }
139: requestInfoE.appendChild(headersE);
140: snooperE.appendChild(requestInfoE);
141:
142: // <channel-runtime-data>
143: Element channelRuntimeDataE = doc
144: .createElement("channel-runtime-data");
145:
146: // <locales>
147: Locale[] locales = runtimeData.getLocales();
148: if (locales == null) { // Take this out if locales are guaranteed
149: locales = new Locale[] { Locale.getDefault() };
150: }
151: Element localesE = LocaleManager.xmlValueOf(locales)
152: .getDocumentElement();
153: channelRuntimeDataE.appendChild(doc.importNode(localesE, true));
154:
155: snooperE.appendChild(channelRuntimeDataE);
156:
157: doc.appendChild(snooperE);
158:
159: ResourceBundle l18n = ResourceBundle.getBundle(bundleLocation,
160: runtimeData.getLocales()[0]);
161: // Now perform the transformation
162: XSLT xslt = XSLT.getTransformer(this );
163: xslt.setResourceBundle(l18n);
164: xslt.setXML(doc);
165: xslt.setXSL(sslLocation, runtimeData.getBrowserInfo());
166: xslt.setTarget(out);
167: xslt.transform();
168: }
169:
170: /**
171: * Adds a text node with the given name and value. If the value is null then no text node
172: * is added to the new node, but the new node is still added to parentElement
173: * @param parentElement parent of the node to be added
174: * @param name name of the node to add
175: * @param value String value of the node to add
176: */
177: private void addInfo(Element parentElement, String name,
178: String value) {
179: Document doc = parentElement.getOwnerDocument();
180: Element e = doc.createElement(name);
181: if (value != null) {
182: e.appendChild(doc.createTextNode(value));
183: }
184: parentElement.appendChild(e);
185: }
186: }
|