felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1757888 - in /felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal: handler/PreprocessorHandler.java runtime/dto/RegistryRuntime.java runtime/dto/RuntimeDTOBuilder.java whiteboard/WhiteboardManager.java
Date Fri, 26 Aug 2016 16:09:43 GMT
Author: cziegeler
Date: Fri Aug 26 16:09:43 2016
New Revision: 1757888

URL: http://svn.apache.org/viewvc?rev=1757888&view=rev
Log:
FELIX-5298 : [RFC 223] 5.2 Request Preprocessing

Added:
    felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java
  (with props)
Modified:
    felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RegistryRuntime.java
    felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
    felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java

Added: felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java?rev=1757888&view=auto
==============================================================================
--- felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java
(added)
+++ felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java
Fri Aug 26 16:09:43 2016
@@ -0,0 +1,162 @@
+/*
+ * 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.felix.http.base.internal.handler;
+
+import java.io.IOException;
+
+import javax.annotation.Nonnull;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.felix.http.base.internal.logger.SystemLogger;
+import org.apache.felix.http.base.internal.runtime.PreprocessorInfo;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.http.runtime.dto.DTOConstants;
+import org.osgi.service.http.whiteboard.Preprocessor;
+
+/**
+ * The preprocessor handler handles the initialization and destruction of preprocessor
+ * objects.
+ */
+public class PreprocessorHandler implements Comparable<PreprocessorHandler>
+{
+    private final PreprocessorInfo info;
+
+    private final ServletContext context;
+
+    private final BundleContext bundleContext;
+
+    private volatile Preprocessor preprocessor;
+
+    public PreprocessorHandler(final BundleContext bundleContext,
+            final ServletContext context,
+            final PreprocessorInfo info)
+    {
+        this.bundleContext = bundleContext;
+        this.context = context;
+        this.info = info;
+    }
+
+    @Override
+    public int compareTo(final PreprocessorHandler other)
+    {
+        return this.info.compareTo(other.info);
+    }
+
+    public ServletContext getContext()
+    {
+        return this.context;
+    }
+
+    public PreprocessorInfo getPreprocessorInfo()
+    {
+        return this.info;
+    }
+
+    public int init()
+    {
+        final ServiceReference<Preprocessor> serviceReference = this.info.getServiceReference();
+        this.preprocessor = this.bundleContext.getService(serviceReference);
+
+        if (this.preprocessor == null)
+        {
+            return DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE;
+        }
+
+        try
+        {
+            this.preprocessor.init(new FilterConfigImpl(this.preprocessor.getClass().getName(),
+                    getContext(),
+                    getPreprocessorInfo().getInitParameters()));
+        }
+        catch (final Exception e)
+        {
+            SystemLogger.error(this.getPreprocessorInfo().getServiceReference(),
+                    "Error during calling init() on preprocessor " + this.preprocessor,
+                    e);
+
+            this.preprocessor = null;
+            this.bundleContext.ungetService(serviceReference);
+
+            return DTOConstants.FAILURE_REASON_EXCEPTION_ON_INIT;
+        }
+
+        return -1;
+    }
+
+    public boolean destroy()
+    {
+        if (this.preprocessor == null)
+        {
+            return false;
+        }
+
+        try
+        {
+            preprocessor.destroy();
+        }
+        catch ( final Exception ignore )
+        {
+            // we ignore this
+            SystemLogger.error(this.getPreprocessorInfo().getServiceReference(),
+                    "Error during calling destroy() on preprocessor " + this.preprocessor,
+                    ignore);
+        }
+        this.preprocessor = null;
+        this.bundleContext.ungetService(this.info.getServiceReference());
+
+        return true;
+    }
+
+    public void handle(@Nonnull final ServletRequest req,
+            @Nonnull final ServletResponse res,
+            @Nonnull final FilterChain chain) throws ServletException, IOException
+    {
+        this.preprocessor.doFilter(req, res, chain);
+    }
+
+    public boolean dispose()
+    {
+        // fully destroy the preprocessor
+        return this.destroy();
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return 31 + info.hashCode();
+    }
+
+    @Override
+    public boolean equals(final Object obj)
+    {
+        if (this == obj)
+        {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass() )
+        {
+            return false;
+        }
+        final PreprocessorHandler other = (PreprocessorHandler) obj;
+        return info.equals(other.info);
+    }
+}

Propchange: felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/handler/PreprocessorHandler.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Modified: felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RegistryRuntime.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RegistryRuntime.java?rev=1757888&r1=1757887&r2=1757888&view=diff
==============================================================================
--- felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RegistryRuntime.java
(original)
+++ felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RegistryRuntime.java
Fri Aug 26 16:09:43 2016
@@ -20,18 +20,22 @@ package org.apache.felix.http.base.inter
 
 import java.util.Collection;
 
+import org.osgi.service.http.runtime.dto.PreprocessorDTO;
 import org.osgi.service.http.runtime.dto.ServletContextDTO;
 
 public final class RegistryRuntime
 {
     private final Collection<ServletContextDTO> contextDTOs;
     private final FailedDTOHolder failedDTOHolder;
+    private final Collection<PreprocessorDTO> preprocessorDTOs;
 
     public RegistryRuntime(final FailedDTOHolder failedDTOHolder,
-            final Collection<ServletContextDTO> contextDTOs)
+            final Collection<ServletContextDTO> contextDTOs,
+            final Collection<PreprocessorDTO> preprocessorDTOs)
     {
         this.failedDTOHolder = failedDTOHolder;
         this.contextDTOs = contextDTOs;
+        this.preprocessorDTOs = preprocessorDTOs;
     }
 
     public FailedDTOHolder getFailedDTOHolder()
@@ -43,4 +47,9 @@ public final class RegistryRuntime
     {
         return this.contextDTOs;
     }
+
+    public Collection<PreprocessorDTO> getPreprocessorDTOs()
+    {
+        return this.preprocessorDTOs;
+    }
 }

Modified: felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java?rev=1757888&r1=1757887&r2=1757888&view=diff
==============================================================================
--- felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
(original)
+++ felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
Fri Aug 26 16:09:43 2016
@@ -34,6 +34,7 @@ import org.osgi.service.http.runtime.dto
 import org.osgi.service.http.runtime.dto.FailedResourceDTO;
 import org.osgi.service.http.runtime.dto.FailedServletContextDTO;
 import org.osgi.service.http.runtime.dto.FailedServletDTO;
+import org.osgi.service.http.runtime.dto.PreprocessorDTO;
 import org.osgi.service.http.runtime.dto.RuntimeDTO;
 import org.osgi.service.http.runtime.dto.ServletContextDTO;
 
@@ -54,6 +55,7 @@ public final class RuntimeDTOBuilder
         final RuntimeDTO runtimeDTO = new RuntimeDTO();
         runtimeDTO.serviceDTO = createServiceDTO();
         runtimeDTO.servletContextDTOs = createContextDTOs();
+        runtimeDTO.preprocessorDTOs = createPreprocessorDTOs();
 
         runtimeDTO.failedErrorPageDTOs = registry.getFailedDTOHolder().failedErrorPageDTOs.toArray(new
FailedErrorPageDTO[registry.getFailedDTOHolder().failedErrorPageDTOs.size()]);
         runtimeDTO.failedFilterDTOs = registry.getFailedDTOHolder().failedFilterDTOs.toArray(new
FailedFilterDTO[registry.getFailedDTOHolder().failedFilterDTOs.size()]);
@@ -100,4 +102,10 @@ public final class RuntimeDTOBuilder
         final Collection<ServletContextDTO> contexts = registry.getServletContextDTOs();
         return contexts.toArray(new ServletContextDTO[contexts.size()]);
     }
+
+    private PreprocessorDTO[] createPreprocessorDTOs()
+    {
+        final Collection<PreprocessorDTO> dtos = registry.getPreprocessorDTOs();
+        return dtos.toArray(new PreprocessorDTO[dtos.size()]);
+    }
 }

Modified: felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java?rev=1757888&r1=1757887&r2=1757888&view=diff
==============================================================================
--- felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
(original)
+++ felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
Fri Aug 26 16:09:43 2016
@@ -21,6 +21,7 @@ import static org.osgi.service.http.runt
 import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_UNKNOWN;
 import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_VALIDATION_FAILED;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -31,11 +32,16 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 import javax.annotation.Nonnull;
+import javax.servlet.FilterChain;
 import javax.servlet.ServletContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
+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.HttpSession;
@@ -47,6 +53,7 @@ import org.apache.felix.http.base.intern
 import org.apache.felix.http.base.internal.handler.HttpServiceServletHandler;
 import org.apache.felix.http.base.internal.handler.HttpSessionWrapper;
 import org.apache.felix.http.base.internal.handler.ListenerHandler;
+import org.apache.felix.http.base.internal.handler.PreprocessorHandler;
 import org.apache.felix.http.base.internal.handler.ServletHandler;
 import org.apache.felix.http.base.internal.handler.WhiteboardFilterHandler;
 import org.apache.felix.http.base.internal.handler.WhiteboardListenerHandler;
@@ -63,6 +70,7 @@ import org.apache.felix.http.base.intern
 import org.apache.felix.http.base.internal.runtime.ServletInfo;
 import org.apache.felix.http.base.internal.runtime.WhiteboardServiceInfo;
 import org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder;
+import org.apache.felix.http.base.internal.runtime.dto.PreprocessorDTOBuilder;
 import org.apache.felix.http.base.internal.runtime.dto.RegistryRuntime;
 import org.apache.felix.http.base.internal.runtime.dto.ServletContextDTOBuilder;
 import org.apache.felix.http.base.internal.service.HttpServiceFactory;
@@ -85,6 +93,7 @@ import org.osgi.service.http.context.Ser
 import org.osgi.service.http.runtime.HttpServiceRuntime;
 import org.osgi.service.http.runtime.HttpServiceRuntimeConstants;
 import org.osgi.service.http.runtime.dto.DTOConstants;
+import org.osgi.service.http.runtime.dto.PreprocessorDTO;
 import org.osgi.service.http.runtime.dto.ServletContextDTO;
 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
 import org.osgi.util.tracker.ServiceTracker;
@@ -109,6 +118,8 @@ public final class WhiteboardManager
     /** A map with all servlet/filter registrations, mapped by abstract info. */
     private final Map<WhiteboardServiceInfo<?>, List<WhiteboardContextHandler>>
servicesMap = new HashMap<WhiteboardServiceInfo<?>, List<WhiteboardContextHandler>>();
 
+    private volatile List<PreprocessorHandler> preprocessorHandlers = Collections.emptyList();
+
     private final HandlerRegistry registry;
 
     private final FailureStateHandler failureStateHandler = new FailureStateHandler();
@@ -224,6 +235,7 @@ public final class WhiteboardManager
 
         this.serviceRuntime.setServiceReference(null);
 
+        this.preprocessorHandlers = Collections.emptyList();
         this.contextMap.clear();
         this.servicesMap.clear();
         this.failureStateHandler.clear();
@@ -588,7 +600,25 @@ public final class WhiteboardManager
             {
                 if ( info instanceof PreprocessorInfo )
                 {
-                    // TODO
+                    final PreprocessorHandler handler = new PreprocessorHandler(this.httpBundleContext,
+                            this.webContext, ((PreprocessorInfo)info));
+                    final int result = handler.init();
+                    if ( result == -1 )
+                    {
+                        synchronized ( this.preprocessorHandlers )
+                        {
+                            final List<PreprocessorHandler> newList = new ArrayList<PreprocessorHandler>(this.preprocessorHandlers);
+                            newList.add(handler);
+                            Collections.sort(newList);
+                            this.preprocessorHandlers = newList;
+                        }
+                    }
+                    else
+                    {
+                        final String type = info.getClass().getSimpleName().substring(0,
info.getClass().getSimpleName().length() - 4);
+                        SystemLogger.debug("Ignoring invalid " + type + " service " + info.getServiceReference());
+                        this.failureStateHandler.add(info, FAILURE_REASON_VALIDATION_FAILED);
+                    }
                     return true;
                 }
                 synchronized ( this.contextMap )
@@ -646,7 +676,22 @@ public final class WhiteboardManager
             {
                 if ( info instanceof PreprocessorInfo )
                 {
-                    // TODO
+                    synchronized ( this.preprocessorHandlers )
+                    {
+                        final List<PreprocessorHandler> newList = new ArrayList<PreprocessorHandler>(this.preprocessorHandlers);
+                        final Iterator<PreprocessorHandler> iter = newList.iterator();
+                        while ( iter.hasNext() )
+                        {
+                            final PreprocessorHandler handler = iter.next();
+                            if ( handler.getPreprocessorInfo().compareTo((PreprocessorInfo)info)
== 0 )
+                            {
+                                iter.remove();
+                                this.preprocessorHandlers = newList;
+                                return;
+                            }
+                        }
+                        // not found, nothing to do
+                    }
                     return;
                 }
                 final List<WhiteboardContextHandler> handlerList = this.servicesMap.remove(info);
@@ -895,7 +940,14 @@ public final class WhiteboardManager
             }
         }
 
-        return new RegistryRuntime(failedDTOHolder, contextDTOs);
+        final List<PreprocessorDTO> preprocessorDTOs = new ArrayList<PreprocessorDTO>();
+        final List<PreprocessorHandler> localHandlers = this.preprocessorHandlers;
+        for(final PreprocessorHandler handler : localHandlers)
+        {
+            preprocessorDTOs.add(PreprocessorDTOBuilder.build(handler.getPreprocessorInfo(),
-1));
+        }
+
+        return new RegistryRuntime(failedDTOHolder, contextDTOs, preprocessorDTOs);
     }
 
     /**
@@ -904,10 +956,35 @@ public final class WhiteboardManager
      * @param req The request
      * @param res The response
      * @return {@code true} to continue with dispatching, {@code false} to terminate the
request.
+     * @throws IOException
+     * @throws ServletException
      */
     public boolean invokePreprocessors(final HttpServletRequest req, final HttpServletResponse
res)
+    throws ServletException, IOException
     {
-        // TODO Auto-generated method stub
-        return true;
+        final List<PreprocessorHandler> localHandlers = this.preprocessorHandlers;
+        if ( localHandlers.isEmpty() )
+        {
+            return true;
+        }
+        final AtomicBoolean result = new AtomicBoolean(true);
+        final FilterChain chain = new FilterChain() {
+
+            @Override
+            public void doFilter(final ServletRequest request, final ServletResponse response)
+            throws IOException, ServletException {
+                result.set(true);
+            }
+        };
+        for(final PreprocessorHandler handler : localHandlers)
+        {
+            result.set(false);
+            handler.handle(req, res, chain);
+            if ( !result.get() )
+            {
+                break;
+            }
+        }
+        return result.get();
     }
 }



Mime
View raw message