users@jersey.java.net

Re: [Jersey] Call another Resource with some other values

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 04 Jan 2010 10:46:43 +0100

Hi Cemo,

As you noted the tricky bit is letting the application inform the
runtime what the internal redirection will be.


A simple solution is for the resource to implement a standard
validation interface a method of which is invoked with method details
of the method for which validation failed, and perhaps why validation
failed.

Or one can specify the method name to invoke with the same parameters
as the validation method:

     @POST
     @Path("pathA")
     @ValidationEnabled("failed")
     public Response postPageA(@Inject Account acc) { ... }

     public Response failed(Account acc) { ... }

Currently you will need to ensure that the failed method has the same
return type, otherwise you will get an error from the runtime, unless
you thrown an exception that is mapped to a response from the AOP-ed
logic.

Paul.

On Dec 30, 2009, at 12:26 PM, Cemo Koc wrote:

>
> Hi Paul ,
>
> Actually I have solved almost all problems I have encountered. But I
> need
> still your help.
>
> First of all I have changed the my way because I understood it is
> not a good
> way. Now I have a such an implementation.
>
>
> public class AbstractResource {
>
> /**
> * For Validation
> */
> private Boolean hasError = false;
>
> }
>
>
> public class myResource extends AbstractResource {
>
> @GET
> @Path("pathA/{ad}")
> public Response getPageA(@PathParam Integer accId){
> // Returning a simple page with a form
>
> }
>
> @POST
> @Path("pathA")
> @ValidationEnabled
> public Response postPageA(@Inject Account acc){
> if (!isHasError()) {
> // do business logic
> // redirect to a new URL.... For example It will use
> getPageB
> return
> Response.status(302).location(uri.build()).build();
> } else {
> return getPageA(acc.getId());
> }
>
> }
> }
>
>
>
>
> public class ValidationAdvice {
>
> @Around("@annotation(valAnno)")
> public Object check(ProceedingJoinPoint jp, ValidationEnabled
> valAnno) {
>
> // validation ---> JSR 303
> Map<String, String> map = validate(jp, valAnno);
>
> // If Problem occurs
> if (!map.isEmpty()) {
> // add error flag to parent of resource (AbstractResource)
> addErrorFlagToTarget(jp.getTarget());
> // Proceed method
> Object o = jp.proceed();
> // add posted values to Response Object and returns it
> // Note that o is a instance of Response Class
> return o;
>
> }
> else
> return jp.proceed();
> ;
> }
> }
>
>
>
>
> I hope that I could explain clearly my usage...
>
> My question will be about jersey internal invocation part of
> resources. My
> implementation about adding errors to each resource is not very
> good... I
> prefer doing with AOP that part too... But I do not know how to call
>
> instead of this
>
> @ValidationEnabled
> public Response postPageA(@Inject Account acc){/**/}
>
> I prefer this
>
> @ValidationEnabled(URL="pathA/{ad}")
> public Response postPageA(@Inject Account acc){/**/}
>
>
> And a result of this, my ugly implementation about setting error and
> returning page will not be used anymore...
>
> My question is simply:
>
> 1) How can I call a resource in another resource... For example my url
> string will be like this "pathA/{ad}"
>
> but note that in my advice I have no knowledge about what is "ad"
> and my
> resource context. I want a generic solution...
>
>
> Thank you so much
>
>
>
> Paul Sandoz wrote:
>>
>> Hi Cemo,
>>
>> Note that clients, such as browsers, when receiving a response to a
>> POST request, do not automatically redirect to the Location URI for a
>> redirection-based response.
>>
>> It seems the right thing to do here is to return the error data in
>> the
>> POST response. That response can contain a link to return the the
>> page
>> to POST again. However, I would expect in browser-based cases to use
>> AJAX such that original page can be updated accordingly without
>> having
>> to go back and forth.
>>
>> Paul.
>>
>>
>
> --
> View this message in context: http://n2.nabble.com/Call-another-Resource-with-some-other-values-tp4205347p4231858.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>