001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.context;
006:
007: import javax.servlet.http.HttpServletRequest;
008:
009: import java.util.Properties;
010: import java.util.Locale;
011: import java.util.Map;
012: import java.util.HashMap;
013: import java.util.Set;
014: import java.util.HashSet;
015: import java.util.Enumeration;
016:
017: import java.io.File;
018: import java.io.IOException;
019: import java.io.FileInputStream;
020:
021: import com.sun.portal.desktop.encode.EncoderClassNames;
022:
023: public class PropertiesClientContext implements ClientContext {
024: protected Properties properties = null;
025:
026: protected static final String DEFAULTCLIENTTYPE_KEY = "defaultClientType";
027: protected static final String CONTENTTYPE_KEY = "contentType";
028: protected static final String CLIENTPATH_KEY = "clientPath";
029: protected static final String CLIENTTYPE_KEY = "clientType";
030: protected static final String CHARSET_KEY = "charset";
031: protected static final String AUTHLESSSTATE_KEY = "authlessState";
032: protected static final String ENCODERCLASSNAME_KEY = "encoderClassName";
033:
034: private static final String PROPS_KEY_COOKIE_SUPPORT = "cookieSupport";
035: private static final String PROPS_COOKIE_SUPPORT_TRUE = "true";
036: private static final String PROPS_COOKIE_SUPPORT_FALSE = "false";
037:
038: protected static final String DEFAULT_FILENAME = "/etc/opt/SUNWportal/client-context.properties";
039:
040: private static Map AUTHLESS_STATES = null;
041:
042: static {
043: AUTHLESS_STATES = new HashMap();
044: AUTHLESS_STATES.put("none", new Short(AUTHLESS_STATE_NONE));
045: AUTHLESS_STATES.put("server", new Short(AUTHLESS_STATE_SERVER));
046: AUTHLESS_STATES.put("client", new Short(AUTHLESS_STATE_CLIENT));
047: }
048:
049: public PropertiesClientContext() {
050: this (DEFAULT_FILENAME);
051: }
052:
053: public PropertiesClientContext(String filename) {
054: properties = new Properties();
055:
056: FileInputStream fis = null;
057: try {
058: fis = new FileInputStream(new File(filename));
059: properties.load(fis);
060: } catch (IOException ioe) {
061: throw new ContextError("PropertiesClientContext(): ", ioe);
062: }
063: }
064:
065: public void init() {
066: //nothing needs to be done for property based client context
067: }
068:
069: public String getContentType(String clientType) {
070: return properties.getProperty(CONTENTTYPE_KEY);
071: }
072:
073: public String getClientPath(String clientType) {
074: return properties.getProperty(CLIENTPATH_KEY);
075: }
076:
077: public String getClientType(HttpServletRequest req) {
078: return properties.getProperty(CLIENTTYPE_KEY);
079: }
080:
081: public String getDefaultClientType() {
082: return properties.getProperty(DEFAULTCLIENTTYPE_KEY);
083: }
084:
085: public String getClientTypeProperty(String clientType, String key) {
086: return properties.getProperty(key);
087: }
088:
089: public Set getClientTypeProperties(String clientType, String key) {
090: Set p = new HashSet();
091: p.add(properties.getProperty(key));
092: return p;
093: }
094:
095: public String getCharset(String clientType, Locale locale) {
096: return properties.getProperty(CHARSET_KEY);
097: }
098:
099: public short getCookieSupport(String clientType) {
100: String cookieSupport = properties
101: .getProperty(PROPS_KEY_COOKIE_SUPPORT);
102: short retval = COOKIE_SUPPORT_DETECT;
103: // support for cookieDetect, cant't assume boolean
104: if (cookieSupport != null) {
105: if (cookieSupport
106: .equalsIgnoreCase(PROPS_COOKIE_SUPPORT_TRUE)) {
107: retval = COOKIE_SUPPORT_TRUE;
108: } else if (cookieSupport
109: .equalsIgnoreCase(PROPS_COOKIE_SUPPORT_FALSE)) {
110: retval = COOKIE_SUPPORT_FALSE;
111: } else {
112: retval = COOKIE_SUPPORT_DETECT;
113: }
114: }
115: return retval;
116: }
117:
118: public String getEncoderClassName(String clientType) {
119: String cn = properties.getProperty(ENCODERCLASSNAME_KEY);
120:
121: if (cn != null && cn.length() != 0) {
122: return cn;
123: } else {
124: return EncoderClassNames.ENCODER_DEFAULT;
125: }
126: }
127:
128: private short getDefaultAuthlessState(String clientType) {
129: boolean genericHTML = false;
130:
131: String property = getClientTypeProperty(clientType,
132: "genericHTML");
133: if (property != null) {
134: genericHTML = property.equals("true");
135: } else if (clientType.equals("genericHTML")) {
136: genericHTML = true;
137: }
138: //
139: // only "html" devices default to having authless state.
140: // non-html devices default to having no authless state.
141: //
142: if (genericHTML) {
143: return AUTHLESS_STATE_CLIENT;
144: } else {
145: return AUTHLESS_STATE_NONE;
146: }
147: }
148:
149: public short getAuthlessState(String clientType) {
150: String authlessState = properties
151: .getProperty(AUTHLESSSTATE_KEY);
152: if (authlessState == null || authlessState.length() == 0) {
153: return getDefaultAuthlessState(clientType);
154: }
155:
156: Short as = (Short) AUTHLESS_STATES.get(authlessState);
157: if (as == null) {
158: return getDefaultAuthlessState(clientType);
159: }
160:
161: return as.shortValue();
162: }
163: }
|