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);
}
}
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);
}
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();
}
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();
}
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));
}
Aggregations