wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r811670 - in /incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal: DeploymentConfiguration.java contexts/UriInfoImpl.java handlers/ServerMessageContext.java servlet/RestFilter.java
Date Sat, 05 Sep 2009 16:29:43 GMT
Author: bluk
Date: Sat Sep  5 16:29:42 2009
New Revision: 811670

URL: http://svn.apache.org/viewvc?rev=811670&view=rev
Log:
Add RestFilter

See [WINK-155]

Added:
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestFilter.java
  (with props)
Modified:
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/DeploymentConfiguration.java
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/UriInfoImpl.java
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/ServerMessageContext.java

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=811670&r1=811669&r2=811670&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
Sat Sep  5 16:29:42 2009
@@ -29,6 +29,7 @@
 import java.util.Properties;
 import java.util.Map.Entry;
 
+import javax.servlet.FilterConfig;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.ws.rs.WebApplicationException;
@@ -112,6 +113,7 @@
     // servlet configuration
     private ServletConfig             servletConfig;
     private ServletContext            servletContext;
+    private FilterConfig              filterConfig;
 
     private String[]                  httpMethodOverrideHeaders;
 
@@ -211,6 +213,14 @@
         this.servletConfig = servletConfig;
     }
 
+    public FilterConfig getFilterConfig() {
+        return filterConfig;
+    }
+
+    public void setFilterConfig(FilterConfig filterConfig) {
+        this.filterConfig = filterConfig;
+    }
+
     public ServletContext getServletContext() {
         return servletContext;
     }

Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/UriInfoImpl.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/UriInfoImpl.java?rev=811670&r1=811669&r2=811670&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/UriInfoImpl.java
(original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/UriInfoImpl.java
Sat Sep  5 16:29:42 2009
@@ -27,22 +27,24 @@
 import java.util.List;
 import java.util.Properties;
 
+import javax.servlet.FilterConfig;
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.apache.wink.common.internal.MultivaluedMapImpl;
 import org.apache.wink.common.internal.PathSegmentImpl;
 import org.apache.wink.common.internal.i18n.Messages;
+import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
 import org.apache.wink.common.internal.uri.UriEncoder;
 import org.apache.wink.common.internal.utils.UriHelper;
 import org.apache.wink.server.handlers.MessageContext;
 import org.apache.wink.server.internal.handlers.SearchResult;
 import org.apache.wink.server.internal.registry.ResourceInstance;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class UriInfoImpl implements UriInfo {
 
@@ -325,7 +327,10 @@
         if (contextPath != null) {
             builder.append(contextPath);
         }
-        if (request.getServletPath() != null) {
+
+        boolean isServlet =
+            RuntimeContextTLS.getRuntimeContext().getAttribute(FilterConfig.class) == null;
+        if (request.getServletPath() != null && isServlet) {
             builder.append(request.getServletPath());
         }
         if (builder.charAt(builder.length() - 1) != '/') {
@@ -334,7 +339,7 @@
         return builder.toString();
     }
 
-    private String buildRequestPath(HttpServletRequest request) {
+    private static String buildRequestPath(HttpServletRequest request) {
         // we cannot use request.getPathInfo() since it cuts off the ';'
         // parameters on Tomcat
         String requestPath = request.getRequestURI();
@@ -348,7 +353,9 @@
         }
 
         // cut off the servlet path from the beginning
-        if (request.getServletPath() != null) {
+        boolean isServlet =
+            RuntimeContextTLS.getRuntimeContext().getAttribute(FilterConfig.class) == null;
+        if (request.getServletPath() != null && isServlet) {
             requestPath = requestPath.substring(request.getServletPath().length());
         }
 

Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/ServerMessageContext.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/ServerMessageContext.java?rev=811670&r1=811669&r2=811670&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/ServerMessageContext.java
(original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/handlers/ServerMessageContext.java
Sat Sep  5 16:29:42 2009
@@ -24,6 +24,7 @@
 import java.io.OutputStream;
 import java.util.Properties;
 
+import javax.servlet.FilterConfig;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
@@ -78,6 +79,7 @@
         setAttribute(HttpServletResponseWrapper.class, responseWrapper);
         setAttribute(ServletContext.class, configuration.getServletContext());
         setAttribute(ServletConfig.class, configuration.getServletConfig());
+        setAttribute(FilterConfig.class, configuration.getFilterConfig());
         setAttribute(DeploymentConfiguration.class, configuration);
         setAttribute(ResourceRegistry.class, configuration.getResourceRegistry());
         setAttribute(ProvidersRegistry.class, configuration.getProvidersRegistry());

Added: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestFilter.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestFilter.java?rev=811670&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestFilter.java
(added)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestFilter.java
Sat Sep  5 16:29:42 2009
@@ -0,0 +1,147 @@
+/*******************************************************************************
+ * 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.servlet;
+
+import java.io.IOException;
+import java.util.Enumeration;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+import org.apache.wink.server.internal.DeploymentConfiguration;
+
+/**
+ * <p>
+ * Filter that is used by the runtime to handle the incoming request.
+ * </p>
+ * Initialization and configuration options are the same as the RestServlet.
+ * 
+ * @see RestServlet
+ */
+public class RestFilter implements Filter {
+
+    private RestServlet restServlet;
+
+    private static class FilteredHttpServletResponse extends HttpServletResponseWrapper {
+
+        public FilteredHttpServletResponse(HttpServletResponse response) {
+            super(response);
+        }
+
+        private int statusCode;
+
+        @Override
+        public void setStatus(int statusCode) {
+            super.setStatus(statusCode);
+            this.statusCode = statusCode;
+        }
+
+        @Override
+        public void setStatus(int statusCode, String msg) {
+            super.setStatus(statusCode, msg);
+            this.statusCode = statusCode;
+        }
+
+        int getStatusCode() {
+            return this.statusCode;
+        }
+
+    }
+
+    public void doFilter(ServletRequest servletRequest,
+                         ServletResponse servletResponse,
+                         FilterChain chain) throws IOException, ServletException {
+
+        if (servletRequest instanceof HttpServletRequest && servletResponse instanceof
HttpServletResponse) {
+            FilteredHttpServletResponse httpServletResponse =
+                new FilteredHttpServletResponse((HttpServletResponse)servletResponse);
+            restServlet.service((HttpServletRequest)servletRequest,
+                                (HttpServletResponse)httpServletResponse);
+
+            if (!httpServletResponse.isCommitted() && httpServletResponse.getStatusCode()
== HttpServletResponse.SC_NOT_FOUND) {
+                /*
+                 * reset the status to 200 so that if a future
+                 * HttpServletResponse call uses an include (or writes
+                 * directly), then the status code is like the filter was never
+                 * invoked
+                 */
+                httpServletResponse.setStatus(HttpServletResponse.SC_OK);
+                chain.doFilter(servletRequest, servletResponse);
+            }
+        } else {
+            chain.doFilter(servletRequest, servletResponse);
+        }
+    }
+
+    protected static class RestServletForFilter extends RestServlet {
+        protected FilterConfig    filterConfig;
+
+        private static final long serialVersionUID = 5230595914609866319L;
+
+        public RestServletForFilter(FilterConfig config) {
+            super();
+            this.filterConfig = config;
+        }
+
+        protected DeploymentConfiguration getDeploymentConfiguration()
+            throws ClassNotFoundException, InstantiationException, IllegalAccessException,
+            IOException {
+            DeploymentConfiguration deploymentConfiguration = super.getDeploymentConfiguration();
+            deploymentConfiguration.setFilterConfig(filterConfig);
+            return deploymentConfiguration;
+        }
+    }
+
+    public void init(final FilterConfig filterConfig) throws ServletException {
+        restServlet = new RestServletForFilter(filterConfig);
+
+        restServlet.init(new ServletConfig() {
+
+            public String getServletName() {
+                return filterConfig.getFilterName();
+            }
+
+            public ServletContext getServletContext() {
+                return filterConfig.getServletContext();
+            }
+
+            public Enumeration<?> getInitParameterNames() {
+                return filterConfig.getInitParameterNames();
+            }
+
+            public String getInitParameter(String paramName) {
+                return filterConfig.getInitParameter(paramName);
+            }
+        });
+    }
+
+    public void destroy() {
+        restServlet.destroy();
+    }
+}

Propchange: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/servlet/RestFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message