List of usage examples for javax.servlet.http HttpServlet HttpServlet
public HttpServlet()
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_Servlet13() throws Exception { Servlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @Override/*w ww . j a v a 2 s.c om*/ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter writer = resp.getWriter(); writer.write(req.getQueryString()); writer.write("|"); writer.write(req.getParameter("p")); writer.write("|"); writer.write(Arrays.toString(req.getParameterValues("p"))); } }; Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S13"); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/Servlet13/*"); registrations.add(getBundleContext().registerService(Servlet.class, servlet, props)); String result = requestAdvisor.request("Servlet13/a?p=1&p=2"); Assert.assertEquals("p=1&p=2|1|[1, 2]", result); }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_ServletExactMatchPrecidence() throws Exception { Servlet sA = new HttpServlet() { private static final long serialVersionUID = 1L; @Override//from w ww .jav a 2 s .c o m protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write('a'); } }; Servlet sB = new HttpServlet() { private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write('b'); } }; HttpService httpService = getHttpService(); HttpContext httpContext = httpService.createDefaultHttpContext(); httpService.registerServlet("*.txt", sA, null, httpContext); httpService.registerServlet("/files/help.txt", sB, null, httpContext); Assert.assertEquals("b", requestAdvisor.request("files/help.txt")); }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_Servlet16() throws Exception { Servlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @Override/*from w w w. j av a 2 s . co m*/ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { Part part = req.getPart("file"); Assert.assertNotNull(part); String submittedFileName = part.getSubmittedFileName(); String contentType = part.getContentType(); long size = part.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/*"); props.put("equinox.http.multipartSupported", Boolean.TRUE); 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("resource1.txt"))); Map<String, List<String>> result = requestAdvisor.upload("Servlet16/do", map); Assert.assertEquals("200", result.get("responseCode").get(0)); Assert.assertEquals("resource1.txt|text/plain|1", result.get("responseBody").get(0)); }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void test_Servlet17() throws Exception { Servlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @Override/*from ww w . j ava2 s .c o m*/ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { Part part = req.getPart("file"); Assert.assertNotNull(part); String submittedFileName = getSubmittedFileName(part); String contentType = part.getContentType(); long size = part.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/*"); props.put("equinox.http.multipartSupported", Boolean.TRUE); 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_commonsFileUpload() throws Exception { Servlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @Override//from w ww . j a va 2 s .c o m 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_ServletContext2() throws Exception { Servlet servlet = new HttpServlet() { private static final long serialVersionUID = 1L; @Override/*from w w w. j a va 2 s .co m*/ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { getServletContext().setAttribute("name", null); } }; Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S1"); props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/S1/*"); registrations.add(getBundleContext().registerService(Servlet.class, servlet, props)); Map<String, List<String>> response = requestAdvisor.request("S1/a", null); String responseCode = response.get("responseCode").get(0); Assert.assertEquals("200", responseCode); }
From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java
public void testServletContextUnsupportedOperations() { final AtomicReference<ServletContext> contextHolder = new AtomicReference<ServletContext>(); Servlet unsupportedServlet = new HttpServlet() { private static final long serialVersionUID = 1L; @Override//from w w w . ja v a 2 s .co m public void init(ServletConfig config) throws ServletException { contextHolder.set(config.getServletContext()); } }; ServiceRegistration<Servlet> servletReg = null; Dictionary<String, Object> servletProps = new Hashtable<String, Object>(); servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/sessions"); try { servletReg = getBundleContext().registerService(Servlet.class, unsupportedServlet, servletProps); } catch (Exception e) { fail("Unexpected exception: " + e); } finally { if (servletReg != null) { servletReg.unregister(); } } ServletContext context = contextHolder.get(); assertNotNull("Null context.", context); for (Method m : getUnsupportedMethods()) { checkMethod(m, context); } }
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 w w w. ja 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_ServletContextHelper13() throws Exception { BundleContext bundleContext = getBundleContext(); Bundle bundle = bundleContext.getBundle(); // test that the helper handlesecurity is called before the filter by setting an attribute on the request ServletContextHelper servletContextHelper = new ServletContextHelper(bundle) { @Override//from w ww . j a v a2 s . c o m public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException { request.setAttribute(getName(), Boolean.TRUE); return super.handleSecurity(request, response); } }; Filter f1 = new Filter() { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request.getAttribute(getName()) == Boolean.TRUE) { request.setAttribute(getName() + ".fromFilter", Boolean.TRUE); } chain.doFilter(request, response); } @Override public void destroy() { } }; Servlet s1 = new HttpServlet() { private static final long serialVersionUID = 1L; @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { res.getWriter().print(req.getAttribute(getName() + ".fromFilter")); } }; 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, "/"); registrations.add( bundleContext.registerService(ServletContextHelper.class, servletContextHelper, contextProps)); Dictionary<String, String> filterProps = new Hashtable<String, String>(); filterProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_FILTER_PATTERN, "/*"); filterProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); registrations.add(bundleContext.registerService(Filter.class, f1, filterProps)); Dictionary<String, String> servletProps = new Hashtable<String, String>(); servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "S1"); servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, "/s1"); servletProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)"); registrations.add(bundleContext.registerService(Servlet.class, s1, servletProps)); String actual = requestAdvisor.request("s1"); Assert.assertEquals(Boolean.TRUE.toString(), 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/*from w w w . ja va2 s . c o m*/ 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(); } } }