KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > httpPresentation > HttpUtil


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: HttpUtil.java,v 1.2 2005/03/24 10:51:16 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.appserver.server.httpPresentation;
30
31 import java.io.IOException JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Hashtable JavaDoc;
34
35 import com.lutris.http.HttpUtils;
36 import com.lutris.util.KeywordValueException;
37 import com.lutris.util.KeywordValueTable;
38
39
40 /**
41  * Class containing various static methods that are useful in implementing
42  * presentations.
43  */

44 public class HttpUtil {
45
46     /**
47      * Private constructor, prevent instantiation.
48      */

49     private HttpUtil() {}
50
51     /**
52      * Convert a HTTP query string object to a keyword/value table.
53      *
54      * @param query Hash table containing the parsed key/value pairs.
55      * @return A keyword/value table with the parsed string.
56      */

57     public static KeywordValueTable buildQueryTable(Hashtable JavaDoc query)
58         throws KeywordValueException {
59         Enumeration JavaDoc arguments = query.keys();
60         KeywordValueTable table = new KeywordValueTable();
61         while (arguments.hasMoreElements()) {
62             String JavaDoc key = (String JavaDoc)(arguments.nextElement());
63             table.set(key, query.get(key));
64         }
65         return table;
66     }
67
68     /**
69      * Convert a HTTP query string to a keyword/value table.
70      *
71      * @param queryString Query string.
72      * @return A keyword/value table with the parsed string.
73      * FIX: better description of format.
74      */

75     public static KeywordValueTable buildQueryTable(String JavaDoc queryString)
76         throws KeywordValueException {
77     return buildQueryTable(HttpUtils.parseQueryString(queryString));
78     }
79
80     /**
81      * Read in post data return it as a string. If content is not
82      * "application/x-www-form-urlencoded", return an empty string. FIX:
83      * change mime type handling as part of form upload, should generate an
84      * error if we get something we can't handle. this should be part of a
85      * module to handle input restructured.
86      */

87     private static String JavaDoc getPostData(HttpPresentationRequest httpRequest)
88             throws HttpPresentationException {
89         String JavaDoc cType = httpRequest.getContentType().toLowerCase();
90         if (cType.indexOf("application/x-www-form-urlencoded") < 0) {
91             return "";
92         } else {
93             byte[] buffer = new byte[httpRequest.getContentLength()];
94             try {
95         int count = 0;
96         int c = 0;
97         boolean done = false;
98         while ((count < buffer.length) && !done) {
99             c = httpRequest.getInputStream().read(buffer,
100                     count, buffer.length - count);
101                     if (c == -1) {
102                         done = true;
103                     } else {
104                         count += c;
105                     }
106         }
107             } catch (IOException JavaDoc except) {
108                 throw new HttpPresentationException(except);
109             }
110         return new String JavaDoc(buffer);
111         }
112     }
113
114     /**
115      * Get the query string for the request and save it in the page data.
116      * If this is a post, read the query string first.
117      *
118      * @param request HTTP request data.
119      * @exception HttpPresentationException
120      * If an error occurs accessing the request.
121      */

122     public static String JavaDoc getQueryFromRequest(HttpPresentationRequest httpRequest)
123         throws HttpPresentationException {
124
125         String JavaDoc queryString;
126         if (httpRequest.getMethod().equals("POST")) {
127             // POST method
128
queryString = getPostData(httpRequest);
129         } else {
130             // GET method
131
queryString = httpRequest.getQueryString();
132         }
133         if (queryString == null) {
134             queryString = "";
135         }
136         return queryString;
137     }
138 }
139
Popular Tags