Re: How to hook end of POST

2008-09-12 Thread Kenji Tayama
Hi Jérôme,

Thanks for the solutions.
I will be using Application.getConnectorService() so that I do not
need to touch Representations or Resources.

Many thanks,
Kenji Tayama

2008/9/12 Jerome Louvel <[EMAIL PROTECTED]>:
>
> Hi Kenji,
>
> This is the expected behavior as the response entity is actually written by
> the server connector.
>
> There is a way to get a hook right before and right after the writing via
> the ConnectorService (see Application.getConnectorService()).
>
> Depending on what you want to achieve, another option would be to create a
> custom representation that would do some post-processing/cleaning after the
> write(OutputStream) is called.
>
> Best regards,
> Jérôme Louvel
> --
> Restlet ~ Founder and Lead developer ~ http://www.restlet.org
> Noelios Technologies ~ Co-founder ~ http://www.noelios.com
>
> -Message d'origine-
> De : Kenji Tayama [mailto:[EMAIL PROTECTED]
> Envoyé : vendredi 12 septembre 2008 07:40
> À : discuss@restlet.tigris.org
> Objet : Re: How to hook end of POST
>
> Hi Thierry,
>
> Thank you for the sample codes.
> I modifed the codes so they can show this problem.
>
> You can see that Filter#afterHandle is called before
> WriterRepresentation#write in the console.
>
> Regards,
> Kenji Tayama
>
> 2008/9/11 Thierry Boileau <[EMAIL PROTECTED]>:
>> Mail sent on the 09/10 and apparently lost.
>> ---
>>
>> Hello Kenji,
>>
>> I'm a little surprised by the fact that the Filter#afterHandle method does
>> not work as expected. What kind of filter are you suspecting to fail?
>> Anyway, I send you a sample code containing 3 classes: an application, a
>> resource and a filter.
>> The application puts the filter as its own root restlet, thus the
>> afterHandle method of the filter is called after all methods.
>>
>> Please feel free to detail your need, I may have missed something.
>>
>> Best regards,
>> Thierry Boileau
>> --
>> Restlet ~ Core developer ~ http://www.restlet.org
>> Noelios Technologies ~ Co-founder ~ http://www.noelios.com
>>
>> Hi,
>>
>> I'm trying to hook the end of POST calls, and I've tried these :
>>
>> Filter#afterHandle
>> Handler#handleXxx
>> Application#handle
>>
>> Works fine for GET requests, but for POST requests, these get called
> before
>> response data is sent to the browser.
>>
>> Is there any way to hook the point when response data is sent?
>>
>> Thanks,
>> Kenji Tayama
>>
>>
>
>


Re: How to hook end of POST

2008-09-11 Thread Kenji Tayama
Hi Thierry,

Thank you for the sample codes.
I modifed the codes so they can show this problem.

You can see that Filter#afterHandle is called before
WriterRepresentation#write in the console.

Regards,
Kenji Tayama

2008/9/11 Thierry Boileau <[EMAIL PROTECTED]>:
> Mail sent on the 09/10 and apparently lost.
> ---
>
> Hello Kenji,
>
> I'm a little surprised by the fact that the Filter#afterHandle method does
> not work as expected. What kind of filter are you suspecting to fail?
> Anyway, I send you a sample code containing 3 classes: an application, a
> resource and a filter.
> The application puts the filter as its own root restlet, thus the
> afterHandle method of the filter is called after all methods.
>
> Please feel free to detail your need, I may have missed something.
>
> Best regards,
> Thierry Boileau
> --
> Restlet ~ Core developer ~ http://www.restlet.org
> Noelios Technologies ~ Co-founder ~ http://www.noelios.com
>
> Hi,
>
> I'm trying to hook the end of POST calls, and I've tried these :
>
> Filter#afterHandle
> Handler#handleXxx
> Application#handle
>
> Works fine for GET requests, but for POST requests, these get called before
> response data is sent to the browser.
>
> Is there any way to hook the point when response data is sent?
>
> Thanks,
> Kenji Tayama
>
>
package testPost;

import org.restlet.Application;
import org.restlet.Client;
import org.restlet.Component;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.Protocol;
import org.restlet.resource.StringRepresentation;

public class TestApplication extends Application {
public static void main(String[] args) throws Exception {
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);
component.getDefaultHost()
.attachDefault(
new 
TestApplication(component.getContext()

.createChildContext()));

component.start();

Client client = new Client(Protocol.HTTP);
client.post("http://localhost:8182/";, new 
StringRepresentation("test"));

component.stop();
}

public TestApplication(Context context) {
super(context);
}

@Override
public Restlet createRoot() {
Router router = new Router(getContext());
router.attachDefault(TestResource.class);

// Filter all requests handled by the application.
return new TestFilter(getContext(), router);
}

}
package testPost;

import org.restlet.Context;
import org.restlet.Filter;
import org.restlet.Restlet;
import org.restlet.data.Request;
import org.restlet.data.Response;

public class TestFilter extends Filter {

public TestFilter(Context context, Restlet next) {
super(context, next);
}

@Override
protected void afterHandle(Request request, Response response) {
System.out.println("start afterHandle");
super.afterHandle(request, response);
System.out.println("end afterHandle");
}

@Override
protected int beforeHandle(Request request, Response response) {
System.out.println("start beforeHandle");
return super.beforeHandle(request, response);
}

@Override
protected int doHandle(Request request, Response response) {
System.out.println("start doHandle");
return super.doHandle(request, response);
}

}
package testPost;

import java.io.IOException;
import java.io.Writer;

import org.restlet.Context;
import org.restlet.data.CharacterSet;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.WriterRepresentation;

public class TestResource extends Resource {

/**
 * Handle post calls
 */
@Override
public void acceptRepresentation(Representation entity)
throws ResourceException {
System.out.println("before setEntity");

WriterRepresentation rep = new 
TestWriterRepresentation(entity.getMediaType());
rep.setCharacterSet(CharacterSet.UTF_8);
getResponse().setStatus(Status.SUCCESS_OK);
getResponse().setEntity(rep);

System.out.printl

How to hook end of POST

2008-09-09 Thread Kenji Tayama
Hi,

I'm trying to hook the end of POST calls, and I've tried these :

Filter#afterHandle
Handler#handleXxx
Application#handle

Works fine for GET requests, but for POST requests, these get called before
response data is sent to the browser.

Is there any way to hook the point when response data is sent?

Thanks,
Kenji Tayama