001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/velocity/tags/sakai_2-4-1/tool/src/java/org/sakaiproject/vm/ActionURL.java $
003: * $Id: ActionURL.java 7519 2006-04-09 13:02:00Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.vm;
021:
022: import java.util.Hashtable;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.servlet.http.HttpServletRequest;
027:
028: import org.sakaiproject.thread_local.cover.ThreadLocalManager;
029: import org.sakaiproject.tool.api.ToolURL;
030: import org.sakaiproject.tool.api.ToolURLManager;
031:
032: /**
033: * <p>
034: * PortletActionURL provides a URL with settable and re-settable parameters based on a portlet window's ActionURL base URL.
035: * </p>
036: */
037: public class ActionURL {
038: /** The parameter for portlet window id (pid). */
039: public final static String PARAM_PID = "pid";
040:
041: /** The parameter for site. */
042: public final static String PARAM_SITE = "site";
043:
044: /** The parameter for page. */
045: public final static String PARAM_PAGE = "page";
046:
047: /** The parameter for paneld. */
048: public final static String PARAM_PANEL = "panel";
049:
050: /** The base url to the portlet. */
051: protected String m_base = null;
052:
053: /** parameters. */
054: protected Map m_parameters = new Hashtable();
055:
056: /** The portlet window id, if any. */
057: protected String m_pid = null;
058:
059: /** The panel, if any. */
060: protected String m_panel = null;
061:
062: /** The site, if any. */
063: protected String m_site = null;
064:
065: /** The site pge, if any. */
066: protected String m_page = null;
067:
068: /** Is this an Action URL */
069: protected boolean m_isAction = false;
070:
071: /** Is this a Resource URL */
072: protected String m_resourcePath = null;
073:
074: /** Pre-formatted query string, in lieu of <name, value> parameters */
075: protected String m_QueryString = "";
076:
077: /** HttpServletRequest * */
078: protected HttpServletRequest m_request;
079:
080: /**
081: * Construct with a base URL to the portlet, no parameters
082: *
083: * @param base
084: * The base URL
085: */
086: public ActionURL(String base, HttpServletRequest request) {
087: m_base = base;
088: m_request = request;
089: }
090:
091: /**
092: * "Reset" the URL by clearing the parameters.
093: *
094: * @return this.
095: */
096: public ActionURL reset() {
097: m_parameters = new Hashtable();
098: m_isAction = false;
099: m_resourcePath = null;
100: m_QueryString = "";
101: return this ;
102: }
103:
104: /**
105: * Set or replace (or remove if value is null) a parameter
106: *
107: * @param name
108: * The parameter name.
109: * @param value
110: * The parameter value.
111: * @return this.
112: */
113: public ActionURL setParameter(String name, String value) {
114: if (value == null) {
115: m_parameters.remove(name);
116: }
117:
118: else {
119: m_parameters.put(name, value);
120: }
121:
122: return this ;
123: }
124:
125: /**
126: * Set this URL to be an 'action' URL, one that usually does a Form POST
127: *
128: * @return this
129: */
130:
131: public ActionURL setAction() {
132: m_isAction = true;
133: return this ;
134: }
135:
136: /**
137: * Set or reset the pid.
138: *
139: * @param pid
140: * The portlet window id.
141: */
142: public ActionURL setPid(String pid) {
143: m_pid = pid;
144: return this ;
145: }
146:
147: /**
148: * Set or reset the site.
149: *
150: * @param site
151: * The site id.
152: */
153: public ActionURL setSite(String site) {
154: m_site = site;
155: return this ;
156: }
157:
158: /**
159: * Set or reset the page.
160: *
161: * @param page
162: * The page id.
163: */
164: public ActionURL setPage(String page) {
165: m_page = page;
166: return this ;
167: }
168:
169: /**
170: * Set or reset the panel.
171: *
172: * @param panel
173: * The panel id.
174: */
175: public ActionURL setPanel(String panel) {
176: m_panel = panel;
177: return this ;
178: }
179:
180: /**
181: * Reneder the URL with parameters
182: *
183: * @return The URL.
184: */
185: public String toString() {
186: String toolURL = getToolURL();
187: if (toolURL != null)
188: return toolURL;
189:
190: String rv = m_base;
191: char c = '?';
192: if (m_parameters.size() > 0) {
193: for (Iterator iEntries = m_parameters.entrySet().iterator(); iEntries
194: .hasNext();) {
195: Map.Entry entry = (Map.Entry) iEntries.next();
196: rv = rv + c + entry.getKey() + "=" + entry.getValue();
197: c = '&';
198: }
199: }
200:
201: // Add pre-formatted query string as is
202: if ((m_QueryString != null) && (m_QueryString.length() > 0)) {
203: rv = rv + c + m_QueryString;
204: c = '&';
205: }
206:
207: // add the pid if defined and not overridden
208: if ((m_pid != null) && (!m_parameters.containsKey(PARAM_PID))) {
209: rv = rv + c + PARAM_PID + "=" + m_pid;
210: c = '&';
211: }
212:
213: // add the site if defined and not overridden
214: if ((m_site != null) && (!m_parameters.containsKey(PARAM_SITE))) {
215: rv = rv + c + PARAM_SITE + "=" + m_site;
216: c = '&';
217: }
218:
219: // add the page if defined and not overridden
220: if ((m_page != null) && (!m_parameters.containsKey(PARAM_PAGE))) {
221: rv = rv + c + PARAM_PAGE + "=" + m_page;
222: c = '&';
223: }
224:
225: // add the panel if defined and not overridden
226: if ((m_panel != null)
227: && (!m_parameters.containsKey(PARAM_PANEL))) {
228: rv = rv + c + PARAM_PANEL + "=" + m_panel;
229: c = '&';
230: }
231:
232: reset();
233: return rv;
234: }
235:
236: private String getToolURL() {
237: ToolURLManager urlManager = getToolURLManager();
238: // ToolURLManager is not set, use default implementation
239: if (urlManager == null)
240: return null;
241:
242: ToolURL url = null;
243: String path = m_base;
244: if (m_isAction) {
245: url = urlManager.createActionURL();
246: } else if (m_resourcePath != null) {
247: url = urlManager.createResourceURL();
248: path = m_resourcePath;
249: } else {
250: url = urlManager.createRenderURL();
251: }
252: if (url != null) {
253: if ((this .m_QueryString != null)
254: && (this .m_QueryString.length() > 0)) {
255: if (path.indexOf('?') == -1) {
256: path = path + '?' + this .m_QueryString;
257: } else {
258: path = path + '&' + this .m_QueryString;
259: }
260: }
261: url.setPath(path);
262: if ((m_pid != null)
263: && (!m_parameters.containsKey(PARAM_PID))) {
264: m_parameters.put(PARAM_PID, m_pid);
265: }
266:
267: // add the site if defined and not overridden
268: if ((m_site != null)
269: && (!m_parameters.containsKey(PARAM_SITE))) {
270: m_parameters.put(PARAM_SITE, m_site);
271: }
272:
273: // add the page if defined and not overridden
274: if ((m_page != null)
275: && (!m_parameters.containsKey(PARAM_PAGE))) {
276: m_parameters.put(PARAM_PAGE, m_page);
277: }
278:
279: // add the panel if defined and not overridden
280: if ((m_panel != null)
281: && (!m_parameters.containsKey(PARAM_PANEL))) {
282: m_parameters.put(PARAM_PANEL, m_panel);
283: }
284: url.setParameters(m_parameters);
285: reset();
286: return url.toString();
287: }
288: return null;
289: }
290:
291: private ToolURLManager getToolURLManager() {
292: HttpServletRequest request = m_request;
293: if (request == null) {
294: request = (HttpServletRequest) ThreadLocalManager
295: .get(ToolURL.HTTP_SERVLET_REQUEST);
296: }
297: if (request != null) {
298: return (ToolURLManager) request
299: .getAttribute(ToolURL.MANAGER);
300: }
301: return null;
302: }
303:
304: /**
305: * {@inheritDoc}
306: */
307: public boolean equals(Object obj) {
308: boolean equals = false;
309:
310: if ((obj != null) && (obj instanceof ActionURL)) {
311: equals = ((ActionURL) obj).toString().equals(toString());
312: }
313:
314: return equals;
315: }
316:
317: /**
318: * @param resource
319: * Whether the URL is a resource
320: */
321: public ActionURL setResourcePath(String path) {
322: m_resourcePath = path;
323: return this ;
324: }
325:
326: /**
327: * @param queryString
328: * The m_QueryString to set.
329: */
330: public ActionURL setQueryString(String queryString) {
331: m_QueryString = queryString;
332: return this;
333: }
334:
335: }
|