001: package com.sun.portal.proxylet.util;
002:
003: import java.lang.*;
004: import java.util.*;
005: import java.io.*;
006: import javax.servlet.*;
007: import javax.servlet.http.*;
008:
009: import com.sun.portal.desktop.DesktopException;
010: import com.sun.portal.desktop.DesktopRequest;
011: import com.sun.portal.desktop.context.DesktopContext;
012:
013: public class ParameterMap extends HashMap {
014: public ParameterMap(Map p) {
015: super (p);
016: }
017:
018: public ParameterMap(String charset, HttpServletRequest req)
019: throws DesktopException {
020: this (charset, req, false);
021: }
022:
023: public ParameterMap(String charset, HttpServletRequest req,
024: boolean readFromParameters) throws DesktopException {
025: Hashtable data = null;
026:
027: if (readFromParameters) {
028: for (Enumeration e = req.getParameterNames(); e
029: .hasMoreElements();) {
030: String name = (String) e.nextElement();
031: String[] val = new String[1];
032: val[0] = req.getParameter(name);
033: put(name, val);
034: }
035: } else if (req.getMethod().equalsIgnoreCase("POST")) {
036: ServletInputStream is = null;
037: try {
038: is = req.getInputStream();
039: } catch (IOException ioe) {
040:
041: }
042:
043: data = HttpUtils.parsePostData(req.getContentLength(), is);
044: } else if (req.getMethod().equalsIgnoreCase("GET")) {
045: String query = req.getQueryString();
046:
047: if (query == null) {
048: query = "";
049: }
050:
051: data = HttpUtils.parseQueryString(query);
052: }
053:
054: if (data != null) {
055: putAll(data);
056: decodeParams(charset);
057: }
058: }
059:
060: private void decodeParams(String charset) {
061: for (Iterator i = keySet().iterator(); i.hasNext();) {
062: String key = (String) i.next();
063: Object o = get(key);
064: if (o instanceof String[]) {
065: String vals[] = (String[]) o;
066: for (int j = 0; j < vals.length; j++) {
067: vals[j] = I18n.decodeCharset(vals[j], charset);
068: }
069: }
070: }
071: }
072:
073: //
074: // for get[String|Int|Boolean] methods, if the array size is >1, then
075: // only the first element is returned.
076: //
077:
078: public String getString(String key) {
079: String[] array = (String[]) get(key);
080:
081: if ((array == null) || (array.length == 0)) {
082: return null;
083: }
084:
085: String val = array[0];
086:
087: return val;
088: }
089:
090: public int getInt(String key) throws NumberFormatException {
091: String[] array = (String[]) get(key);
092:
093: if ((array == null) || (array.length == 0)) {
094: throw new NumberFormatException("null parameter");
095: }
096:
097: return Integer.parseInt(array[0]);
098: }
099:
100: public boolean getBoolean(String key) {
101: String[] array = (String[]) get(key);
102:
103: if ((array == null) || (array.length == 0)) {
104: return false;
105: }
106:
107: boolean val = Boolean.valueOf(array[0]).booleanValue();
108: return val;
109: }
110:
111: public String[] getArray(String key) {
112: String[] val = (String[]) get(key);
113:
114: return val;
115: }
116:
117: public String[] putArray(String key, String[] val) {
118: String[] old = getArray(key);
119: put(key, val);
120:
121: return old;
122: }
123:
124: public String putString(String key, String val) {
125: String old = getString(key);
126:
127: String[] arrayVal = new String[1];
128: arrayVal[0] = val;
129:
130: putArray(key, arrayVal);
131:
132: return old;
133: }
134:
135: public String toString() {
136: StringBuffer s = new StringBuffer();
137: s.append("{ ");
138:
139: for (Iterator i = keySet().iterator(); i.hasNext();) {
140: String key = (String) i.next();
141: String[] val = getArray(key);
142:
143: s.append("[ ");
144: for (int j = 0; j < val.length; j++) {
145: s.append(key).append("=").append(I18n.print(val[j]))
146: .append(" ");
147: }
148: s.append("] ");
149: }
150: s.append("}");
151:
152: return s.toString();
153: }
154: }
|