You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Jun Inamori <j-...@osa.att.ne.jp> on 2000/05/08 02:21:43 UTC
Proposal ParseMime
Hi again,
I posted the message about the new class:
org.apache.tomcat.request.ParseMime
about 3 hours ago.
The title was 'Re: Proposal(NEW) FixCharset'.
In this mail, I'll describe the modification after my previous message
and the additional information.
1. Changes after my previous message.
1-1. Re-package
org.apache.tomcat.request.MimeFile was re-packaged to
org.apache.tomcat.util.MimeFile
1-2. Modified code
In org.apache.tomcat.request.ParseMime, the following fragment of code
was changed
InputStream is=mbp.getInputStream();
String t_enc=mbp.getEncoding();
if(t_enc!=null){
is=MimeUtility.decode(is,t_enc);
}
to
InputStream is=mbp.getInputStream();
String[] t_encs=mbp.getHeader("Content-Transfer-Encoding");
if(t_encs!=null){
is=MimeUtility.decode(is,t_encs[0]);
}
NOTE: As for MimeBodyPart, we should not rely on 'getXXX' method to get
the specified header. Instead, getHeader("XXX") is safer.
2. Additional information
2-1. Modified related class
To process 'multipart/form-data' by ParseMime, the following class must
be modified:
org.apache.tomcat.core.ServletWrapper
org.apache.tomcat.core.RequestInterceptor
org.apache.tomcat.core.BaseInterceptor
org.apache.tomcat.core.Request
org.apache.tomcat.core.RequestImpl
org.apache.tomcat.core.HttpServletRequestFacade
I attached the diffs of these classes at the end of this mail.
2-2. How to access the uploaded information
Files uploaded by 'multipart/form-data' is stored in 'HashTable'(static
private Hashtable files) in 'RequestImpl'. Key is the parameter name.
For example, if the FORM element of our HTML looks like:
<FORM ACTION="/juntest/jsp/MimeJSP.jsp"
METHOD=POST
ENCTYPE="multipart/form-data">
<BR>
Please input some text:
<BR>
<INPUT TYPE=text NAME=text_1>
<BR>
Please choose file:
<INPUT TYPE=file NAME=file_1>
<BR>
<INPUT TYPE=submit VALUE="Submit">
</FORM>
then the key to the uploaded file is 'file_1'.
The contents of the uploaded file can be retrieved as 'byte[]' via
public byte[] getFileBody(String name) {
return request.getFileBody(name);
}
public Enumeration getFileNames() {
return request.getFileNames();
}
of 'HttpServletRequestFacade' class.
Because these method are not the members of 'HttpServletRequest', we
have to cast 'HttpServletRequest' to 'HttpServletRequestFacade' within
our Servlet or JSPs.
If you have a good idea to access these information through the standard
API, please post the message.
As for the normal parameters, we can get the value through
'getParameter(String name)' of 'ServletRequest', even though they are
submitted though 'multipart/form-data'.
2-3. Setting 'charset' on the server-side
To get the parameter string decoded properly, we may have to provide the
charset information to ParseMime class. This can be done by server.xml,
which looks like this:
<RequestInterceptor
className="org.apache.tomcat.request.ParseMime"
Charset="Shift_JIS" />
With the charset supplied in this way, ParseMime can convert 'byte[]' to
'String', even if the WWW browser does not send the 'charset' attribute.
(Currently, this is true for most case.)
--
Happy Java programming!
Jun Inamori
E-mail: j-office@osa.att.ne.jp
URL: http://www.oop-reserch.com
PS:
I atached the diffs of the related classes:
*** diffs ***
*** For org.apache.tomcat.core.ServletWrapper ***
389,401d388
< //Start:Jun Inamori modified
< RequestInterceptor cI[]=context.getRequestInterceptors();
< for( int i=0; i<cI.length; i++ ) {
< //Parse and decode parameters.
< //FixParameter is responsible for this.
< int result=0;
< result=cI[i].afterRead( req, res );
< if(result!=0 && result!=200){
< contextM.handleError(req, res,null,result);
< }
< }
< //End:Jun Inamori moodified
<
505,509c492
< //Start:Jun Inamori modified
< // Array of RequestInterceptor already is retrieved
< // at the top of this method
< //RequestInterceptor cI[]=context.getRequestInterceptors();
< //End:Jun Inamori modified
---
> RequestInterceptor cI[]=context.getRequestInterceptors();
*** diffs ***
*** For org.apache.tomcat.core.RequestInterceptor ***
112,116d111
< //Start:Jun Inamori modified
< /** Called at first in ServletWrapper.handleRequest()
< */
< public int afterRead(Request request, Response response);
< //End:Jun Inamori modified
*** diffs ***
*** For org.apache.tomcat.core.BaseInterceptor ***
100,104d99
< //Start:Jun Inamori
< public int afterRead(Request request, Response response) {
< return 0;
< }
< //End:Jun Inamori
*** diffs ***
*** For org.apache.tomcat.core.Request ***
298,299d297
< //Start:Jun Inamori
< public byte[] getFileBody(String name);
301d298
< public Enumeration getFileNames();
303,307d299
< public void setFiles( Hashtable h );
<
< public Hashtable getFiles();
< //End:Jun Inamori
<
*** diffs ***
*** For org.apache.tomcat.core.RequestImpl ***
74a75
>
108,112d108
<
< //Start:Jun Inamori
< protected Hashtable files = new Hashtable();
< //End:Jun Inamori
<
223a220
> handleParameters();
227a225
> handleParameters();
231,244d228
< //Start:Jun Inamori
< public byte[] getFileBody(String name) {
< MimeFile mime=(MimeFile)files.get(name);
< if(mime==null){
< return null;
< }
< return mime.getBody();
< }
<
< public Enumeration getFileNames() {
< return files.keys();
< }
< //End:Jun Inamori
<
503,513d486
< //Start:Jun Inamori
< public void setFiles( Hashtable h ) {
< if(h!=null)
< this.files=h;
< }
<
< public Hashtable getFiles() {
< return files;
< }
< //End:Jun Inamori
<
593a567,582
> private void handleParameters() {
> if(!didParameters) {
> String qString=getQueryString();
> if(qString!=null) {
> didParameters=true;
> RequestUtil.processFormData( qString, parameters );
> }
> }
> if (!didReadFormData) {
> didReadFormData = true;
> Hashtable postParameters=RequestUtil.readFormData( this );
> if(postParameters!=null)
> parameters = RequestUtil.mergeParameters(parameters, postParameters);
> }
> }
>
600,602d588
< //Start:Jun Inamori
< files.clear();
< //End:Jun Inamori
*** diffs ***
*** For org.apache.tomcat.core.HttpServletRequestFacade ***
203,214d202
< //Start:Jun Inamori
< //NOTE: These are NOT members of HttpServletRequest.
< // We must cast it to HttpServletRequestFacade.
< public byte[] getFileBody(String name) {
< return request.getFileBody(name);
< }
<
< public Enumeration getFileNames() {
< return request.getFileNames();
< }
< //End:Jun Inamori
<