View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.util;
22  
23  import java.io.File;
24  import java.util.*;
25  import java.util.Enumeration;
26  import java.util.HashMap;
27  import java.util.Map;
28  import javax.servlet.ServletConfig;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpSession;
31  import org.apache.commons.fileupload.*;
32  import org.apache.commons.fileupload.DiskFileUpload;
33  import org.apache.commons.fileupload.FileUpload;
34  /***
35   * The servlet utiltity
36   * @author TiongHiang Lee (thlee@onemindsoft.org)
37   * @version $Id: ServletUtils.java,v 1.3 2004/10/23 15:27:01 thlee Exp $ $Name:  $
38   */
39  public final class ServletUtils
40  {
41  
42      /***
43       * Constructor
44       */
45      private ServletUtils()
46      {
47      };
48  
49      /***
50       * Get the request environment
51       * @param req the request
52       * @return the environment in a map
53       */
54      public static Map getRequestEnvironment(HttpServletRequest req)
55      {
56          Map m = new HashMap();
57          m.put("REQUEST", req);
58          m.put("REQUEST_AUTH_TYPE", req.getAuthType());
59          //m.put("REQUEST_CONTEXT_PATH", req.getContextPath());
60          m.put("REQUEST_COOKIES", req.getCookies());
61          Enumeration enum = req.getHeaderNames();
62          while (enum.hasMoreElements())
63          {
64              String header = (String) enum.nextElement();
65              String value = req.getHeader(header);
66              m.put(header, value);
67          }
68          m.put("REQUEST_METHOD", req.getMethod());
69          m.put("PATH_INFO", req.getPathInfo());
70          m.put("PATH_TRANSLATED", req.getPathTranslated());
71          m.put("QUERY_STRING", req.getQueryString());
72          m.put("REMOTE_ADDR", req.getRemoteAddr());
73          m.put("REMOTE_HOST", req.getRemoteHost());
74          m.put("REMOTE_USER", req.getRemoteUser());
75          m.put("REQUESTED_SESSION_ID", req.getRequestedSessionId());
76          m.put("REQUEST_URI", req.getRequestURI());
77          //m.put("REQUEST_URL", req.getRequestURL());
78          m.put("SERVLET_PATH", req.getServletPath());
79          m.put("SESSION", req.getSession(true));
80          //env
81          return m;
82      }
83  
84      /***
85       * Get request attributes (Only for jsdk 2.3)
86       * @param req the request
87       * @return the servlet attributes
88       */
89      public static Map getExtraRequestEnvironment(HttpServletRequest req)
90      {
91          Map m = new HashMap();
92          Enumeration enum = req.getAttributeNames();
93          while (enum.hasMoreElements())
94          {
95              String attr = (String) enum.nextElement();
96              m.put(attr, req.getAttribute(attr));
97          }
98          m.put("CHARACTER_ENCODING", req.getCharacterEncoding());
99          m.put("CONTENT_LENGTH", new Integer(req.getContentLength()));
100         m.put("CONTENT_TYPE", req.getContentType());
101         m.put("REQUEST_PROTOCOL", req.getProtocol());
102         m.put("REQUEST_SCHEME", req.getScheme());
103         m.put("SERVER_NAME", req.getServerName());
104         m.put("SERVER_PORT", new Integer(req.getServerPort()));
105         return m;
106     }
107 
108     /***
109      * Get the servlet environment from the config
110      * @param config the config
111      * @return the environment in a map
112      */
113     public static Map getServletEnvironment(ServletConfig config)
114     {
115         Map m = new HashMap();
116         m.put("SERVLET_CONTEXT", config.getServletContext());
117         return m;
118     }
119 
120     /***
121      * Get the request parameters in a mp
122      * @param req the request
123      * @return the environment in the map
124      */
125     public static Map getRequestParameters(HttpServletRequest req, DiskFileUpload upload) throws FileUploadException
126     {
127         Map m = new HashMap();
128         if (FileUpload.isMultipartContent(req))
129         {
130             // Parse the request
131             List items = upload.parseRequest(req);
132             Iterator iter = items.iterator();
133             while (iter.hasNext())
134             {
135                 FileItem item = (FileItem) iter.next();
136                 if (item.isFormField())
137                 {
138                     m.put(item.getFieldName(), item.getString());
139                 } else
140                 {
141                     String fieldName = item.getFieldName();
142                     m.put(fieldName + ".FILENAME", item.getName()); //file name
143                     m.put(fieldName + ".CONTENT_TYPE", item.getContentType());
144                     m.put(fieldName + ".ITEM", item);                    
145                 }
146             }
147         } else
148         {
149             Enumeration enum = req.getParameterNames();
150             while (enum.hasMoreElements())
151             {
152                 String key = (String) enum.nextElement();
153                 String[] value = req.getParameterValues(key);
154                 if (value.length == 0)
155                 {
156                     m.put(key, null);
157                 } else if (value.length == 1)
158                 {
159                     m.put(key, value[0]);
160                 } else
161                 {
162                     m.put(key, value);
163                 }
164             }
165         }
166         return m;
167     }
168 }