Example usage for javax.servlet ServletContext setAttribute

List of usage examples for javax.servlet ServletContext setAttribute

Introduction

In this page you can find the example usage for javax.servlet ServletContext setAttribute.

Prototype

public void setAttribute(String name, Object object);

Source Link

Document

Binds an object to a given attribute name in this ServletContext.

Usage

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Set the set of supported Open-ID providers to use.
 * @param ctx The Servlet-Context/*w  w  w  .  j  a v  a  2s  .c  o  m*/
 * @param providers The providers we accept.
 */
public static void setOpenIdProviders(ServletContext ctx, Set<String> providers) {
    ctx.setAttribute(Constants.OPENID_PROVIDERS, providers);
}

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Sets the WebConfig into the ServletContext.
 *
 * @param context a ServletContext/*from  w w  w .j a  v a2  s  .  c o m*/
 * @param webConfig a WebConfig object
 */
public static void setWebConfig(ServletContext context, WebConfig webConfig) {
    context.setAttribute(Constants.WEBCONFIG, webConfig);
}

From source file:org.red5.net.websocket.WebSocketPlugin.java

/**
 * Returns a new instance of WsServerContainer if one does not already exist.
 * //from  w w w  . j av a 2 s .c o m
 * @param servletContext
 * @return WsServerContainer
 */
public static ServerContainer getWsServerContainerInstance(ServletContext servletContext) {
    String path = servletContext.getContextPath();
    // handle root
    if (path.length() == 0) {
        path = "/";
    }
    log.info("getWsServerContainerInstance: {}", path);
    DefaultWsServerContainer container;
    if (containerMap.containsKey(path)) {
        container = containerMap.get(path);
    } else {
        // instance a server container for WS
        container = new DefaultWsServerContainer(servletContext);
        if (log.isDebugEnabled()) {
            log.debug("Attributes: {} params: {}", Collections.list(servletContext.getAttributeNames()),
                    Collections.list(servletContext.getInitParameterNames()));
        }
        // get a configurator instance
        ServerEndpointConfig.Configurator configurator = (ServerEndpointConfig.Configurator) WebSocketPlugin
                .getWsConfiguratorInstance();
        // check for sub protocols
        log.debug("Checking for subprotocols");
        List<String> subProtocols = new ArrayList<>();
        Optional<Object> subProtocolsAttr = Optional
                .ofNullable(servletContext.getInitParameter("subProtocols"));
        if (subProtocolsAttr.isPresent()) {
            String attr = (String) subProtocolsAttr.get();
            log.debug("Subprotocols: {}", attr);
            if (StringUtils.isNotBlank(attr)) {
                if (attr.contains(",")) {
                    // split them up
                    Stream.of(attr.split(",")).forEach(entry -> {
                        subProtocols.add(entry);
                    });
                } else {
                    subProtocols.add(attr);
                }
            }
        } else {
            // default to allowing any subprotocol
            subProtocols.add("*");
        }
        log.debug("Checking for CORS");
        // check for allowed origins override in this servlet context
        Optional<Object> crossOpt = Optional.ofNullable(servletContext.getAttribute("crossOriginPolicy"));
        if (crossOpt.isPresent() && Boolean.valueOf((String) crossOpt.get())) {
            Optional<String> opt = Optional.ofNullable((String) servletContext.getAttribute("allowedOrigins"));
            if (opt.isPresent()) {
                ((DefaultServerEndpointConfigurator) configurator).setAllowedOrigins(opt.get().split(","));
            }
        }
        log.debug("Checking for endpoint override");
        // check for endpoint override and use default if not configured
        String wsEndpointClass = Optional.ofNullable((String) servletContext.getAttribute("wsEndpointClass"))
                .orElse("org.red5.net.websocket.server.DefaultWebSocketEndpoint");
        try {
            // locate the endpoint class
            Class<?> endpointClass = Class.forName(wsEndpointClass);
            log.debug("startWebSocket - endpointPath: {} endpointClass: {}", path, endpointClass);
            // build an endpoint config
            ServerEndpointConfig serverEndpointConfig = ServerEndpointConfig.Builder.create(endpointClass, path)
                    .configurator(configurator).subprotocols(subProtocols).build();
            // set the endpoint on the server container
            container.addEndpoint(serverEndpointConfig);
        } catch (Throwable t) {
            log.warn("WebSocket endpoint setup exception", t);
        }
        // store container for lookup
        containerMap.put(path, container);
        // add session listener
        servletContext.addListener(new HttpSessionListener() {

            @Override
            public void sessionCreated(HttpSessionEvent se) {
                log.debug("sessionCreated: {}", se.getSession().getId());
                ServletContext sc = se.getSession().getServletContext();
                // Don't trigger WebSocket initialization if a WebSocket Server Container is already present
                if (sc.getAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE) == null) {
                    // grab the container using the servlet context for lookup
                    DefaultWsServerContainer serverContainer = (DefaultWsServerContainer) WebSocketPlugin
                            .getWsServerContainerInstance(sc);
                    // set the container to the context for lookup
                    sc.setAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE, serverContainer);
                }
            }

            @Override
            public void sessionDestroyed(HttpSessionEvent se) {
                log.debug("sessionDestroyed: {}", se);
                container.closeAuthenticatedSession(se.getSession().getId());
            }

        });
    }
    // set the container to the context for lookup
    servletContext.setAttribute(Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE, container);
    return container;
}

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Set the SearchRepository for global (public) objects in the servlet context.
 *
 * @param context the servlet context/*from   www  .  j a v a 2s .  c  om*/
 * @param repo the SearchRepository object
 */
public static void setGlobalSearchRepository(ServletContext context, SearchRepository repo) {
    context.setAttribute(Constants.GLOBAL_SEARCH_REPOSITORY, repo);
}

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Sets the InterMineAPI in the servlet context.
 *
 * @param servletContext the webapp servlet context
 * @param im the InterMineAPI object//from w ww.  j a  va 2 s.com
 */
public static void setInterMineAPI(ServletContext servletContext, InterMineAPI im) {
    servletContext.setAttribute(Constants.INTERMINE_API, im);
}

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Sets the web properties in the session.
 *
 * @param servletContext a ServletContext object
 * @param props a Properties object/*from  w  w  w  .  ja  v a 2  s  . c o m*/
 */
public static void setWebProperties(ServletContext servletContext, Properties props) {
    servletContext.setAttribute(Constants.WEB_PROPERTIES, props);
}

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Set the AutoCompleter into the ServletContext.
 *
 * @param servletContext a ServletContext object
 * @param ac an AutoCompleter object//w  w w  .  j a  v  a  2  s  .  c o  m
 */
public static void setAutoCompleter(ServletContext servletContext, AutoCompleter ac) {
    servletContext.setAttribute(Constants.AUTO_COMPLETER, ac);
}

From source file:org.metawidget.example.struts.addressbook.plugin.AddressBookPlugIn.java

public void init(ActionServlet servlet, ModuleConfig config) {

    ServletContext context = servlet.getServletContext();

    // Application-wide Controllers

    context.setAttribute("contacts", new ContactsController());
    context.setAttribute("communications", new CommunicationsController());

    // Application-wide Converters

    ConvertUtils.register(new EnumConverter(), ContactType.class);
    ConvertUtils.register(new EnumConverter(), Gender.class);
    ConvertUtils.register(new ToStringConverter(), String.class);

    DateConverter converterDate = new DateConverter();
    converterDate.setLocale(Locale.getDefault());
    ConvertUtils.register(converterDate, Date.class);
}

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Sets the aspect categories into the servlet context.
 *
 * @param servletContext the ServletContext
 * @param categories the Set of aspect names
 *//*from   w w  w  .  j  a  va 2 s  .  com*/
public static void setCategories(ServletContext servletContext, Set<String> categories) {
    servletContext.setAttribute(Constants.CATEGORIES, categories);
}

From source file:org.intermine.web.logic.session.SessionMethods.java

/**
 * Sets the Map of aspects.//from  ww  w. j a  va 2s  .  c o  m
 *
 * @param servletContext a ServletContext object
 * @param aspects a Map
 */
public static void setAspects(ServletContext servletContext, Map<String, Aspect> aspects) {
    servletContext.setAttribute(Constants.ASPECTS, aspects);
}