📄 clearspacemanager.java
字号:
String path = ClearspaceAuthProvider.URL_PREFIX + "authenticate/" + username + "/" + password; executeRequest(GET, path); return true; } catch (Exception e) { // Nothing to do. } return false; } /** * Tests the web services connection with Clearspace given the manager's current configuration. * * @return True if connection test was successful. */ public Boolean testConnection() { // Test invoking a simple method try { // If there is a problem with the URL or the user/password this service throws an exception String path = IM_URL_PREFIX + "testCredentials"; executeRequest(GET, path); return true; } catch (Exception e) { // It is not ok, return false. } return false; } /** * Returns the Clearspace service URI; e.g. <tt>https://localhost:80/clearspace</tt>. * This value is stored as the Jive Property <tt>clearspace.uri</tt>. * * @return the Clearspace service URI. */ public String getConnectionURI() { return uri; } /** * Sets the URI of the Clearspace service; e.g., <tt>https://localhost:80/clearspace</tt>. * This value is stored as the Jive Property <tt>clearspace.uri</tt>. * * @param uri the Clearspace service URI. */ public void setConnectionURI(String uri) { if (!uri.endsWith("/")) { uri = uri + "/"; } this.uri = uri; properties.put("clearspace.uri", uri); //Updates the host/port attributes updateHostPort(); if (isEnabled()) { startClearspaceConfig(); } } /** * Returns the password, configured in Clearspace, that Openfire will use to authenticate * with Clearspace to perform it's integration. * * @return the password Openfire will use to authenticate with Clearspace. */ public String getSharedSecret() { return sharedSecret; } /** * Sets the shared secret for the Clearspace service we're connecting to. * * @param sharedSecret the password configured in Clearspace to authenticate Openfire. */ public void setSharedSecret(String sharedSecret) { // Set new password for external component ExternalComponentConfiguration configuration = new ExternalComponentConfiguration("clearspace", ExternalComponentConfiguration.Permission.allowed, sharedSecret); try { ExternalComponentManager.allowAccess(configuration); } catch (ModificationNotAllowedException e) { Log.warn("Failed to configure password for Clearspace", e); } // After updating the component information we can update the field, but not before. // If it is done before, OF won't be able to execute the updateSharedsecret webservice // since it would try with the new password. this.sharedSecret = sharedSecret; properties.put("clearspace.sharedSecret", sharedSecret); } /** * Returns true if Clearspace is being used as the backend of Openfire. When * integrated with Clearspace then users and groups will be pulled out from * Clearspace. User authentication will also rely on Clearspace. * * @return true if Clearspace is being used as the backend of Openfire. */ public boolean isEnabled() { return AuthFactory.getAuthProvider() instanceof ClearspaceAuthProvider; } public void start() throws IllegalStateException { super.start(); if (isEnabled()) { // Before starting up service make sure there is a default secret if (ExternalComponentManager.getDefaultSecret() == null || "".equals(ExternalComponentManager.getDefaultSecret())) { try { ExternalComponentManager.setDefaultSecret(StringUtils.randomString(10)); } catch (ModificationNotAllowedException e) { Log.warn("Failed to set a default secret to external component service", e); } } // Make sure that external component service is enabled if (!ExternalComponentManager.isServiceEnabled()) { try { ExternalComponentManager.setServiceEnabled(true); } catch (ModificationNotAllowedException e) { Log.warn("Failed to start external component service", e); } } // Listen for changes to external component settings ExternalComponentManager.addListener(this); // Starts the clearspace configuration task startClearspaceConfig(); } } /** * */ private synchronized void startClearspaceConfig() { // If the task is running, stop it if (configClearspaceTask != null) { configClearspaceTask.cancel(); Log.debug("Stopping previous configuration Clearspace task."); } // Create and schedule a confi task every minute configClearspaceTask = new ConfigClearspaceTask(); // Wait some time to start the task until Openfire has binding address TaskEngine.getInstance().schedule(configClearspaceTask, JiveConstants.SECOND * 10, JiveConstants.MINUTE); Log.debug("Starting configuration Clearspace task in 10 seconds."); } private synchronized void configClearspace() throws UnauthorizedException { Log.debug("Starting Clearspace configuration."); List<String> bindInterfaces = getServerInterfaces(); if (bindInterfaces.size() == 0) { // We aren't up and running enough to tell Clearspace what interfaces to bind to. Log.debug("No bind interfaces found to config Clearspace"); throw new IllegalStateException("There are no binding interfaces."); } try { XMPPServerInfo serverInfo = XMPPServer.getInstance().getServerInfo(); String path = IM_URL_PREFIX + "configureComponent/"; // Creates the XML with the data Document groupDoc = DocumentHelper.createDocument(); Element rootE = groupDoc.addElement("configureComponent"); Element domainE = rootE.addElement("domain"); domainE.setText(serverInfo.getXMPPDomain()); for (String bindInterface : bindInterfaces) { Element hostsE = rootE.addElement("hosts"); hostsE.setText(bindInterface); } Element portE = rootE.addElement("port"); portE.setText(String.valueOf(ExternalComponentManager.getServicePort())); Log.debug("Trying to configure Clearspace with: Domain: " + serverInfo.getXMPPDomain() + ", hosts: " + bindInterfaces.toString() + ", port: " + port); executeRequest(POST, path, rootE.asXML()); //Done, Clearspace was configured correctly, clear the task Log.debug("Clearspace was configured, stopping the task."); TaskEngine.getInstance().cancelScheduledTask(configClearspaceTask); configClearspaceTask = null; } catch (UnauthorizedException ue) { throw ue; } catch (Exception e) { // It is not supported exception, wrap it into an UnsupportedOperationException throw new UnsupportedOperationException("Unexpected error", e); } } private List<String> getServerInterfaces() { List<String> bindInterfaces = new ArrayList<String>(); String interfaceName = JiveGlobals.getXMLProperty("network.interface"); String bindInterface = null; if (interfaceName != null) { if (interfaceName.trim().length() > 0) { bindInterface = interfaceName; } } int adminPort = JiveGlobals.getXMLProperty("adminConsole.port", 9090); int adminSecurePort = JiveGlobals.getXMLProperty("adminConsole.securePort", 9091); if (bindInterface == null) { try { Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); for (NetworkInterface netInterface : Collections.list(nets)) { Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); for (InetAddress address : Collections.list(addresses)) { if ("127.0.0.1".equals(address.getHostAddress())) { continue; } if (address.getHostAddress().startsWith("0.")) { continue; } Socket socket = new Socket(); InetSocketAddress remoteAddress = new InetSocketAddress(address, adminPort > 0 ? adminPort : adminSecurePort); try { socket.connect(remoteAddress); bindInterfaces.add(address.getHostAddress()); break; } catch (IOException e) { // Ignore this address. Let's hope there is more addresses to validate } } } } catch (SocketException e) { // We failed to discover a valid IP address where the admin console is running return null; } } else { bindInterfaces.add(bindInterface); } return bindInterfaces; } private void updateClearspaceSharedSecret(String newSecret) { try { String path = IM_URL_PREFIX + "updateSharedSecret/"; // Creates the XML with the data Document groupDoc = DocumentHelper.createDocument(); Element rootE = groupDoc.addElement("updateSharedSecret"); rootE.addElement("newSecret").setText(newSecret); executeRequest(POST, path, groupDoc.asXML()); } catch (UnauthorizedException ue) { Log.error("Error updating the password of Clearspace", ue); } catch (Exception e) { Log.error("Error updating the password of Clearspace", e); } } public void serviceEnabled(boolean enabled) throws ModificationNotAllowedException { // Do not let admins shutdown the external component service if (!enabled) { throw new ModificationNotAllowedException("Service cannot be disabled when integrated with Clearspace."); } } public void portChanged(int newPort) throws ModificationNotAllowedException { startClearspaceConfig(); } public void defaultSecretChanged(String newSecret) throws ModificationNotAllowedException { // Do nothing }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -