Search in sources :

Example 61 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project opennms by OpenNMS.

the class NodeLabelChangeServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String nodeIdString = request.getParameter("node");
    String labelType = request.getParameter("labeltype");
    String userLabel = request.getParameter("userlabel");
    if (nodeIdString == null) {
        throw new MissingParameterException("node", new String[] { "node", "labeltype", "userlabel" });
    }
    if (labelType == null) {
        throw new MissingParameterException("labeltype", new String[] { "node", "labeltype", "userlabel" });
    }
    if (userLabel == null) {
        throw new MissingParameterException("userlabel", new String[] { "node", "labeltype", "userlabel" });
    }
    try {
        final int nodeId = WebSecurityUtils.safeParseInt(nodeIdString);
        final OnmsNode node = NetworkElementFactory.getInstance(getServletContext()).getNode(nodeId);
        NodeLabel nodeLabel = BeanUtils.getBean("daoContext", "nodeLabel", NodeLabel.class);
        NodeLabel oldLabel = new NodeLabelDaoImpl(node.getLabel(), node.getLabelSource());
        NodeLabel newLabel = null;
        if (labelType.equals("auto")) {
            newLabel = nodeLabel.computeLabel(nodeId);
        } else if (labelType.equals("user")) {
            newLabel = new NodeLabelDaoImpl(userLabel, NodeLabelSource.USER);
        } else {
            throw new ServletException("Unexpected labeltype value: " + labelType);
        }
        final String newNodeLabel = newLabel.getLabel();
        boolean managedByProvisiond = node.getForeignSource() != null && node.getForeignId() != null;
        if (managedByProvisiond) {
            WebApplicationContext beanFactory = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            final TransactionTemplate transactionTemplate = beanFactory.getBean(TransactionTemplate.class);
            final RequisitionAccessService requisitionService = beanFactory.getBean(RequisitionAccessService.class);
            transactionTemplate.execute(new TransactionCallback<RequisitionNode>() {

                @Override
                public RequisitionNode doInTransaction(TransactionStatus status) {
                    MultivaluedMap<String, String> params = new MultivaluedHashMap<String, String>();
                    params.putSingle("node-label", newNodeLabel);
                    requisitionService.updateNode(node.getForeignSource(), node.getForeignId(), params);
                    return requisitionService.getNode(node.getForeignSource(), node.getForeignId());
                }
            });
        }
        this.sendLabelChangeEvent(nodeId, oldLabel, newLabel);
        if (managedByProvisiond) {
            response.sendRedirect(Util.calculateUrlBase(request, "admin/nodelabelProvisioned.jsp?node=" + nodeIdString + "&foreignSource=" + node.getForeignSource()));
        } else {
            final WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            final TransactionTemplate transactionTemplate = context.getBean(TransactionTemplate.class);
            final NodeLabel newLabelFinal = newLabel;
            transactionTemplate.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    try {
                        nodeLabel.assignLabel(nodeId, newLabelFinal);
                    } catch (SQLException e) {
                        LOG.error("Failed to change label on node with id: {} to: {}", nodeId, newLabelFinal, e);
                        throw Throwables.propagate(e);
                    }
                }
            });
            response.sendRedirect(Util.calculateUrlBase(request, "element/node.jsp?node=" + nodeIdString));
        }
    } catch (SQLException e) {
        throw new ServletException("Database exception", e);
    } catch (Throwable e) {
        throw new ServletException("Exception sending node label change event", e);
    }
}
Also used : RequisitionNode(org.opennms.netmgt.provision.persist.requisition.RequisitionNode) OnmsNode(org.opennms.netmgt.model.OnmsNode) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ServletException(javax.servlet.ServletException) NodeLabel(org.opennms.netmgt.dao.api.NodeLabel) RequisitionAccessService(org.opennms.web.svclayer.api.RequisitionAccessService) NodeLabelDaoImpl(org.opennms.netmgt.dao.hibernate.NodeLabelDaoImpl) MissingParameterException(org.opennms.web.servlet.MissingParameterException) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 62 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project gocd by gocd.

the class ApiSessionFilterIntegrationTest method setUp.

@Before
public void setUp() throws Exception {
    WebApplicationContext wac = mock(WebApplicationContext.class);
    when(wac.getBean("filterChainProxy", javax.servlet.Filter.class)).thenReturn(filterChainProxy);
    when(wac.getBean("backupService")).thenReturn(mock(BackupService.class));
    filterChainProxy.init(new MockFilterConfig());
    servletContext = new MockServletContext();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    proxy = new DelegatingFilterProxy("filterChainProxy", wac);
    proxy.setServletContext(servletContext);
}
Also used : BackupService(com.thoughtworks.go.server.service.BackupService) WebApplicationContext(org.springframework.web.context.WebApplicationContext) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) Before(org.junit.Before)

Example 63 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project gocd by gocd.

the class ConsoleLogSocketServlet method init.

@Override
public void init() throws ServletException {
    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    socketCreator = wac.getBean(ConsoleLogSocketCreator.class);
    securityService = wac.getBean(SecurityService.class);
    super.init();
}
Also used : SecurityService(com.thoughtworks.go.server.service.SecurityService) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 64 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project uPortal by Jasig.

the class PortalApplicationContextLocator method getApplicationContext.

/**
 * If running in a web application the existing {@link WebApplicationContext} will be returned.
 * if not a singleton {@link ApplicationContext} is created if needed and returned. Unless a
 * {@link WebApplicationContext} is specifically needed this method should be used as it will
 * work both when running in and out of a web application
 *
 * @return The {@link ApplicationContext} for the portal.
 */
public static ApplicationContext getApplicationContext() {
    final ServletContext context = servletContext;
    if (context != null) {
        getLogger().debug("Using WebApplicationContext");
        if (applicationContextCreator.isCreated()) {
            final IllegalStateException createException = new IllegalStateException("A portal managed ApplicationContext has already been created but now a ServletContext is available and a WebApplicationContext will be returned. " + "This situation should be resolved by delaying calls to this class until after the web-application has completely initialized.");
            getLogger().error(createException, createException);
            getLogger().error("Stack trace of original ApplicationContext creator", directCreatorThrowable);
            throw createException;
        }
        final WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
        if (webApplicationContext == null) {
            throw new IllegalStateException("ServletContext is available but WebApplicationContextUtils.getWebApplicationContext(ServletContext) returned null. Either the application context failed to load or is not yet done loading.");
        }
        return webApplicationContext;
    }
    return applicationContextCreator.get();
}
Also used : ServletContext(javax.servlet.ServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 65 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project hudson-2.x by hudson.

the class LDAPSecurityRealm method createSecurityComponents.

public SecurityComponents createSecurityComponents() {
    Binding binding = new Binding();
    binding.setVariable("instance", this);
    BeanBuilder builder = new BeanBuilder();
    builder.parse(Hudson.getInstance().servletContext.getResourceAsStream("/WEB-INF/security/LDAPBindSecurityRealm.groovy"), binding);
    WebApplicationContext appContext = builder.createApplicationContext();
    ldapTemplate = new LdapTemplate(findBean(InitialDirContextFactory.class, appContext));
    return new SecurityComponents(findBean(AuthenticationManager.class, appContext), new LDAPUserDetailsService(appContext));
}
Also used : Binding(groovy.lang.Binding) BeanBuilder(hudson.util.spring.BeanBuilder) AuthenticationManager(org.acegisecurity.AuthenticationManager) LdapTemplate(org.acegisecurity.ldap.LdapTemplate) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Aggregations

WebApplicationContext (org.springframework.web.context.WebApplicationContext)103 Test (org.junit.Test)32 ServletContext (javax.servlet.ServletContext)17 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)15 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)13 ApplicationContext (org.springframework.context.ApplicationContext)11 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)11 HashMap (java.util.HashMap)9 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)9 MockServletContext (org.springframework.mock.web.test.MockServletContext)8 ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)7 Filter (javax.servlet.Filter)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)6 Binding (groovy.lang.Binding)5 BeanBuilder (hudson.util.spring.BeanBuilder)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)5 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)4 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)4