List of usage examples for java.util Dictionary put
public abstract V put(K key, V value);
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void testWBServletDefaultContextAdaptor2() throws Exception { Dictionary<String, String> helperProps = new Hashtable<String, String>(); helperProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "testContext" + getName()); helperProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/testContext"); helperProps.put(TEST_PATH_CUSTOMIZER_NAME, getName()); ServiceRegistration<ServletContextHelper> helperReg = getBundleContext() .registerService(ServletContextHelper.class, new TestServletContextHelperFactory(), helperProps); ServiceRegistration<ContextPathCustomizer> pathAdaptorReg = null; try {//from ww w. jav a2 s .c om Map<String, String> params = new HashMap<String, String>(); params.put(TEST_PROTOTYPE_NAME, getName()); params.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, '/' + getName()); params.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=" + "testContext" + getName() + ")"); params.put(STATUS_PARAM, getName()); params.put("servlet.init." + TEST_PATH_CUSTOMIZER_NAME, getName()); String actual = doRequest(CONFIGURE, params); Assert.assertEquals(getName(), actual); actual = requestAdvisor.request("testContext/" + getName()); Assert.assertEquals(getName(), actual); ContextPathCustomizer pathAdaptor = new TestContextPathAdaptor(null, "testPrefix", getName()); pathAdaptorReg = getBundleContext().registerService(ContextPathCustomizer.class, pathAdaptor, null); actual = requestAdvisor.request("testPrefix/testContext/" + getName()); Assert.assertEquals(getName(), actual); pathAdaptorReg.unregister(); pathAdaptorReg = null; actual = requestAdvisor.request("testContext/" + getName()); Assert.assertEquals(getName(), actual); } finally { helperReg.unregister(); if (pathAdaptorReg != null) { pathAdaptorReg.unregister(); } } }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void testWBServletDefaultContextAdaptor3() throws Exception { // test the ContextPathCustomizer with a ServletContextHelper that has a '/' context path Dictionary<String, String> helperProps = new Hashtable<String, String>(); helperProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "testContext" + getName()); helperProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/"); helperProps.put(TEST_PATH_CUSTOMIZER_NAME, getName()); ServiceRegistration<ServletContextHelper> helperReg = getBundleContext() .registerService(ServletContextHelper.class, new TestServletContextHelperFactory(), helperProps); ServiceRegistration<ContextPathCustomizer> pathAdaptorReg = null; try {//from ww w.j a va 2 s. com Map<String, String> params = new HashMap<String, String>(); params.put(TEST_PROTOTYPE_NAME, getName()); params.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, '/' + getName()); params.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=" + "testContext" + getName() + ")"); params.put(STATUS_PARAM, getName()); params.put("servlet.init." + TEST_PATH_CUSTOMIZER_NAME, getName()); String actual = doRequest(CONFIGURE, params); Assert.assertEquals(getName(), actual); actual = requestAdvisor.request(getName()); Assert.assertEquals(getName(), actual); ContextPathCustomizer pathAdaptor = new TestContextPathAdaptor(null, "testPrefix", getName()); pathAdaptorReg = getBundleContext().registerService(ContextPathCustomizer.class, pathAdaptor, null); actual = requestAdvisor.request("testPrefix/" + getName()); Assert.assertEquals(getName(), actual); pathAdaptorReg.unregister(); pathAdaptorReg = null; actual = requestAdvisor.request(getName()); Assert.assertEquals(getName(), actual); } finally { helperReg.unregister(); if (pathAdaptorReg != null) { pathAdaptorReg.unregister(); } } }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_Listener4() throws Exception { BaseServletContextAttributeListener scal1 = new BaseServletContextAttributeListener(); Servlet s1 = new BaseServlet("a"); Collection<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>(); try {//from w ww .ja v a2 s . co m Dictionary<String, String> listenerProps = new Hashtable<String, String>(); listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true"); registrations.add(getBundleContext().registerService(ServletContextAttributeListener.class, scal1, listenerProps)); Dictionary<String, String> servletProps1 = new Hashtable<String, String>(); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S1"); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s"); registrations.add(getBundleContext().registerService(Servlet.class, s1, servletProps1)); requestAdvisor.request("s"); Assert.assertTrue(scal1.added.get()); Assert.assertFalse(scal1.replaced.get()); Assert.assertFalse(scal1.removed.get()); requestAdvisor.request("s"); Assert.assertTrue(scal1.added.get()); Assert.assertTrue(scal1.replaced.get()); Assert.assertFalse(scal1.removed.get()); requestAdvisor.request("s"); Assert.assertTrue(scal1.added.get()); Assert.assertTrue(scal1.replaced.get()); Assert.assertTrue(scal1.removed.get()); } finally { for (ServiceRegistration<?> registration : registrations) { registration.unregister(); } } }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_ServletContextHelper9() throws Exception { String expected1 = "c"; String expected2 = "d"; BundleContext bundleContext = getBundleContext(); Bundle bundle = bundleContext.getBundle(); ServletContextHelper servletContextHelper = new ServletContextHelper(bundle) { };//from w w w . java 2s.c om Servlet s1 = new BaseServlet(expected1); Servlet s2 = new BaseServlet(expected2); Collection<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>(); try { Dictionary<String, String> contextProps = new Hashtable<String, String>(); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "a"); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/a"); registrations.add( bundleContext.registerService(ServletContextHelper.class, servletContextHelper, contextProps)); Dictionary<String, String> servletProps1 = new Hashtable<String, String>(); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S1"); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s"); registrations.add(bundleContext.registerService(Servlet.class, s1, servletProps1)); Dictionary<String, String> servletProps2 = new Hashtable<String, String>(); servletProps2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S1"); servletProps2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s"); servletProps2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); registrations.add(bundleContext.registerService(Servlet.class, s2, servletProps2)); String actual = requestAdvisor.request("s"); Assert.assertEquals(expected1, actual); actual = requestAdvisor.request("a/s"); Assert.assertEquals(expected2, actual); } finally { for (ServiceRegistration<?> registration : registrations) { registration.unregister(); } } }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_commonsFileUpload() throws Exception { Servlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @Override//from w w w . j av a 2 s. com protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { boolean isMultipart = ServletFileUpload.isMultipartContent(req); Assert.assertTrue(isMultipart); DiskFileItemFactory factory = new DiskFileItemFactory(); ServletContext servletContext = this.getServletConfig().getServletContext(); File repository = (File) servletContext.getAttribute("javax.servlet.context.tempdir"); factory.setRepository(repository); ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> items = null; try { @SuppressWarnings("unchecked") List<FileItem> parseRequest = upload.parseRequest(req); items = parseRequest; } catch (FileUploadException e) { e.printStackTrace(); } Assert.assertNotNull(items); Assert.assertFalse(items.isEmpty()); FileItem fileItem = items.get(0); String submittedFileName = fileItem.getName(); String contentType = fileItem.getContentType(); long size = fileItem.getSize(); PrintWriter writer = resp.getWriter(); writer.write(submittedFileName); writer.write("|"); writer.write(contentType); writer.write("|" + size); } }; Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S16"); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet16/*"); registrations.add(getBundleContext().registerService(Servlet.class, servlet, props)); Map<String, List<Object>> map = new HashMap<String, List<Object>>(); map.put("file", Arrays.<Object>asList(getClass().getResource("blue.png"))); Map<String, List<String>> result = requestAdvisor.upload("Servlet16/do", map); Assert.assertEquals("200", result.get("responseCode").get(0)); Assert.assertEquals("blue.png|image/png|292", result.get("responseBody").get(0)); }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_Listener10() throws Exception { BaseServletContextListener scl1 = new BaseServletContextListener(); BaseServletContextListener scl2 = new BaseServletContextListener(); BaseServletContextListener scl3 = new BaseServletContextListener(); Dictionary<String, String> listenerProps = new Hashtable<String, String>(); listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true"); registrations.add(getBundleContext().registerService(ServletContextListener.class, scl1, listenerProps)); listenerProps = new Hashtable<String, String>(); listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true"); registrations.add(getBundleContext().registerService(ServletContextListener.class, scl2, listenerProps)); Dictionary<String, String> contextProps = new Hashtable<String, String>(); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "a"); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/a"); registrations//from ww w . j av a 2s .c o m .add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() { }, contextProps)); listenerProps = new Hashtable<String, String>(); listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true"); listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); registrations.add(getBundleContext().registerService(ServletContextListener.class, scl3, listenerProps)); ServletContext servletContext1 = scl1.servletContext; ServletContext servletContext2 = scl2.servletContext; ServletContext servletContext3 = scl3.servletContext; Assert.assertNotNull(servletContext1); Assert.assertNotNull(servletContext2); Assert.assertNotNull(servletContext3); Assert.assertTrue(servletContext1.equals(servletContext1)); Assert.assertTrue(servletContext2.equals(servletContext2)); Assert.assertTrue(servletContext3.equals(servletContext3)); Assert.assertTrue(servletContext1.equals(servletContext2)); Assert.assertFalse(servletContext1.equals(servletContext3)); Assert.assertFalse(servletContext2.equals(servletContext3)); // Asserts two invocations return the same value Assert.assertEquals(servletContext1.hashCode(), servletContext1.hashCode()); Assert.assertEquals(servletContext2.hashCode(), servletContext2.hashCode()); Assert.assertEquals(servletContext3.hashCode(), servletContext3.hashCode()); Assert.assertEquals(servletContext1.hashCode(), servletContext2.hashCode()); Assert.assertNotEquals(servletContext1.hashCode(), servletContext3.hashCode()); Assert.assertNotEquals(servletContext2.hashCode(), servletContext3.hashCode()); }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_ServletContextHelperVisibility() throws Exception { String expected1 = "c"; BundleContext bundleContext = getBundleContext(); Bundle bundle = bundleContext.getBundle(); ServletContextHelper servletContextHelper = new ServletContextHelper(bundle) { };/*from www . j a v a 2 s. c o m*/ Servlet s1 = new BaseServlet(expected1); Collection<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>(); try { // register a hook that hides the helper from the registering bundle registrations.add(bundleContext.registerService(FindHook.class, new FindHook() { @Override public void find(BundleContext context, String name, String filter, boolean allServices, Collection<ServiceReference<?>> references) { if (ServletContextHelper.class.getName().equals(name) && context.getBundle().equals(getBundleContext().getBundle())) { references.clear(); } } }, null)); Dictionary<String, String> contextProps = new Hashtable<String, String>(); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "a"); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/a"); registrations.add( bundleContext.registerService(ServletContextHelper.class, servletContextHelper, contextProps)); Dictionary<String, String> servletProps2 = new Hashtable<String, String>(); servletProps2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S1"); servletProps2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s"); servletProps2.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); registrations.add(bundleContext.registerService(Servlet.class, s1, servletProps2)); try { requestAdvisor.request("a/s"); } catch (FileNotFoundException e) { // expected } } finally { for (ServiceRegistration<?> registration : registrations) { registration.unregister(); } } }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_ServletContextHelper12() throws Exception { String expected1 = "a,b,1"; BundleContext bundleContext = getBundleContext(); Bundle bundle = bundleContext.getBundle(); ServletContextHelper servletContextHelper = new ServletContextHelper(bundle) { };/*from ww w. j a v a 2 s . c o m*/ Servlet s1 = new HttpServlet() { private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StringBuilder builder = new StringBuilder(); builder.append(request.getServletContext().getInitParameter("a")).append(','); builder.append(request.getServletContext().getInitParameter("b")).append(','); builder.append(request.getServletContext().getInitParameter("c")); response.getWriter().print(builder.toString()); } }; Collection<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>(); try { Dictionary<String, Object> contextProps = new Hashtable<String, Object>(); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "a"); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/a"); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX + "a", "a"); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX + "b", "b"); contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_INIT_PARAM_PREFIX + "c", new Integer(1)); registrations.add( bundleContext.registerService(ServletContextHelper.class, servletContextHelper, contextProps)); Dictionary<String, String> servletProps1 = new Hashtable<String, String>(); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S1"); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s"); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); registrations.add(bundleContext.registerService(Servlet.class, s1, servletProps1)); String actual = requestAdvisor.request("a/s"); Assert.assertEquals(expected1, actual); } finally { for (ServiceRegistration<?> registration : registrations) { registration.unregister(); } } }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_Listener9() throws Exception { Servlet sA = new HttpServlet() { private static final long serialVersionUID = 1L; @Override/*w ww . j a v a2s .c om*/ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { RequestDispatcher requestDispatcher = req.getRequestDispatcher("/s9B"); requestDispatcher.include(req, resp); } }; Servlet sB = new HttpServlet() { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); writer.write("S9 included"); } }; BaseServletRequestListener srl1 = new BaseServletRequestListener(); Collection<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>(); try { Dictionary<String, String> listenerProps = new Hashtable<String, String>(); listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true"); registrations .add(getBundleContext().registerService(ServletRequestListener.class, srl1, listenerProps)); Dictionary<String, String> servletProps1 = new Hashtable<String, String>(); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S9A"); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s9A"); registrations.add(getBundleContext().registerService(Servlet.class, sA, servletProps1)); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S9B"); servletProps1.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s9B"); registrations.add(getBundleContext().registerService(Servlet.class, sB, servletProps1)); String result = requestAdvisor.request("s9A"); Assert.assertEquals("S9 included", result); Assert.assertEquals(0, srl1.number.get()); } finally { for (ServiceRegistration<?> registration : registrations) { registration.unregister(); } } }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void basicFilterTest22(String servlet1Pattern, String servlet2Pattern, String filterPattern, String expected, String[] dispatchers) throws Exception { final AtomicReference<HttpServletRequestWrapper> httpServletRequestWrapper = new AtomicReference<HttpServletRequestWrapper>(); final AtomicReference<HttpServletResponseWrapper> httpServletResponseWrapper = new AtomicReference<HttpServletResponseWrapper>(); Servlet servlet1 = new BaseServlet() { private static final long serialVersionUID = 1L; @Override/* w w w .j ava 2 s.c om*/ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("index.jsp").forward(request, response); } }; Servlet servlet2 = new BaseServlet("a") { private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if ((httpServletRequestWrapper.get() != null) && !request.equals(httpServletRequestWrapper.get())) { throw new ServletException("not the same request"); } if ((httpServletResponseWrapper.get() != null) && !response.equals(httpServletResponseWrapper.get())) { throw new ServletException("not the same response"); } response.getWriter().print(content); } }; Filter filter = new TestFilter() { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.getWriter().write('b'); httpServletRequestWrapper.set(new HttpServletRequestWrapper((HttpServletRequest) request)); httpServletResponseWrapper.set(new HttpServletResponseWrapper((HttpServletResponse) response)); chain.doFilter(httpServletRequestWrapper.get(), httpServletResponseWrapper.get()); response.getWriter().write('b'); } }; Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, servlet1Pattern); registrations.add(getBundleContext().registerService(Servlet.class, servlet1, props)); props = new Hashtable<String, Object>(); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, servlet2Pattern); registrations.add(getBundleContext().registerService(Servlet.class, servlet2, props)); props = new Hashtable<String, Object>(); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_NAME, "F22"); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_DISPATCHER, dispatchers); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, filterPattern); registrations.add(getBundleContext().registerService(Filter.class, filter, props)); String response = requestAdvisor.request("f22/a"); Assert.assertEquals(expected, response); }