<div dir="ltr">&gt;<span style="font-family:arial,sans-serif;font-size:13px">I&#39;m just not sure about the producers, it looks like too much.</span><div style><span style="font-family:arial,sans-serif;font-size:13px">Yeah, it is actually not required to have a producer, you can simply implement PaginationStrategy and that is enough. I&#39;ll remove the producer part from the doc before I add this to the user guide. </span></div>
<div style><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div style><span style="font-family:arial,sans-serif;font-size:13px">Thanks</span></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On 22 February 2013 10:07, Bruno Oliveira <span dir="ltr">&lt;<a href="mailto:bruno@abstractj.org" target="_blank">bruno@abstractj.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

                <div>
                    Hi Dan!
                </div><div><br></div><div>+1 for </div><div><blockquote type="cite"><div dir="ltr"><ul style="margin:15px 0px;padding:0px 0px 0px 30px;font-family:Helvetica,arial,freesans,clean,sans-serif;font-size:14px;line-height:22px">
<li>Extend AbstractPaginationStrategy<br style="margin-top:0px"></li><li>Implement PaginationStrategy</li></ul></div></blockquote></div><div><br></div><div>I&#39;m just not sure about the producers, it looks like too much.</div>

                <div><div><br></div><div><br></div><div><div>-- </div><div>&quot;The measure of a man is what he does with power&quot; - Plato</div><div>-</div><div>@abstractj</div><div>-</div><div>Volenti Nihil Difficile</div>
</div></div><div><div class="h5">
                 
                <p style="color:#a0a0a8">On Tuesday, February 19, 2013 at 10:51 AM, Daniel Bevenius wrote:</p>
                </div></div><blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px">
                    <span><div><div><div class="h5"><div><div dir="ltr">For AEROGEAR-863 &quot;<span style="font-family:Arial,FreeSans,Helvetica,sans-serif">Make PagingStrategy pluggable&quot; the following suggestion is being proposed:</span><div>
<span style="font-family:Arial,FreeSans,Helvetica,sans-serif"><br>
</span></div><div><div><h2 style="border-bottom-style:solid;border-bottom-width:1px;font-size:24px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:0px 0px 10px;padding:0px;border-bottom-color:rgb(204,204,204)">

Custom PaginationStrategy</h2><p style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:0px 0px 15px">This section describes how a custom pagination strategy can be implemented in AeroGear Controller.<br>

Depending on if you need a completely different strategy, or if you simply want to change/add/rename the returned HTTP response headers, you have two options:</p><ul style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:15px 0px;padding:0px 0px 0px 30px">

<li>Extend AbstractPaginationStrategy<br style="margin-top:0px"></li><li>Implement PaginationStrategy<br style="margin-top:0px"></li></ul><h3 style="font-size:18px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:20px 0px 10px;padding:0px">

<a name="13d012842d46cb73_extend-abstractpaginationstrategy" href="https://gist.github.com/danbev/ed51e57e9a1f718f1848#extend-abstractpaginationstrategy" style="color:rgb(65,131,196);text-decoration:initial;display:block;padding-left:30px" target="_blank"></a>Extend AbstractPaginationStrategy</h3>

<p style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:0px 0px 15px">You would extend AbstractPaginationStrategy if you simply want to change/add/rename the returned HTTP response headers.</p>

<h4 style="font-size:16px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:20px 0px 10px;padding:0px"><a name="13d012842d46cb73_example-of-extending-abstractpaginationstrategy" href="https://gist.github.com/danbev/ed51e57e9a1f718f1848#example-of-extending-abstractpaginationstrategy" style="color:rgb(65,131,196);text-decoration:initial;display:block;padding-left:30px" target="_blank"></a>Example of extending AbstractPaginationStrategy:</h4>

<div style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;padding:0px;border:0px"><pre style="font-size:13px;line-height:19px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;word-wrap:break-word;margin-top:15px;margin-bottom:15px;background-color:rgb(248,248,248);border:1px solid rgb(204,204,204);overflow:auto;padding:6px 10px;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">
<span style="font-weight:bold">public</span> <span style="font-weight:bold">class</span> <span style="color:rgb(68,85,136);font-weight:bold">MyStrategy</span> <span style="font-weight:bold">extends</span> <span style="color:rgb(51,51,51)">AbstractPaginationStrategy</span> <span style="font-weight:bold">{</span>

    <span>@Override</span>
    <span style="font-weight:bold">public</span> <span style="color:rgb(68,85,136);font-weight:bold">void</span> <span style="color:rgb(153,0,0);font-weight:bold">setResponseHeaders</span><span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">PaginationMetadata</span> <span style="color:rgb(51,51,51)">md</span><span style="font-weight:bold">,</span>  
        <span style="color:rgb(51,51,51)">HttpServletResponse</span> <span style="color:rgb(51,51,51)">response</span><span style="font-weight:bold">,</span>  
        <span style="color:rgb(68,85,136);font-weight:bold">int</span> <span style="color:rgb(51,51,51)">resultSize</span><span style="font-weight:bold">)</span> <span style="font-weight:bold">{</span>
        <span style="font-weight:bold">for</span> <span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">Entry</span><span style="font-weight:bold">&lt;</span><span style="color:rgb(51,51,51)">String</span><span style="font-weight:bold">,</span> <span style="color:rgb(51,51,51)">String</span><span style="font-weight:bold">&gt;</span> <span style="color:rgb(51,51,51)">entry</span> <span style="font-weight:bold">:</span> <span style="color:rgb(51,51,51)">md</span><span style="font-weight:bold">.</span><span style="color:teal">getHeaders</span><span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">resultSize</span><span style="font-weight:bold">).</span><span style="color:teal">entrySet</span><span style="font-weight:bold">())</span> <span style="font-weight:bold">{</span>
            <span style="color:rgb(51,51,51)">response</span><span style="font-weight:bold">.</span><span style="color:teal">setHeader</span><span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">entry</span><span style="font-weight:bold">.</span><span style="color:teal">getKey</span><span style="font-weight:bold">(),</span> <span style="color:rgb(51,51,51)">entry</span><span style="font-weight:bold">.</span><span style="color:teal">getValue</span><span style="font-weight:bold">());</span>
        <span style="font-weight:bold">}</span>
    <span style="font-weight:bold">}</span>

<span style="font-weight:bold">}</span>
</pre></div><p style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:15px 0px">This implementation actually does exactly what the default implementation does. But you can of course modify this to suite your own requirements. You have access to all the <a href="http://aerogear.org/docs/specs/aerogear-controller/org/jboss/aerogear/controller/router/rest/pagination/PagingMetadata.html" style="color:rgb(65,131,196);text-decoration:initial" target="_blank">metadata</a> for the pagination, like the raw <a href="http://aerogear.org/docs/specs/aerogear-controller/org/jboss/aerogear/controller/router/rest/pagination/Links.html" style="color:rgb(65,131,196);text-decoration:initial" target="_blank">links</a> etc, so you could assemble headers in any way you see fit.</p>

<h3 style="font-size:18px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:20px 0px 10px;padding:0px"><a name="13d012842d46cb73_implement-paginationstrategy" href="https://gist.github.com/danbev/ed51e57e9a1f718f1848#implement-paginationstrategy" style="color:rgb(65,131,196);text-decoration:initial;display:block;padding-left:30px" target="_blank"></a>Implement PaginationStrategy</h3>

<p style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:0px 0px 15px">This will give you more control over the pagination strategy. The interface you have to implement looks like this:</p>

<div style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;padding:0px;border:0px"><pre style="font-size:13px;line-height:19px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;word-wrap:break-word;margin-top:15px;margin-bottom:15px;background-color:rgb(248,248,248);border:1px solid rgb(204,204,204);overflow:auto;padding:6px 10px;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">
<span style="color:rgb(153,153,136);font-style:italic">/**</span>
<span style="color:rgb(153,153,136);font-style:italic"> * A strategy for implementing pagination in AeroGear Controller.</span>
<span style="color:rgb(153,153,136);font-style:italic"> */</span>
<span style="font-weight:bold">public</span> <span style="font-weight:bold">interface</span> <span style="color:rgb(68,85,136);font-weight:bold">PaginationStrategy</span> <span style="font-weight:bold">{</span>

   <span style="color:rgb(153,153,136);font-style:italic">/**</span>
<span style="color:rgb(153,153,136);font-style:italic">     * Creates a PaginationInfo instance.</span>
<span style="color:rgb(153,153,136);font-style:italic">     * How this information is gathered, be it from an Annotation on the target </span>
<span style="color:rgb(153,153,136);font-style:italic">     * endpoint method, or by using separate request parameters is up to the </span>
<span style="color:rgb(153,153,136);font-style:italic">     * concrete implementation.</span>
<span style="color:rgb(153,153,136);font-style:italic">     * </span>
<span style="color:rgb(153,153,136);font-style:italic">     * @param routeContext the {@link RouteContext} of the route being processed.</span>
<span style="color:rgb(153,153,136);font-style:italic">     * @param arguments the extracted arguments from the current request.</span>
<span style="color:rgb(153,153,136);font-style:italic">     * @return {@link PaginationInfo} the information requred for paging. </span>
<span style="color:rgb(153,153,136);font-style:italic">     */</span>
    <span style="color:rgb(51,51,51)">PaginationInfo</span> <span style="color:rgb(153,0,0);font-weight:bold">createPaginationInfo</span><span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">RouteContext</span> <span style="color:rgb(51,51,51)">routeContext</span><span style="font-weight:bold">,</span>  
        <span style="color:rgb(51,51,51)">Map</span><span style="font-weight:bold">&lt;</span><span style="color:rgb(51,51,51)">String</span><span style="font-weight:bold">,</span> <span style="color:rgb(51,51,51)">Object</span><span style="font-weight:bold">&gt;</span> <span style="color:rgb(51,51,51)">arguments</span><span style="font-weight:bold">);</span>

    <span style="color:rgb(153,153,136);font-style:italic">/**</span>
<span style="color:rgb(153,153,136);font-style:italic">     * Called before the target endpoint method has been invoked and enables a </span>
<span style="color:rgb(153,153,136);font-style:italic">     * concrete strategy to manipulate the arguments that will be passed to </span>
<span style="color:rgb(153,153,136);font-style:italic">     * the target endpoint method.</span>
<span style="color:rgb(153,153,136);font-style:italic">     * </span>
<span style="color:rgb(153,153,136);font-style:italic">     * @param pagingInfo the {@link PaginationInfo} instance created by </span>
<span style="color:rgb(153,153,136);font-style:italic">     * this strategy.</span>
<span style="color:rgb(153,153,136);font-style:italic">     * @param arguments the extracted arguments from the current request.</span>
<span style="color:rgb(153,153,136);font-style:italic">     *</span>
<span style="color:rgb(153,153,136);font-style:italic">     * @return {@code Object[]} the arguments that will be passed to the target </span>
<span style="color:rgb(153,153,136);font-style:italic">     * endpoint method.</span>
<span style="color:rgb(153,153,136);font-style:italic">     */</span>
    <span style="color:rgb(51,51,51)">Object</span><span style="font-weight:bold">[]</span> <span style="color:rgb(153,0,0);font-weight:bold">preInvocation</span><span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">PaginationInfo</span> <span style="color:rgb(51,51,51)">pagingInfo</span><span style="font-weight:bold">,</span> <span style="color:rgb(51,51,51)">Map</span><span style="font-weight:bold">&lt;</span><span style="color:rgb(51,51,51)">String</span><span style="font-weight:bold">,</span> <span style="color:rgb(51,51,51)">Object</span><span style="font-weight:bold">&gt;</span> <span style="color:rgb(51,51,51)">arguments</span><span style="font-weight:bold">);</span>  

    <span style="color:rgb(153,153,136);font-style:italic">/**</span>
<span style="color:rgb(153,153,136);font-style:italic">     * Called after the target endpoint method has been invoked and </span>
<span style="color:rgb(153,153,136);font-style:italic">     * allows the strategy to set HTTP Response headers.  </span>
<span style="color:rgb(153,153,136);font-style:italic">     * </span>
<span style="color:rgb(153,153,136);font-style:italic">     * @param result the result returned from the target endpoint method.</span>
<span style="color:rgb(153,153,136);font-style:italic">     * @param routeContext the {@link RouteContext}.</span>
<span style="color:rgb(153,153,136);font-style:italic">     * @param pagingInfo the {@link PaginationInfo} instance created by this strategy.</span>
<span style="color:rgb(153,153,136);font-style:italic">     *</span>
<span style="color:rgb(153,153,136);font-style:italic">     * @return {@code Object} Either the unchanged result or a modified result </span>
<span style="color:rgb(153,153,136);font-style:italic">     * depending on the underlying implementation.</span>
<span style="color:rgb(153,153,136);font-style:italic">     */</span>
    <span style="color:rgb(51,51,51)">Object</span> <span style="color:rgb(153,0,0);font-weight:bold">postInvocation</span><span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">Object</span> <span style="color:rgb(51,51,51)">result</span><span style="font-weight:bold">,</span>  
        <span style="color:rgb(51,51,51)">RouteContext</span> <span style="color:rgb(51,51,51)">routeContext</span><span style="font-weight:bold">,</span>  
        <span style="color:rgb(51,51,51)">PaginationInfo</span> <span style="color:rgb(51,51,51)">pagingInfo</span><span style="font-weight:bold">);</span>

<span style="font-weight:bold">}</span>
</pre></div><p style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:15px 0px">The <code style="font-size:12px;line-height:normal;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;margin:0px 2px;padding:0px 5px;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;white-space:nowrap">createPaginationInfo</code> method is called by AeroGear Controller prior to invoking the target endpoint method. The arguments extracted from the current request are made available to this method. The implementation can choose whatever way it likes to match information from the request that are related to pagination. This could be using an annotation on the target endpoint method (which is what the default implementation does) or it could simply pick known parameters from the request.</p>

<p style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:15px 0px">The <code style="font-size:12px;line-height:normal;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;margin:0px 2px;padding:0px 5px;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;white-space:nowrap">preInvocation</code> methods gives the strategy a chance to modify the actual arguments that will be used to invoke the target endpoint method. Depending on the concrete implementation there might not be anything to be done here other than simply returning the value of the arguments map. But, for example with the default strategy, an endpoint has the option to accept a <code style="font-size:12px;line-height:normal;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;margin:0px 2px;padding:0px 5px;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;white-space:nowrap">PaginationInfo</code> type a parameter. In this case we have to add this instance to the arguments before calling the endpoint.</p>

<p style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:15px 0px">The <code style="font-size:12px;line-height:normal;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;margin:0px 2px;padding:0px 5px;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;white-space:nowrap">postInvocation</code> method is called after the target endpoint method has been invoked allows the strategy to set the HTTP Response headers. This method has access to the results of the invocation which is can use to decide on what links should be returned, for example one might not want to return a previous next link header if there is no more data (the number of items in the results is less than the limit).</p>

<h2 style="border-bottom-style:solid;border-bottom-width:1px;font-size:24px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:20px 0px 10px;padding:0px;border-bottom-color:rgb(204,204,204)"><a name="13d012842d46cb73_configuring-the-strategy-to-be-used" href="https://gist.github.com/danbev/ed51e57e9a1f718f1848#configuring-the-strategy-to-be-used" style="color:rgb(65,131,196);text-decoration:initial;display:block;padding-left:30px" target="_blank"></a>Configuring the Strategy to be used</h2>

<p style="line-height:22px;font-size:14px;font-family:Helvetica,arial,freesans,clean,sans-serif;margin:0px 0px 15px">Configuration is done by adding a CDI Producer, for example:</p><div style="line-height:22px;font-size:14px;margin-bottom:0px!important;font-family:Helvetica,arial,freesans,clean,sans-serif;padding:0px;border:0px">

<pre style="font-size:13px;line-height:19px;font-family:Consolas,&#39;Liberation Mono&#39;,Courier,monospace;word-wrap:break-word;margin-top:15px;margin-bottom:15px;background-color:rgb(248,248,248);border:1px solid rgb(204,204,204);overflow:auto;padding:6px 10px;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">
<span style="font-weight:bold">public</span> <span style="font-weight:bold">class</span> <span style="color:rgb(68,85,136);font-weight:bold">PaginationStrategyProducer</span> <span style="font-weight:bold">{</span>

    <span>@Produces</span>  
    <span style="font-weight:bold">public</span> <span style="color:rgb(51,51,51)">PaginationStrategy</span> <span style="color:rgb(153,0,0);font-weight:bold">createPagingStrategy</span><span style="font-weight:bold">()</span> <span style="font-weight:bold">{</span>
        <span style="font-weight:bold">return</span> <span style="font-weight:bold">new</span> <span style="color:rgb(153,0,0);font-weight:bold">AbstractPaginationStrategy</span><span style="font-weight:bold">()</span> <span style="font-weight:bold">{</span>
            <span>@Override</span>
            <span style="font-weight:bold">public</span> <span style="color:rgb(68,85,136);font-weight:bold">void</span> <span style="color:rgb(153,0,0);font-weight:bold">setResponseHeaders</span><span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">PaginationMetadata</span> <span style="color:rgb(51,51,51)">md</span><span style="font-weight:bold">,</span>  
                <span style="color:rgb(51,51,51)">HttpServletResponse</span> <span style="color:rgb(51,51,51)">response</span><span style="font-weight:bold">,</span>  
                <span style="color:rgb(68,85,136);font-weight:bold">int</span> <span style="color:rgb(51,51,51)">resultSize</span><span style="font-weight:bold">)</span> <span style="font-weight:bold">{</span>
                <span style="color:rgb(51,51,51)">Set</span><span style="font-weight:bold">&lt;</span><span style="color:rgb(51,51,51)">Entry</span><span style="font-weight:bold">&lt;</span><span style="color:rgb(51,51,51)">String</span><span style="font-weight:bold">,</span> <span style="color:rgb(51,51,51)">String</span><span style="font-weight:bold">&gt;&gt;</span> <span style="color:rgb(51,51,51)">hds</span> <span style="font-weight:bold">=</span> <span style="color:rgb(51,51,51)">md</span><span style="font-weight:bold">.</span><span style="color:teal">getHeaders</span><span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">resultSize</span><span style="font-weight:bold">).</span><span style="color:teal">entrySet</span><span style="font-weight:bold">();</span>
                <span style="font-weight:bold">for</span> <span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">Entry</span><span style="font-weight:bold">&lt;</span><span style="color:rgb(51,51,51)">String</span><span style="font-weight:bold">,</span> <span style="color:rgb(51,51,51)">String</span><span style="font-weight:bold">&gt;</span> <span style="color:rgb(51,51,51)">header</span> <span style="font-weight:bold">:</span> <span style="color:rgb(51,51,51)">hds</span><span style="font-weight:bold">)</span> <span style="font-weight:bold">{</span>
                    <span style="color:rgb(51,51,51)">response</span><span style="font-weight:bold">.</span><span style="color:teal">setHeader</span><span style="font-weight:bold">(</span><span style="color:rgb(51,51,51)">header</span><span style="font-weight:bold">.</span><span style="color:teal">getKey</span><span style="font-weight:bold">(),</span> <span style="color:rgb(51,51,51)">header</span><span style="font-weight:bold">.</span><span style="color:teal">getValue</span><span style="font-weight:bold">());</span>
                <span style="font-weight:bold">}</span>
            <span style="font-weight:bold">}</span>
        <span style="font-weight:bold">};</span>
    <span style="font-weight:bold">}</span>

<span style="font-weight:bold">}</span></pre></div></div></div></div>
</div></div></div><div><div>_______________________________________________</div><div>aerogear-dev mailing list</div><div><a href="mailto:aerogear-dev@lists.jboss.org" target="_blank">aerogear-dev@lists.jboss.org</a></div>
<div><a href="https://lists.jboss.org/mailman/listinfo/aerogear-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/aerogear-dev</a></div></div></div></span>
                 
                 
                 
                 
                </blockquote>
                 
                <div>
                    <br>
                </div>
            <br>_______________________________________________<br>
aerogear-dev mailing list<br>
<a href="mailto:aerogear-dev@lists.jboss.org">aerogear-dev@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/aerogear-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/aerogear-dev</a><br></blockquote></div><br></div>