You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@synapse.apache.org by Malintha Amarasinghe <ma...@gmail.com> on 2015/10/21 06:14:29 UTC

Sending a custom response back to the client from a Handler in API

Hi All,

We are implementing a custom handler for an API (extending AbstractHandler)
such that it authenticates the client using incoming request headers and
gives a custom authentication failure message back to the client when an
authentication failure happens. We set the custom fault envelope to the
message context and send it back to the client like below.

messageContext.setEnvelope(*faultEnvelope*);
Axis2Sender.sendBack(messageContext);

When but the problem we face is when the client give a POST request to the
API with a request body, this does not send the custom response back to the
client. Instead of that, it resend the same request payload sent by the
client back to him.

Highly appreciate if you can suggest us a better way to solve this.

Thank you.
*Malintha Amarasinghe,*
Undergraduate,
Dept.of Computer Science and Engineering,
University of Moratuwa.

Re: Sending a custom response back to the client from a Handler in API

Posted by Isuru Udana <is...@gmail.com>.
Hi Malintha,

Which version of synapse are you using ?

If it is a released version, following code snippet will do.

 synCtx.setTo(null);
 synCtx.setResponse(true);
 Axis2Sender.sendBack(synCtx);

But if you are using a pack build from truck which has passthrough
transport, you need to build the message by calling
RelayUtils,buildMessage() prior to above code snippet.




On Tue, Oct 27, 2015 at 5:32 AM, Hiranya Jayathilaka <hi...@gmail.com>
wrote:

> You might have to clear the To header on MessageContext.
>
>
> On Oct 20, 2015, at 9:14 PM, Malintha Amarasinghe <
> malintha.prasan@gmail.com> wrote:
>
> Hi All,
>
> We are implementing a custom handler for an API (extending
> AbstractHandler) such that it authenticates the client using incoming
> request headers and gives a custom authentication failure message back to
> the client when an authentication failure happens. We set the custom
> fault envelope to the message context and send it back to the client like
> below.
>
> messageContext.setEnvelope(*faultEnvelope*);
> Axis2Sender.sendBack(messageContext);
>
> When but the problem we face is when the client give a POST request to the
> API with a request body, this does not send the custom response back to the
> client. Instead of that, it resend the same request payload sent by the
> client back to him.
>
> Highly appreciate if you can suggest us a better way to solve this.
>
> Thank you.
> *Malintha Amarasinghe,*
> Undergraduate,
> Dept.of Computer Science and Engineering,
> University of Moratuwa.
>
>
> --
> Hiranya Jayathilaka
> Mayhem Lab/RACE Lab;
> Dept. of Computer Science, UCSB;  http://cs.ucsb.edu
> E-mail: hiranya@cs.ucsb.edu <hi...@wso2.com>;  Mobile: +1 (805) 895-7443
> Blog: http://techfeast-hiranya.blogspot.com
>
>


-- 
*Isuru Udana*
Associate Technical Lead




*; WSO2 Inc.; http://wso2.com <http://wso2.com>email: isudana@gmail.com
<is...@gmail.com> blog: http://mytecheye.blogspot.com/
<http://mytecheye.blogspot.com/>*

Re: Sending a custom response back to the client from a Handler in API

Posted by Hiranya Jayathilaka <hi...@gmail.com>.
You might have to clear the To header on MessageContext. 


On Oct 20, 2015, at 9:14 PM, Malintha Amarasinghe <ma...@gmail.com> wrote:

> Hi All,
> 
> We are implementing a custom handler for an API (extending AbstractHandler) such that it authenticates the client using incoming request headers and gives a custom authentication failure message back to the client when an authentication failure happens. We set the custom fault envelope to the message context and send it back to the client like below.
> 
> messageContext.setEnvelope(faultEnvelope);
> Axis2Sender.sendBack(messageContext);
> 
> When but the problem we face is when the client give a POST request to the API with a request body, this does not send the custom response back to the client. Instead of that, it resend the same request payload sent by the client back to him.
> 
> Highly appreciate if you can suggest us a better way to solve this.
> 
> Thank you.
> Malintha Amarasinghe,
> Undergraduate,
> Dept.of Computer Science and Engineering,
> University of Moratuwa.

--
Hiranya Jayathilaka
Mayhem Lab/RACE Lab;
Dept. of Computer Science, UCSB;  http://cs.ucsb.edu
E-mail: hiranya@cs.ucsb.edu;  Mobile: +1 (805) 895-7443
Blog: http://techfeast-hiranya.blogspot.com