001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/velocity/tags/sakai_2-4-1/tool/src/java/org/sakaiproject/util/ParameterParser.java $
003: * $Id: ParameterParser.java 21413 2007-02-13 17:22:50Z jimeng@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.util;
021:
022: import java.io.IOException;
023: import java.util.Iterator;
024:
025: import javax.servlet.http.HttpServletRequest;
026:
027: /**
028: * ParameterParser is a wrapper over the request that provides compatibility with Sakai 1.5 and before.
029: */
030: public class ParameterParser {
031: /** The request. */
032: protected HttpServletRequest m_req = null;
033:
034: /**
035: * Construct with this request.
036: *
037: * @param req
038: * The current request.
039: */
040: public ParameterParser(HttpServletRequest req) {
041: m_req = req;
042: }
043:
044: /**
045: * Access the parameter names.
046: *
047: * @return An Iterator of parameter names (String).
048: */
049: public Iterator getNames() {
050: return new EnumerationIterator(m_req.getParameterNames());
051: }
052:
053: /**
054: * Get a (String) parameter by name.
055: *
056: * @param name
057: * The parameter name.
058: * @return The parameter value, or null if it's not defined.
059: */
060: public String get(String name) {
061: return m_req.getParameter(name);
062: }
063:
064: /**
065: * Get a (String) parameter by name.
066: *
067: * @param name
068: * The parameter name.
069: * @return The parameter value, or null if it's not defined.
070: */
071: public String getString(String name) {
072: return get(name);
073: }
074:
075: /**
076: * Get a (String[]) multi-valued parameter by name.
077: *
078: * @param name
079: * The parameter name.
080: * @return The parameter values array (of String), or null if it's not defined.
081: */
082: public String[] getStrings(String name) {
083: return m_req.getParameterValues(name);
084: }
085:
086: /**
087: * Get a boolean parameter by name.
088: *
089: * @param name
090: * The parameter name.
091: * @return The parameter boolean value, or false if it's not defined.
092: */
093: public boolean getBoolean(String name) {
094: return "true".equalsIgnoreCase(get(name));
095: }
096:
097: /**
098: * Get an int parameter by name, with default.
099: *
100: * @param name
101: * The parameter name.
102: * @param deflt
103: * The default value.
104: * @return The parameter int value, or the default if it's not defined or not int.
105: */
106: public int getInt(String name, int deflt) {
107: try {
108: return Integer.parseInt(get(name));
109: } catch (Throwable t) {
110: return deflt;
111: }
112: }
113:
114: /**
115: * Get an int parameter by name.
116: *
117: * @param name
118: * The parameter name.
119: * @return The parameter int value, or 0 if it's not defined or not int.
120: */
121: public int getInt(String name) {
122: return getInt(name, 0);
123: }
124:
125: /**
126: * Clean the user input string of strange newlines, etc.
127: *
128: * @param value
129: * The user input string.
130: * @return value cleaned of string newlines, etc.
131: */
132: public String getCleanString(String name) {
133: String value = getString(name);
134: if (value == null)
135: return null;
136: if (value.length() == 0)
137: return value;
138:
139: final int len = value.length();
140: StringBuffer buf = new StringBuffer();
141:
142: for (int i = 0; i < len; i++) {
143: char c = value.charAt(i);
144: char next = 0;
145: if (i + 1 < len)
146: next = value.charAt(i + 1);
147:
148: switch (c) {
149: case '\r': {
150: // detect CR LF, make it a \n
151: if (next == '\n') {
152: buf.append('\n');
153: // eat the next character
154: i++;
155: } else {
156: buf.append(c);
157: }
158:
159: }
160: break;
161:
162: default: {
163: buf.append(c);
164: }
165: }
166: }
167:
168: if (buf.charAt(buf.length() - 1) == '\n') {
169: buf.setLength(buf.length() - 1);
170: }
171:
172: return buf.toString();
173: }
174:
175: /**
176: * Access the pathInfo.
177: *
178: * @return The pathInfo.
179: */
180: public String getPath() {
181: return m_req.getPathInfo();
182: }
183:
184: /**
185: * Get a FileItem parameter by name.
186: *
187: * @param name
188: * The parameter name.
189: * @return The parameter FileItem value, or null if it's not defined.
190: */
191: public FileItem getFileItem(String name) {
192: // wrap the Apache FileItem in our own homegrown FileItem
193: Object o = m_req.getAttribute(name);
194: if (o != null
195: && o instanceof org.apache.commons.fileupload.FileItem) {
196: org.apache.commons.fileupload.FileItem item = (org.apache.commons.fileupload.FileItem) o;
197: try {
198: return new FileItem(item.getName(), item
199: .getContentType(), item.getInputStream());
200: } catch (IOException e) {
201: return new FileItem(item.getName(), item
202: .getContentType(), item.get());
203: }
204: }
205:
206: return null;
207: }
208:
209: }
|