You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@turbine.apache.org by Ex...@nokia.com on 2001/03/30 11:36:43 UTC
RE: Encoding in TurbineVelocityService (long)
OK, thanks to Costas' experience, I think that something like this can be
done.
1) The parameter parser parses three kinds of parameters:
- path info
- query
- POST
The path info and query parameters are part of the URL and are by default
encoded with US-ASCII (HTTP 1.1/RFC2068, URL/RFC1738). Browsers/Servers can
optionally support national characters in URLs, but it's quite unreliable as
you have to know beforehand that all parties really implement that support.
National characters can also be encoded to query parameters with URL
encoding (%xx).
POST parameters are in a request body, which may be encoded with any
character encoding. The content type header should be present if a body is
included, otherwise the server doesn't know what to do with the body.
Especially, HTML form requests must use a content type multipart/form-data
(HTML Form/RFC1867). As with other content types, a charset specifying the
encoding of the content can be included as an optional parameter to the
content type header. If the charset parameter is not included, the default
encoding is ISO-8859-1 (HTTP 1.1/RFC2068).
We haven't noticed the problems pointed out by Rafal in his remark 3), but
the above spec has worked quite well. In Turbine, the parameter parser works
otherwise as specified above, but uses US-ASCII instead of ISO-8859-1 as the
default encoding. However, it only sets the encoding as its internal
property and lets the upload service to do the actual parsing. The upload
service doesn't seem to use the encoding property, but parses the form using
JVM's default encoding. This can be easily fixed.
2) The response must be encoded by setting the desired charset for the
HttpServletResponse before obtaining a writer from the response. The charset
can't be set directly, but either by setting the desired locale with
setLocale() or by including the charset as a parameter to the content type
header with setContentType().
The servlet API says that 'setLocale() Sets the locale of the response,
setting the headers (including the Content-Type's charset) as appropriate'.
If the locale is not set, the default locale of the server (JVM) should be
used. Unfortunately, the mappings between locales and charsets are not at
all clear. They can depend on the protocol, browser, operator, etc. I've
looked at Catalina source and found out that they use a mapping table
between languages and charsets, but are a bit indefinite when to update the
content type header and when to leave it unchanged.
We've used an approach where the Turbine servlet has a default locale
property. If it is not set, the server's default will be used. If it is set,
the servlet sets the locale for the response, BUT takes care of the content
type header by itself by setting the character set for RunData. The charset
for the locale is found from a MimeTypeService maintaining mappings between
locales and charsets. A locale spec consists of three parts:
lang_country_variant. The variant can be whatever is appropriate for the
application, like a markup language spec, browser spec, etc. The service
looks for charsets using the following search:
lang_country_variant=<charset>
_country_variant=<charset>
lang__variant=<charset>
__variant=<charset>
lang_country=<charset>
_country=<charset>
lang=<charset>
The service contains defaults for several lang mappings and more specific
ones can be defined in a property file, e.g. __wml=UTF-8. The service caches
results of the search to improve performance.
In addition, the servlet has a default charset property, which can override
the default locale setting (note that the locale setting affects other
headers than the content type in the response also). If the charset is
specified, it is set for RunData.
Any object in the request processing chain can explicitly change the default
by setting a new charset value for RunData.
We have a customized servlet that delegates processing to "subservlets"
based on URL, or other dynamic data, making it possible to support several
locales/encodings within the same web-app. The above mechanism is useful for
such customizations, but I think that subservlets themselves are so
application dependent that they can be left out of this topic.
Turbine localization service already has all of the above properties, but
they are not fully used for this purpose. That can be changed quite easily.
BTW. The MimeTypeService maintains also mappings between file extensions and
the corresponding MIME types. This is useful when images in different
formats from static files are given as a response, and also when the web-app
supports several markup languages. The charset part of the service overlaps
with the functionality of the servlet engine and the MIME type part with the
functionality of the web server. This is not good, but as the current
standards don't guarantee the desired behaviour we haven't had other
alternatives.
3) According to Costas, Velocity is capable of parsing templates with
different encodings by providing it the desired encoding in a writer. Thus
the writer provided by VelocityService should have the same encoding, which
is set for RunData. Also the dynamic content produced by objects referenced
from templates by VTL should use the same encoding. This was done in the
sample included in my first mail.
More comments? I'm willing to do the work during the next weekend including
MimeTypeService.
-- Ilkka
> -----Original Message-----
> From: ext Costas Stergiou [mailto:csterg@aias.gr]
> Sent: 29. March 2001 17:00
> To: turbine-user@jakarta.apache.org
> Subject: Re: Encoding in TurbineVelocityService + TDK1.a12 bugs
>
>
>
> ----- Original Message -----
> From: "Jason van Zyl" <jv...@apache.org>
> To: <tu...@jakarta.apache.org>
> Sent: Thursday, March 29, 2001 4:53 PM
> Subject: Re: Encoding in TurbineVelocityService + TDK1.a12 bugs
>
>
> > Costas Stergiou wrote:
> > >
> > > Hi,
> > > I could add some more info on this discussion. I have
> enough experience
> > > working with encodings (especially supporting greek: iso-8859-7)
> > >
> > > After getting TDK1.1a12 all my templates appeared with
> question marks
> > > because
> > > the TurbineVelocityService uses always the 8859_1 for byte to char
> > > conversions.
> > > After changing this to iso-8859-7 everything came back to normal.
> > >
> > > Some thoughts on the implementation:
> > > 1. The use of the "8859_1" encoding in
> TurbineVelocityService is not
> > > correct.
> > > We should be able to set the character encoding to use
> when converting
> > > bytes to chars in TurbineVelocityService. One approach
> would be to use
> > > the template.encoding property of Velocity (that would
> solve enough
> > > problems),
> > > but that is 'hard-coded' in a properties file, so
> supporting more than
> one
> > > encodings 'on-the-fly' would not be possible.
> >
> > Yes, you are correct. This is my fault I forgot to
> implement this before
> > I built the TDK, my apologies. I will correct this now so that the
> > encoding specified in TR.props for the Velocity service is
> _actually_
> > used :-)
> >
> > > 2. To be more flexible (and in order to really support
> many languages in
> > > the same Turbine-app), the TurbineVelocityService should use the
> > > charset that is set in the data.getCharSet()
> (data.getContentType()), as
> > > Ilkka Priha has said in a previous e-mail.
> > > This is the only way that you can support at the same
> time more than one
> > > encodings.
> >
> > Yes, I'm talking about this will Illka but for now I will make the
> > encoding type configurable.
> >
> > > 3. Rafal Krzewski also added another important issue to
> this discussion:
> > > the way the broswer sends back data (from input fields,
> etc) and how
> > > they are converted into Strings within the servlet engine...
> > > The answer is: the servlet engine is using the the 8859_1
> (at least
> tomcat)
> > > to convert the bytes to chars.
> > > On the other hand, in order to support your encoding (if
> > > we suppose that the data posted from the browser are in a
> 'known' or at
> > > least 'expected' encoding), another on-the-fly conversion
> should occur
> > > within
> > > the ParameterParser.getString() methods that convert the
> Strings (as
> read
> > > from the request) from the encoding of the servlet engine
> (8859_1) to
> > > the desired one.
> > > To show how this can be done (at least how I do it in
> order to support
> the
> > > iso-8859-7 charset): in ParameterParser I put an extra conversion:
> > >
> > > private String __convert(String value){
> > > String tmp = null;
> > > try{
> > > tmp = new String( value.getBytes("iso-8859-1"),
> "iso-8859-7" );
> > > }catch(Exception e){
> > > tmp = value;
> > > }
> > >
> > > return tmp;
> > > }
> > >
> > > At this point, unfortunately, I don't think that there is a way to
> support
> > > more
> > > than one encodings for the incoming parameters,
> > > but at least we can support the one that is set in
> > > the properties file (charset). This is also important!!!
> > >
> > > 4. Since every time I get a new TDK I have to patch in the above
> changes,
> > > I would like to help make the above changes permanent and
> commit them so
> > > TDK can really support correctly at least the specified
> encoding in all
> > > levels
> > > (Velocity + Parameters).
> > >
> > > I am willing to give any more info, help, assistance,
> code, time or
> anything
> > > that
> > > is needed to solve these issue completely.
> >
> > Sure, that would be great. I think between the four of us (myself,
> > Illka, Rafal, and
> > yourself) we can implement something in the next couple of
> days so that
> > things work correctly encoding wise.
>
> OK, count me in.
> Thanks
>
> >
> > > Thanks,
> > > waiting for comment, actions
> > > Costas
> >
> > --
> > jvz.
> >
> > Jason van Zyl
> > jvanzyl@apache.org
> >
> > http://jakarta.apache.org/velocity
> > http://jakarta.apache.org/turbine
> > http://tambora.zenplex.org
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> turbine-user-help@jakarta.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: turbine-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org
Re: Encoding in TurbineVelocityService (long)
Posted by Jason van Zyl <jv...@apache.org>.
Ext-Ilkka.Priha@nokia.com wrote:
>
> OK, thanks to Costas' experience, I think that something like this can be
> done.
>
> 1) The parameter parser parses three kinds of parameters:
> - path info
> - query
> - POST
>
> The path info and query parameters are part of the URL and are by default
> encoded with US-ASCII (HTTP 1.1/RFC2068, URL/RFC1738). Browsers/Servers can
> optionally support national characters in URLs, but it's quite unreliable as
> you have to know beforehand that all parties really implement that support.
> National characters can also be encoded to query parameters with URL
> encoding (%xx).
>
> POST parameters are in a request body, which may be encoded with any
> character encoding. The content type header should be present if a body is
> included, otherwise the server doesn't know what to do with the body.
> Especially, HTML form requests must use a content type multipart/form-data
> (HTML Form/RFC1867). As with other content types, a charset specifying the
> encoding of the content can be included as an optional parameter to the
> content type header. If the charset parameter is not included, the default
> encoding is ISO-8859-1 (HTTP 1.1/RFC2068).
>
> We haven't noticed the problems pointed out by Rafal in his remark 3), but
> the above spec has worked quite well. In Turbine, the parameter parser works
> otherwise as specified above, but uses US-ASCII instead of ISO-8859-1 as the
> default encoding. However, it only sets the encoding as its internal
> property and lets the upload service to do the actual parsing. The upload
> service doesn't seem to use the encoding property, but parses the form using
> JVM's default encoding. This can be easily fixed.
>
> 2) The response must be encoded by setting the desired charset for the
> HttpServletResponse before obtaining a writer from the response. The charset
> can't be set directly, but either by setting the desired locale with
> setLocale() or by including the charset as a parameter to the content type
> header with setContentType().
>
> The servlet API says that 'setLocale() Sets the locale of the response,
> setting the headers (including the Content-Type's charset) as appropriate'.
> If the locale is not set, the default locale of the server (JVM) should be
> used. Unfortunately, the mappings between locales and charsets are not at
> all clear. They can depend on the protocol, browser, operator, etc. I've
> looked at Catalina source and found out that they use a mapping table
> between languages and charsets, but are a bit indefinite when to update the
> content type header and when to leave it unchanged.
>
> We've used an approach where the Turbine servlet has a default locale
> property. If it is not set, the server's default will be used. If it is set,
> the servlet sets the locale for the response, BUT takes care of the content
> type header by itself by setting the character set for RunData. The charset
> for the locale is found from a MimeTypeService maintaining mappings between
> locales and charsets. A locale spec consists of three parts:
> lang_country_variant. The variant can be whatever is appropriate for the
> application, like a markup language spec, browser spec, etc. The service
> looks for charsets using the following search:
>
> lang_country_variant=<charset>
> _country_variant=<charset>
> lang__variant=<charset>
> __variant=<charset>
> lang_country=<charset>
> _country=<charset>
> lang=<charset>
>
> The service contains defaults for several lang mappings and more specific
> ones can be defined in a property file, e.g. __wml=UTF-8. The service caches
> results of the search to improve performance.
>
> In addition, the servlet has a default charset property, which can override
> the default locale setting (note that the locale setting affects other
> headers than the content type in the response also). If the charset is
> specified, it is set for RunData.
>
> Any object in the request processing chain can explicitly change the default
> by setting a new charset value for RunData.
>
> We have a customized servlet that delegates processing to "subservlets"
> based on URL, or other dynamic data, making it possible to support several
> locales/encodings within the same web-app. The above mechanism is useful for
> such customizations, but I think that subservlets themselves are so
> application dependent that they can be left out of this topic.
>
> Turbine localization service already has all of the above properties, but
> they are not fully used for this purpose. That can be changed quite easily.
>
> BTW. The MimeTypeService maintains also mappings between file extensions and
> the corresponding MIME types. This is useful when images in different
> formats from static files are given as a response, and also when the web-app
> supports several markup languages. The charset part of the service overlaps
> with the functionality of the servlet engine and the MIME type part with the
> functionality of the web server. This is not good, but as the current
> standards don't guarantee the desired behaviour we haven't had other
> alternatives.
>
> 3) According to Costas, Velocity is capable of parsing templates with
> different encodings by providing it the desired encoding in a writer. Thus
> the writer provided by VelocityService should have the same encoding, which
> is set for RunData. Also the dynamic content produced by objects referenced
> from templates by VTL should use the same encoding. This was done in the
> sample included in my first mail.
>
> More comments? I'm willing to do the work during the next weekend including
> MimeTypeService.
>
Hi Illka, sorry for not responding quicker but this looks like a good
plan. I'm +1 for these changes. I'm just finishing some work on the
template system then I would be glad to help as the encoding issue
are a weakness of mine and I'd like to correct that :-)
If you're planning any changes to the template service can you hold
off until I finish committing my changes. Thanks.
--
jvz.
Jason van Zyl
jvanzyl@apache.org
http://jakarta.apache.org/velocity
http://jakarta.apache.org/turbine
http://tambora.zenplex.org
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-user-help@jakarta.apache.org