private Service getConfiguredService(String serviceName) { ServiceConfigFactory sfact = new ServiceConfigFactory(); Service[] services = sfact.getServices(); for (Service service : services) { if (service.getName().equals(serviceName)) { return service; } } return null; }
/* * Create an Authenticator so that we can provide authentication, if * needed, when go to connect to the URL */ Authenticator createAuthenticatorUsingConfigCredentials() { Service service = getConfiguredService(JMX_HTTP_ADAPTER_NAME); if (service == null) { // Didn't find the service we were looking for LOG.warn("Could not find configured service for '{}'", JMX_HTTP_ADAPTER_NAME); return null; } org.opennms.netmgt.config.service.Attribute[] attribs = service.getAttribute(); if (attribs == null) { // the AuthenticationMethod is not set, so no authentication return null; } boolean usingBasic = false; for (org.opennms.netmgt.config.service.Attribute attrib : attribs) { if (attrib.getName().equals("AuthenticationMethod")) { if (!attrib.getValue().getContent().equals("basic")) { LOG.error( "AuthenticationMethod is \"{}\", but only \"basic\" is supported", attrib.getValue()); return null; } usingBasic = true; break; } } if (!usingBasic) { // AuthenticationMethod is not set to basic, so no authentication return null; } Invoke[] invokes = service.getInvoke(); if (invokes == null) { // No username or password could be set return null; } String username = null; String password = null; for (Invoke invoke : invokes) { if (invoke.getMethod().equals("addAuthorization")) { Argument[] args = invoke.getArgument(); if (args != null && args.length == 2 && args[0].getContent().equals("manager")) { username = args[0].getContent(); password = args[1].getContent(); break; } } } if (username == null || password == null) { // Didn't find a username or password return null; } final String username_f = username; final String password_f = password; return new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username_f, password_f.toCharArray()); } }; }