users@grizzly.java.net

Re: URLDecode requests

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Tue, 06 Apr 2010 17:52:18 +0200

Hi Oliver,

I think the overriding of the method service(String uri, Request req,
Response res) throws Exception; will do what you need.
See the sample bellow.

Thanks.

WBR,
Alexey.

         GrizzlyAdapter ga = new GrizzlyAdapter() {
             public void service(final GrizzlyRequest grizzlyRequest,
final GrizzlyResponse grizzlyResponse) {
                 try {
                     grizzlyResponse.setContentType("text/
plain;charset=\"UTF-8\"");
                      
grizzlyResponse.getOutputStream().write("hello".getBytes());
                     grizzlyResponse.getOutputStream().flush();
                 } catch (Exception e) {
                 }
             }

             @Override
             protected void service(String uri, Request req, Response
res) throws Exception {
                 // URI decoding
                 MessageBytes decodedURI = req.decodedURI();
                 if (decodedURI.isNull()) {
                     decodedURI.duplicate(req.requestURI());
                     try {
                         HttpRequestURIDecoder.decode(decodedURI,
req.getURLDecoder());
                     } catch (IOException ioe) {
                         res.setStatus(400);
                         res.setMessage("Invalid URI: " +
ioe.getMessage());
                         return;
                     }
                 }

                 super.service(decodedURI.toString(), req, res);
             }
         };



On Apr 3, 2010, at 18:44 , Oliver Schrenk wrote:

> Hi Alexey,
>
> sorry for the late reply.
>
> I wasnt clear. The default implementation of StaticResourcesAdapter
> doesn't decode the URLs and maps it to the correct ressource
>
> For example
> utf8+encoded.zip should map to "root/utf encoded.zip" and send that
> file to the requesting client.
>
> The docs
>
> https://grizzly.dev.java.net/nonav/apidocs/com/sun/grizzly/tcp/StaticResourcesAdapter.html
>
> state that I should extend StaticResourcesAdapter. How can I then
> replace the default handler?
>
> Best regards
> Oliver
>
>
> Am 26.03.2010 um 11:29 schrieb Oleksiy Stashok:
>
>> Hi Oliver,
>>
>> IMO you don't need to do any custom transformation. By default
>> UTF-8 is used to decode the requestURI.
>> Here is small sample:
>>
>> final GrizzlyWebServer w = new GrizzlyWebServer(8888, "~/
>> Downloads/", false);
>>
>> GrizzlyAdapter ga = new GrizzlyAdapter() {
>> public void service(final GrizzlyRequest grizzlyRequest,
>> final GrizzlyResponse grizzlyResponse) {
>> try {
>> grizzlyResponse.setContentType("text/
>> plain;charset=\"UTF-8\"");
>>
>> grizzlyResponse.getOutputStream().write("hello".getBytes());
>> grizzlyResponse.getOutputStream().flush();
>> } catch (Exception e) {
>> }
>> }
>> };
>> w.addGrizzlyAdapter(ga, new String[]{"/hello"});
>>
>> GrizzlyAdapter ga2 = new GrizzlyAdapter() {
>> public void service(final GrizzlyRequest grizzlyRequest,
>> final GrizzlyResponse grizzlyResponse) {
>> try {
>> grizzlyResponse.setContentType("text/
>> plain;charset=\"UTF-8\"");
>>
>> grizzlyResponse
>> .getOutputStream().write("Привет!".getBytes("UTF-8"));
>> grizzlyResponse.getOutputStream().flush();
>> } catch (Exception e) {
>> }
>> }
>> };
>> w.addGrizzlyAdapter(ga2, new String[]{"/привет"});
>>
>> So, when in browser you'll request "http://localhost:8888/hello" -
>> you'll see result "hello", if you'll type "http://localhost:8888/при
>> вет" - you'll get "привет" response.
>>
>> Hope this will help.
>>
>> WBR,
>> Alexey.
>>
>>
>> On Mar 25, 2010, at 17:25 , Oliver Schrenk wrote:
>>
>>> Hi,
>>>
>>> I'm using grizzly-http-webserver-1.9.18-k to serve static files,
>>> but also need support for URL-encoded requests (in UTF-8). Is it
>>> possible to forward the request, response to the default Adapter?
>>> Something like:
>>>
>>> GrizzlyAdapter ga = new GrizzlyAdapter(rootDirectory.toString()) {
>>>
>>> @Override
>>> public void service(GrizzlyRequest request, GrizzlyResponse
>>> response) {
>>> String uri = "";
>>> try {
>>> uri = URLDecoder.decode(request.getDecodedRequestURI(), "UTF-8");
>>> } catch (UnsupportedEncodingException e) {
>>> // TODO Auto-generated catch block
>>> e.printStackTrace();
>>> }
>>> request.setDecodedRequestURI(uri);
>>> dispatch(request, response);
>>> }
>>> };
>>> ws.addGrizzlyAdapter(ga, new String[] { "/*" });
>>>
>>> Or is there another (better) way?
>>>
>>> Best regards
>>> Oliver
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
>>> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
>> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: users-help_at_grizzly.dev.java.net
>