📄 clearspacemanager.java
字号:
public void permissionPolicyChanged(ExternalComponentManager.PermissionPolicy newPolicy) throws ModificationNotAllowedException { // Do nothing } public void componentAllowed(String subdomain, ExternalComponentConfiguration configuration) throws ModificationNotAllowedException { if (subdomain.startsWith("clearspace")) { updateClearspaceSharedSecret(configuration.getSecret()); } } public void componentBlocked(String subdomain) throws ModificationNotAllowedException { if (subdomain.startsWith("clearspace")) { throw new ModificationNotAllowedException("Communication with Clearspace cannot be blocked."); } } public void componentSecretUpdated(String subdomain, String newSecret) throws ModificationNotAllowedException { if (subdomain.startsWith("clearspace")) { updateClearspaceSharedSecret(newSecret); } } public void componentConfigurationDeleted(String subdomain) throws ModificationNotAllowedException { // Do not let admins delete configuration of Clearspace component if (subdomain.startsWith("clearspace")) { throw new ModificationNotAllowedException("Use 'Profile Settings' to change password."); } } /** * Makes a rest request of either type GET or DELETE at the specified urlSuffix. * <p/> * urlSuffix should be of the form /userService/users * * @param type Must be GET or DELETE * @param urlSuffix The url suffix of the rest request * @return The response as a xml doc. * @throws ConnectException Thrown if there are issues perfoming the request. * @throws Exception Thrown if the response from Clearspace contains an exception. */ public Element executeRequest(HttpType type, String urlSuffix) throws ConnectException, Exception { assert (type == HttpType.GET || type == HttpType.DELETE); return executeRequest(type, urlSuffix, null); } public Element executeRequest(HttpType type, String urlSuffix, String xmlParams) throws ConnectException, Exception { if (Log.isDebugEnabled()) { Log.debug("Outgoing REST call [" + type + "] to " + urlSuffix + ": " + xmlParams); } String wsUrl = getConnectionURI() + WEBSERVICES_PATH + urlSuffix; String secret = getSharedSecret(); HttpClient client = new HttpClient(); HttpMethod method; // Configures the authentication client.getParams().setAuthenticationPreemptive(true); Credentials credentials = new UsernamePasswordCredentials(OPENFIRE_USERNAME, secret); AuthScope scope = new AuthScope(host, port, AuthScope.ANY_REALM); client.getState().setCredentials(scope, credentials); // Creates the method switch (type) { case GET: method = new GetMethod(wsUrl); break; case POST: PostMethod pm = new PostMethod(wsUrl); StringRequestEntity requestEntity = new StringRequestEntity(xmlParams); pm.setRequestEntity(requestEntity); method = pm; break; case PUT: PutMethod pm1 = new PutMethod(wsUrl); StringRequestEntity requestEntity1 = new StringRequestEntity(xmlParams); pm1.setRequestEntity(requestEntity1); method = pm1; break; case DELETE: method = new DeleteMethod(wsUrl); break; default: throw new IllegalArgumentException(); } method.setRequestHeader("Accept", "text/xml"); method.setDoAuthentication(true); try { // Executes the request client.executeMethod(method); // Parses the result String body = method.getResponseBodyAsString(); if (Log.isDebugEnabled()) { Log.debug("Outgoing REST call results: " + body); } // Checks the http status if (method.getStatusCode() != 200) { throw new ConnectException("Error connecting to Clearspace, http status code: " + method.getStatusCode()); } Element response = localParser.get().parseDocument(body).getRootElement(); // Check for exceptions checkFault(response); // Since there is no exception, returns the response return response; } catch (DocumentException e) { throw new ConnectException("Error parsing the response of Clearspace.", e); } catch (HttpException e) { throw new ConnectException("Error peforming http request to Clearspace", e); } catch (IOException e) { throw new ConnectException("Error peforming http request to Clearspace.", e); } finally { method.releaseConnection(); } } private void checkFault(Element response) throws Exception { Node node = response.selectSingleNode("ns1:faultstring"); if (node != null) { String exceptionText = node.getText(); // Text accepted samples: // 'java.lang.Exception: Exception message' // 'java.lang.Exception' // Get the exception class and message if any int index = exceptionText.indexOf(":"); String className; String message; // If there is no message, save the class only if (index == -1) { className = exceptionText; message = null; } else { // Else save both className = exceptionText.substring(0, index); message = exceptionText.substring(index + 2); } // Map the exception to a Openfire one, if possible if (exceptionMap.containsKey(className)) { className = exceptionMap.get(className); } //Tries to create an instance with the message Exception exception; try { Class exceptionClass = Class.forName(className); if (message == null) { exception = (Exception) exceptionClass.newInstance(); } else { Constructor constructor = exceptionClass.getConstructor(String.class); exception = (Exception) constructor.newInstance(message); } } catch (Exception e) { // failed to create an specific exception, creating a standard one. exception = new Exception(exceptionText); } throw exception; } } /** * Returns the Clearspace user id the user by username. * * @param username Username to retrieve ID of. * @return The ID number of the user in Clearspace. * @throws org.jivesoftware.openfire.user.UserNotFoundException * If the user was not found. */ protected long getUserID(String username) throws UserNotFoundException { // Gets the part before of @ of the username param if (username.contains("@")) { // User's id are only for local users if (!XMPPServer.getInstance().isLocal(new JID(username))) { throw new UserNotFoundException("Cannot load user of remote server: " + username); } username = username.substring(0, username.lastIndexOf("@")); } // Checks if it is in the cache if (userIDCache.containsKey(username)) { return userIDCache.get(username); } // Gets the user's ID from Clearspace try { String path = ClearspaceUserProvider.USER_URL_PREFIX + "users/" + username; Element element = executeRequest(org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.GET, path); Long id = Long.valueOf(WSUtils.getElementText(element.selectSingleNode("return"), "ID")); userIDCache.put(username, id); return id; } catch (UserNotFoundException unfe) { // It is a supported exception, throw it again throw unfe; } catch (Exception e) { // It is not a supported exception, wrap it into a UserNotFoundException throw new UserNotFoundException("Unexpected error", e); } } /** * Returns the Clearspace user id the user by JID. * * @param user JID of user to retrieve ID of. * @return The ID number of the user in Clearspace. * @throws org.jivesoftware.openfire.user.UserNotFoundException * If the user was not found. */ protected long getUserID(JID user) throws UserNotFoundException { // User's id are only for local users XMPPServer server = XMPPServer.getInstance(); if (!server.isLocal(user)) { throw new UserNotFoundException("Cannot load user of remote server: " + user.toString()); } String username = JID.unescapeNode(user.getNode()); return getUserID(username); } /** * Returns the Clearspace group id of the group. * * @param groupname Name of the group to retrieve ID of. * @return The ID number of the group in Clearspace. * @throws org.jivesoftware.openfire.group.GroupNotFoundException * If the group was not found. */ protected long getGroupID(String groupname) throws GroupNotFoundException { if (groupIDCache.containsKey(groupname)) { return groupIDCache.get(groupname); } try { String path = ClearspaceGroupProvider.URL_PREFIX + "groups/" + groupname; Element element = executeRequest(org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.GET, path); Long id = Long.valueOf(WSUtils.getElementText(element.selectSingleNode("return"), "ID")); // Saves it into the cache groupIDCache.put(groupname, id); return id; } catch (GroupNotFoundException gnfe) { // It is a supported exception, throw it again throw gnfe; } catch (Exception e) { // It is not a supported exception, wrap it into a GroupNotFoundException throw new GroupNotFoundException("Unexpected error", e); } } private class ConfigClearspaceTask extends TimerTask { public void run() { try { Log.debug("Trying to configure Clearspace."); configClearspace(); } catch (UnauthorizedException e) { Log.warn("Unauthorization problem trying to configure Clearspace, trying again in 1 minute", e); // TODO: Mark that there is an authorization problem } catch (Exception e) { Log.warn("Unknown problem trying to configure Clearspace, trying again in 1 minute", e); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -