001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: HttpUtil.java,v 1.2 2006-06-15 13:44:07 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.httpPresentation;
025:
026: import java.io.IOException;
027: import java.util.Enumeration;
028: import java.util.Hashtable;
029:
030: import com.lutris.http.HttpUtils;
031: import com.lutris.util.KeywordValueException;
032: import com.lutris.util.KeywordValueTable;
033:
034: /**
035: * Class containing various static methods that are useful in implementing
036: * presentations.
037: */
038: public class HttpUtil {
039:
040: /**
041: * Private constructor, prevent instantiation.
042: */
043: private HttpUtil() {
044: }
045:
046: /**
047: * Convert a HTTP query string object to a keyword/value table.
048: *
049: * @param query Hash table containing the parsed key/value pairs.
050: * @return A keyword/value table with the parsed string.
051: */
052: public static KeywordValueTable buildQueryTable(Hashtable query)
053: throws KeywordValueException {
054: Enumeration arguments = query.keys();
055: KeywordValueTable table = new KeywordValueTable();
056: while (arguments.hasMoreElements()) {
057: String key = (String) (arguments.nextElement());
058: table.set(key, query.get(key));
059: }
060: return table;
061: }
062:
063: /**
064: * Convert a HTTP query string to a keyword/value table.
065: *
066: * @param queryString Query string.
067: * @return A keyword/value table with the parsed string.
068: * FIX: better description of format.
069: */
070: public static KeywordValueTable buildQueryTable(String queryString)
071: throws KeywordValueException {
072: return buildQueryTable(HttpUtils.parseQueryString(queryString));
073: }
074:
075: /**
076: * Read in post data return it as a string. If content is not
077: * "application/x-www-form-urlencoded", return an empty string. FIX:
078: * change mime type handling as part of form upload, should generate an
079: * error if we get something we can't handle. this should be part of a
080: * module to handle input restructured.
081: */
082: private static String getPostData(
083: HttpPresentationRequest httpRequest)
084: throws HttpPresentationException {
085: String cType = httpRequest.getContentType().toLowerCase();
086: if (cType.indexOf("application/x-www-form-urlencoded") < 0) {
087: return "";
088: } else {
089: byte[] buffer = new byte[httpRequest.getContentLength()];
090: try {
091: int count = 0;
092: int c = 0;
093: boolean done = false;
094: while ((count < buffer.length) && !done) {
095: c = httpRequest.getInputStream().read(buffer,
096: count, buffer.length - count);
097: if (c == -1) {
098: done = true;
099: } else {
100: count += c;
101: }
102: }
103: } catch (IOException except) {
104: throw new HttpPresentationException(except);
105: }
106: return new String(buffer);
107: }
108: }
109:
110: /**
111: * Get the query string for the request and save it in the page data.
112: * If this is a post, read the query string first.
113: *
114: * @param request HTTP request data.
115: * @exception HttpPresentationException
116: * If an error occurs accessing the request.
117: */
118: public static String getQueryFromRequest(
119: HttpPresentationRequest httpRequest)
120: throws HttpPresentationException {
121:
122: String queryString;
123: if (httpRequest.getMethod().equals("POST")) {
124: // POST method
125: queryString = getPostData(httpRequest);
126: } else {
127: // GET method
128: queryString = httpRequest.getQueryString();
129: }
130: if (queryString == null) {
131: queryString = "";
132: }
133: return queryString;
134: }
135: }
|