Java Code Examples for org.springframework.web.context.WebApplicationContext#getBeansOfType()

The following examples show how to use org.springframework.web.context.WebApplicationContext#getBeansOfType() . 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: MvcUriComponentsBuilder.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * An alternative to {@link #fromMappingName(String)} that accepts a
 * {@code UriComponentsBuilder} representing the base URL. This is useful
 * when using MvcUriComponentsBuilder outside the context of processing a
 * request or to apply a custom baseUrl not matching the current request.
 * <p><strong>Note:</strong> This method extracts values from "Forwarded"
 * and "X-Forwarded-*" headers if found. See class-level docs.
 * @param builder the builder for the base URL; the builder will be cloned
 * and therefore not modified and may be re-used for further calls.
 * @param name the mapping name
 * @return a builder to prepare the URI String
 * @throws IllegalArgumentException if the mapping name is not found or
 * if there is no unique match
 * @since 4.2
 */
public static MethodArgumentBuilder fromMappingName(@Nullable UriComponentsBuilder builder, String name) {
	WebApplicationContext wac = getWebApplicationContext();
	Assert.notNull(wac, "No WebApplicationContext. ");
	Map<String, RequestMappingInfoHandlerMapping> map = wac.getBeansOfType(RequestMappingInfoHandlerMapping.class);
	List<HandlerMethod> handlerMethods = null;
	for (RequestMappingInfoHandlerMapping mapping : map.values()) {
		handlerMethods = mapping.getHandlerMethodsForMappingName(name);
		if (handlerMethods != null) {
			break;
		}
	}
	if (handlerMethods == null) {
		throw new IllegalArgumentException("Mapping not found: " + name);
	}
	else if (handlerMethods.size() != 1) {
		throw new IllegalArgumentException("No unique match for mapping " + name + ": " + handlerMethods);
	}
	else {
		HandlerMethod handlerMethod = handlerMethods.get(0);
		Class<?> controllerType = handlerMethod.getBeanType();
		Method method = handlerMethod.getMethod();
		return new MethodArgumentBuilder(builder, controllerType, method);
	}
}
 
Example 2
Source File: MvcUriComponentsBuilder.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * An alternative to {@link #fromMappingName(String)} that accepts a
 * {@code UriComponentsBuilder} representing the base URL. This is useful
 * when using MvcUriComponentsBuilder outside the context of processing a
 * request or to apply a custom baseUrl not matching the current request.
 * <p><strong>Note:</strong> This method extracts values from "Forwarded"
 * and "X-Forwarded-*" headers if found. See class-level docs.
 * @param builder the builder for the base URL; the builder will be cloned
 * and therefore not modified and may be re-used for further calls.
 * @param name the mapping name
 * @return a builder to prepare the URI String
 * @throws IllegalArgumentException if the mapping name is not found or
 * if there is no unique match
 * @since 4.2
 */
public static MethodArgumentBuilder fromMappingName(@Nullable UriComponentsBuilder builder, String name) {
	WebApplicationContext wac = getWebApplicationContext();
	Assert.notNull(wac, "No WebApplicationContext. ");
	Map<String, RequestMappingInfoHandlerMapping> map = wac.getBeansOfType(RequestMappingInfoHandlerMapping.class);
	List<HandlerMethod> handlerMethods = null;
	for (RequestMappingInfoHandlerMapping mapping : map.values()) {
		handlerMethods = mapping.getHandlerMethodsForMappingName(name);
		if (handlerMethods != null) {
			break;
		}
	}
	if (handlerMethods == null) {
		throw new IllegalArgumentException("Mapping not found: " + name);
	}
	else if (handlerMethods.size() != 1) {
		throw new IllegalArgumentException("No unique match for mapping " + name + ": " + handlerMethods);
	}
	else {
		HandlerMethod handlerMethod = handlerMethods.get(0);
		Class<?> controllerType = handlerMethod.getBeanType();
		Method method = handlerMethod.getMethod();
		return new MethodArgumentBuilder(builder, controllerType, method);
	}
}
 
Example 3
Source File: DispatchController.java    From TrackRay with GNU General Public License v3.0 6 votes vote down vote up
@Deprecated
public void attack(Task task, ExecutorService exec) {
    WebApplicationContext context = getAppContext();
    Map<String, AbstractPOC> beans = context.getBeansOfType(AbstractPOC.class);
    AbstractPlugin simpleVul = (AbstractPlugin) context.getBean("simpleVulRule");
    SysLog.info("开始漏洞检测");

    for (String targeturl : task.getTargets()) {

        simpleVul.setParam(new HashMap<String,Object>(){{put("target",targeturl);put("task",task);}});
        exec.submit(simpleVul);

        for (Map.Entry<String, AbstractPOC> entry : beans.entrySet()) {
            AbstractPOC exp = entry.getValue();
            exp.setTask(task);
            exp.setTarget(targeturl);
            String bean = entry.getKey();
            SysLog.info(bean+" exploit executeing...");
            exec.execute(exp);
        }
    }


}
 
Example 4
Source File: ContextLoaderListener.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent event) {
	String serviceName = event.getServletContext().getInitParameter("appName");
	System.setProperty("serviceName", serviceName == null ? "undefined" : serviceName);
	super.contextInitialized(event);
	WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
	SpringInstanceProvider provider = new SpringInstanceProvider(applicationContext);
	InstanceFactory.loadFinished(provider);
	
	Map<String, ApplicationStartedListener> interfaces = applicationContext.getBeansOfType(ApplicationStartedListener.class);
	if(interfaces != null){
		for (ApplicationStartedListener listener : interfaces.values()) {
			System.out.println(">>>begin to execute listener:"+listener.getClass().getName());
			listener.onApplicationStarted(applicationContext);
			System.out.println("<<<<finish execute listener:"+listener.getClass().getName());
		}
	}
}
 
Example 5
Source File: GrailsDataBinder.java    From AlgoTrader with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Collects all PropertyEditorRegistrars in the application context and
 * calls them to register their custom editors
 *
 * @param registry The PropertyEditorRegistry instance
 */
private static void registerCustomEditors(PropertyEditorRegistry registry) {
	final ServletContext servletContext = ServletContextHolder.getServletContext();
	if (servletContext == null) {
		return;
	}

	WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
	if (context == null) {
		return;
	}

	Map<String, PropertyEditorRegistrar> editors = context.getBeansOfType(PropertyEditorRegistrar.class);
	for (PropertyEditorRegistrar editorRegistrar : editors.values()) {
		editorRegistrar.registerCustomEditors(registry);
	}
}
 
Example 6
Source File: MvcUriComponentsBuilder.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static String getPathPrefix(Class<?> controllerType) {
	WebApplicationContext wac = getWebApplicationContext();
	if (wac != null) {
		Map<String, RequestMappingHandlerMapping> map = wac.getBeansOfType(RequestMappingHandlerMapping.class);
		for (RequestMappingHandlerMapping mapping : map.values()) {
			if (mapping.isHandler(controllerType)) {
				String prefix = mapping.getPathPrefix(controllerType);
				if (prefix != null) {
					return prefix;
				}
			}
		}
	}
	return "";
}
 
Example 7
Source File: MvcUriComponentsBuilder.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static String getPathPrefix(Class<?> controllerType) {
	WebApplicationContext wac = getWebApplicationContext();
	if (wac != null) {
		Map<String, RequestMappingHandlerMapping> map = wac.getBeansOfType(RequestMappingHandlerMapping.class);
		for (RequestMappingHandlerMapping mapping : map.values()) {
			if (mapping.isHandler(controllerType)) {
				String prefix = mapping.getPathPrefix(controllerType);
				if (prefix != null) {
					return prefix;
				}
			}
		}
	}
	return "";
}
 
Example 8
Source File: PluginServiceImpl.java    From TrackRay with GNU General Public License v3.0 5 votes vote down vote up
public Map<String,MVCPlugin> findMVCPlugins(){
    HashMap<String, MVCPlugin> map = new HashMap<>();
    WebApplicationContext context = getContext();
    Map<String, MVCPlugin> bean = context.getBeansOfType(MVCPlugin.class);
    for (Map.Entry<String, MVCPlugin> entry : bean.entrySet()) {
        MVCPlugin plugin =  entry.getValue();
        Rule rule = plugin.currentRule();
        Plugin p = plugin.currentPlugin();
        map.put(p.value(),plugin);
    }
    return map;
}
 
Example 9
Source File: PluginServiceImpl.java    From TrackRay with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 查找普通插件方法
 * @return
 */
@Override
public JSONArray findPlugins(){
    WebApplicationContext context = getContext();
    Map<String, CommonPlugin> bean = context.getBeansOfType(CommonPlugin.class);
    JSONArray arr = new JSONArray();
    for (Map.Entry<String, CommonPlugin> entry : bean.entrySet()) {
        JSONObject obj = new JSONObject();
        AbstractPlugin plugin =  entry.getValue();
        Rule rule = plugin.currentRule();
        Plugin p = plugin.currentPlugin();

        if (p==null || rule == null)
            continue;
        if (plugin instanceof MVCPlugin)
            continue;
        obj.put("plugin_key",entry.getKey());

        JSONObject base = new JSONObject();
        base.put("author",p.author());
        base.put("title",p.title());
        base.put("desc",p.desc());
        base.put("link",p.link());
        base.put("time",p.time());
        obj.put("base_info",base);

        JSONObject rules = new JSONObject();
        rules.put("sync",rule.sync());

        ArrayList<String> ruleparam = new ArrayList<>();
        ArrayList<String> defparam = new ArrayList<>();
        ArrayList<String> descparam = new ArrayList<>();
        Param[] params = rule.params();
        if (!rule.defParam() && params[0].key().equals("RHOST") && params[3].key().equals("SSL")){

        }else{
            for (Param param : params) {
                ruleparam.add(param.key());
                defparam.add(param.defaultValue());
                descparam.add(param.desc());
            }
        }

        rules.put("params",ruleparam);
        rules.put("def_params",defparam);
        rules.put("desc_params",descparam);

        rules.put("enable",rule.enable());
        rules.put("websocket",rule.websocket());
        obj.put("plugin_rule",rules);
        arr.add(obj);
    }
    return arr;
}
 
Example 10
Source File: PluginServiceImpl.java    From TrackRay with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 查找WEBSOCKET插件方法
 * @return
 */
@Override
public JSONArray findWebsocketPlugins(){
    WebApplicationContext context = getContext();
    Map<String, WebSocketPlugin> bean = context.getBeansOfType(WebSocketPlugin.class);
    JSONArray arr = new JSONArray();
    for (Map.Entry<String, WebSocketPlugin> entry : bean.entrySet()) {
        JSONObject obj = new JSONObject();
        AbstractPlugin plugin =  entry.getValue();
        Rule rule = plugin.currentRule();
        Plugin p = plugin.currentPlugin();

        if (p==null || rule == null)
            continue;
        obj.put("plugin_key",entry.getKey());

        JSONObject base = new JSONObject();
        base.put("author",p.author());
        base.put("title",p.title());
        base.put("desc",p.desc());
        base.put("link",p.link());
        base.put("time",p.time());
        obj.put("base_info",base);

        JSONObject rules = new JSONObject();
        rules.put("sync",rule.sync());

        ArrayList<String> ruleparam = new ArrayList<>();
        ArrayList<String> defparam = new ArrayList<>();
        ArrayList<String> descparam = new ArrayList<>();
        Param[] params = rule.params();
        if (!rule.defParam() && params[0].key().equals("RHOST") && params[3].key().equals("SSL")){

        }else{
            for (Param param : params) {
                ruleparam.add(param.key());
                defparam.add(param.defaultValue());
                descparam.add(param.desc());
            }
        }

        rules.put("params",ruleparam);
        rules.put("def_params",defparam);
        rules.put("desc_params",descparam);

        rules.put("enable",rule.enable());
        rules.put("websocket",rule.websocket());
        obj.put("plugin_rule",rules);
        arr.add(obj);
    }
    return arr;
}