001: /* Copyright 2001 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;
007:
008: import java.util.Enumeration;
009: import java.util.HashMap;
010: import java.util.Iterator;
011: import java.util.Map;
012:
013: import javax.servlet.http.Cookie;
014: import javax.servlet.http.HttpServletRequest;
015:
016: /*
017: * This class contains basic information about the browser.
018: * It stores all of the headers and cookies
019: */
020: public class BrowserInfo {
021: protected Cookie[] cookies;
022: protected Map headers;
023:
024: /**
025: * Constructs a new empty browser info
026: */
027: public BrowserInfo() {
028: // Note: A default constructor is needed
029: // in order to use Apache Axis's BeanSerializer to
030: // serialize this object, so please don't remove it
031: }
032:
033: /**
034: * Constructs a new browser info with supplied cookies and header info
035: * @param cookies an array of cookies
036: * @param headers a Map of headers
037: */
038: public BrowserInfo(Cookie[] cookies, Map headers) {
039: this .cookies = cookies;
040: this .headers = headers;
041: }
042:
043: /**
044: * Construct a new browser info based on HTTP request.
045: * @param req a <code>HttpServletRequest</code> value
046: */
047: public BrowserInfo(HttpServletRequest req) {
048: headers = new HashMap();
049: for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) {
050: String hName = (String) e.nextElement();
051: // Request header names are case insensitive, so BrowserInfo must be too!!
052: headers.put(hName.toLowerCase(), req.getHeader(hName));
053: }
054: cookies = req.getCookies();
055: }
056:
057: public Cookie[] getCookies() {
058: return cookies;
059: }
060:
061: public void setCookies(Cookie[] cookies) {
062: this .cookies = cookies;
063: }
064:
065: /**
066: * Get value of a particular header.
067: * @param hName a <code>String</code> value
068: * @return a <code>String</code> value
069: */
070: public String getHeader(String hName) {
071: return (String) headers.get(hName.toLowerCase());
072: }
073:
074: /**
075: * Get the headers as a Map.
076: * @return a map of headers
077: */
078: public Map getHeaders() {
079: return headers;
080: }
081:
082: /**
083: * Sets the headers.
084: * @param headers a Map of headers
085: */
086: public void setHeaders(Map headers) {
087: this .headers = headers;
088: }
089:
090: /**
091: * Obtain a "user-agent" header contained in the request.
092: * @return a <code>String</code> value
093: */
094: public String getUserAgent() {
095: String ua = (String) headers.get("user-agent");
096: if (ua == null || ua.equals("")) {
097: ua = MediaManager.NULL_USER_AGENT;
098: }
099: return ua;
100: }
101:
102: /**
103: * Overrides Object's toString(). The string form of this object is
104: * sometimes used to generate a key for caching objects in the portal.
105: * @return state the state of this object in string form
106: */
107: public String toString() {
108: StringBuffer sb = new StringBuffer(1024);
109: // Skip cookies for now and just print out headers
110: Iterator iter = headers.keySet().iterator();
111: while (iter.hasNext()) {
112: String header = (String) iter.next();
113: sb.append("[").append(header).append("]");
114: sb.append("=");
115: sb.append("[").append(headers.get(header)).append("] ");
116: }
117: return sb.toString();
118: }
119: }
|