001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/portal/tags/sakai_2-4-1/portal-util/util/src/java/org/sakaiproject/portal/util/ToolURLManagerImpl.java $
003: * $Id: ToolURLManagerImpl.java 29143 2007-04-19 01:10:38Z ajpoland@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.portal.util;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.sakaiproject.tool.api.ToolURL;
029: import org.sakaiproject.tool.api.ToolURLManager;
030:
031: /**
032: * This is a default implementation of the ToolURLManager and it comes with a
033: * default implementation of ToolURL for all three URL types (render, action and
034: * resource). This implementation can be used by most portals that don't do any
035: * special URL encoding.
036: *
037: * @author <a href="mailto:vgoenka@sungardsct.com">Vishal Goenka</a>
038: */
039: public class ToolURLManagerImpl implements ToolURLManager {
040:
041: private HttpServletResponse m_response;
042:
043: /**
044: * Constructor for ToolURLComponent
045: *
046: * @param req
047: * HttpServletRequest that the URLs will be generated for
048: * @param resp
049: * HttpServletResponse that the URL will be encoded for
050: */
051:
052: public ToolURLManagerImpl(HttpServletResponse resp) {
053: m_response = resp;
054: }
055:
056: /*
057: * (non-Javadoc)
058: *
059: * @see org.sakaiproject.api.kernel.tool.ToolURLManager#createLinkURL()
060: */
061: public ToolURL createRenderURL() {
062: return new MyToolURL();
063: }
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see org.sakaiproject.api.kernel.tool.ToolURLManager#createActionURL()
069: */
070: public ToolURL createActionURL() {
071: return new MyToolURL();
072: }
073:
074: /*
075: * (non-Javadoc)
076: *
077: * @see org.sakaiproject.api.kernel.tool.ToolURLManager#createResourceURL()
078: */
079: public ToolURL createResourceURL() {
080: return new MyToolURL();
081: }
082:
083: /**
084: * This is a simple implemention of ToolURL that does the most obvious
085: * encoding of URLs.
086: *
087: * @author vgoenka
088: */
089: public class MyToolURL implements ToolURL {
090: protected String path;
091:
092: protected Map parameters;
093:
094: public MyToolURL() {
095: path = "";
096: parameters = new HashMap();
097: }
098:
099: public void setPath(String path) {
100: this .path = path;
101: }
102:
103: public void setParameter(String name, String value) {
104: if (value == null) {
105: parameters.remove(name);
106: } else {
107: parameters.put(name, value);
108: }
109: }
110:
111: public void setParameter(String name, String[] values) {
112: if ((values == null) || (values.length == 0)) {
113: parameters.remove(name);
114: } else {
115: parameters.put(name, values);
116: }
117: }
118:
119: public void setParameters(Map parameters) {
120: this .parameters = parameters;
121: }
122:
123: @Override
124: public String toString() {
125: StringBuilder rv = new StringBuilder(path);
126: // Since we allow pre-formatted query strings to be added to the
127: // path, it may already contain
128: // some parameters
129: char c = (path.indexOf('?') == -1) ? '?' : '&';
130: if (parameters.size() > 0) {
131: for (Iterator iEntries = parameters.entrySet()
132: .iterator(); iEntries.hasNext();) {
133: Map.Entry entry = (Map.Entry) iEntries.next();
134: String key = (String) entry.getKey();
135: Object val = entry.getValue();
136: if (val instanceof String[]) {
137: String[] values = (String[]) val;
138: for (int i = 0; i < values.length; i++) {
139: rv.append(c).append(key).append("=")
140: .append(values[i]);
141: c = '&';
142: }
143: } else {
144: rv.append(c).append(key).append("=").append(
145: (String) val);
146: c = '&';
147: }
148: }
149: }
150: return m_response.encodeURL(rv.toString());
151: }
152: }
153:
154: }
|