001: /**
002: * Copyright 2003 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.wsrp.consumer.wsrpwindow;
013:
014: import java.util.HashMap;
015: import java.util.Map;
016: import java.util.Iterator;
017: import java.util.logging.Logger;
018:
019: import com.sun.portal.container.WindowState;
020: import com.sun.portal.container.ChannelMode;
021: import com.sun.portal.container.ChannelURL;
022: import javax.servlet.http.HttpServletRequest;
023: import com.sun.portal.desktop.DesktopRequest;
024: import com.sun.portal.desktop.context.ProviderContextThreadLocalizer;
025: import com.sun.portal.providers.context.ProviderContext;
026: import com.sun.portal.wsrp.common.WSRPToContainerMap;
027:
028: import com.sun.portal.desktop.DesktopRequest;
029:
030: import java.net.URLEncoder;
031:
032: //
033: // TODO: secure thing -- what is allowed or not
034: //
035: public class WSRPWindowChannelURL implements ChannelURL {
036:
037: private String _desktopURL;
038: private Map _params = new HashMap();
039: private String _action;
040: private ChannelMode _newChannelMode;
041: private WindowState _newWindowState;
042: private boolean _secure = false;
043:
044: public final static String KEYWORD_PREFIX = DesktopRequest.PREFIX
045: + "window.";
046: public final static String NEW_CHANNEL_MODE = KEYWORD_PREFIX
047: + "newChannelMode";
048: public final static String NEW_WINDOW_STATE = KEYWORD_PREFIX
049: + "newWindowState";
050: public final static String PORTLET_ACTION = KEYWORD_PREFIX
051: + "portletAction";
052:
053: public WSRPWindowChannelURL(String desktopURL) {
054: _desktopURL = desktopURL;
055: }
056:
057: public void setChannelMode(ChannelMode newChannelMode) {
058: _newChannelMode = newChannelMode;
059: }
060:
061: public void setWindowState(WindowState newWindowState) {
062: _newWindowState = newWindowState;
063: }
064:
065: public void setURLType(String type) {
066: _action = type;
067: }
068:
069: public void setParameter(String name, String value) {
070: if (_params == null) {
071: _params = new HashMap();
072: }
073: _params.put(name, value);
074: }
075:
076: public void setParameter(String name, String[] values) {
077: if (_params == null) {
078: _params = new HashMap();
079: }
080: _params.put(name, values);
081: }
082:
083: public void setParameters(Map parametersMap) {
084: _params = parametersMap;
085: }
086:
087: public void setSecure(boolean secure) {
088: _secure = secure;
089: }
090:
091: public WindowState getWindowState() {
092: return _newWindowState;
093: }
094:
095: public ChannelMode getChannelMode() {
096: return _newChannelMode;
097: }
098:
099: public String getURLType() {
100: return _action;
101: }
102:
103: public Map getParameters() {
104: return _params;
105: }
106:
107: public boolean isSecure() {
108: return _secure;
109: }
110:
111: public String toString() {
112: StringBuffer buf = new StringBuffer();
113:
114: buf.append(_desktopURL);
115: buf.append("&").append(PORTLET_ACTION).append("=").append(
116: WSRPToContainerMap.mapURLTypeToWSRP(_action));
117: if (_newWindowState != null) {
118: buf
119: .append("&")
120: .append(NEW_WINDOW_STATE)
121: .append("=")
122: .append(
123: WSRPToContainerMap
124: .mapWindowStateToWSRP(_newWindowState));
125: }
126: if (_newChannelMode != null) {
127: buf
128: .append("&")
129: .append(NEW_CHANNEL_MODE)
130: .append("=")
131: .append(
132: WSRPToContainerMap
133: .mapChannelModeToWSRP(_newChannelMode));
134: }
135:
136: if (_params != null) {
137: Iterator i = _params.keySet().iterator();
138: while (i.hasNext()) {
139: String key = (String) i.next();
140: Object val = _params.get(key);
141:
142: if (isEncodingNeeded(key)) {
143: key = URLEncoder.encode(key);
144: }
145:
146: if (val instanceof String) {
147: if (isEncodingNeeded((String) val)) {
148: val = URLEncoder.encode((String) val);
149: }
150: buf.append("&").append(key);
151: buf.append("=").append((String) val);
152: } else if (val instanceof String[]) {
153: String[] vals = (String[]) val;
154: for (int j = 0; j < vals.length; j++) {
155: if (isEncodingNeeded(vals[j])) {
156: vals[j] = URLEncoder.encode(vals[j]);
157: }
158: buf.append("&").append(key);
159: buf.append("=").append((String) (vals[j]));
160: }
161: }
162: }
163: }
164:
165: return buf.toString();
166: }
167:
168: /**
169: * Before any URL encoding, check to see if encoding is needed.
170: *
171: * @param value value to be checked
172: * @return <code>boolean</code>
173: */
174: private boolean isEncodingNeeded(String value) {
175: boolean needsEncoding = false;
176: int length = value.length();
177: char c;
178: for (int i = 0; !needsEncoding && i < length; i++) {
179: c = value.charAt(i);
180: needsEncoding = (c < 'a' || c > 'z')
181: && (c < 'A' || c > 'Z') && c != '.' && c != '-'
182: && c != '*' && c != '_';
183: }
184: return needsEncoding;
185: }
186:
187: // TODO Need to be optimize
188: // use constants from the WSRPSpecKeys
189:
190: String getTemplate(String urlType) {
191: StringBuffer buf = new StringBuffer();
192: buf.append(_desktopURL);
193: buf.append("&");
194: buf.append(PORTLET_ACTION).append("=").append("{wsrp-urlType}");
195:
196: buf.append("&");
197: buf.append(NEW_WINDOW_STATE).append("=").append(
198: "{wsrp-windowState}");
199:
200: buf.append("&");
201: buf.append(NEW_CHANNEL_MODE).append("=").append("{wsrp-mode}");
202: buf.append("&wsrp-navigationalState={wsrp-navigationalState}");
203:
204: if (ChannelURL.ACTION_URL_TYPE.equals(urlType)) {
205: buf
206: .append("&wsrp-interactionState={wsrp-interactionState}");
207: }
208:
209: return buf.toString();
210: }
211:
212: }
|