cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r840751 - in /websites/production/cxf/content: cache/docs.pageCache docs/jax-rs-basics.html
Date Tue, 04 Dec 2012 16:48:10 GMT
Author: buildbot
Date: Tue Dec  4 16:48:10 2012
New Revision: 840751

Log:
Production update by buildbot for cxf

Modified:
    websites/production/cxf/content/cache/docs.pageCache
    websites/production/cxf/content/docs/jax-rs-basics.html

Modified: websites/production/cxf/content/cache/docs.pageCache
==============================================================================
Binary files - no diff available.

Modified: websites/production/cxf/content/docs/jax-rs-basics.html
==============================================================================
--- websites/production/cxf/content/docs/jax-rs-basics.html (original)
+++ websites/production/cxf/content/docs/jax-rs-basics.html Tue Dec  4 16:48:10 2012
@@ -728,6 +728,22 @@ A custom ResourceComparator can be used 
 <div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
 <pre class="code-java">
 
+<span class="code-keyword">import</span> java.io.UnsupportedEncodingException;
+<span class="code-keyword">import</span> java.net.URLDecoder;
+<span class="code-keyword">import</span> java.util.HashSet;
+<span class="code-keyword">import</span> java.util.List;
+<span class="code-keyword">import</span> java.util.Set;
+
+<span class="code-keyword">import</span> javax.ws.rs.core.MultivaluedMap;
+
+<span class="code-keyword">import</span> org.apache.cxf.jaxrs.ext.ResourceComparator;
+<span class="code-keyword">import</span> org.apache.cxf.jaxrs.model.ClassResourceInfo;
+<span class="code-keyword">import</span> org.apache.cxf.jaxrs.model.OperationResourceInfo;
+<span class="code-keyword">import</span> org.apache.cxf.jaxrs.model.OperationResourceInfoComparator;
+<span class="code-keyword">import</span> org.apache.cxf.jaxrs.model.Parameter;
+<span class="code-keyword">import</span> org.apache.cxf.jaxrs.util.JAXRSUtils;
+<span class="code-keyword">import</span> org.apache.cxf.message.Message;
+
 <span class="code-keyword">public</span> class QueryResourceInfoComperator <span
class="code-keyword">extends</span> OperationResourceInfoComparator <span class="code-keyword">implements</span>
          ResourceComparator {
 
@@ -749,12 +765,8 @@ A custom ResourceComparator can be used 
          <span class="code-keyword">if</span> (cxfResult != 0)
              <span class="code-keyword">return</span> cxfResult;
 
-         <span class="code-comment">// Compare QueryParam annotations
-</span>         Set&lt;<span class="code-object">String</span>&gt;
 qParams = getParams((<span class="code-object">String</span>) message.get(Message.QUERY_STRING));
-         <span class="code-object">int</span> op1Counter = getMatchingRate(getAnnotations(oper1.getMethodToInvoke().getParameterAnnotations()),
-                 qParams);
-         <span class="code-object">int</span> op2Counter = getMatchingRate(getAnnotations(oper2.getMethodToInvoke().getParameterAnnotations()),
-                 qParams);
+         <span class="code-object">int</span> op1Counter = getMatchingRate(oper1,
message);
+         <span class="code-object">int</span> op2Counter = getMatchingRate(oper2,
message);
 
          <span class="code-keyword">return</span> op1Counter == op2Counter
                  ? 0
@@ -765,62 +777,48 @@ A custom ResourceComparator can be used 
 
      /**
       * This method calculates a number indicating a good or bad match between
-      * queryParams from request and annotated method parameters. A higher number
-      * means a better match.
+      * values provided within the request and expected method parameters. A
+      * higher number means a better match.
       *
-      * @param annotations
-      *            Map contains name of QueryParam method parameters as a key and
-      *            a <span class="code-object">Boolean</span> value indicating
an existing <span class="code-keyword">default</span> value <span class="code-keyword">for</span>
<span class="code-keyword">this</span>
-      *            parameter.
-      * @param queryParams
-      *            A Set of query parameters provided within the request
+      * @param operation
+      *            The operation to be rated, based on contained parameterInfo
+      *            values.
+      * @param message
+      *            A message containing query from user request
       * @<span class="code-keyword">return</span> A positive or negative number,
indicating a good match between
       *         query and method
       */
-     <span class="code-keyword">protected</span> <span class="code-object">int</span>
getMatchingRate(Map&lt;<span class="code-object">String</span>, <span class="code-object">Boolean</span>&gt;
 annotations, Set&lt;<span class="code-object">String</span>&gt;  queryParams)
{
-         <span class="code-object">int</span> rate = 0;
-         <span class="code-keyword">for</span> (<span class="code-object">String</span>
anno : annotations.keySet()) {
-             <span class="code-keyword">if</span> (queryParams.contains(anno))
{
-                 <span class="code-comment">// URL query matches one method parameter
-</span>                 rate += 2;
-             } <span class="code-keyword">else</span> <span class="code-keyword">if</span>
(!annotations.get(anno).booleanValue()) {
-                 <span class="code-comment">// No <span class="code-keyword">default</span>
value exists <span class="code-keyword">for</span> method parameter
-</span>                 rate -= 1;
-             }
-         }
-         <span class="code-keyword">return</span> rate;
-     }
+     <span class="code-keyword">protected</span> <span class="code-object">int</span>
getMatchingRate(OperationResourceInfo operation, Message message) {
 
-     /**
-      * @param opParamAnnos
-      *            Array containing all annotations <span class="code-keyword">for</span>
all method parameters
-      * @<span class="code-keyword">return</span> Key in Map is QueryParam name,
and Value indicates <span class="code-keyword">if</span> a <span class="code-keyword">default</span>
-      *         value is present.
-      */
-     <span class="code-keyword">protected</span> Map&lt;<span class="code-object">String</span>,
<span class="code-object">Boolean</span>&gt;  getAnnotations(Annotation[][]
opParamAnnos) {
-         Map&lt;<span class="code-object">String</span>, <span class="code-object">Boolean</span>&gt;
 parameterAnnos = <span class="code-keyword">new</span> HashMap&lt;<span
class="code-object">String</span>, <span class="code-object">Boolean</span>&gt;();
+         List&lt;Parameter&gt;  params = operation.getParameters();
+         <span class="code-keyword">if</span> (params == <span class="code-keyword">null</span>
|| params.size() == 0)
+             <span class="code-keyword">return</span> 0;
 
-         <span class="code-keyword">if</span> (opParamAnnos.length == 0)
-             <span class="code-keyword">return</span> parameterAnnos;
+         <span class="code-comment">// Get Request QueryParams
+</span>         Set&lt;<span class="code-object">String</span>&gt;
 qParams = getParams((<span class="code-object">String</span>) message.get(Message.QUERY_STRING));
 
-         <span class="code-keyword">for</span> (Annotation[] pAnnos : opParamAnnos)
{
-             QueryParam qParam = <span class="code-keyword">null</span>;
-             DefaultValue dValue = <span class="code-keyword">null</span>;
-             <span class="code-keyword">for</span> (Annotation anno : pAnnos)
{
-                 <span class="code-keyword">if</span> (anno <span class="code-keyword">instanceof</span>
QueryParam)
-                     qParam = (QueryParam) anno;
-                 <span class="code-keyword">if</span> (anno <span class="code-keyword">instanceof</span>
DefaultValue)
-                     dValue = (DefaultValue) anno;
+         <span class="code-object">int</span> rate = 0;
+         <span class="code-keyword">for</span> (Parameter p : params) {
+             <span class="code-keyword">switch</span> (p.getType()) {
+             <span class="code-keyword">case</span> QUERY:
+                 <span class="code-keyword">if</span> (qParams.contains(p.getName()))
+                     rate += 2;
+                 <span class="code-keyword">else</span> <span class="code-keyword">if</span>
(p.getDefaultValue() == <span class="code-keyword">null</span>)
+                     rate -= 1;
+                 <span class="code-keyword">break</span>;
+             <span class="code-comment">// optionally support other parameter types
such as headers, etc
+</span>             <span class="code-comment">// <span class="code-keyword">case</span>
HEADER:
+</span>             <span class="code-comment">//  <span class="code-keyword">break</span>;
+</span>             <span class="code-keyword">default</span>:
+                 <span class="code-keyword">break</span>;
              }
-             parameterAnnos.put(qParam.value(), <span class="code-object">Boolean</span>.valueOf((dValue
!= <span class="code-keyword">null</span>)));
          }
-
-         <span class="code-keyword">return</span> parameterAnnos;
+         <span class="code-keyword">return</span> rate;
      }
 
      /**
       * @param query
-      *            URL Query
+      *            URL Query Example: 'key=value&amp;key2=value2'
       * @<span class="code-keyword">return</span> A Set of all keys, contained
within query.
       */
      <span class="code-keyword">protected</span> Set&lt;<span class="code-object">String</span>&gt;
 getParams(<span class="code-object">String</span> query) {
@@ -828,19 +826,11 @@ A custom ResourceComparator can be used 
          <span class="code-keyword">if</span> (query == <span class="code-keyword">null</span>
|| query.length() == 0)
              <span class="code-keyword">return</span> params;
 
-         <span class="code-keyword">try</span> {
-             <span class="code-keyword">for</span> (<span class="code-object">String</span>
param : query.split(<span class="code-quote">"&amp;"</span>)) {
-                 <span class="code-object">String</span> pair[] = param.split(<span
class="code-quote">"="</span>);
-                 <span class="code-object">String</span> key = URLDecoder.decode(pair[0],
<span class="code-quote">"UTF-8"</span>);
-                 params.add(key);
-             }
-         } <span class="code-keyword">catch</span> (UnsupportedEncodingException
e) {
-             e.printStackTrace();
-         }
-         <span class="code-keyword">return</span> params;
+         MultivaluedMap&lt;<span class="code-object">String</span>, <span
class="code-object">String</span>&gt; allQueries =
+             JAXRSUtils.getStructuredParams(query, <span class="code-quote">"&amp;"</span>,
<span class="code-keyword">false</span>, <span class="code-keyword">false</span>);
+         <span class="code-keyword">return</span> allQueries.keySet();
      }
-}
-
+} 
 </pre>
 </div></div>
 
@@ -867,7 +857,7 @@ A custom ResourceComparator can be used 
 </pre>
 </div></div> 
 
-<p>Using the custom comparator will lead to getFoo() method accepting a single query
parameter selected when a request URI has only one query parameter, and getFoo() method accepting
multiple query parameters selected when a request URI has at least two query parameters. Further
customizations can be made to get the parameter values affecting the selection process too.
  </p>
+<p>Using the custom comparator will lead to getFoo() method accepting a single query
parameter selected when a request URI has only one query parameter, and getFoo() method accepting
multiple query parameters selected when a request URI has at least two query parameters. Further
customizations may also be possible.   </p>
 
 <h1><a shape="rect" name="JAX-RSBasics-Contextannotations"></a>Context
annotations</h1>
 



Mime
View raw message