javax.servlet.GenericServlet Java Examples

The following examples show how to use javax.servlet.GenericServlet. 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: FreeMarkerView.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Invoked on startup. Looks for a single FreeMarkerConfig bean to
 * find the relevant Configuration for this factory.
 * <p>Checks that the template for the default Locale can be found:
 * FreeMarker will check non-Locale-specific templates if a
 * locale-specific one is not found.
 * @see freemarker.cache.TemplateCache#getTemplate
 */
@Override
protected void initServletContext(ServletContext servletContext) throws BeansException {
	if (getConfiguration() != null) {
		this.taglibFactory = new TaglibFactory(servletContext);
	}
	else {
		FreeMarkerConfig config = autodetectConfiguration();
		setConfiguration(config.getConfiguration());
		this.taglibFactory = config.getTaglibFactory();
	}

	GenericServlet servlet = new GenericServletAdapter();
	try {
		servlet.init(new DelegatingServletConfig());
	}
	catch (ServletException ex) {
		throw new BeanInitializationException("Initialization of GenericServlet adapter failed", ex);
	}
	this.servletContextHashModel = new ServletContextHashModel(servlet, getObjectWrapper());
}
 
Example #2
Source File: FreeMarkerView.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Invoked on startup. Looks for a single FreeMarkerConfig bean to
 * find the relevant Configuration for this factory.
 * <p>Checks that the template for the default Locale can be found:
 * FreeMarker will check non-Locale-specific templates if a
 * locale-specific one is not found.
 * @see freemarker.cache.TemplateCache#getTemplate
 */
@Override
protected void initServletContext(ServletContext servletContext) throws BeansException {
	if (getConfiguration() != null) {
		this.taglibFactory = new TaglibFactory(servletContext);
	}
	else {
		FreeMarkerConfig config = autodetectConfiguration();
		setConfiguration(config.getConfiguration());
		this.taglibFactory = config.getTaglibFactory();
	}

	GenericServlet servlet = new GenericServletAdapter();
	try {
		servlet.init(new DelegatingServletConfig());
	}
	catch (ServletException ex) {
		throw new BeanInitializationException("Initialization of GenericServlet adapter failed", ex);
	}
	this.servletContextHashModel = new ServletContextHashModel(servlet, getObjectWrapper());
}
 
Example #3
Source File: ModuleManager.java    From openfire-ofmeet-plugin with Apache License 2.0 6 votes vote down vote up
private static GenericServlet instantiateServlet( final String servletClassname ) throws ClassNotFoundException, IllegalAccessException, InstantiationException, ServletException, NoSuchFieldException
{
    final Class<?> theClass = Thread.currentThread().getContextClassLoader().loadClass( servletClassname );

    final Object instance = theClass.newInstance();
    if ( !(instance instanceof GenericServlet) )
    {
        throw new IllegalArgumentException( "Could not load servlet instance" );
    }

    // TODO find better way than using reflection to get the servletConfig instance.
    Field field = PluginServlet.class.getDeclaredField( "servletConfig" );
    field.setAccessible( true );
    try {
        ( (GenericServlet) instance ).init( (ServletConfig) field.get( null ) );
    }
    finally
    {
        field.setAccessible( false );
    }

    return ( (GenericServlet) instance );
}
 
Example #4
Source File: FreeMarkerView.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invoked on startup. Looks for a single FreeMarkerConfig bean to
 * find the relevant Configuration for this factory.
 * <p>Checks that the template for the default Locale can be found:
 * FreeMarker will check non-Locale-specific templates if a
 * locale-specific one is not found.
 * @see freemarker.cache.TemplateCache#getTemplate
 */
@Override
protected void initServletContext(ServletContext servletContext) throws BeansException {
	if (getConfiguration() != null) {
		this.taglibFactory = new TaglibFactory(servletContext);
	}
	else {
		FreeMarkerConfig config = autodetectConfiguration();
		setConfiguration(config.getConfiguration());
		this.taglibFactory = config.getTaglibFactory();
	}

	GenericServlet servlet = new GenericServletAdapter();
	try {
		servlet.init(new DelegatingServletConfig());
	}
	catch (ServletException ex) {
		throw new BeanInitializationException("Initialization of GenericServlet adapter failed", ex);
	}
	this.servletContextHashModel = new ServletContextHashModel(servlet, getObjectWrapper());
}
 
Example #5
Source File: FreeMarkerView.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Invoked on startup. Looks for a single FreeMarkerConfig bean to
 * find the relevant Configuration for this factory.
 * <p>Checks that the template for the default Locale can be found:
 * FreeMarker will check non-Locale-specific templates if a
 * locale-specific one is not found.
 * @see freemarker.cache.TemplateCache#getTemplate
 */
@Override
protected void initServletContext(ServletContext servletContext) throws BeansException {
	if (getConfiguration() != null) {
		this.taglibFactory = new TaglibFactory(servletContext);
	}
	else {
		FreeMarkerConfig config = autodetectConfiguration();
		setConfiguration(config.getConfiguration());
		this.taglibFactory = config.getTaglibFactory();
	}

	GenericServlet servlet = new GenericServletAdapter();
	try {
		servlet.init(new DelegatingServletConfig());
	}
	catch (ServletException ex) {
		throw new BeanInitializationException("Initialization of GenericServlet adapter failed", ex);
	}
	this.servletContextHashModel = new ServletContextHashModel(servlet, getObjectWrapper());
}
 
Example #6
Source File: PluginServlet.java    From Openfire with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the correct servlet with mapping checks.
 *
 * @param pathInfo the pathinfo to map to the servlet.
 * @return the mapped servlet, or null if no servlet was found.
 */
private GenericServlet getServlet(String pathInfo) {
    pathInfo = pathInfo.substring(1).toLowerCase();

    GenericServlet servlet = servlets.get(pathInfo);
    if (servlet == null) {
        for (String key : servlets.keySet()) {
            int index = key.indexOf("/*");
            String searchkey = key;
            if (index != -1) {
                searchkey = key.substring(0, index);
            }
            if (searchkey.startsWith(pathInfo) || pathInfo.startsWith(searchkey)) {
                servlet = servlets.get(key);
                break;
            }
        }
    }
    return servlet;
}
 
Example #7
Source File: FreeMarkerInlineRenderBootstrap.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * Initialize FreeMarker elements after servlet context and FreeMarker configuration have both
 * been populated.
 */
private static void finishConfig() {
    if (freeMarkerConfig != null && servletContext != null) {
        taglibFactory = new TaglibFactory(servletContext);
        
        objectWrapper = freeMarkerConfig.getObjectWrapper();
        if (objectWrapper == null) {
            objectWrapper = ObjectWrapper.DEFAULT_WRAPPER;
        }

        GenericServlet servlet = new ServletAdapter();
        try {
            servlet.init(new DelegatingServletConfig());
        } catch (ServletException ex) {
            throw new BeanInitializationException("Initialization of GenericServlet adapter failed", ex);
        }
        
        servletContextHashModel = new ServletContextHashModel(servlet, ObjectWrapper.DEFAULT_WRAPPER);
        
        LOG.info("Freemarker configuration complete");
    }
}
 
Example #8
Source File: Utils.java    From smart-cache with Apache License 2.0 5 votes vote down vote up
public static Boolean getBoolean(GenericServlet servlet, String key) {
    String property = servlet.getInitParameter(key);
    if ("true".equals(property)) {
        return Boolean.TRUE;
    } else if ("false".equals(property)) {
        return Boolean.FALSE;
    }
    return null;
}
 
Example #9
Source File: ODataServletTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void prepareServlet(final GenericServlet servlet) throws Exception {
  // private transient ServletConfig config;
  Field configField = GenericServlet.class.getDeclaredField("config");
  configField.setAccessible(true);
  configField.set(servlet, configMock);

  String factoryClassName = ODataServiceFactoryImpl.class.getName();
  Mockito.when(configMock.getInitParameter(ODataServiceFactory.FACTORY_LABEL)).thenReturn(factoryClassName);
}
 
Example #10
Source File: PluginServlet.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Registers a live servlet for a plugin programmatically, does not
 * initialize the servlet.
 * 
 * @param pluginManager the plugin manager
 * @param plugin the owner of the servlet
 * @param servlet the servlet.
 * @param relativeUrl the relative url where the servlet should be bound
 * @return the effective url that can be used to initialize the servlet
 * @throws ServletException if the servlet is null
 */
public static String registerServlet(PluginManager pluginManager,
        Plugin plugin, GenericServlet servlet, String relativeUrl)
        throws ServletException {

    String pluginName = pluginManager.getPluginPath(plugin).getFileName().toString();
    PluginServlet.pluginManager = pluginManager;
    if (servlet == null) {
        throw new ServletException("Servlet is missing");
    }
    String pluginServletUrl = pluginName + relativeUrl;
    servlets.put((pluginName + relativeUrl).toLowerCase(), servlet);
    return PLUGINS_WEBROOT + pluginServletUrl;
    
}
 
Example #11
Source File: PluginServlet.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Unregister a live servlet for a plugin programmatically. Does not call
 * the servlet destroy method.
 * 
 * @param plugin the owner of the servlet
 * @param url the relative url where servlet has been bound
 * @return the unregistered servlet, so that it can be destroyed
 * @throws ServletException if the URL is missing
 */
public static GenericServlet unregisterServlet(Plugin plugin, String url)
        throws ServletException {
    String pluginName = pluginManager.getPluginPath(plugin).getFileName().toString();
    if (url == null) {
        throw new ServletException("Servlet URL is missing");
    }
    String fullUrl = pluginName + url;
    return servlets.remove(fullUrl.toLowerCase());
}
 
Example #12
Source File: PluginServlet.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Handles a request for a JSP page. It checks to see if a servlet is mapped
 * for the JSP URL. If one is found, request handling is passed to it. If no
 * servlet is found, a 404 error is returned.
 *
 * @param pathInfo the extra path info.
 * @param request  the request object.
 * @param response the response object.
 * @throws ServletException if a servlet exception occurs while handling the request.
 * @throws IOException      if an IOException occurs while handling the request.
 */
private void handleJSP(String pathInfo, HttpServletRequest request,
                       HttpServletResponse response) throws ServletException, IOException {
    // Strip the starting "/" from the path to find the JSP URL.
    String jspURL = pathInfo.substring(1);

    GenericServlet servlet = servlets.get(jspURL.toLowerCase());
    if (servlet != null) {
        servlet.service(request, response);
    }
    else {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
}
 
Example #13
Source File: PluginServlet.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Handles a request for a Servlet. If one is found, request handling is passed to it.
 * If no servlet is found, a 404 error is returned.
 *
 * @param pathInfo the extra path info.
 * @param request  the request object.
 * @param response the response object.
 * @throws ServletException if a servlet exception occurs while handling the request.
 * @throws IOException      if an IOException occurs while handling the request.
 */
private void handleServlet(String pathInfo, HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {
    // Strip the starting "/" from the path to find the JSP URL.
    GenericServlet servlet = getServlet(pathInfo);
    if (servlet != null) {
        servlet.service(request, response);
    }
    else {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
}
 
Example #14
Source File: ModuleManager.java    From openfire-ofmeet-plugin with Apache License 2.0 4 votes vote down vote up
public synchronized void loadModule( String moduleClassname, Path path ) throws ClassNotFoundException, IllegalAccessException, InstantiationException, ServletException, NoSuchFieldException
{
    Log.debug( "Loading module '{}' from '{}'.", moduleClassname, path );

    Log.trace( "Initialize the class loader dedicated to this module." );
    final ModuleClassLoader classLoader = new ModuleClassLoader( parent );
    classLoader.addDirectory( path.toFile(), false );

    Log.trace( "Instantiate the module, using its own classloader." );
    final Module module = (Module) classLoader.loadClass( moduleClassname ).newInstance();

    modulesByClassName.put( moduleClassname, module );
    classLoaderByModule.put( module, classLoader );

    Log.trace( "Initialize the module in its own classloader." );
    final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
    try
    {
        Thread.currentThread().setContextClassLoader( classLoader );
        module.initialize( manager, pluginDirectory );

        Log.trace( "Registering module servlets (if any)." );
        final Map<String, String> servlets = module.getServlets();
        if ( servlets != null )
        {
            for ( final Map.Entry<String, String> entry : servlets.entrySet() )
            {
                final GenericServlet servlet = instantiateServlet( entry.getValue() );
                PluginServlet.registerServlet(
                    XMPPServer.getInstance().getPluginManager(),
                    plugin,
                    servlet,
                    entry.getKey()
                );
            }
        }
    }
    finally
    {
        Thread.currentThread().setContextClassLoader( oldLoader );
    }
}
 
Example #15
Source File: VaadinServletService.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
protected PwaRegistry getPwaRegistry() {
    return Optional.ofNullable(getServlet())
            .map(GenericServlet::getServletContext)
            .map(PwaRegistry::getInstance).orElse(null);
}