dev@grizzly.java.net

Re: UTF8 encoding problem

From: Jeanfrancois Arcand <Jeanfrancois.Arcand_at_Sun.COM>
Date: Wed, 25 Jun 2008 20:14:35 -0400

Salut,

you have two solutions (unless I'm mistaken). The first one consist of
cut& pasting the code from GrizzlyAdapter:

> 131 // URI decoding
> 132 MessageBytes decodedURI = req.decodedURI();
> 133 decodedURI.duplicate(req.requestURI());
> 134 try {
> 135 req.getURLDecoder().convert(decodedURI, false);
> 136 } catch (IOException ioe) {
> 137 res.setStatus(400);
> 138 res.setMessage("Invalid URI: " + ioe.getMessage());
> 139 return;
> 140 }
> 141
> 142 convertURI(decodedURI, request);
> 143
> 144 // Normalize decoded URI
> 145 if (!normalize(decodedURI)) {
> 146 res.setStatus(400);
> 147 res.setMessage("Invalid URI");
> 148 return;
> 149 }

The class is here:

https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/tcp/http11/GrizzlyAdapter.html

Mainly, the decoding part happens here:

> protected void convertURI(MessageBytes uri, GrizzlyRequest request) throws Exception {
> 222 ByteChunk bc = uri.getByteChunk();
> 223 CharChunk cc = uri.getCharChunk();
> 224 cc.allocate(bc.getLength(), -1);
> 225
> 226 B2CConverter conv = request.getURIConverter();
> 227 try {
> 228 if (conv == null) {
> 229 conv = new B2CConverter("utf-8");
> 230 request.setURIConverter(conv);
> 231 } else {
> 232 conv.recycle();
> 233 }
> 234 } catch (IOException e) {
> 235 // Ignore
> 236 }
> 237 if (conv != null) {
> 238 try {
> 239 conv.convert(bc, cc);
> 240 uri.setChars(cc.getBuffer(), cc.getStart(), cc.getLength());
> 241 return;
> 242 } catch (IOException e) {
> 243 cc.recycle();
> 244 }
> 245 }
> 246
> 247 // Default encoding: fast conversion
> 248 byte[] bbuf = bc.getBuffer();
> 249 char[] cbuf = cc.getBuffer();
> 250 int start = bc.getStart();
> 251 for (int i = 0; i < bc.getLength(); i++) {
> 252 cbuf[i] = (char) (bbuf[i + start] & 0xff);
> 253 }
> 254 uri.setChars(cbuf, 0, bc.getLength());
> 255 }

As an alternative, you might want to port your Adapter code and instead
extend the GrizzlyAdapter class, like I did for the Servlet Container:

https://grizzly.dev.java.net/nonav/xref/com/sun/grizzly/http/servlet/ServletAdapter.html

Let me know if I get it right or I'm mistaken.

Thanks

-- Jeanfrancois


Cam Bazz wrote:
> hello,
>
> the request in question is a : com.sun.grizzly.tcp.Request
>
> attached please find the adapter code.
>
> Best,
> -C.B.
>
> On Wed, Jun 25, 2008 at 7:29 PM, Jeanfrancois Arcand
> <Jeanfrancois.Arcand_at_sun.com <mailto:Jeanfrancois.Arcand_at_sun.com>> wrote:
>
> Salut,
>
>
> Cam Bazz wrote:
>
> Hello,
>
> I made a simple http async request processor as described in
> Jean-Francois's blog and hooked it up to a lucene index, so I
> can make simple operations such as insert or delete from the
> index in a sync manner.
>
>
> Interesting :-)
>
>
>
> however I am using utf-8 fonts. I have been trying for a day,
> but I could not find a solution to the font problem. the
> request.setCharacterEncoding("UTF-8") does not have an effect.
>
> namely, I talk to the grizzly daemon from another servlet, which
> encodes the uri parameters before sending it to grizzly daemon.
> basically for non standard characters it sends %C4%C1 alike
> characters
>
> when the service(Request req, Response res) receives the uri, it
> wont decode them properly.
>
> req.getParameters().getUndecodedParameter("paramName") will also
> return a empty string.
>
>
> Can you share a test case with me? Looks like a bug IMO. I know it
> works when you use GrizzlyRequest, so I need to understand why doing
> it directly using Request doesn't work.
>
> Thanks
>
> -- Jeanfrancois
>
>
>
>
> (I could decode it on my own, if I knew the undecoded version)
>
> any ideas on this one?
>
> Best Regards,
> -C.B.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
> <mailto:dev-unsubscribe_at_grizzly.dev.java.net>
> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net
> <mailto:dev-help_at_grizzly.dev.java.net>
>
>
>
> ------------------------------------------------------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net