[Apiman-user] API policy response data handler

Balu S sbalu27 at gmail.com
Thu Feb 2 12:40:12 EST 2017


Hi Marc,

I shall explain my use case. From our API call, we return error response
(in XML or JSON)  for 401, 500 and so on. However, Apiman metrics seems to
just consider them as good response (it is, as Apiman received the response
back) and show as successful response. So I have made custom policy to
intercept the response to know if it is failure and trigger the Policy
Failure. This is fairly simple and straight forward as the response code
will just do the purpose. But I want also to add some additional
information about the failure to Policy Failure. This additional
information is in the original error response which will be lost once
doFailure() happens. And no we don't want to those additional information
in some HTTP headers to pass around. Hence I implemented responseHandler()
to handle the response buffer and like you pointed out, it seems to be too
late to meddle the response.

So ideally, there are possible 2 solution

   - Apiman metrics can interpret the response as unsuccessful for such
error response from API call.
   - Handle the response buffer data before the write() call to response
outputstream.

Do you see any alternative solution?

Thanks
Balu

On Thu, Feb 2, 2017 at 6:15 PM, Marc Savy <marc.savy at redhat.com> wrote:

> Hi,
>
> Perhaps URLRewritingPolicy https://github.com/apiman/
> apiman/blob/master/gateway/engine/policies/src/main/java/
> io/apiman/gateway/engine/policies/URLRewritingPolicy.java be an
> informative place to start?
>
> - Apiman streams data, so the client may be receiving data already by the
> time you've determined you want to cancel (the connection is already
> established; headers have been sent) - it's often too late to gracefully
> cancel. You could try throwing an exception and seeing what happens (not
> recommended practice!).
>
> If that doesn't work, perhaps you can explain your use-case more clearly
> and explicitly so we can see what the alternatives are?
>
> - Policies are *static* instances, if you are assigning that buffer to the
> object then it's as if you were writing "static Buffer  buffer" and
> different requests will all share that variable (and thus swap it out
> repeatedly!).
>
> Regards,
> Marc
>
>
>
>
>
> On 2 February 2017 at 16:56, Balu S <sbalu27 at gmail.com> wrote:
>
>> Hello,
>> I'm trying to parse the response using responseDataHandler() in the
>> custom policy. In cases, if the response from API is of certain content, I
>> would like the Apiman to consider as failure. But I don't find a way to
>> throw policy failure from responseDataHandler(). And I cannot achieve this
>> in doApply() as the ApiResponse object does not have "content" to parse.
>>
>> Also, what I found is write(chunk) in the AbstractStream is called after
>> doApply, so I cannot set any attributes in it to fetch it in doApply() and
>> trigger doFailure().
>>
>> For example, in below call, how to throw as policy failure after parsing
>> the contents ? Or how can I access response content even before write()
>> method.
>>
>>
>> *URLRewritingPolicy.java*
>>
>>     @Override
>>     protected IReadWriteStream<ApiResponse> responseDataHandler(ApiResponse
>> response,
>>             IPolicyContext context, URLRewritingConfig
>> policyConfiguration) {
>>         if (policyConfiguration.isProcessResponseBody()) {
>>             return new URLRewritingStream(context.get
>> Component(IBufferFactoryComponent.class), response,
>>                     policyConfiguration.getFromRegex(),
>> policyConfiguration.getToReplacement());
>>         } else {
>>             return null;
>>         }
>>     }
>>
>> *URLRewritingStream.java*
>>
>>     /**
>>      * @see io.apiman.gateway.engine.io.AbstractStream#write(io.apiman.
>> gateway.engine.io.IApimanBuffer)
>>      */
>>     @Override
>>     public void write(IApimanBuffer chunk) {
>>         if (buffer == null) {
>>             buffer = bufferFactory.cloneBuffer(chunk);
>>         } else {
>>             buffer.append(chunk);
>>         }
>>         atEnd = false;
>>         processBuffer();
>>     }
>>
>>
>> Best regards
>> Balu
>>
>> _______________________________________________
>> Apiman-user mailing list
>> Apiman-user at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/apiman-user
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/apiman-user/attachments/20170202/70667782/attachment.html 


More information about the Apiman-user mailing list