/** @since servlet-api-3.0 */
    @Override
    public ServletRegistration.Dynamic addServlet(
        String servletName, Class<? extends Servlet> servletClass) {
      if (!isStarting()) throw new IllegalStateException();

      if (servletName == null || "".equals(servletName.trim()))
        throw new IllegalStateException("Missing servlet name");

      if (!_enabled) throw new UnsupportedOperationException();

      final ServletHandler handler = ServletContextHandler.this.getServletHandler();
      ServletHolder holder = handler.getServlet(servletName);
      if (holder == null) {
        // new servlet
        holder = handler.newServletHolder(Source.JAVAX_API);
        holder.setName(servletName);
        holder.setHeldClass(servletClass);
        handler.addServlet(holder);
        return dynamicHolderAdded(holder);
      }

      // complete a partial registration
      if (holder.getClassName() == null && holder.getHeldClass() == null) {
        holder.setHeldClass(servletClass);
        return holder.getRegistration();
      } else return null; // existing completed registration for servlet name
    }
    @Override
    public ServletRegistration getServletRegistration(String servletName) {
      if (!_enabled) throw new UnsupportedOperationException();

      final ServletHolder holder =
          ServletContextHandler.this.getServletHandler().getServlet(servletName);
      return (holder == null) ? null : holder.getRegistration();
    }
    @Override
    public Map<String, ? extends ServletRegistration> getServletRegistrations() {
      if (!_enabled) throw new UnsupportedOperationException();

      HashMap<String, ServletRegistration> registrations =
          new HashMap<String, ServletRegistration>();
      ServletHandler handler = ServletContextHandler.this.getServletHandler();
      ServletHolder[] holders = handler.getServlets();
      if (holders != null) {
        for (ServletHolder holder : holders)
          registrations.put(holder.getName(), holder.getRegistration());
      }
      return registrations;
    }
示例#4
0
  @Test
  public void testInitParams() throws Exception {
    ServletHolder holder = new ServletHolder(Source.JAVAX_API);
    ServletRegistration reg = holder.getRegistration();
    try {
      reg.setInitParameter(null, "foo");
      fail("null name accepted");
    } catch (IllegalArgumentException e) {
    }
    try {
      reg.setInitParameter("foo", null);
      fail("null value accepted");
    } catch (IllegalArgumentException e) {
    }
    reg.setInitParameter("foo", "bar");
    assertFalse(reg.setInitParameter("foo", "foo"));

    Set<String> clash = reg.setInitParameters(Collections.singletonMap("foo", "bax"));
    assertTrue("should be one clash", clash != null && clash.size() == 1);

    try {
      reg.setInitParameters(Collections.singletonMap((String) null, "bax"));
      fail("null name in map accepted");
    } catch (IllegalArgumentException e) {
    }
    try {
      reg.setInitParameters(Collections.singletonMap("foo", (String) null));
      fail("null value in map accepted");
    } catch (IllegalArgumentException e) {
    }

    Set<String> clash2 = reg.setInitParameters(Collections.singletonMap("FOO", "bax"));
    assertTrue("should be no clash", clash2.isEmpty());
    assertEquals(
        "setInitParameters should not replace existing non-clashing init parameters",
        2,
        reg.getInitParameters().size());
  }
 /**
  * notification that a ServletRegistration has been created so we can track the annotations
  *
  * @param holder new holder created through the api.
  * @return the ServletRegistration.Dynamic
  */
 protected ServletRegistration.Dynamic dynamicHolderAdded(ServletHolder holder) {
   return holder.getRegistration();
 }