You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by nf...@apache.org on 2009/07/16 08:26:59 UTC

svn commit: r794548 - in /incubator/wink/trunk: wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ wink-server/src/main/java/org/apache/wink/server/internal/registry/ wink-server/src/main/java/org/apache/wink/server/internal/r...

Author: nfischer
Date: Thu Jul 16 06:26:59 2009
New Revision: 794548

URL: http://svn.apache.org/viewvc?rev=794548&view=rev
Log:
Fix for method is invoked but no HttpMethod [WINK-103]

Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/MethodMetadata.java
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/HtmlServiceDocumentResource.java
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/ServiceDocumentCollectionData.java
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/utils/SystemLinksBuilderImpl.java
    incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/EncodedTest.java
    incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/FindResourceMethodTest.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/MethodMetadata.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/MethodMetadata.java?rev=794548&r1=794547&r2=794548&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/MethodMetadata.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/MethodMetadata.java Thu Jul 16 06:26:59 2009
@@ -22,7 +22,6 @@
 
 import java.lang.reflect.Method;
 import java.util.ArrayList;
-import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -34,13 +33,14 @@
 
     private ClassMetadata    parent;
     private Method           reflectionMethod;
-    private Set<String>      httpMethod;
+    private String           httpMethod;
     private List<Injectable> formalParameters;
 
     public MethodMetadata(ClassMetadata parent) {
         this.parent = parent;
-        this.httpMethod = new LinkedHashSet<String>(); // linked hash sets to make the behaviour deterministic
         this.formalParameters = new ArrayList<Injectable>();
+        this.httpMethod = null;
+        this.reflectionMethod = null;
     }
 
     public Method getReflectionMethod() {
@@ -51,9 +51,13 @@
         this.reflectionMethod = reflectionMethod;
     }
 
-    public Set<String> getHttpMethod() {
+    public String getHttpMethod() {
         return httpMethod;
     }
+    
+    public void setHttpMethod(String httpMethod) {
+        this.httpMethod = httpMethod;
+    }
 
     public List<Injectable> getFormalParameters() {
         return formalParameters;

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java?rev=794548&r1=794547&r2=794548&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java Thu Jul 16 06:26:59 2009
@@ -27,7 +27,6 @@
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.lang.reflect.Type;
-import java.util.Set;
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DefaultValue;
@@ -144,10 +143,10 @@
             MethodMetadata methodMetadata = createMethodMetadata(method);
             if (methodMetadata != null) {
                 String path = methodMetadata.getPath();
-                // sub-resource
+                String httpMethod = methodMetadata.getHttpMethod();
                 if (path != null) {
-                    Set<String> httpMethods = methodMetadata.getHttpMethod();
-                    if (!httpMethods.isEmpty()) {
+                    // sub-resource
+                    if (httpMethod != null) {
                         // sub-resource method
                         getMetadata().getSubResourceMethods().add(methodMetadata);
                     } else {
@@ -193,16 +192,16 @@
         
         boolean hasAnnotation = false;
         
-        Path path = getPath(method);
-        if (path != null) {
+        HttpMethod httpMethod = getHttpMethod(method);
+        if (httpMethod != null) {
             hasAnnotation = true;
-            metadata.addPath(path.value());
+            metadata.setHttpMethod(httpMethod.value());
         }
 
-        HttpMethod httpMethod = getHttpMethod(method);
-        if (httpMethod != null) {
+        Path path = getPath(method);
+        if (path != null) {
             hasAnnotation = true;
-            metadata.getHttpMethod().add(httpMethod.value());
+            metadata.addPath(path.value());
         }
 
         String[] consumes = getConsumes(method);
@@ -255,9 +254,18 @@
                 }
             }
 
+            // annotations are not inherited. ignore this method.
             return null;
         }
 
+        // check if it's a valid resource method/sub-resource method/sub-resource locator,
+        // since there is at least one JAX-RS annotation on the method
+        if (metadata.getHttpMethod() == null && metadata.getPath() == null) {
+            logger.warn("The method {} in class {} is not annotated with an http method designator nor the Path annotation. " +
+                    "This method will be ignored.", method.getName(), method.getDeclaringClass().getCanonicalName());
+            return null;
+        }
+        
         parseMethodParameters(method, metadata);
 
         return metadata;

Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java?rev=794548&r1=794547&r2=794548&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/registry/ResourceRegistry.java Thu Jul 16 06:26:59 2009
@@ -213,7 +213,7 @@
         }
 
         for (MethodMetadata method : resourceMethods) {
-            set.addAll(method.getHttpMethod());
+            set.add(method.getHttpMethod());
         }
 
         return set;
@@ -426,14 +426,20 @@
      */
     private boolean filterByHttpMethod(MethodRecord record, RuntimeContext context) {
         String httpMethod = context.getRequest().getMethod();
-        Set<String> recordHttpMethod = record.getMetadata().getHttpMethod();
-        // non existing http method, it's ok
-        if (recordHttpMethod.size() == 0) {
+        String recordHttpMethod = record.getMetadata().getHttpMethod();
+        
+        // non existing http method (with a path on the method), 
+        // then it's a sub-resource locator and it's ok
+        if (recordHttpMethod == null) {
             return false;
         }
-        if (!recordHttpMethod.contains(httpMethod)) {
+        
+        // the http method is different than the request http method, 
+        // then the resource method should be filtered 
+        if (!recordHttpMethod.equals(httpMethod)) {
             return true;
         }
+        
         return false;
     }
 

Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/HtmlServiceDocumentResource.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/HtmlServiceDocumentResource.java?rev=794548&r1=794547&r2=794548&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/HtmlServiceDocumentResource.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/HtmlServiceDocumentResource.java Thu Jul 16 06:26:59 2009
@@ -263,8 +263,8 @@
     }
 
     private boolean providesOpenSearch(MethodMetadata method) {
-        if (method.getHttpMethod().contains(HttpMethod.GET)
-                && method.getProduces().contains(MediaTypeUtils.OPENSEARCH_TYPE)) {
+        if (method.getHttpMethod().equals(HttpMethod.GET)
+                && method.getProduces().equals(MediaTypeUtils.OPENSEARCH_TYPE)) {
             return true;
         }
         return false;

Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/ServiceDocumentCollectionData.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/ServiceDocumentCollectionData.java?rev=794548&r1=794547&r2=794548&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/ServiceDocumentCollectionData.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/resources/ServiceDocumentCollectionData.java Thu Jul 16 06:26:59 2009
@@ -238,12 +238,12 @@
     private static Set<MediaType> getCollectionConsumes(ClassMetadata metadata) {
         Set<MediaType> consumes = new HashSet<MediaType>();
         for (MethodMetadata method : metadata.getResourceMethods()) {
-            if (method.getHttpMethod().contains(HttpMethod.POST)) {
+            if (method.getHttpMethod().equals(HttpMethod.POST)) {
                 consumes.addAll(method.getConsumes());
             }
         }
         for (MethodMetadata method : metadata.getSubResourceMethods()) {
-            if (method.getHttpMethod().contains(HttpMethod.POST)) {
+            if (method.getHttpMethod().equals(HttpMethod.POST)) {
                 consumes.addAll(method.getConsumes());
             }
         }

Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/utils/SystemLinksBuilderImpl.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/utils/SystemLinksBuilderImpl.java?rev=794548&r1=794547&r2=794548&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/utils/SystemLinksBuilderImpl.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/utils/SystemLinksBuilderImpl.java Thu Jul 16 06:26:59 2009
@@ -145,8 +145,8 @@
         if (systemLinksToGenerate.contains(LinkType.EDIT)
             && getLink(set, AtomConstants.ATOM_REL_EDIT) == null) {
             for (MethodMetadata methodRecord : methods) {
-                Set<String> httpMethods = methodRecord.getHttpMethod();
-                if (httpMethods.contains(HttpMethod.PUT) || httpMethods.contains(HttpMethod.DELETE)) {
+                String httpMethod = methodRecord.getHttpMethod();
+                if (httpMethod.equalsIgnoreCase(HttpMethod.PUT) || httpMethod.equalsIgnoreCase(HttpMethod.DELETE)) {
                     set.add(createLink(AtomConstants.ATOM_REL_EDIT, null, selfUri));
                     break;
                 }
@@ -176,7 +176,7 @@
         Set<MediaType> getResponses = new LinkedHashSet<MediaType>();
         if (methods != null) {
             for (MethodMetadata methodRecord : methods) {
-                if (methodRecord.getHttpMethod().contains(HttpMethod.GET)) {
+                if (methodRecord.getHttpMethod().equals(HttpMethod.GET)) {
                     getResponses.addAll(methodRecord.getProduces());
                 }
             }

Modified: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/EncodedTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/EncodedTest.java?rev=794548&r1=794547&r2=794548&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/EncodedTest.java (original)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/EncodedTest.java Thu Jul 16 06:26:59 2009
@@ -88,7 +88,7 @@
     @Encoded
     @Path("/encoded")
     public static class EncodedResource {
-
+        @GET
         @Produces(MediaType.TEXT_HTML)
         public String getParam(@QueryParam("kulu") String kulu) {
             return kulu;

Modified: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/FindResourceMethodTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/FindResourceMethodTest.java?rev=794548&r1=794547&r2=794548&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/FindResourceMethodTest.java (original)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/registry/FindResourceMethodTest.java Thu Jul 16 06:26:59 2009
@@ -432,7 +432,16 @@
         public String get() {
             return "ResourceMultipleHttpMethods.get";
         }
-    }    
+    }
+    
+    @Path("/noHttpMethod")
+    public static class ResourceNoHttpMethod {
+        @Produces("text/plain")
+        public String get() {
+            fail("method ResourceNoHttpMethod.get shouldn't have been invoked");
+            return "ResourceNoHttpMethod.get";
+        }
+    }
 
     
     // /// -- Tests --
@@ -734,6 +743,10 @@
         request = MockRequestConstructor.constructMockRequest("GET", "/multipleHttpMethods", "text/plain");
         response = invoke(request);
         assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
+
+        request = MockRequestConstructor.constructMockRequest("GET", "/noHttpMethod", "text/plain");
+        response = invoke(request);
+        assertEquals(HttpStatus.METHOD_NOT_ALLOWED.getCode(), response.getStatus());
     }
     
     // // -- Helpers --