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/28 01:34:50 UTC
svn commit: r1662863 -
/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpWhiteboardTest.java
Author: cziegeler
Date: Sat Feb 28 00:34:50 2015
New Revision: 1662863
URL: http://svn.apache.org/r1662863
Log:
FELIX-4100 : Support osgi.http.whiteboard.target whiteboard property. Apply patch from Raluca Grigoras
Added:
felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpWhiteboardTest.java (with props)
Added: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpWhiteboardTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpWhiteboardTest.java?rev=1662863&view=auto
==============================================================================
--- felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpWhiteboardTest.java (added)
+++ felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpWhiteboardTest.java Sat Feb 28 00:34:50 2015
@@ -0,0 +1,391 @@
+/*
+ * 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.itest;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
+import static javax.servlet.http.HttpServletResponse.SC_FORBIDDEN;
+import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
+import static javax.servlet.http.HttpServletResponse.SC_OK;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import javax.servlet.Filter;
+import javax.servlet.Servlet;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
+
+@RunWith(JUnit4TestRunner.class)
+public class HttpWhiteboardTest extends BaseIntegrationTest
+{
+
+ private static final String SERVICE_HTTP_PORT = "org.osgi.service.http.port";
+
+ /**]
+ * Test that a servlet with the org.osgi.http.whiteboard.target property not set
+ * is registered with the whiteboard
+ */
+ @Test
+ public void testServletNoTargetProperty() throws Exception
+ {
+ CountDownLatch initLatch = new CountDownLatch(1);
+ CountDownLatch destroyLatch = new CountDownLatch(1);
+
+ TestServlet servlet = new TestServlet(initLatch, destroyLatch)
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+ {
+ resp.getWriter().print("It works!");
+ resp.flushBuffer();
+ }
+ };
+
+ Dictionary<String, Object> props = new Hashtable<String, Object>();
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/servletAlias");
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servletName");
+
+ ServiceRegistration<?> reg = m_context.registerService(Servlet.class.getName(), servlet, props);
+
+ try {
+ assertTrue(initLatch.await(600, TimeUnit.SECONDS));
+ URL testURL = createURL("/servletAlias");
+ assertContent("It works!", testURL);
+ } finally {
+ reg.unregister();
+ }
+ assertTrue(destroyLatch.await(600, TimeUnit.SECONDS));
+ }
+
+ /**
+ * Test that a servlet with the org.osgi.http.whiteboard.target property matching the
+ * HttpServiceRuntime properties is registered with the whiteboard.
+ *
+ * In the current implementation the HttpServiceRuntime properties are the same as the
+ * HttpService properties.
+ *
+ */
+ @Test
+ public void testServletTargetMatchPort() throws Exception
+ {
+ CountDownLatch initLatch = new CountDownLatch(1);
+ CountDownLatch destroyLatch = new CountDownLatch(1);
+
+ TestServlet servlet = new TestServlet(initLatch, destroyLatch)
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+ {
+ resp.getWriter().print("matchingServlet");
+ resp.flushBuffer();
+ }
+ };
+
+ Dictionary<String, Object> props = new Hashtable<String, Object>();
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/servletAlias");
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servletName");
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET, "(" + SERVICE_HTTP_PORT + "=8080" + ")");
+
+ ServiceRegistration<?> reg = m_context.registerService(Servlet.class.getName(), servlet, props);
+
+ try {
+ assertTrue(initLatch.await(5, TimeUnit.SECONDS));
+ URL testURL = createURL("/servletAlias");
+ assertContent("matchingServlet", testURL);
+ } finally {
+ reg.unregister();
+ }
+
+ assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
+ }
+
+ /**
+ * Test that a servlet with the org.osgi.http.whiteboard.target property not matching
+ * the properties of the HttpServiceRuntime is not registered with the whiteboard.
+ *
+ */
+ @Test
+ public void testServletTargetNotMatchPort() throws Exception
+ {
+ CountDownLatch initLatch = new CountDownLatch(1);
+ CountDownLatch destroyLatch = new CountDownLatch(1);
+
+ TestServlet nonMatchingServlet = new TestServlet(initLatch, destroyLatch)
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+ {
+ resp.getWriter().print("nonMatchingServlet");
+ resp.flushBuffer();
+ }
+ };
+
+ Dictionary<String, Object> props = new Hashtable<String, Object>();
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/servletAlias");
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servletName");
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET, "(" + SERVICE_HTTP_PORT + "=8282" + ")");
+
+ ServiceRegistration<?> reg = m_context.registerService(Servlet.class.getName(), nonMatchingServlet, props);
+
+ try {
+ // the servlet will not be registered, its init method will not be called, await must return false due to timeout
+ assertFalse(initLatch.await(5, TimeUnit.SECONDS));
+ URL testURL = createURL("/servletAlias");
+ assertResponseCode(404, testURL);
+ } finally {
+ reg.unregister();
+ }
+ }
+
+ /**
+ * Test that a filter with no target property set is correctly registered with the whiteboard
+ *
+ */
+ @Test
+ public void testFilterNoTargetProperty() throws Exception
+ {
+ CountDownLatch initLatch = new CountDownLatch(3);
+ CountDownLatch destroyLatch = new CountDownLatch(3);
+
+ TestServlet servlet1 = new TestServlet(initLatch, destroyLatch)
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+ {
+ resp.getWriter().print("servlet1");
+ resp.flushBuffer();
+ }
+ };
+ Dictionary<String, Object> props1 = new Hashtable<String, Object>();
+ props1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet/1");
+ props1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servlet1");
+ props1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET, "(" + SERVICE_HTTP_PORT + "=8080" + ")");
+
+ TestServlet servlet2 = new TestServlet(initLatch, destroyLatch)
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+ {
+ resp.getWriter().print("servlet2");
+ resp.flushBuffer();
+ }
+ };
+ Dictionary<String, Object> props2 = new Hashtable<String, Object>();
+ props2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet/2");
+ props2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servle2");
+ props2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET, "(" + SERVICE_HTTP_PORT + "=8080" + ")");
+
+ TestFilter filter = new TestFilter(initLatch, destroyLatch)
+ {
+ @Override
+ protected void filter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException
+ {
+ String param = req.getParameter("param");
+ if("forbidden".equals(param))
+ {
+ resp.reset();
+ resp.sendError(SC_FORBIDDEN);
+ resp.flushBuffer();
+ }
+ else
+ {
+ chain.doFilter(req, resp);
+ }
+ }
+ };
+
+ Dictionary<String, Object> props = new Hashtable<String, Object>();
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, "/servlet/1");
+ props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servletName");
+
+ ServiceRegistration<?> reg1 = m_context.registerService(Servlet.class.getName(), servlet1, props1);
+ ServiceRegistration<?> reg2 = m_context.registerService(Servlet.class.getName(), servlet2, props2);
+ ServiceRegistration<?> reg = m_context.registerService(Filter.class.getName(), filter, props);
+
+ assertTrue(initLatch.await(5, TimeUnit.SECONDS));
+
+ assertResponseCode(SC_FORBIDDEN, createURL("/servlet/1?param=forbidden"));
+ assertContent("servlet1", createURL("/servlet/1?param=any"));
+ assertContent("servlet1", createURL("/servlet/1"));
+
+ assertResponseCode(SC_OK, createURL("/servlet/2?param=forbidden"));
+ assertContent("servlet2", createURL("/servlet/2?param=forbidden"));
+
+ reg1.unregister();
+ reg2.unregister();
+ reg.unregister();
+
+ assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
+ }
+
+ @Test
+ public void testFilterTargetMatchPort() throws Exception
+ {
+ CountDownLatch initLatch = new CountDownLatch(2);
+ CountDownLatch destroyLatch = new CountDownLatch(2);
+
+ TestServlet servlet = new TestServlet(initLatch, destroyLatch)
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+ {
+ resp.getWriter().print("servlet");
+ resp.flushBuffer();
+ }
+ };
+ Dictionary<String, Object> sprops = new Hashtable<String, Object>();
+ sprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet");
+ sprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servlet1");
+ sprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET, "(" + SERVICE_HTTP_PORT + "=8080" + ")");
+
+ TestFilter filter = new TestFilter(initLatch, destroyLatch)
+ {
+ @Override
+ protected void filter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException
+ {
+ String param = req.getParameter("param");
+ if("forbidden".equals(param))
+ {
+ resp.reset();
+ resp.sendError(SC_FORBIDDEN);
+ resp.flushBuffer();
+ }
+ else
+ {
+ chain.doFilter(req, resp);
+ }
+ }
+ };
+
+ Dictionary<String, Object> fprops = new Hashtable<String, Object>();
+ fprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, "/servlet");
+ fprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servletName");
+ fprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET, "(" + SERVICE_HTTP_PORT + "=8080" + ")");
+
+ ServiceRegistration<?> sreg = m_context.registerService(Servlet.class.getName(), servlet, sprops);
+ ServiceRegistration<?> freg = m_context.registerService(Filter.class.getName(), filter, fprops);
+
+ assertTrue(initLatch.await(5, TimeUnit.SECONDS));
+
+ assertResponseCode(SC_FORBIDDEN, createURL("/servlet?param=forbidden"));
+ assertContent("servlet", createURL("/servlet?param=any"));
+ assertContent("servlet", createURL("/servlet"));
+
+ sreg.unregister();
+ freg.unregister();
+
+ assertTrue(destroyLatch.await(5, TimeUnit.SECONDS));
+ }
+
+ @Test
+ public void testFilterTargetNotMatchPort() throws Exception
+ {
+ CountDownLatch servletInitLatch = new CountDownLatch(1);
+ CountDownLatch servletDestroyLatch = new CountDownLatch(1);
+
+ CountDownLatch filterInitLatch = new CountDownLatch(1);
+ CountDownLatch filterDestroyLatch = new CountDownLatch(1);
+
+ TestServlet servlet = new TestServlet(servletInitLatch, servletDestroyLatch)
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
+ {
+ resp.getWriter().print("servlet");
+ resp.flushBuffer();
+ }
+ };
+ Dictionary<String, Object> sprops = new Hashtable<String, Object>();
+ sprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet");
+ sprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servlet1");
+ sprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET, "(" + SERVICE_HTTP_PORT + "=8080" + ")");
+
+ TestFilter filter = new TestFilter(filterInitLatch, filterDestroyLatch)
+ {
+ @Override
+ protected void filter(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException
+ {
+ String param = req.getParameter("param");
+ if("forbidden".equals(param))
+ {
+ resp.reset();
+ resp.sendError(SC_FORBIDDEN);
+ resp.flushBuffer();
+ }
+ else
+ {
+ chain.doFilter(req, resp);
+ }
+ }
+ };
+
+ Dictionary<String, Object> fprops = new Hashtable<String, Object>();
+ fprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, "/servlet");
+ fprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_INIT_PARAM_PREFIX + ".myname", "servletName");
+ fprops.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET, "(" + SERVICE_HTTP_PORT + "=8181" + ")");
+
+ ServiceRegistration<?> sreg = m_context.registerService(Servlet.class.getName(), servlet, sprops);
+ ServiceRegistration<?> freg = m_context.registerService(Filter.class.getName(), filter, fprops);
+
+ // servlet is registered
+ assertTrue(servletInitLatch.await(5, TimeUnit.SECONDS));
+ // fitler is not registered, timeout occurs
+ assertFalse(filterInitLatch.await(5, TimeUnit.SECONDS));
+
+ assertResponseCode(SC_OK, createURL("/servlet?param=forbidden"));
+ assertContent("servlet", createURL("/servlet?param=forbidden"));
+ assertContent("servlet", createURL("/servlet?param=any"));
+ assertContent("servlet", createURL("/servlet"));
+
+ sreg.unregister();
+ freg.unregister();
+
+ assertTrue(servletDestroyLatch.await(5, TimeUnit.SECONDS));
+ assertFalse(filterDestroyLatch.await(5, TimeUnit.SECONDS));
+ }
+}
Propchange: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpWhiteboardTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: felix/trunk/http/itest/src/test/java/org/apache/felix/http/itest/HttpWhiteboardTest.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url