private List<EndpointInfo> getConsolidatedEndpoints(
     ServletContext context,
     List<EndpointInfo> ddEndpointList,
     Set<Class<?>> wsClassSet,
     List<Source> metadata) {
   List<EndpointInfo> consList = new ArrayList<>();
   for (Class<?> c : wsClassSet) {
     EndpointInfo endpointInfo = findEndpointInfo(ddEndpointList, c);
     endpointInfo.implType = c;
     if (endpointInfo.servletLink == null) {
       endpointInfo.servletLink = getDefaultServletLink(c);
     }
     ServletRegistration reg = context.getServletRegistration(endpointInfo.servletLink);
     if (reg != null) {
       Collection<String> mappings = reg.getMappings();
       if (mappings.isEmpty()) {
         endpointInfo.urlPattern = getDefaultUrlPattern(c);
       } else {
         endpointInfo.urlPattern = mappings.iterator().next();
       }
     } else {
       endpointInfo.urlPattern = getDefaultUrlPattern(c);
     }
     endpointInfo.features = new WebServiceFeature[0];
     endpointInfo.metadata = metadata;
     consList.add(endpointInfo);
   }
   return consList;
 }
Esempio n. 2
0
 @Override
 public void init(ServletConfig config) throws ServletException {
   HelperLoader.init();
   ServletContext servletContext = config.getServletContext();
   ServletRegistration jspServlet = servletContext.getServletRegistration("jsp");
   jspServlet.addMapping(ConfigHelper.getAppJspPath() + "*");
   ServletRegistration defaultServlet = servletContext.getServletRegistration("default");
   defaultServlet.addMapping(ConfigHelper.getAppAssetPath() + "*");
 }
  /**
   * Add endpoint mapping to cas servlet.
   *
   * @param sce the sce
   * @param mapping the mapping
   */
  protected final void addEndpointMappingToCasServlet(
      final ServletContextEvent sce, final String mapping) {
    logger.info("Adding [{}] to {} servlet context", mapping, WebUtils.CAS_SERVLET_NAME);
    final ServletRegistration registration = getCasServletRegistration(sce);
    if (registration != null) {

      registration.addMapping(mapping);
      logger.info("Added [{}] to {} servlet context", mapping, WebUtils.CAS_SERVLET_NAME);
    }
  }
  public void onStartup(Set<Class<?>> wsClassSet, ServletContext context) throws ServletException {
    LOGGER.info("WAR has webservice classes: " + wsClassSet);

    // TODO Check if metadata-complete is true in web.xml

    // Get the WSDL and schema documents in /WEB-INF/wsdl dir
    List<Source> metadata = new ArrayList<>();
    try {
      Set<URL> docs = new HashSet<>();
      collectDocs(docs, new ServletResourceLoader(context), JAXWS_WSDL_DD_DIR);
      for (URL url : docs) {
        Source source = new StreamSource(url.openStream(), url.toExternalForm());
        metadata.add(source);
      }
    } catch (IOException me) {
      throw new ServletException(me);
    }

    // Parse /WEB-INF/webservices.xml DD
    List<EndpointInfo> ddEndpointList = parseDD(context);

    // Consolidate endpoints from DD and annotations
    List<EndpointInfo> endpointInfoList =
        getConsolidatedEndpoints(context, ddEndpointList, wsClassSet, metadata);
    if (endpointInfoList.isEmpty()) {
      return; // No web service endpoints
    }

    EndpointAdapterFactory factory = new EndpointAdapterFactory();

    List<EndpointAdapter> adapters = new ArrayList<>();

    for (EndpointInfo endpointInfo : endpointInfoList) {
      ServletRegistration reg = context.getServletRegistration(endpointInfo.servletLink);
      if (reg == null) {
        reg = context.addServlet(endpointInfo.servletLink, WSServlet.class);
      } else {
        // NEED the following in servlet API to override <servlet-class>
        // reg.setClassName(endpointInfo.implType);
      }
      Collection<String> mappings = reg.getMappings();
      if (!mappings.contains(endpointInfo.urlPattern)) {
        reg.addMapping(endpointInfo.urlPattern);
      }
      EndpointAdapter adapter = factory.createAdapter(endpointInfo);
      adapters.add(adapter);
    }

    for (EndpointAdapter adapter : adapters) {
      adapter.publish();
    }

    context.setAttribute(WSServlet.JAXWS_RI_RUNTIME_INFO, adapters);
  }
 @Override
 public void init(ServletConfig servletConfig) throws ServletException {
   // 初始化 Helper 类
   HelperLoader.init();
   // 获取 ServletContext 对象(用于注册Servlet)
   ServletContext servletContext = servletConfig.getServletContext();
   // 注册处理 JSP 的 Servlet
   ServletRegistration jspServlet = servletContext.getServletRegistration("jsp");
   jspServlet.addMapping(ConfigHelper.getAppJspPath() + "*");
   // 注册处理静态资源的默认 Servlet
   ServletRegistration defaultServlet = servletContext.getServletRegistration("default");
   defaultServlet.addMapping(ConfigHelper.getAppAssetPath() + "*");
 }
Esempio n. 6
0
  public WebApplication(
      TomcatGenericWebappsDeployer tomcatGenericWebappsDeployer, Context context, File webappFile) {
    this.tomcatGenericWebappsDeployer = tomcatGenericWebappsDeployer;
    this.context = context;
    setWebappFile(webappFile);
    setLastModifiedTime(webappFile.lastModified());

    boolean isFaulty = checkFaultyWebappParam(context);
    if (isFaulty || !"STARTED".equalsIgnoreCase(context.getStateName())) {
      return;
    }

    String serviceListPathParamName = "service-list-path";
    String serviceListPathParam =
        context.getServletContext().getInitParameter(serviceListPathParamName);
    if ("".equals(serviceListPathParam) || serviceListPathParam == null) {
      Map<String, ? extends ServletRegistration> servletRegs =
          context.getServletContext().getServletRegistrations();
      for (ServletRegistration servletReg : servletRegs.values()) {
        serviceListPathParam = servletReg.getInitParameter(serviceListPathParamName);
        if (!"".equals(serviceListPathParam) || serviceListPathParam != null) {
          break;
        }
      }
    }
    if ("".equals(serviceListPathParam) || serviceListPathParam == null) {
      serviceListPathParam = "/services";
    } else {
      serviceListPathParam = "";
    }
    setServiceListPath(serviceListPathParam);

    String versionString = context.getName();
    if (context.getName().startsWith("/t/")) {
      // remove tenant context
      versionString = versionString.substring(context.getName().lastIndexOf("/webapps/") + 9);
    } else if (context.getName().startsWith("/")) {
      versionString = versionString.substring(1);
    }
    if (versionString.indexOf("/") > -1) {
      versionString = versionString.substring(versionString.indexOf("/"));
      setVersion(versionString);
    } else {
      setVersion(WebappsConstants.DEFAULT_VERSION);
    }
  }
  public static void main(String[] args) throws Exception {

    HttpServer server = HttpServer.createSimpleServer("/", 8080);
    WebappContext ctx = new WebappContext("api");
    ServletRegistration jerseyServlet =
        ctx.addServlet("jersey", org.glassfish.jersey.servlet.ServletContainer.class);
    jerseyServlet.addMapping("/api/*");
    jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "com.resource");
    jerseyServlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
    ctx.deploy(server);
    try {
      server.start();
      System.out.println("Press any key to stop the server....");
      System.in.read();
    } finally {
      server.shutdownNow();
    }
  }
Esempio n. 8
0
  @Override
  public void startServer() throws Exception {
    server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI));

    AtmosphereServlet atmoServlet = new AtmosphereServlet();

    WebappContext context = new WebappContext("Chatty", "/chatty");

    ServletRegistration atmosphereRegistration = context.addServlet("Atmosphere", atmoServlet);
    atmosphereRegistration.addMapping("/atmos/*");

    ServletContainer jerseyContainer = new ServletContainer(resourceConfig);
    ServletRegistration jerseyRegistration = context.addServlet("Jersey", jerseyContainer);
    jerseyRegistration.addMapping("/api/*");

    context.deploy(server);

    server.start();
  }
Esempio n. 9
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());
  }
Esempio n. 10
0
 @Override
 protected void doAdditionalModuleStartLogic() throws Exception {
   if (StringUtils.isNotBlank(dispatchServletName)) {
     DispatcherServlet loaderServlet =
         new DispatcherServlet(
             (WebApplicationContext)
                 ((SpringResourceLoader) rootResourceLoader.getResourceLoaders().get(0))
                     .getContext());
     ServletRegistration registration =
         getServletContext().addServlet(dispatchServletName, loaderServlet);
     registration.addMapping("/" + dispatchServletName + "/*");
     if (dispatchServletMappings != null) {
       dispatchServletMappings
           .stream()
           .map(mapping -> "/" + mapping + "/*")
           .forEach(registration::addMapping);
     }
     if (mapFilters) {
       for (String filterName : filtersToMap) {
         FilterRegistration filter = getServletContext().getFilterRegistration(filterName);
         filter.addMappingForServletNames(null, true, dispatchServletName);
       }
     }
     if (enableSpringSecurity) {
       DelegatingFilterProxy filterProxy =
           new DelegatingFilterProxy(
               SPRING_SECURITY_FILTER_CHAIN,
               (WebApplicationContext)
                   ((SpringResourceLoader) rootResourceLoader.getResourceLoaders().get(0))
                       .getContext());
       FilterRegistration.Dynamic securityFilter =
           getServletContext()
               .addFilter(KC_PREFIX + getModuleName() + SPRING_SECURITY_FILTER_PROXY, filterProxy);
       securityFilter.addMappingForServletNames(null, true, dispatchServletName);
     }
   }
 }