Java Code Examples for org.eclipse.jetty.servlet.ServletHolder#setInitParameters()

The following examples show how to use org.eclipse.jetty.servlet.ServletHolder#setInitParameters() . 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: HttpServer2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Add an internal servlet in the server, with initialization parameters.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param params init parameters
 */
public void addInternalServlet(String name, String pathSpec,
                               Class<? extends HttpServlet> clazz, Map<String, String> params) {
  // Jetty doesn't like the same path spec mapping to different servlets, so
  // if there's already a mapping for this pathSpec, remove it and assume that
  // the newest one is the one we want
  final ServletHolder sh = new ServletHolder(clazz);
  sh.setName(name);
  sh.setInitParameters(params);
  final ServletMapping[] servletMappings =
      webAppContext.getServletHandler().getServletMappings();
  for (int i = 0; i < servletMappings.length; i++) {
    if (servletMappings[i].containsPathSpec(pathSpec)) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Found existing {} servlet at path {}; will replace mapping with {} servlet"
            , servletMappings[i].getServletName(), pathSpec, sh.getName());
      }
      ServletMapping[] newServletMappings =
          ArrayUtil.removeFromArray(servletMappings, servletMappings[i]);
      webAppContext.getServletHandler()
          .setServletMappings(newServletMappings);
      break;
    }
  }
  webAppContext.addServlet(sh, pathSpec);
}
 
Example 2
Source File: HttpServer2.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private static WebAppContext createWebAppContext(Builder b,
    AccessControlList adminsAcl, final String appDir) {
  WebAppContext ctx = new WebAppContext();
  ctx.setDefaultsDescriptor(null);
  ServletHolder holder = new ServletHolder(new DefaultServlet());
  Map<String, String> params = ImmutableMap.<String, String>builder()
      .put("acceptRanges", "true")
      .put("dirAllowed", "false")
      .put("gzip", "true")
      .put("useFileMappedBuffer", "true")
      .build();
  holder.setInitParameters(params);
  ctx.setWelcomeFiles(new String[] {"index.html"});
  ctx.addServlet(holder, "/");
  ctx.setDisplayName(b.name);
  ctx.setContextPath("/");
  ctx.setWar(appDir + "/" + b.name);
  String tempDirectory = b.conf.get(HTTP_TEMP_DIR_KEY);
  if (tempDirectory != null && !tempDirectory.isEmpty()) {
    ctx.setTempDirectory(new File(tempDirectory));
    ctx.setAttribute("javax.servlet.context.tempdir", tempDirectory);
  }
  ctx.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, b.conf);
  ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl);
  addNoCacheFilter(ctx);
  return ctx;
}
 
Example 3
Source File: HttpServer2.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Add an internal servlet in the server, with initialization parameters.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param params init parameters
 */
public void addInternalServlet(String name, String pathSpec,
    Class<? extends HttpServlet> clazz, Map<String, String> params) {
  // Jetty doesn't like the same path spec mapping to different servlets, so
  // if there's already a mapping for this pathSpec, remove it and assume that
  // the newest one is the one we want
  final ServletHolder sh = new ServletHolder(clazz);
  sh.setName(name);
  sh.setInitParameters(params);
  final ServletMapping[] servletMappings =
      webAppContext.getServletHandler().getServletMappings();
  for (int i = 0; i < servletMappings.length; i++) {
    if (servletMappings[i].containsPathSpec(pathSpec)) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Found existing " + servletMappings[i].getServletName() +
            " servlet at path " + pathSpec + "; will replace mapping" +
            " with " + sh.getName() + " servlet");
      }
      ServletMapping[] newServletMappings =
          ArrayUtil.removeFromArray(servletMappings, servletMappings[i]);
      webAppContext.getServletHandler()
          .setServletMappings(newServletMappings);
      break;
    }
  }
  webAppContext.addServlet(sh, pathSpec);
}
 
Example 4
Source File: HttpServer.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public void addServlet(String path, Servlet servlet, Map<String,String> initParams) {
   try {
      ServletHolder holder = new ServletHolder(servlet);
      holder.setInitParameters(initParams);

      context.addServlet(holder, path);
   } catch (Exception ex) {
      throw new RuntimeException(ex);
   }
}
 
Example 5
Source File: HttpServer2.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static WebAppContext createWebAppContext(Builder b,
                                                 AccessControlList adminsAcl, final String appDir) {
  WebAppContext ctx = new WebAppContext();
  ctx.setDefaultsDescriptor(null);
  ServletHolder holder = new ServletHolder(new DefaultServlet());
  Map<String, String> params = ImmutableMap. <String, String> builder()
      .put("acceptRanges", "true")
      .put("dirAllowed", "false")
      .put("gzip", "true")
      .put("useFileMappedBuffer", "true")
      .build();
  holder.setInitParameters(params);
  ctx.setWelcomeFiles(new String[] {"index.html"});
  ctx.addServlet(holder, "/");
  ctx.setDisplayName(b.name);
  ctx.setContextPath("/");
  ctx.setWar(appDir + "/" + b.name);
  String tempDirectory = b.conf.get(HTTP_TEMP_DIR_KEY);
  if (tempDirectory != null && !tempDirectory.isEmpty()) {
    ctx.setTempDirectory(new File(tempDirectory));
    ctx.setAttribute("javax.servlet.context.tempdir", tempDirectory);
  }
  ctx.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, b.conf);
  ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl);
  addNoCacheFilter(ctx);
  return ctx;
}
 
Example 6
Source File: ServerManager.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public void addServlet(String path, Class<? extends Servlet> servlet, Map<String, String> initParameters) {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath(path);

    ServletHolder holder = new ServletHolder(servlet);
    holder.setInitParameters(initParameters);
    context.addServlet(holder, path);
    handlers.add(context);
}
 
Example 7
Source File: JettyConfigurationHelper.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
static void addServlets( ServletContextHandler root, Iterable<ServiceReference<Servlet>> servlets )
{
    // Iterate the available servlets and add it to the server
    for( ServiceReference<Servlet> servlet : servlets )
    {
        ServletInfo servletInfo = servlet.metaInfo( ServletInfo.class );
        String servletPath = servletInfo.getPath();
        Servlet servletInstance = servlet.get();
        ServletHolder holder = new ServletHolder( servletInstance );
        holder.setInitParameters( servletInfo.initParams() );
        root.addServlet( holder, servletPath );
    }
}
 
Example 8
Source File: HttpServer.java    From sensorhub with Mozilla Public License 2.0 5 votes vote down vote up
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());
}
 
Example 9
Source File: HttpServer2.java    From knox with Apache License 2.0 5 votes vote down vote up
private static WebAppContext createWebAppContext(Builder b,
                                                 AccessControlList adminsAcl, final String appDir) {
  WebAppContext ctx = new WebAppContext();
  ctx.setDefaultsDescriptor(null);
  ServletHolder holder = new ServletHolder(new DefaultServlet());
  Map<String, String> params = ImmutableMap. <String, String> builder()
                                   .put("acceptRanges", "true")
                                   .put("dirAllowed", "false")
                                   .put("gzip", "true")
                                   .put("useFileMappedBuffer", "true")
                                   .build();
  holder.setInitParameters(params);
  ctx.setWelcomeFiles(new String[] {"index.html"});
  ctx.addServlet(holder, "/");
  ctx.setDisplayName(b.name);
  ctx.setContextPath("/");
  ctx.setWar(appDir + "/" + b.name);
  String tempDirectory = b.conf.get(HTTP_TEMP_DIR_KEY);
  if (tempDirectory != null && !tempDirectory.isEmpty()) {
    ctx.setTempDirectory(new File(tempDirectory));
    ctx.setAttribute("javax.servlet.context.tempdir", tempDirectory);
  }
  ctx.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, b.conf);
  ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl);
  addNoCacheFilter(ctx);
  return ctx;
}
 
Example 10
Source File: HttpServer2.java    From knox with Apache License 2.0 5 votes vote down vote up
/**
 * Add an internal servlet in the server, with initialization parameters.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param params init parameters
 */
public void addInternalServlet(String name, String pathSpec,
                               Class<? extends HttpServlet> clazz, Map<String, String> params) {
  // Jetty doesn't like the same path spec mapping to different servlets, so
  // if there's already a mapping for this pathSpec, remove it and assume that
  // the newest one is the one we want
  final ServletHolder sh = new ServletHolder(clazz);
  sh.setName(name);
  sh.setInitParameters(params);
  final ServletMapping[] servletMappings =
      webAppContext.getServletHandler().getServletMappings();
  for (ServletMapping servletMapping : servletMappings) {
    if (servletMapping.containsPathSpec(pathSpec)) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Found existing " + servletMapping.getServletName() +
                      " servlet at path " + pathSpec + "; will replace mapping" +
                      " with " + sh.getName() + " servlet");
      }
      ServletMapping[] newServletMappings =
          ArrayUtil.removeFromArray(servletMappings, servletMapping);
      webAppContext.getServletHandler()
          .setServletMappings(newServletMappings);
      break;
    }
  }
  webAppContext.addServlet(sh, pathSpec);
}
 
Example 11
Source File: HttpServer2.java    From knox with Apache License 2.0 5 votes vote down vote up
private static WebAppContext createWebAppContext(Builder b,
                                                 AccessControlList adminsAcl, final String appDir) {
  WebAppContext ctx = new WebAppContext();
  ctx.setDefaultsDescriptor(null);
  ServletHolder holder = new ServletHolder(new DefaultServlet());
  Map<String, String> params = ImmutableMap. <String, String> builder()
                                   .put("acceptRanges", "true")
                                   .put("dirAllowed", "false")
                                   .put("gzip", "true")
                                   .put("useFileMappedBuffer", "true")
                                   .build();
  holder.setInitParameters(params);
  ctx.setWelcomeFiles(new String[] {"index.html"});
  ctx.addServlet(holder, "/");
  ctx.setDisplayName(b.name);
  ctx.setContextPath("/");
  ctx.setWar(appDir + "/" + b.name);
  String tempDirectory = b.conf.get(HTTP_TEMP_DIR_KEY);
  if (tempDirectory != null && !tempDirectory.isEmpty()) {
    ctx.setTempDirectory(new File(tempDirectory));
    ctx.setAttribute("javax.servlet.context.tempdir", tempDirectory);
  }
  ctx.getServletContext().setAttribute(CONF_CONTEXT_ATTRIBUTE, b.conf);
  ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl);
  addNoCacheFilter(ctx);
  return ctx;
}
 
Example 12
Source File: HttpServer2.java    From knox with Apache License 2.0 5 votes vote down vote up
/**
 * Add an internal servlet in the server, with initialization parameters.
 * Note: This method is to be used for adding servlets that facilitate
 * internal communication and not for user facing functionality. For
 * servlets added using this method, filters (except internal Kerberos
 * filters) are not enabled.
 *
 * @param name The name of the servlet (can be passed as null)
 * @param pathSpec The path spec for the servlet
 * @param clazz The servlet class
 * @param params init parameters
 */
public void addInternalServlet(String name, String pathSpec,
                               Class<? extends HttpServlet> clazz, Map<String, String> params) {
  // Jetty doesn't like the same path spec mapping to different servlets, so
  // if there's already a mapping for this pathSpec, remove it and assume that
  // the newest one is the one we want
  final ServletHolder sh = new ServletHolder(clazz);
  sh.setName(name);
  sh.setInitParameters(params);
  final ServletMapping[] servletMappings =
      webAppContext.getServletHandler().getServletMappings();
  for (ServletMapping servletMapping : servletMappings) {
    if (servletMapping.containsPathSpec(pathSpec)) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Found existing " + servletMapping.getServletName() +
                      " servlet at path " + pathSpec + "; will replace mapping" +
                      " with " + sh.getName() + " servlet");
      }
      ServletMapping[] newServletMappings =
          ArrayUtil.removeFromArray(servletMappings, servletMapping);
      webAppContext.getServletHandler()
          .setServletMappings(newServletMappings);
      break;
    }
  }
  webAppContext.addServlet(sh, pathSpec);
}
 
Example 13
Source File: ServerRpcProvider.java    From incubator-retired-wave with Apache License 2.0 5 votes vote down vote up
/**
 * Add a servlet to the servlet registry. This servlet will be attached to the
 * specified URL pattern when the server is started up.
 *
 * @param urlPattern the URL pattern for paths. Eg, '/foo', '/foo/*'.
 * @param servlet the servlet class to bind to the specified paths.
 * @param initParams the map with init params, can be null or empty.
 * @return the {@link ServletHolder} that holds the servlet.
 */
public ServletHolder addServlet(String urlPattern, Class<? extends HttpServlet> servlet,
    @Nullable Map<String, String> initParams) {
  ServletHolder servletHolder = new ServletHolder(servlet);
  if (initParams != null) {
    servletHolder.setInitParameters(initParams);
  }
  servletRegistry.add(Pair.of(urlPattern, servletHolder));
  return servletHolder;
}
 
Example 14
Source File: ServerRpcProvider.java    From swellrt with Apache License 2.0 3 votes vote down vote up
/**
 * Add a servlet to the servlet registry. This servlet will be attached to the
 * specified URL pattern when the server is started up.
 *
 * @param urlPattern
 *          the URL pattern for paths. Eg, '/foo', '/foo/*'.
 * @param servlet
 *          the servlet class to bind to the specified paths.
 * @param initParams
 *          the map with init params, can be null or empty.
 * @return the {@link ServletHolder} that holds the servlet.
 */
public ServletHolder addServlet(String urlPattern, Class<? extends HttpServlet> servlet,
    @Nullable Map<String, String> initParams) {
  ServletHolder servletHolder = new ServletHolder(servlet);
  if (initParams != null) {
    servletHolder.setInitParameters(initParams);
  }
  servletRegistry.add(Pair.of(urlPattern, servletHolder));
  return servletHolder;
}