You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2015/02/01 16:19:31 UTC

svn commit: r1656313 - in /felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal: CustomErrorHandler.java JettyService.java

Author: cziegeler
Date: Sun Feb  1 15:19:30 2015
New Revision: 1656313

URL: http://svn.apache.org/r1656313
Log:
FELIX-4548 : Implement the missing errors registration

Added:
    felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/CustomErrorHandler.java   (with props)
Modified:
    felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java

Added: felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/CustomErrorHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/CustomErrorHandler.java?rev=1656313&view=auto
==============================================================================
--- felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/CustomErrorHandler.java (added)
+++ felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/CustomErrorHandler.java Sun Feb  1 15:19:30 2015
@@ -0,0 +1,94 @@
+/*
+ * 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.jetty.internal;
+
+import static javax.servlet.RequestDispatcher.ERROR_EXCEPTION_TYPE;
+import static javax.servlet.RequestDispatcher.ERROR_MESSAGE;
+import static javax.servlet.RequestDispatcher.ERROR_STATUS_CODE;
+import static org.eclipse.jetty.http.HttpMethod.GET;
+import static org.eclipse.jetty.http.HttpMethod.HEAD;
+import static org.eclipse.jetty.http.HttpMethod.POST;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.http.base.internal.DispatcherServlet;
+import org.eclipse.jetty.http.HttpFields;
+import org.eclipse.jetty.http.HttpHeader;
+import org.eclipse.jetty.http.HttpMethod;
+import org.eclipse.jetty.http.MimeTypes;
+import org.eclipse.jetty.server.Request;
+import org.eclipse.jetty.server.handler.ErrorHandler;
+import org.eclipse.jetty.util.ByteArrayISO8859Writer;
+
+/**
+ * Provides a hook into the error-handling of Jetty, allowing us to handle all status codes >= 400 and/or exceptions thrown by servlets/filters.
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public class CustomErrorHandler extends ErrorHandler
+{
+    private final DispatcherServlet dispatcher;
+
+    /**
+     * Creates a new {@link CustomErrorHandler} instance.
+     * 
+     * @param dispatcher the dispatcher servlet that is going to retrieve the error page for us.
+     */
+    public CustomErrorHandler(DispatcherServlet dispatcher)
+    {
+        this.dispatcher = dispatcher;
+    }
+
+    @Override
+    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
+    {
+        // We're handling this request either by delegating it to a specific error handler, or by writing a custom error page ourselves...
+        baseRequest.setHandled(true);
+
+        HttpMethod method = HttpMethod.valueOf(request.getMethod());
+        if (!GET.equals(method) && !POST.equals(method) && !HEAD.equals(method))
+        {
+            return;
+        }
+
+        int exceptionRC = (Integer) request.getAttribute(ERROR_STATUS_CODE);
+        String reason = (String) request.getAttribute(ERROR_MESSAGE);
+        Class<?> exceptionType = (Class<?>) request.getAttribute(ERROR_EXCEPTION_TYPE);
+
+        if (!this.dispatcher.handleError(request, response, exceptionRC, (exceptionType == null) ? null : exceptionType.getName()))
+        {
+            if (getCacheControl() != null)
+            {
+                response.setHeader(HttpHeader.CACHE_CONTROL.asString(), getCacheControl());
+            }
+
+            ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(4096);
+            response.setContentType(MimeTypes.Type.TEXT_HTML_8859_1.asString());
+
+            handleErrorPage(request, writer, exceptionRC, reason);
+            writer.flush();
+            response.setContentLength(writer.size());
+            writer.writeTo(response.getOutputStream());
+            writer.destroy();
+        }
+    }
+}

Propchange: felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/CustomErrorHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/CustomErrorHandler.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/CustomErrorHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java?rev=1656313&r1=1656312&r2=1656313&view=diff
==============================================================================
--- felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java (original)
+++ felix/trunk/http/jetty/src/main/java/org/apache/felix/http/jetty/internal/JettyService.java Sun Feb  1 15:19:30 2015
@@ -117,6 +117,7 @@ public final class JettyService extends
         this.deployments = new LinkedHashMap<String, Deployment>();
         this.executor = Executors.newSingleThreadExecutor(new ThreadFactory()
         {
+            @Override
             public Thread newThread(Runnable runnable)
             {
                 Thread t = new Thread(runnable);
@@ -240,6 +241,7 @@ public final class JettyService extends
             this.server.addLifeCycleListener(this);
 
             this.server.addBean(new HashLoginService("OSGi HTTP Service Realm"));
+            this.server.addBean(new CustomErrorHandler(this.dispatcher));
 
             this.parent = new ContextHandlerCollection();
 
@@ -725,11 +727,13 @@ public final class JettyService extends
         });
     }
 
+    @Override
     public Object addingBundle(Bundle bundle, BundleEvent event)
     {
         return detectWebAppBundle(bundle);
     }
 
+    @Override
     public void modifiedBundle(Bundle bundle, BundleEvent event, Object object)
     {
         detectWebAppBundle(bundle);
@@ -749,6 +753,7 @@ public final class JettyService extends
         return null;
     }
 
+    @Override
     public void removedBundle(Bundle bundle, BundleEvent event, Object object)
     {
         String contextPath = bundle.getHeaders().get(HEADER_WEB_CONTEXT_PATH);
@@ -766,6 +771,7 @@ public final class JettyService extends
         }
     }
 
+    @Override
     public Object addingService(ServiceReference reference)
     {
         Object service = this.context.getService(reference);
@@ -773,11 +779,13 @@ public final class JettyService extends
         return service;
     }
 
+    @Override
     public void modifiedService(ServiceReference reference, Object service)
     {
         this.eventAdmin = (EventAdmin) service;
     }
 
+    @Override
     public void removedService(ServiceReference reference, Object service)
     {
         this.context.ungetService(reference);
@@ -871,6 +879,7 @@ public final class JettyService extends
      */
     abstract static class JettyOperation implements Callable<Void>
     {
+        @Override
         public Void call() throws Exception
         {
             ClassLoader cl = Thread.currentThread().getContextClassLoader();