001: /**
002: * $Id: PortletWindowChannelURL.java,v 1.5 2005/12/06 06:31:29 vs152012 Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.providers.portletwindow;
014:
015: import com.sun.portal.container.ChannelMode;
016: import com.sun.portal.container.ChannelURL;
017: import com.sun.portal.container.WindowState;
018: import com.sun.portal.desktop.DesktopRequest;
019: import com.sun.portal.log.common.PortalLogger;
020:
021: import java.io.UnsupportedEncodingException;
022: import java.net.URLEncoder;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028:
029: //
030: // TODO: secure thing -- what is allowed or not
031: //
032:
033: public class PortletWindowChannelURL implements ChannelURL {
034:
035: private String _desktopURL;
036: private Map _params = new HashMap();
037: private String _action;
038: private ChannelMode _newChannelMode;
039: private WindowState _newWindowState;
040: private boolean _secure = false;
041: private static Logger logger = PortalLogger
042: .getLogger(PortletWindowChannelURL.class);
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: private static final String ENC = "UTF-8";
053:
054: public PortletWindowChannelURL(String desktopURL) {
055: _desktopURL = desktopURL;
056: }
057:
058: public void setChannelMode(ChannelMode newChannelMode) {
059: _newChannelMode = newChannelMode;
060: }
061:
062: public void setWindowState(WindowState newWindowState) {
063: _newWindowState = newWindowState;
064: }
065:
066: public void setURLType(String type) {
067: _action = type;
068: }
069:
070: public void setParameter(String name, String value) {
071: if (_params == null) {
072: _params = new HashMap();
073: }
074: _params.put(name, value);
075: }
076:
077: public void setParameter(String name, String[] values) {
078: if (_params == null) {
079: _params = new HashMap();
080: }
081: _params.put(name, values);
082: }
083:
084: public void setParameters(Map parametersMap) {
085: _params = parametersMap;
086: }
087:
088: public void setSecure(boolean secure) {
089: _secure = secure;
090: }
091:
092: public WindowState getWindowState() {
093: return _newWindowState;
094: }
095:
096: public ChannelMode getChannelMode() {
097: return _newChannelMode;
098: }
099:
100: public String getURLType() {
101: return _action;
102: }
103:
104: public Map getParameters() {
105: return _params;
106: }
107:
108: public boolean isSecure() {
109: return _secure;
110: }
111:
112: public String toString() {
113: StringBuffer buf = new StringBuffer();
114:
115: buf.append(_desktopURL);
116: buf.append("&");
117: buf.append(PORTLET_ACTION).append("=").append(_action);
118: if (_newWindowState != null) {
119: buf.append("&");
120: buf.append(NEW_WINDOW_STATE).append("=").append(
121: _newWindowState);
122: }
123: if (_newChannelMode != null) {
124: buf.append("&");
125: buf.append(NEW_CHANNEL_MODE).append("=").append(
126: _newChannelMode);
127: }
128:
129: if (_params != null) {
130: Iterator i = _params.keySet().iterator();
131: while (i.hasNext()) {
132: String key = (String) i.next();
133: Object val = _params.get(key);
134:
135: try {
136: if (isEncodingNeeded(key)) {
137: key = URLEncoder.encode(key, ENC);
138: }
139:
140: if (val instanceof String) {
141: if (isEncodingNeeded((String) val)) {
142: val = URLEncoder.encode((String) val, ENC);
143: }
144: buf.append("&").append(key);
145: buf.append("=").append((String) val);
146: } else if (val instanceof String[]) {
147: String[] vals = (String[]) val;
148: for (int j = 0; j < vals.length; j++) {
149: if (isEncodingNeeded(vals[j])) {
150: vals[j] = URLEncoder.encode(vals[j],
151: ENC);
152: }
153: buf.append("&").append(key);
154: buf.append("=").append((String) (vals[j]));
155: }
156: }
157: } catch (UnsupportedEncodingException uee) {
158: logger.log(Level.INFO, "PSDT_CSPPPW0003", uee);
159: }
160: }
161: }
162:
163: return buf.toString();
164: }
165:
166: /**
167: * Before any URL encoding, check to see if encoding is needed.
168: *
169: * @param value value to be checked
170: * @return <code>boolean</code>
171: */
172: private boolean isEncodingNeeded(String value) {
173: boolean needsEncoding = false;
174: int length = value.length();
175: char c;
176: for (int i = 0; !needsEncoding && i < length; i++) {
177: c = value.charAt(i);
178: needsEncoding = (c < 'a' || c > 'z')
179: && (c < 'A' || c > 'Z') && c != '.' && c != '-'
180: && c != '*' && c != '_';
181: }
182: return needsEncoding;
183: }
184:
185: }
|