/* $Id: HttpParameterParser.java,v 1.6 2001/04/25 22:16:09 agarcia3 Exp $
webEditor. The new way in content management
Copyright (C) 2001 Alfredo Garcia
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
package webEditor.util;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;
import org.apache.regexp.RE;
/**
* This class will provide a transparent interface for all the HTTP conections
*
* @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
*/
public class HttpParameterParser
{
private HttpServletRequest myRequest;
public HttpParameterParser (HttpServletRequest request) {
this.myRequest = request;
}
/**
* Returns the HTTP parameters of the request, even if this
* parameters comes from a multipart form.
* @param tmpDir Temporal directory.
* @return Hashtable HTTP parameters of the request.
*/
public Hashtable getAllParameters (String tmpDir)
{
Hashtable myParameters = new Hashtable();
try {
String contentType = this.myRequest.getContentType();
RE r = new RE("multipart/form-data");
if ( r.match (" " + contentType) ) {
// We are dealing with a multipart form
MultipartRequest formHandler = new MultipartRequest
(this.myRequest, tmpDir);
Enumeration paramList = formHandler.getParameterNames();
for (; paramList.hasMoreElements() ;) {
String paramName = (String) paramList.nextElement();
myParameters.put (paramName,
(String) formHandler.getParameter (paramName));
}
// Ok, and then , the we store the uploaded files
paramList = formHandler.getFileNames();
for (; paramList.hasMoreElements() ;) {
String paramName = (String) paramList.nextElement();
String fileName = null;
fileName = formHandler.getFilesystemName (paramName);
if ( fileName != null ) {
myParameters.put (paramName, (String) fileName);
}
}
}
else {
// We will try to read the http parameters
Enumeration paramList = this.myRequest.getParameterNames();
for (; paramList.hasMoreElements() ;) {
String paramName = (String) paramList.nextElement();
myParameters.put (paramName,
(String) this.myRequest.getParameter (paramName));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return (myParameters);
}
/**
* Returns the HTTP headers of the request.
* @return Hashtable HTTP headers of the request.
*/
public Hashtable getAllHeaders ()
{
Hashtable myHeaders = new Hashtable();
// We will try to read the http parameters
Enumeration headersList = this.myRequest.getHeaderNames();
for (; headersList.hasMoreElements() ;) {
String headerName = (String) headersList.nextElement();
myHeaders.put (headerName,
(String) this.myRequest.getHeader (headerName));
}
// We also add to the headers the requested URI
String requestURI = this.myRequest.getRequestURI();
myHeaders.put ("requestURI", (String) requestURI);
return (myHeaders);
}
}