You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2010/11/04 21:24:24 UTC

svn commit: r1031217 - in /incubator/wink/trunk/wink-server/src/main: java/org/apache/wink/server/internal/DeploymentConfiguration.java java/org/apache/wink/server/internal/handlers/OptionsMethodWADLHandler.java resources/META-INF/wink-default.properties

Author: bluk
Date: Thu Nov  4 20:24:23 2010
New Revision: 1031217

URL: http://svn.apache.org/viewvc?rev=1031217&view=rev
Log:
Add an OptionsMethodWADLHandler

Add the ability to specify a WADL document to be served
instead of just a plain OPTIONS request.

See [WINK-313]

Added:
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/OptionsMethodWADLHandler.java
Modified:
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/DeploymentConfiguration.java
    incubator/wink/trunk/wink-server/src/main/resources/META-INF/wink-default.properties

Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/DeploymentConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/DeploymentConfiguration.java?rev=1031217&r1=1031216&r2=1031217&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/DeploymentConfiguration.java (original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/DeploymentConfiguration.java Thu Nov  4 20:24:23 2010
@@ -519,7 +519,21 @@ public class DeploymentConfiguration imp
         handlersChain.addHandler(createHandler(Requests.class));
         handlersChain.addHandler(createHandler(ResourceInvocation.class));
         handlersChain.addHandler(createHandler(SearchResultHandler.class));
-        handlersChain.addHandler(createHandler(OptionsMethodHandler.class));
+        String optionsHandler =
+            properties.getProperty("org.apache.wink.server.options.handler",
+                                   OptionsMethodHandler.class.getName());
+        if ("none".equals(optionsHandler)) {
+            optionsHandler = OptionsMethodHandler.class.getName();
+        }
+        logger.trace("org.apache.wink.server.options.handler value is {}", optionsHandler);
+        try {
+            handlersChain.addHandler(createHandler((Class<? extends RequestHandler>)Class
+                .forName(optionsHandler)));
+        } catch (Exception e) {
+            logger.trace("Could not load handlers class so adding default");
+            handlersChain.addHandler(createHandler(OptionsMethodHandler.class));
+        }
+
         handlersChain.addHandler(createHandler(HeadMethodHandler.class));
         handlersChain.addHandler(createHandler(FindRootResourceHandler.class));
         handlersChain.addHandler(createHandler(FindResourceMethodHandler.class));

Added: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/OptionsMethodWADLHandler.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/OptionsMethodWADLHandler.java?rev=1031217&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/OptionsMethodWADLHandler.java (added)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/OptionsMethodWADLHandler.java Thu Nov  4 20:24:23 2010
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *  
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.wink.server.internal.handlers;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import org.apache.wink.common.http.HttpHeadersEx;
+import org.apache.wink.common.http.HttpMethodEx;
+import org.apache.wink.common.http.HttpStatus;
+import org.apache.wink.common.internal.utils.HeaderUtils;
+import org.apache.wink.common.model.wadl.Application;
+import org.apache.wink.common.model.wadl.WADLGenerator;
+import org.apache.wink.server.handlers.HandlersChain;
+import org.apache.wink.server.handlers.MessageContext;
+import org.apache.wink.server.handlers.RequestHandler;
+import org.apache.wink.server.internal.handlers.SearchResult.MethodType;
+import org.apache.wink.server.internal.registry.ResourceRegistry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class OptionsMethodWADLHandler implements RequestHandler {
+
+    private static final Logger logger = LoggerFactory.getLogger(OptionsMethodWADLHandler.class);
+
+    public void handleRequest(MessageContext context, HandlersChain chain) throws Throwable {
+
+        // first thing - proceed the chain
+        chain.doChain(context);
+
+        // check the search result.
+        // if the request is OPTIONS and no method was found, generate the
+        // response automatically
+        SearchResult searchResult = context.getAttribute(SearchResult.class);
+        if (searchResult.isError() && searchResult.getError().getResponse().getStatus() == HttpStatus.METHOD_NOT_ALLOWED
+            .getCode()
+            && context.getHttpMethod().equalsIgnoreCase(HttpMethodEx.OPTIONS)) {
+            // get supported HTTP methods
+            ResourceRegistry resourceRegistry = context.getAttribute(ResourceRegistry.class);
+            Set<Class<?>> c = new HashSet<Class<?>>();
+            Class<?> resourceClass = searchResult.getResource().getResourceClass();
+            c.add(resourceClass);
+            logger.trace("Found the resource classes {}", c);
+            Application app = new WADLHandlerWADLGenerator().generate(context.getUriInfo().getBaseUri().toString(), c);
+            if(context.getUriInfo().getMatchedResources().size() > 1) {
+                // sub-resource locator so lets correct the path
+                app.getResources().get(0).getResource().get(0).setPath(context.getUriInfo().getMatchedURIs().get(0));
+            }
+
+            Set<String> httpMethods = resourceRegistry.getOptions(searchResult.getResource());
+
+            logger
+                .trace("Invoking OPTIONS request handled by runtime with {} resource and {} HTTP methods", //$NON-NLS-1$
+                       searchResult.getResource(),
+                       httpMethods);
+            if (httpMethods.size() > 0) {
+                String allowHeader = HeaderUtils.buildOptionsHeader(httpMethods);
+                // add 'Allow' header to the response
+                context.getAttribute(HttpServletResponse.class).addHeader(HttpHeadersEx.ALLOW,
+                                                                          allowHeader);
+                // remove an error from the search result
+                context.getAttribute(SearchResult.class).setError(null);
+                // set result to no-content
+                context.setResponseStatusCode(Response.Status.OK.getStatusCode());
+                context.setResponseEntity(Response.ok(app).type("application/vnd.sun.wadl+xml")
+                    .build());
+            }
+        }
+    }
+    
+    public void init(Properties props) {
+    }
+
+    private class WADLHandlerWADLGenerator extends WADLGenerator {
+        
+        /*
+         * Assume we know what we're doing internally
+         */
+        @Override
+        protected boolean isResource(Class<?> cls) {
+            return true;
+        }
+    }
+}

Modified: incubator/wink/trunk/wink-server/src/main/resources/META-INF/wink-default.properties
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/resources/META-INF/wink-default.properties?rev=1031217&r1=1031216&r2=1031217&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/resources/META-INF/wink-default.properties (original)
+++ incubator/wink/trunk/wink-server/src/main/resources/META-INF/wink-default.properties Thu Nov  4 20:24:23 2010
@@ -62,3 +62,6 @@ wink.response.useAcceptCharset=false
 # resource methods with a "void" return type will ignore the inherited
 # Produces values.
 org.apache.wink.server.resources.strictInterpretConsumesAndProduces=false
+
+# The class name of the OPTIONS handler
+org.apache.wink.server.options.handler=org.apache.wink.server.internal.handlers.OptionsMethodHandler