cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache CXF Documentation > JAX-RS Redirection
Date Tue, 01 Nov 2011 16:39:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CXF20DOC&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Redirection">JAX-RS
Redirection</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~sergey_beryozkin">Sergey
Beryozkin</a>
    </h4>
        <br/>
                         <h4>Changes (12)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >    &lt;/jaxrs:serviceBeans&gt;
	   <br>    &lt;jaxrs:providers&gt; <br></td></tr>
            <tr><td class="diff-changed-lines" >&lt;ref <span class="diff-changed-words">bean=&quot;dispatchProvider<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">1</span>&quot;/&gt;</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >    &lt;/jaxrs:providers&gt;
 <br>&lt;/jaxrs:server&gt; <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >&lt;bean <span class="diff-changed-words">id=&quot;dispatchProvider<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">1</span>&quot;</span>
class=&quot;org.apache.cxf.jaxrs.provider.RequestDispatcherProvider&quot;&gt;
<br></td></tr>
            <tr><td class="diff-unchanged" >      &lt;property name=&quot;resourcePath&quot;
value=&quot;/book.html&quot;/&gt; <br>&lt;/bean&gt; <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code:xml} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >&lt;bean <span class="diff-changed-words">id=&quot;dispatchProvider<span
class="diff-deleted-chars"style="color:#999;background-color:#fdd;text-decoration:line-through;">1</span>&quot;</span>
class=&quot;org.apache.cxf.jaxrs.provider.RequestDispatcherProvider&quot;&gt;
<br></td></tr>
            <tr><td class="diff-unchanged" >      &lt;property name=&quot;resourcePath&quot;
value=&quot;/book.jsp&quot;/&gt; <br>&lt;/bean&gt; <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Note
how non-intrusive RequestDispatcherProvider is as far as writing the JAX-RS resource code
is concerned, you simply list supported media types in @Produces as usual.  <br> <br></td></tr>
            <tr><td class="diff-unchanged" >RequestDispatcherProvider will make
an instance of Book available as an HttpServletRequest attribute named &quot;org.bar.Book&quot;
by default. this can be customized. If a &quot;beanName&quot; property is set, for
example to &quot;book&quot;, then book.jsp will access a Book instance as a &quot;book&quot;
attribute. If you have say 2 resource methods returning instances of different bean classes,
possibly for different view handlers then a beanNames map property can be used, for example:
 <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">Also
note how non-intrusive RequestDispatcherProvider is as far as writing the JAX-RS resource
code is concerned, you simply list supported media types in @Produces as usual.  <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{code:xml}
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">Finally,
a &#39;servletContextPath&#39; property can be used to have some other ServletContext
(as opposed to the current one) be used for RequestDispatcher look-ups. If set then the current
ServletContext.getContext(servletContextPath) will be used to get the needed ServletContext.
<br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&lt;bean
id=&quot;dispatchProvider&quot; class=&quot;org.apache.cxf.jaxrs.provider.RequestDispatcherProvider&quot;&gt;
<br>      &lt;property name=&quot;classResources&quot;&gt; <br>
         &lt;map&gt; <br>             &lt;entry key=&quot;org.bar.Book&quot;
 value=&quot;/book.jsp&quot;/&gt; <br>             &lt;entry key=&quot;org.bar.Customer&quot;
 value=&quot;/customer.jsp&quot;/&gt; <br>          &lt;/map&gt;
<br>      &lt;/property&gt; <br>      &lt;property name=&quot;beanNames&quot;&gt;
<br>          &lt;map&gt; <br>             &lt;entry key=&quot;org.bar.Book&quot;
 value=&quot;book&quot;/&gt; <br>             &lt;entry key=&quot;org.bar.Customer&quot;
 value=&quot;customer&quot;/&gt; <br>          &lt;/map&gt; <br>
     &lt;/property&gt; <br>&lt;/bean&gt; <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{code}
<br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">The
above configuration says that a &quot;book.jsp&quot; resource will handle an instance
of Book by accessing it as a &quot;book&quot; attribute and a &quot;customer.jsp&quot;
- an instance of Customer by retrieving it as a &quot;customer&quot; attribute. Note
you don&#39;t need to use the &quot;beanNames&quot; property in such cases, a
simpler &quot;beanName&quot; property can do unless you have a single (jsp) resource
dealing with both Book and Customer.  <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">Apart
from making an instance of response class available as HttpServletRequest attribute, RequestDispatcherProvider
will also make all the Path, Query and Matrix parameters available as HttpServletRequest parameters
(as opposed to attributes) by default. For example, given the above code fragment, an HttpServletRequest
parameter named &quot;id&quot; representing a @PathParam(&quot;id&quot;) available
to the view handler, as well as all other query and matrix parameters. <br>Note that
RequestDispatcherProvider can be configured to save all these request parameters as HttpServletRequest
attributes by setting a boolean saveParametersAsAttributes property to true.  <br> <br>Now,
imagine a scenario like this: we have two resource methods returning a ReservationStatus bean.
The first method returns a successful confirmation or uses Response.seeOther(...) to redirect
to a method handling the failed reservation. So both methods return the same ReservationStatus
bean but we will have two different views handling successful and failed reservations respectively.
Here is one way to manage it: <br> <br>{code:xml} <br>&lt;bean id=&quot;reserveRegistrationViews&quot;
class=&quot;org.apache.cxf.jaxrs.provider.RequestDispatcherProvider&quot;&gt;
<br>         &lt;property name=&quot;resourcePaths&quot;&gt; <br>
           &lt;map&gt; <br>              &lt;entry key=&quot;/reservations/reserve/complete&quot;
value=&quot;/forms/reservationConfirm.jsp&quot;/&gt; <br>              &lt;entry
key=&quot;/reservations/reserve/failure&quot; value=&quot;/forms/reservationFailure.jsp&quot;/&gt;
<br>            &lt;/map&gt; <br>         &lt;/property&gt; <br>
        &lt;property name=&quot;beanName&quot; value=&quot;data&quot;/&gt;
<br>&lt;/bean&gt; <br>{code} <br> <br>Given that the same
ReservationStatus bean is returned in both cases, it is actually the original request URI
fragments which are used to match which view handler will deal with a given ReservationStatus.
<br> <br>Finally, a &#39;servletContextPath&#39; property can be used
to have some other ServletContext (as opposed to the current one) be used for RequestDispatcher
look-ups. If set then the current ServletContext.getContext(servletContextPath) will be used
to get the needed ServletContext. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h1. With CXFServlet <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >    &lt;property name=&quot;dispatcherName&quot;
value=&quot;jsp&quot;/&gt; <br>    &lt;property name=&quot;resourcePath&quot;
value=&quot;/WEB-INF/jsp/test.jsp&quot;/&gt; <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">
   &lt;property name=&quot;scope&quot; value=&quot;request&quot;/&gt;
<br></td></tr>
            <tr><td class="diff-unchanged" >&lt;/bean&gt;  <br>{code}
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p><span style="font-size:2em;font-weight:bold"> JAX-RS : Redirection
</span></p>

<div>
<ul>
    <li><a href='#JAX-RSRedirection-WithRequestDispatcherProvider'>With RequestDispatcherProvider</a></li>
    <li><a href='#JAX-RSRedirection-WithCXFServlet'>With CXFServlet</a></li>
    <li><a href='#JAX-RSRedirection-CustomRedirection'>Custom Redirection</a></li>
</ul></div>

<h1><a name="JAX-RSRedirection-WithRequestDispatcherProvider"></a>With RequestDispatcherProvider</h1>

<p><a href="http://svn.apache.org/repos/asf/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/RequestDispatcherProvider.java"
class="external-link" rel="nofollow">RequestDispatcherProvider</a> is a JAXRS MessageBodyWriter
which can redirect to JSP pages, named or default servlets. It can be used to serve all the
responses from a given resource class or restricted to serving a limited set of classes only
using a classResources map property.</p>

<p>Starting from CXF 2.5.0 and 2.4.4 it is also possible to specify that only responses
to requests with matching URIs that will be processed.</p>

<p>At the moment, this provider is statically configured to support text/html content
types, but it can be easily configured to support other content types if needed.  </p>

<p>Here are some examples. Lets assume we have a book.war web application deployed.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;jaxrs:server id=<span class="code-quote">"bookservice1"</span>
address=<span class="code-quote">"/bookstore1"</span>&gt;</span>
    <span class="code-tag">&lt;jaxrs:serviceBeans&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.cxf.systest.jaxrs.BookStoreDispatch"</span>/&gt;</span>
    <span class="code-tag">&lt;/jaxrs:serviceBeans&gt;</span>		  
    <span class="code-tag">&lt;jaxrs:providers&gt;</span>
       <span class="code-tag">&lt;ref bean=<span class="code-quote">"dispatchProvider"</span>/&gt;</span>
    <span class="code-tag">&lt;/jaxrs:providers&gt;</span> 
<span class="code-tag">&lt;/jaxrs:server&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"dispatchProvider"</span>
class=<span class="code-quote">"org.apache.cxf.jaxrs.provider.RequestDispatcherProvider"</span>&gt;</span>
      <span class="code-tag">&lt;property name=<span class="code-quote">"resourcePath"</span>
value=<span class="code-quote">"/book.html"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

</pre>
</div></div>

<p>The above redirects the response to a default book.html page which is available directly
in the /webapps/book folder. Typically one would do it to return some static confirmation
to the client. For example, consider a POST form request that has been processed by a given
JAX-RS method and the only thing that needs to be done now is to return the HTML confirmation
view. Note that JAX-RS MessageBodyWriters are not invoked if the resource method returns no
custom object - which is not needed in the case of the static confirmation, so for RequestDispatcherProvider
be able to redirect to book.html one should simply introduce say an EmptyConfirmation bean
with no properties and return it from the resource method.</p>

<p>Here is another example (omitting jaxrs:server declaration for brewity):</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">

<span class="code-tag">&lt;bean id=<span class="code-quote">"dispatchProvider"</span>
class=<span class="code-quote">"org.apache.cxf.jaxrs.provider.RequestDispatcherProvider"</span>&gt;</span>
      <span class="code-tag">&lt;property name=<span class="code-quote">"resourcePath"</span>
value=<span class="code-quote">"/book.jsp"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

</pre>
</div></div>

<p>The only difference from the previous example is that "/book.jsp" will be delegated
to with the task of creating a view. This is a more interesting example and we presume that
the resource method returns say an instance of the "org.bar.Book" bean:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
@Path(<span class="code-quote">"/books"</span>)
<span class="code-keyword">public</span> Resource {
    @GET
    @Produces({<span class="code-quote">"text/html"</span>, <span class="code-quote">"application/xml"</span>})
    @Path(<span class="code-quote">"{id}"</span>)
    <span class="code-keyword">public</span> Book getBook(@PathParam(<span
class="code-quote">"id"</span>) <span class="code-object">String</span>
id) {
        <span class="code-comment">// <span class="code-keyword">return</span>
the book
</span>    }
}
</pre>
</div></div>

<p>Note how non-intrusive RequestDispatcherProvider is as far as writing the JAX-RS
resource code is concerned, you simply list supported media types in @Produces as usual. </p>

<p>RequestDispatcherProvider will make an instance of Book available as an HttpServletRequest
attribute named "org.bar.Book" by default. this can be customized. If a "beanName" property
is set, for example to "book", then book.jsp will access a Book instance as a "book" attribute.
If you have say 2 resource methods returning instances of different bean classes, possibly
for different view handlers then a beanNames map property can be used, for example: </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">

<span class="code-tag">&lt;bean id=<span class="code-quote">"dispatchProvider"</span>
class=<span class="code-quote">"org.apache.cxf.jaxrs.provider.RequestDispatcherProvider"</span>&gt;</span>
      <span class="code-tag">&lt;property name=<span class="code-quote">"classResources"</span>&gt;</span>
          <span class="code-tag">&lt;map&gt;</span>
             <span class="code-tag">&lt;entry key=<span class="code-quote">"org.bar.Book"</span>
 value=<span class="code-quote">"/book.jsp"</span>/&gt;</span>
             <span class="code-tag">&lt;entry key=<span class="code-quote">"org.bar.Customer"</span>
 value=<span class="code-quote">"/customer.jsp"</span>/&gt;</span>
          <span class="code-tag">&lt;/map&gt;</span>
      <span class="code-tag">&lt;/property&gt;</span>
      <span class="code-tag">&lt;property name=<span class="code-quote">"beanNames"</span>&gt;</span>
          <span class="code-tag">&lt;map&gt;</span>
             <span class="code-tag">&lt;entry key=<span class="code-quote">"org.bar.Book"</span>
 value=<span class="code-quote">"book"</span>/&gt;</span>
             <span class="code-tag">&lt;entry key=<span class="code-quote">"org.bar.Customer"</span>
 value=<span class="code-quote">"customer"</span>/&gt;</span>
          <span class="code-tag">&lt;/map&gt;</span>
      <span class="code-tag">&lt;/property&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

</pre>
</div></div>

<p>The above configuration says that a "book.jsp" resource will handle an instance of
Book by accessing it as a "book" attribute and a "customer.jsp" - an instance of Customer
by retrieving it as a "customer" attribute. Note you don't need to use the "beanNames" property
in such cases, a simpler "beanName" property can do unless you have a single (jsp) resource
dealing with both Book and Customer. </p>

<p>Apart from making an instance of response class available as HttpServletRequest attribute,
RequestDispatcherProvider will also make all the Path, Query and Matrix parameters available
as HttpServletRequest parameters (as opposed to attributes) by default. For example, given
the above code fragment, an HttpServletRequest parameter named "id" representing a @PathParam("id")
available to the view handler, as well as all other query and matrix parameters.<br/>
Note that RequestDispatcherProvider can be configured to save all these request parameters
as HttpServletRequest attributes by setting a boolean saveParametersAsAttributes property
to true. </p>

<p>Now, imagine a scenario like this: we have two resource methods returning a ReservationStatus
bean. The first method returns a successful confirmation or uses Response.seeOther(...) to
redirect to a method handling the failed reservation. So both methods return the same ReservationStatus
bean but we will have two different views handling successful and failed reservations respectively.
Here is one way to manage it:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"reserveRegistrationViews"</span>
class=<span class="code-quote">"org.apache.cxf.jaxrs.provider.RequestDispatcherProvider"</span>&gt;</span>
         <span class="code-tag">&lt;property name=<span class="code-quote">"resourcePaths"</span>&gt;</span>
            <span class="code-tag">&lt;map&gt;</span>
              <span class="code-tag">&lt;entry key=<span class="code-quote">"/reservations/reserve/complete"</span>
value=<span class="code-quote">"/forms/reservationConfirm.jsp"</span>/&gt;</span>
              <span class="code-tag">&lt;entry key=<span class="code-quote">"/reservations/reserve/failure"</span>
value=<span class="code-quote">"/forms/reservationFailure.jsp"</span>/&gt;</span>
            <span class="code-tag">&lt;/map&gt;</span>
         <span class="code-tag">&lt;/property&gt;</span>
         <span class="code-tag">&lt;property name=<span class="code-quote">"beanName"</span>
value=<span class="code-quote">"data"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>Given that the same ReservationStatus bean is returned in both cases, it is actually
the original request URI fragments which are used to match which view handler will deal with
a given ReservationStatus.</p>

<p>Finally, a 'servletContextPath' property can be used to have some other ServletContext
(as opposed to the current one) be used for RequestDispatcher look-ups. If set then the current
ServletContext.getContext(servletContextPath) will be used to get the needed ServletContext.</p>

<h1><a name="JAX-RSRedirection-WithCXFServlet"></a>With CXFServlet</h1>

<p>Please see the "Redirection" section on the <a href="/confluence/display/CXF20DOC/Servlet+Transport"
title="Servlet Transport">Servlet Transport</a> page.</p>

<p>Note that both CXFServlet and JAXRS RequestDispatcherProvider can work together effectively
on executing the redirection requests as described at that page.</p>

<p>If you have CXFServlet listening on "/" (thus effectively catching all the requests)
and also would like to use RequestDispatcher, then make sure that a 'dispatcherName' property
is also set, for example :</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;bean id=<span class="code-quote">"dispatchProvider"</span>
class=<span class="code-quote">"org.apache.cxf.jaxrs.provider.RequestDispatcherProvider"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"dispatcherName"</span>
value=<span class="code-quote">"jsp"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"resourcePath"</span>
value=<span class="code-quote">"/WEB-INF/jsp/test.jsp"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span> 
</pre>
</div></div>

<p>If resources which are redirected to can be made public (i.e, moved out of /WEB-INF)
then alternative option (instead of adding a 'dispatcherName' property to RequestDispatcherProvider
and still have CXFServlet listening on '/') is to configure both RequestDispatcherProvider
and CXFServlet to redirect to resources such as "/jsp/test.jsp".</p>

<h1><a name="JAX-RSRedirection-CustomRedirection"></a>Custom Redirection</h1>

<p>One can borrow some of the code from RequestDispatcherProvider and do the custom
redirection from CXF in interceptors or custom invokers, if you will try to do it then you
will also need to set an AbstractHTTPDestination.REQUEST_REDIRECTED property with a 'true'
value on a current input message.</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Redirection">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=24190950&revisedVersion=4&originalVersion=3">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CXF20DOC/JAX-RS+Redirection?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message