Java Code Examples for javax.servlet.ServletRegistration#getClassName()

The following examples show how to use javax.servlet.ServletRegistration#getClassName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestServletUtils.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSaveUrlPrefixNormal(@Mocked ServletContext servletContext,
    @Mocked ServletRegistration servletRegistration) {
  ClassLoaderScopeContext.clearClassLoaderScopeProperty();
  new Expectations() {
    {
      servletContext.getContextPath();
      result = "/root";
      servletRegistration.getClassName();
      result = RestServlet.class.getName();
      servletRegistration.getMappings();
      result = Arrays.asList("/rest/*");
      servletContext.getServletRegistrations();
      result = Collections.singletonMap("test", servletRegistration);
    }
  };

  ServletUtils.saveUrlPrefix(servletContext);

  Assert.assertThat(ClassLoaderScopeContext.getClassLoaderScopeProperty(DefinitionConst.URL_PREFIX),
      Matchers.is("/root/rest"));
  ClassLoaderScopeContext.clearClassLoaderScopeProperty();
}
 
Example 2
Source File: JSR356WebsocketInitializer.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Tries to determine if the given servlet registration refers to a Vaadin
 * servlet.
 *
 * @param servletRegistration
 *            The servlet registration info for the servlet
 * @param servletContext
 *            the context of the servlet
 * @return false if the servlet is definitely not a Vaadin servlet, true
 *         otherwise
 */
protected boolean isVaadinServlet(ServletRegistration servletRegistration,
        ServletContext servletContext) {
    try {
        String servletClassName = servletRegistration.getClassName();
        if (servletClassName.equals("com.ibm.ws.wsoc.WsocServlet")) {
            // Websphere servlet which implements websocket endpoints,
            // dynamically added
            return false;
        }
        // Must use servletContext class loader to load servlet class to
        // work correctly in an OSGi environment (#20024)
        Class<?> servletClass = servletContext.getClassLoader()
                .loadClass(servletClassName);
        return VaadinServlet.class.isAssignableFrom(servletClass);
    } catch (Exception e) {
        // This will fail in OSGi environments, assume everything is a
        // VaadinServlet
        return true;
    }
}
 
Example 3
Source File: DevModeInitializer.java    From flow with Apache License 2.0 5 votes vote down vote up
@Override
public void process(Set<Class<?>> classes, ServletContext context)
        throws ServletException {
    Collection<? extends ServletRegistration> registrations = context
            .getServletRegistrations().values();

    ServletRegistration vaadinServletRegistration = null;
    for (ServletRegistration registration : registrations) {
        if (registration.getClassName() != null
                && isVaadinServletSubClass(registration.getClassName())) {
            vaadinServletRegistration = registration;
            break;
        }
    }

    DeploymentConfiguration config;
    if (vaadinServletRegistration != null) {
        config = StubServletConfig.createDeploymentConfiguration(context,
                vaadinServletRegistration, VaadinServlet.class);
    } else {
        config = StubServletConfig.createDeploymentConfiguration(context,
                VaadinServlet.class);
    }

    initDevModeHandler(classes, context, config);

    setDevModeStarted(context);
}
 
Example 4
Source File: TestServletUtils.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void setServletParameters_supportUpload(@Mocked ServletContext servletContext, @Mocked Dynamic d1,
    @Mocked ServletRegistration d2) throws IOException {
  Map<String, ServletRegistration> servletRegistrations = new HashMap<>();
  servletRegistrations.put("d1", d1);
  servletRegistrations.put("d2", d2);
  new Expectations() {
    {
      servletContext.getServletRegistrations();
      result = servletRegistrations;
      d1.getClassName();
      result = RestServlet.class.getName();
      d2.getClassName();
      result = HttpServlet.class.getName();
    }
  };

  List<MultipartConfigElement> multipartConfigs = new ArrayList<>();
  new MockUp<Dynamic>(d1) {
    @Mock
    void setMultipartConfig(MultipartConfigElement multipartConfig) {
      multipartConfigs.add(multipartConfig);
    }
  };

  File tempDir = Files.createTempDirectory("temp").toFile();
  File uploadDir = new File(tempDir, "upload");
  ArchaiusUtils.setProperty(RestConst.UPLOAD_DIR, uploadDir.getAbsolutePath());

  ServletUtils.setServletParameters(servletContext);

  Assert.assertEquals(1, multipartConfigs.size());

  MultipartConfigElement multipartConfigElement = multipartConfigs.get(0);
  Assert.assertEquals(uploadDir.getAbsolutePath(), multipartConfigElement.getLocation());
  Assert.assertEquals(-1, multipartConfigElement.getMaxFileSize());
  Assert.assertEquals(-1, multipartConfigElement.getMaxRequestSize());
  Assert.assertEquals(0, multipartConfigElement.getFileSizeThreshold());

  uploadDir.delete();
  tempDir.delete();
  Assert.assertFalse(tempDir.exists());
}