@Override public void delete(String path) { ServletMapping mapping = handler.getServletMapping(path); ServletMapping[] mappings = new ServletMapping [handler.getServletMappings().length - 1]; ServletHolder[] servlets = new ServletHolder [handler.getServlets().length - 1]; int mLength = 0, sLength = 0; for (ServletMapping m : handler.getServletMappings()) { if (!m.equals(mapping)) { mappings[mLength++] = m; } } for (ServletHolder s : handler.getServlets()) { if (!s.equals(handler.getServlet(mapping.getServletName()))) { servlets[sLength++] = s; } } handler.setServletMappings(mappings); handler.setServlets(servlets); }
/** * Removes the previously registered servlet from the specified path * * @param pathSpec * the path spec to remove * @throws WebServerException * if an error occurs */ public synchronized static void removeServlet(String pathSpec) throws WebServerException { if (contexts != null) { try { ServletHandler handler = contexts.getServletHandler(); List<ServletMapping> mappings = new ArrayList<>(); for (ServletMapping mapping : handler.getServletMappings()) { List<String> pathSpecs = new ArrayList<>(); for (String path : mapping.getPathSpecs()) { if (!pathSpec.equals(path)) { pathSpecs.add(path); } } if (!pathSpecs.isEmpty()) { mapping.setPathSpecs(pathSpecs.toArray(new String[pathSpecs.size()])); mappings.add(mapping); } } handler.setServletMappings(mappings.toArray(new ServletMapping[mappings.size()])); } catch (Exception e) { throw new WebServerException(e); } } }
@Override public final Resource getResource(String pathInContext) { ServletContextHandler.Context context = (ServletContextHandler.Context) getServletContext(); ServletContextHandler contextHandler = (ServletContextHandler) context.getContextHandler(); for (ServletMapping mapping: contextHandler.getServletHandler().getServletMappings()) { if (mapping.getServletName().equals(getServletName())) { for (String pathSpec: mapping.getPathSpecs()) { String relativePath = null; if (pathSpec.endsWith("/*")) { pathSpec = StringUtils.substringBeforeLast(pathSpec, "/*"); if (pathInContext.startsWith(pathSpec + "/")) relativePath = pathInContext.substring(pathSpec.length()); } else if (pathSpec.startsWith("*.")) { pathSpec = StringUtils.stripStart(pathSpec, "*"); if (pathInContext.endsWith(pathSpec)) relativePath = pathInContext; } else if (pathSpec.equals(pathInContext)) { relativePath = pathInContext; } if (relativePath != null) { relativePath = StringUtils.stripStart(relativePath, "/"); Resource resource = Resource.newResource(loadResource(relativePath)); if (resource != null && resource.exists()) return resource; } } } } return null; }
/** * Add Jetty's {@code JspServlet} to the given {@link WebAppContext}. * @param context the jetty {@link WebAppContext} */ protected final void addJspServlet(WebAppContext context) { Assert.notNull(context, "Context must not be null"); ServletHolder holder = new ServletHolder(); holder.setName("jsp"); holder.setClassName(getJspServlet().getClassName()); holder.setInitParameter("fork", "false"); holder.setInitParameters(getJspServlet().getInitParameters()); holder.setInitOrder(3); context.getServletHandler().addServlet(holder); ServletMapping mapping = new ServletMapping(); mapping.setServletName("jsp"); mapping.setPathSpecs(new String[] { "*.jsp", "*.jspx" }); context.getServletHandler().addServletMapping(mapping); }
@Override public void unregisterHandler(ServletHolder servlet) { synchronized(lock) { ServletHandler servletHandler = rootContextHandler.getServletHandler(); ServletHolder[] curServlets = servletHandler.getServlets(); List<ServletHolder> newServlets = new ArrayList<>(); newServlets.addAll(Arrays.asList(curServlets)); if(!newServlets.remove(servlet)) { throw new IllegalArgumentException("Servlet not registered"); } List<ServletMapping> newMappings = new ArrayList<>(); newMappings.addAll(Arrays.asList(servletHandler.getServletMappings())); for(Iterator<ServletMapping> it = newMappings.iterator(); it.hasNext(); ) { ServletMapping m = it.next(); if(servlet.getName().equals(m.getServletName())) { for(String path : m.getPathSpecs()) { registeredPaths.remove(path); } it.remove(); break; } } servletHandler.setServlets(newServlets.toArray(new ServletHolder[newServlets.size()])); servletHandler.setServletMappings(newMappings.toArray(new ServletMapping[newMappings.size()])); if(!servlet.isStopped()) { try { servlet.stop(); } catch(Exception e) { throw new HttpConductorException(e); } } } }
@Override public void start() throws ServerException { logger.info("start server, host: {}, port: {}", host, port); try { for (ServletMapping servletMapping : servletContextHandler.getServletHandler().getServletMappings()) { logger.info("jetty servlet mappings: {} register by {}", servletMapping.getPathSpecs(), servletMapping.getServletName()); } server.start(); } catch (Exception e) { throw new JettyServerException(e.getMessage(), e); } }
public synchronized void deployServlet(HttpServlet servlet, Map<String, String> initParams, String... paths) { ServletHolder holder = new ServletHolder(servlet); if (initParams != null) holder.setInitParameters(initParams); ServletMapping mapping = new ServletMapping(); mapping.setServletName(holder.getName()); mapping.setPathSpecs(paths); servletHandler.getServletHandler().addServlet(holder); servletHandler.getServletHandler().addServletMapping(mapping); log.debug("Servlet deployed " + mapping.toString()); }
public synchronized void undeployServlet(HttpServlet servlet) { try { // there is no removeServlet method so we need to do it manually ServletHandler handler = servletHandler.getServletHandler(); // first collect servlets we want to keep List<ServletHolder> servlets = new ArrayList<ServletHolder>(); String nameToRemove = null; for( ServletHolder holder : handler.getServlets() ) { if (holder.getServlet() != servlet) servlets.add(holder); else nameToRemove = holder.getName(); } if (nameToRemove == null) return; // also update servlet path mappings List<ServletMapping> mappings = new ArrayList<ServletMapping>(); for (ServletMapping mapping : handler.getServletMappings()) { if (!nameToRemove.contains(mapping.getServletName())) mappings.add(mapping); } // set the new configuration handler.setServletMappings( mappings.toArray(new ServletMapping[0]) ); handler.setServlets( servlets.toArray(new ServletHolder[0]) ); } catch (ServletException e) { log.error("Error while undeploying servlet", e); } }
/** * Removes the previously registered servlet holder * * @param servlet * the servlet holder * @throws WebServerException * if an error occurs */ public synchronized static void removeServlet(ServletHolder servlet) throws WebServerException { if (contexts != null) { try { ServletHandler handler = contexts.getServletHandler(); List<ServletHolder> servlets = new ArrayList<>(); List<ServletMapping> mappings = new ArrayList<>(); for (ServletHolder sh : handler.getServlets()) { if (servlet != sh) { servlets.add(sh); } } for (ServletMapping mapping : handler.getServletMappings()) { if (!mapping.getServletName().equals(servlet.getName())) { mappings.add(mapping); } else { if (logger.isDebugEnabled()) { for (String path : mapping.getPathSpecs()) { logger.debug("Removed servlet for path {}", path); } } } } handler.setServletMappings(mappings.toArray(new ServletMapping[mappings.size()])); handler.setServlets(servlets.toArray(new ServletHolder[servlets.size()])); } catch (Exception e) { throw new WebServerException(e); } } }
private void updateServlets() { if (handler != null) { final Holder[] snapshot = servlets.toArray(new Holder[servlets.size()]); final ServletHolder[] holders = new ServletHolder[snapshot.length]; final ServletMapping[] mappings = new ServletMapping[snapshot.length]; for (int i = 0; i < snapshot.length; ++i) { holders[i] = snapshot[i].holder; mappings[i] = snapshot[i].mapping; } handler.setServlets(holders); handler.setServletMappings(mappings); } }
private Holder(@NotNull String pathSpec, @NotNull Servlet servlet) { path = pathSpec; holder = new ServletHolder(servlet); mapping = new ServletMapping(); mapping.setServletName(holder.getName()); mapping.setPathSpec(pathSpec); }