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.rproxy.admin;
006:
007: import java.util.LinkedList;
008: import java.util.MissingResourceException;
009: import java.util.ResourceBundle;
010: import java.util.StringTokenizer;
011:
012: import com.iplanet.am.console.base.model.AMAdminLog;
013: import com.iplanet.am.console.base.model.AMModelBase;
014: import com.iplanet.am.util.Debug;
015: import com.iplanet.am.util.Locale;
016: import com.iplanet.jato.ModelManager;
017: import com.iplanet.jato.ModelTypeMap;
018: import com.iplanet.jato.RequestContext;
019: import com.iplanet.sso.SSOException;
020: import com.iplanet.sso.SSOToken;
021: import com.iplanet.sso.SSOTokenManager;
022:
023: /**
024: * Model manager for rewriter admin module.
025: */
026: public class GatewayAdminModelManager extends ModelManager {
027:
028: public static final String SRAP_GATEWAY_MODEL_MGR_KEY = "srapGatewayModelMgrKey";
029:
030: private static final String TOKEN_DELIMITER = "|";
031:
032: protected SSOToken token = null;
033:
034: protected String rbName = "srapgwadminmsg";
035:
036: protected ResourceBundle bundle = null;
037:
038: protected java.util.Locale locale = null;
039:
040: protected AMAdminLog logger = null;
041:
042: private static Debug debug = AMModelBase.debug;
043:
044: static {
045: if (AMModelBase.debug == null) {
046: debug = AMModelBase.debug = com.iplanet.am.util.Debug
047: .getInstance("amConsole");
048: }
049: }
050:
051: /**
052: * Constructor
053: *
054: * @param requestContext
055: * is the JATO request context.
056: * @param typeMap
057: * is the JATO ModelTypeMap object.
058: */
059: public GatewayAdminModelManager(RequestContext requestContext,
060: ModelTypeMap typeMap) {
061: super (requestContext, typeMap);
062: initSSOToken(requestContext);
063: initRB();
064: initLogger();
065: }
066:
067: /**
068: * Initializes the SSO token for use in this request. The SSO token can then
069: * be used as a store for session info across requests.
070: *
071: * @param requestContext
072: * is the JATO request context.
073: */
074: void initSSOToken(RequestContext requestContext) {
075: try {
076: SSOTokenManager manager = SSOTokenManager.getInstance();
077: token = manager.createSSOToken(requestContext.getRequest());
078: manager.getInstance().validateToken(token);
079: } catch (Exception e) {
080: /*
081: * XXX log the error.
082: */
083: token = null;
084: }
085: }
086:
087: /**
088: * Initializes the resuource bundle using the locale from the SSO token.
089: */
090: void initRB() {
091: if (token != null) {
092: try {
093: String lstr = token.getProperty("Locale");
094: locale = Locale.getLocale(lstr);
095: } catch (SSOException ssoe) {
096: locale = java.util.Locale.getDefault();
097: }
098: } else {
099: locale = java.util.Locale.getDefault();
100: }
101: try {
102: bundle = ResourceBundle.getBundle(rbName, locale);
103: } catch (MissingResourceException mre) {
104: // XXX write debug msg
105: }
106: }
107:
108: /**
109: * Initializes the logger using the SSO token.
110: */
111: void initLogger() {
112: if (token != null) {
113: logger = new AMAdminLog(token);
114: }
115: }
116:
117: public void doLog(String msgString) {
118: if (logger != null) {
119: logger.doLog(msgString);
120: }
121: }
122:
123: public static boolean warningEnabled() {
124: return debug.warningEnabled();
125: }
126:
127: public static boolean messageEnabled() {
128: return debug.messageEnabled();
129: }
130:
131: public static void debugWarning(String message) {
132: if (debug.warningEnabled())
133: debug.warning(message);
134: }
135:
136: public static void debugWarning(String message, Throwable t) {
137: if (debug.warningEnabled())
138: debug.warning(message, t);
139: }
140:
141: public static void debugMessage(String message) {
142: if (debug.messageEnabled())
143: debug.message(message);
144: }
145:
146: public static void debugMessage(String message, Throwable t) {
147: if (debug.messageEnabled())
148: debug.message(message, t);
149: }
150:
151: public static void debugError(String message) {
152: debug.error(message);
153: }
154:
155: public static void debugError(String message, Throwable t) {
156: debug.error(message, t);
157: }
158:
159: /**
160: * Returns the localized resource string for the corresponding resource key.
161: */
162: public String getString(String key) {
163: String result = null;
164: try {
165: if (bundle != null) {
166: result = bundle.getString(key);
167: }
168: } catch (MissingResourceException mre) {
169: // XXX write debug msg
170: }
171: return result;
172: }
173:
174: /**
175: * Can the SSO token be used for storing infomation across multiple
176: * requests?
177: *
178: * @return true if the SSO token is used to store session info; otherwise
179: * return false.
180: */
181: protected boolean useSSOToekn() {
182: return (token == null) ? false : true;
183: }
184:
185: protected String getDelimitedValue(String[] values) {
186: StringBuffer sb = new StringBuffer();
187: for (int i = 0; i < values.length; i++) {
188: sb.append(values[i]);
189: if (i < (values.length - 1)) {
190: sb.append(TOKEN_DELIMITER);
191: }
192: }
193: return sb.toString();
194: }
195:
196: protected String[] getUnDelimitedValue(String value) {
197: StringTokenizer st = new StringTokenizer(value, TOKEN_DELIMITER);
198: java.util.List result = new LinkedList();
199: while (st.hasMoreTokens()) {
200: String token = st.nextToken();
201: result.add(token);
202: }
203: return (String[]) result.toArray(new String[] {});
204: }
205:
206: /**
207: * Stores the name/value(s) pair so that the information can be maintained
208: * across multiple requests. Removes any prior value(s) that are stored
209: * under the same name before storing the new value(s).
210: *
211: * @param name
212: * the name under which the value(s) are store.
213: * @param value
214: * an array of String object(s) of the actual value(s).
215: */
216: public void storeToSession(String name, String[] values) {
217: String delimitedValue = null;
218: if ((name != null) && (!name.equals("")) && (values != null)) {
219: delimitedValue = getDelimitedValue(values);
220: }
221:
222: if (useSSOToekn()) {
223: try {
224: token.setProperty(name, delimitedValue);
225: } catch (SSOException ssoe) {
226: /* XXX log this error */
227: }
228: } else {
229: getRequestContext().getRequest().getSession().setAttribute(
230: name, delimitedValue);
231: }
232: }
233:
234: /**
235: * Returns the value(s) associated with the given name in the name/value(s)
236: * store.
237: *
238: * @param name
239: * the name under which the value(s) are store.
240: * @return an array of String object(s) of the actual value(s). Returns an
241: * empty array if no value(s) are found.
242: */
243: public String[] getFromSession(String name) {
244: String delimitedValue = null;
245: if (useSSOToekn()) {
246: try {
247: delimitedValue = token.getProperty(name);
248: } catch (SSOException ssoe) {
249: /* XXX log this error */
250: }
251: } else {
252: delimitedValue = (String) getRequestContext().getRequest()
253: .getSession().getAttribute(name);
254: }
255: return getUnDelimitedValue(delimitedValue);
256: }
257: }
|