Example usage for javax.servlet ServletContext getAttribute

List of usage examples for javax.servlet ServletContext getAttribute

Introduction

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

Prototype

public Object getAttribute(String name);

Source Link

Document

Returns the servlet container attribute with the given name, or null if there is no attribute by that name.

Usage

From source file:com.boylesoftware.web.AbstractWebApplication.java

/**
 * Get web-application object for the specified servlet context. The method
 * can be used in custom application components that run outside controllers
 * (controllers can get the application object as a controller method
 * argument), such as JSP views.//  w w w .jav a2s .co m
 *
 * @param sc The servlet context.
 *
 * @return The web-application object.
 *
 * @throws UnavailableException If web-application object is not available
 * for the specified servlet context.
 */
public static AbstractWebApplication getApplication(final ServletContext sc) throws UnavailableException {

    final AbstractWebApplication webapp = (AbstractWebApplication) sc.getAttribute(WEBAPP_ATTNAME);
    if (webapp == null)
        throw new UnavailableException("WebApplication listener has not been configured.");

    return webapp;
}

From source file:com.concursive.connect.web.portal.PortletManager.java

public static synchronized String checkRegistryService(ServletContext context, ServletConfig config,
        HttpServletRequest request) throws PortletContainerException {
    // Add the registry service which preloads all of the portlets
    String contextPath = (String) context.getAttribute("PortletContextPath");
    if (contextPath == null) {
        PortletContextManager registryService = PortletContextManager.getManager();
        registryService.register(config);
        contextPath = request.getContextPath();
        if (!StringUtils.hasText(contextPath)) {
            // Pluto corrects for using a "/" as the context path
            contextPath = "/";
        }//from ww w  .j  a  va2  s.  c o  m
        context.setAttribute("PortletContextPath", contextPath);
    }
    return contextPath;
}

From source file:org.apache.struts.validator.Resources.java

/**
 * Retrieve <code>MessageResources</code> for the module and bundle.
 *
 * @param application the servlet context
 * @param request     the servlet request
 * @param bundle      the bundle key/*from  w  w w.  jav  a2 s  .c  o m*/
 */
public static MessageResources getMessageResources(ServletContext application, HttpServletRequest request,
        String bundle) {
    if (bundle == null) {
        bundle = Globals.MESSAGES_KEY;
    }

    MessageResources resources = (MessageResources) request.getAttribute(bundle);

    if (resources == null) {
        ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, application);

        resources = (MessageResources) application.getAttribute(bundle + moduleConfig.getPrefix());
    }

    if (resources == null) {
        resources = (MessageResources) application.getAttribute(bundle);
    }

    if (resources == null) {
        throw new NullPointerException("No message resources found for bundle: " + bundle);
    }

    return resources;
}

From source file:com.ery.estorm.http.HttpServer.java

/**
 * Does the user sending the HttpServletRequest has the administrator ACLs?
 * If it isn't the case, response will be modified to send an error to the
 * user.//from   w  ww. j av a 2  s. c  o  m
 * 
 * @param servletContext
 * @param request
 * @param response
 * @return true if admin-authorized, false otherwise
 * @throws IOException
 */
public static boolean hasAdministratorAccess(ServletContext servletContext, HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    Configuration conf = (Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE);

    return true;
}

From source file:com.jsmartframework.web.manager.WebContext.java

/**
 * Returns the attribute carried on {@link HttpServletRequest}, {@link HttpSession} or {@link ServletContext}
 * instances associated with current request being processed.
 *
 * @param name name of the attribute.//from www .java2 s  .  c  o m
 * @return the {@link Object} mapped by attribute name on the current request.
 */
public static Object getAttribute(String name) {
    if (name != null) {
        HttpServletRequest request = getRequest();
        if (request != null && request.getAttribute(name) != null) {
            return request.getAttribute(name);
        }

        HttpSession session = getSession();
        if (session != null) {
            synchronized (session) {
                if (session.getAttribute(name) != null) {
                    return session.getAttribute(name);
                }
            }
        }

        ServletContext application = getApplication();
        if (application.getAttribute(name) != null) {
            return application.getAttribute(name);
        }
    }
    return null;
}

From source file:org.apache.hadoop.http.HttpServer.java

/**
 * Does the user sending the HttpServletRequest has the administrator ACLs? If
 * it isn't the case, response will be modified to send an error to the user.
 * //from  w  w w. j  a  v  a2  s . c o  m
 * @param servletContext
 * @param request
 * @param response
 * @return true if admin-authorized, false otherwise
 * @throws IOException
 */
public static boolean hasAdministratorAccess(ServletContext servletContext, HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    Configuration conf = (Configuration) servletContext.getAttribute(CONF_CONTEXT_ATTRIBUTE);

    // If there is no authorization, anybody has administrator access.
    if (!conf.getBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, false)) {
        return true;
    }

    String remoteUser = request.getRemoteUser();
    if (remoteUser == null) {
        return true;
    }
    AccessControlList adminsAcl = (AccessControlList) servletContext.getAttribute(ADMINS_ACL);
    UserGroupInformation remoteUserUGI = UserGroupInformation.createRemoteUser(remoteUser);
    if (adminsAcl != null) {
        if (!adminsAcl.isUserAllowed(remoteUserUGI)) {
            response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
                    "User " + remoteUser + " is unauthorized to access this page. "
                            + "AccessControlList for accessing this page : " + adminsAcl.toString());
            return false;
        }
    }
    return true;
}

From source file:com.concursive.connect.web.portal.PortletManager.java

private static synchronized boolean checkServicesRegistry(ServletContext context, ApplicationPrefs prefs) {
    if (!prefs.has("CONCURSIVE_SERVICES.SERVER")) {
        context.setAttribute("isConsumerRegistered", "false");
        return false;
    }/*from  ww w .  ja  v  a  2  s  .com*/
    boolean isConsumerRegistered = StringUtils.isTrue((String) context.getAttribute("isConsumerRegistered"));
    if (!isConsumerRegistered) {
        //load the WSRP producer information
        try {
            String wsrpServer = prefs.get(ApplicationPrefs.CONCURSIVE_SERVICES_SERVER);

            String markupURL = wsrpServer + "/services/WSRPBaseService";
            String serviceDescriptionURL = wsrpServer + "/services/WSRPServiceDescriptionService";
            String registrationURL = wsrpServer + "/services/WSRPRegistrationService";
            String portletManagementURL = wsrpServer + "/services/WSRPPortletManagementService";

            String consumerName = prefs.get(ApplicationPrefs.CONCURSIVE_SERVICES_ID);
            String consumerCode = prefs.get(ApplicationPrefs.CONCURSIVE_SERVICES_KEY);
            String consumerAgent = ApplicationVersion.TITLE + " " + ApplicationVersion.VERSION;

            ProducerImpl producer = new ProducerImpl(PortletManager.CONCURSIVE_WSRP_PRODUCER_ID, markupURL,
                    serviceDescriptionURL);
            //ID and misc info
            producer.setID(PortletManager.CONCURSIVE_WSRP_PRODUCER_ID);
            //WSRP registration interface
            producer.setRegistrationInterfaceEndpoint(registrationURL);
            //WSRP portlet management interface
            producer.setPortletManagementInterfaceEndpoint(portletManagementURL);

            //Registration
            RegistrationData registrationData = new RegistrationData();
            registrationData.setConsumerName(consumerName);
            registrationData.setConsumerAgent(consumerAgent);
            registrationData.setRegistrationProperties(new Property[2]);

            //Send the services key as a registration property; Server will authorize by enforcing a valid key
            Property key = new Property();
            key.setName(ApplicationPrefs.CONCURSIVE_SERVICES_KEY);
            key.setStringValue(consumerCode);

            //Send the services key as a registration property; Server will authorize by enforcing a valid key
            Property sessions = new Property();
            sessions.setName(ApplicationPrefs.CONSUMER_SESSION_SUPPORT);
            sessions.setStringValue("true");

            registrationData.setRegistrationProperties(0, key);
            registrationData.setRegistrationProperties(1, sessions);
            //producer.setRegistrationData(registrationData);

            //Register the consumer with the remote producer
            LOG.info("Registering ConcourseConnect consumer with remote producer <" + wsrpServer + ">: "
                    + producer.getID());
            producer.register(registrationData);

            ProducerRegistry producerRegistry = ProducerRegistryImpl.getInstance();
            producerRegistry.addProducer(producer);

            isConsumerRegistered = true;
            context.setAttribute("isConsumerRegistered", "true");
        } catch (WSRPException e) {
            isConsumerRegistered = false;
            LOG.error("Unable to register the consumer with the remote WSRP producer");
            context.setAttribute("isConsumerRegistered", "false");
        }
    }
    return isConsumerRegistered;
}

From source file:org.apache.hadoop.hdfs.server.namenode.FsckServlet.java

@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Map<String, String[]> pmap = request.getParameterMap();
    ServletContext context = getServletContext();
    NameNode nn = (NameNode) context.getAttribute("name.node");
    Configuration conf = (Configuration) context.getAttribute("name.conf");
    NamenodeFsck fscker = new NamenodeFsck(conf, nn, pmap, response);
    fscker.fsck();/*from  w w w  . java  2  s . c o m*/
}

From source file:net.sourceforge.vulcan.web.struts.plugin.SpringMessageResourcesPlugIn.java

@Override
protected void onInit() throws ServletException {
    final WebApplicationContext wac = getWacInternal();

    final ServletContext context = getServletContextInternal();

    if (context.getAttribute(Globals.MESSAGES_KEY) != null) {
        logger.warn("Overriding default MessageResources");
    }//from   ww  w .j a va 2 s  .c o m

    MessageResources messageResources = new MessageResources(null, null) {
        @Override
        public String getMessage(Locale locale, String key) {
            return wac.getMessage(key, null, locale);
        }
    };

    context.setAttribute(Globals.MESSAGES_KEY, messageResources);
}

From source file:org.jasig.springframework.web.portlet.context.PortletContextLoaderListener.java

@Override
public void contextInitialized(ServletContextEvent sce) {
    final ServletContext servletContext = sce.getServletContext();
    if (servletContext.getAttribute(
            PortletApplicationContextUtils2.ROOT_PORTLET_APPLICATION_CONTEXT_LOADER_ATTRIBUTE) != null) {
        throw new IllegalStateException(
                "Cannot initialize root portlet ContextLoader context because there is already a root PortletContextLoader present - "
                        + "check whether you have multiple PortletContextLoaderListener definitions in your web.xml!");
    }//from w ww .java 2  s .  c  o  m

    //Register the portlet context loader with the servlet context
    contextLoader = new PortletContextLoader(servletContext);
    servletContext.setAttribute(
            PortletApplicationContextUtils2.ROOT_PORTLET_APPLICATION_CONTEXT_LOADER_ATTRIBUTE, contextLoader);
}