001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.servlet;
028:
029: import java.io.IOException;
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: import javax.servlet.http.HttpServletRequest;
034:
035: /**
036: * Utility methods that may assist in writing Servlets.
037: */
038: public final class ServletUtil {
039:
040: private ServletUtil() {
041: // just utility functions.
042: }
043:
044: /**
045: * Get the "/$name" encoded Agent name from the request path.
046: */
047: public static String getEncodedAgentName(HttpServletRequest request) {
048: String uri = request.getRequestURI();
049:
050: // return everything after the '$' & before the '/'
051: String name = new String();
052: uri = uri.substring(uri.indexOf('$') + 1);
053: name = uri.substring(0, uri.indexOf('/'));
054: return name;
055: }
056:
057: /**
058: * Get the path after the "/$name".
059: */
060: public static String getPath(HttpServletRequest request) {
061: // return everything beyond $name/ in the uri
062: String uri = request.getRequestURI();
063: int begin = uri.indexOf('/', 2);
064: return uri.substring(begin);
065: }
066:
067: /**
068: * Encode a String for HTML.
069: * <p>
070: * The String should only be one line (i.e. no CRLFs).
071: * <pre>
072: * Converts:
073: * & to &amp;
074: * < to &lt;
075: * > to &gt;
076: * </pre>
077: */
078: public static final String encodeForHTML(final String s) {
079: int slen = ((s != null) ? s.length() : 0);
080: StringBuffer sb = new StringBuffer((int) (1.10 * (double) slen));
081: for (int i = 0; i < slen; i++) {
082: char ci = s.charAt(i);
083: switch (ci) {
084: default:
085: if ((ci >= ' ') && (ci <= '~')) {
086: sb.append(ci);
087: } else {
088: // unsupported?
089: sb.append("?");
090: }
091: break;
092: case '&':
093: sb.append("&");
094: break;
095: case '<':
096: sb.append("<");
097: break;
098: case '>':
099: sb.append(">");
100: break;
101: }
102: }
103: return sb.toString();
104: }
105:
106: /**
107: * Encode a String for Java.
108: * <pre>
109: * Converts:
110: * " to \\\"
111: * \ to \\
112: * CRLF to \\n
113: * </pre>
114: * Can be used to create javascript pages.
115: */
116: public static final String encodeForJava(final String s) {
117: int slen = ((s != null) ? s.length() : 0);
118: StringBuffer sb = new StringBuffer((int) (1.10 * (double) slen));
119: for (int i = 0; i < slen; i++) {
120: char ci = s.charAt(i);
121: switch (ci) {
122: default:
123: if ((ci >= ' ') && (ci <= '~')) {
124: sb.append(ci);
125: } else {
126: // unsupported?
127: sb.append("?");
128: }
129: break;
130: case '\"':
131: sb.append("\\\"");
132: break;
133: case '\\':
134: sb.append("\\\\");
135: break;
136: case '\n':
137: sb.append("\\n");
138: break;
139: }
140: }
141: return sb.toString();
142: }
143:
144: /**
145: * Simplify the parsing of URL parameters.
146: *
147: * @see ParamVisitor inner-class defined at the end of this class
148: */
149: public static void parseParams(ParamVisitor vis,
150: HttpServletRequest req) throws IOException {
151: Map m = req.getParameterMap();
152: parseParams(vis, m);
153: }
154:
155: /**
156: * Given a <code>Map</code> of (name, value) pairs, call back
157: * to the given <code>ParamVisitor</code>'s "setParam(name,value)"
158: * method.
159: *
160: * @see ParamVisitor inner-class defined at the end of this class
161: */
162: public static void parseParams(ParamVisitor vis, Map m) {
163: if (m.isEmpty()) {
164: return;
165: }
166: Iterator iter = m.entrySet().iterator();
167: while (iter.hasNext()) {
168: Map.Entry me = (Map.Entry) iter.next();
169: String key = (String) me.getKey();
170: if (key == null) {
171: continue;
172: }
173: String[] values = (String[]) me.getValue();
174: if ((values == null) || (values.length <= 0)) {
175: continue;
176: }
177: String value = values[0];
178: if (value == null) {
179: continue;
180: }
181: vis.setParam(key, value);
182: }
183: }
184:
185: /**
186: * Simple callback API for use with "setParams(..)".
187: */
188: public interface ParamVisitor {
189: void setParam(String key, String value);
190: }
191:
192: }
|