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

The following examples show how to use javax.servlet.ServletRegistration#getMappings() . 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: ServletUtils.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
static String[] collectUrlPatterns(ServletContext servletContext, Class<?> servletCls) {
  List<ServletRegistration> servlets = findServletRegistrations(servletContext, servletCls);
  if (servlets.isEmpty()) {
    return new String[] {};
  }

  ServletRegistration servletRegistration = servlets.get(0);
  Collection<String> mappings = servletRegistration.getMappings();
  if (servlets.size() > 1) {
    LOGGER.info("Found {} {} registered, select the first one, mappings={}.",
        servlets.size(),
        servletCls.getName(),
        mappings);
  }
  return filterUrlPatterns(mappings);
}
 
Example 2
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 3
Source File: WebDiagnosticCollector.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
private Map<String, Object> buildServletList() {
    Map<String, Object> servletMap = new HashMap<>();
    for (Map.Entry<String, ? extends ServletRegistration> servletRegistrationEntry : servletContext
            .getServletRegistrations().entrySet()) {
        ServletRegistration servletRegistration = servletRegistrationEntry.getValue();
        Map<String, Object> servletRegistrationInfo = new HashMap<>();

        servletRegistrationInfo.put("class", servletRegistration.getClassName());
        servletRegistrationInfo.put("parameters", servletRegistration.getInitParameters());
        Collection<String> mappings = servletRegistration.getMappings();
        if (mappings == null) {
            mappings = new ArrayList<>();
        }
        servletRegistrationInfo.put("mappings", Sets.newLinkedHashSet(mappings));

        servletMap.put(servletRegistrationEntry.getKey(), servletRegistrationInfo);
    }

    return servletMap;
}
 
Example 4
Source File: ServletHttpHandlerAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private String getServletPath(ServletConfig config) {
	String name = config.getServletName();
	ServletRegistration registration = config.getServletContext().getServletRegistration(name);
	if (registration == null) {
		throw new IllegalStateException("ServletRegistration not found for Servlet '" + name + "'");
	}

	Collection<String> mappings = registration.getMappings();
	if (mappings.size() == 1) {
		String mapping = mappings.iterator().next();
		if (mapping.equals("/")) {
			return "";
		}
		if (mapping.endsWith("/*")) {
			String path = mapping.substring(0, mapping.length() - 2);
			if (!path.isEmpty() && logger.isDebugEnabled()) {
				logger.debug("Found servlet mapping prefix '" + path + "' for '" + name + "'");
			}
			return path;
		}
	}

	throw new IllegalArgumentException("Expected a single Servlet mapping: " +
			"either the default Servlet mapping (i.e. '/'), " +
			"or a path based mapping (e.g. '/*', '/foo/*'). " +
			"Actual mappings: " + mappings + " for Servlet '" + name + "'");
}
 
Example 5
Source File: ServletHttpHandlerAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
private String getServletPath(ServletConfig config) {
	String name = config.getServletName();
	ServletRegistration registration = config.getServletContext().getServletRegistration(name);
	if (registration == null) {
		throw new IllegalStateException("ServletRegistration not found for Servlet '" + name + "'");
	}

	Collection<String> mappings = registration.getMappings();
	if (mappings.size() == 1) {
		String mapping = mappings.iterator().next();
		if (mapping.equals("/")) {
			return "";
		}
		if (mapping.endsWith("/*")) {
			String path = mapping.substring(0, mapping.length() - 2);
			if (!path.isEmpty() && logger.isDebugEnabled()) {
				logger.debug("Found servlet mapping prefix '" + path + "' for '" + name + "'");
			}
			return path;
		}
	}

	throw new IllegalArgumentException("Expected a single Servlet mapping: " +
			"either the default Servlet mapping (i.e. '/'), " +
			"or a path based mapping (e.g. '/*', '/foo/*'). " +
			"Actual mappings: " + mappings + " for Servlet '" + name + "'");
}
 
Example 6
Source File: TomEEWebConfigProvider.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public List<ServletMapping> getFacesServletMappings(final ExternalContext externalContext) {
    final List<ServletMapping> facesServletMappings = super.getFacesServletMappings(externalContext);
    try { // getContext() is a runtime object where getServletRegistrations() is forbidden so unwrap
        final ServletContext sc = ServletContext.class.cast(Reflections.get(externalContext.getContext(), "sc"));
        if (sc != null && sc.getServletRegistrations() != null) {
            for (final Map.Entry<String, ? extends ServletRegistration> reg : sc.getServletRegistrations().entrySet()) {
                final ServletRegistration value = reg.getValue();
                if ("javax.faces.webapp.FacesServlet".equals(value.getClassName())) {
                    for (final String mapping : value.getMappings()) {
                        final Class<?> clazz = sc.getClassLoader().loadClass(value.getClassName());
                        final org.apache.myfaces.shared_impl.webapp.webxml.ServletMapping mappingImpl =
                                new org.apache.myfaces.shared_impl.webapp.webxml.ServletMapping(
                                    value.getName(), clazz, mapping);
                        facesServletMappings.add(new ServletMappingImpl(mappingImpl));
                    }
                }
            }
        } else {
            facesServletMappings.addAll(super.getFacesServletMappings(externalContext));
        }
    } catch (final Exception e) { // don't fail cause our cast failed
        facesServletMappings.clear();
        facesServletMappings.addAll(super.getFacesServletMappings(externalContext));
    }
    return facesServletMappings;
}
 
Example 7
Source File: ServletUtil.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
/**
 * Returns all servlet mappings for the given servlet name.
 */
public static Collection<String> getServletMappings(ServletContext servletContext, String servletName) {
    ServletRegistration reg = servletContext.getServletRegistration(servletName);
    if (reg == null) return null;
    return reg.getMappings();
}