📄 clearspacemanager.java
字号:
/** * $RCSfile$ * $Revision$ * $Date$ * * Copyright (C) 2008 Jive Software. All rights reserved. * * This software is published under the terms of the GNU Public License (GPL), * a copy of which is included in this distribution, or a commercial license * agreement with Jive. */package org.jivesoftware.openfire.clearspace;import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.auth.AuthScope;import org.apache.commons.httpclient.methods.*;import org.dom4j.*;import org.dom4j.io.XMPPPacketReader;import org.jivesoftware.openfire.XMPPServer;import org.jivesoftware.openfire.XMPPServerInfo;import org.jivesoftware.openfire.auth.AuthFactory;import org.jivesoftware.openfire.auth.UnauthorizedException;import static org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.GET;import static org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.POST;import org.jivesoftware.openfire.component.ExternalComponentConfiguration;import org.jivesoftware.openfire.component.ExternalComponentManager;import org.jivesoftware.openfire.component.ExternalComponentManagerListener;import org.jivesoftware.openfire.container.BasicModule;import org.jivesoftware.openfire.group.GroupNotFoundException;import org.jivesoftware.openfire.net.MXParser;import org.jivesoftware.openfire.user.UserNotFoundException;import org.jivesoftware.util.*;import org.jivesoftware.util.cache.DefaultCache;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlPullParserFactory;import org.xmpp.packet.JID;import java.io.IOException;import java.lang.reflect.Constructor;import java.net.*;import java.util.*;/** * Centralized administration of Clearspace connections. The {@link #getInstance()} method * should be used to get an instance. The following properties configure this manager: * <p/> * <ul> * <li>clearspace.uri</li> * <li>clearspace.sharedSecret</li> * </ul> * * @author Daniel Henninger */public class ClearspaceManager extends BasicModule implements ExternalComponentManagerListener { private ConfigClearspaceTask configClearspaceTask; /** * Different kind of HTTP request types */ public enum HttpType { /** * Represents an HTTP Get request. And it's equivalent to a SQL SELECTE. */ GET, /** * Represents an HTTP Post request. And it's equivalent to a SQL UPDATE. */ POST, /** * Represents an HTTP Delete request. And it's equivalent to a SQL DELETE. */ DELETE, /** * Represents an HTTP Put requests.And it's equivalent to a SQL CREATE. */ PUT } /** * This is the username of the user that Openfires uses to connect * to Clearspace. It is fixed a well known by Openfire and Clearspace. */ private static final String OPENFIRE_USERNAME = "openfire_SHRJKZCNU53"; private static final String WEBSERVICES_PATH = "rpc/rest/"; protected static final String IM_URL_PREFIX = "imService/"; private static ThreadLocal<XMPPPacketReader> localParser = null; private static XmlPullParserFactory factory = null; static { try { factory = XmlPullParserFactory.newInstance(MXParser.class.getName(), null); factory.setNamespaceAware(true); } catch (XmlPullParserException e) { Log.error("Error creating a parser factory", e); } // Create xmpp parser to keep in each thread localParser = new ThreadLocal<XMPPPacketReader>() { protected XMPPPacketReader initialValue() { XMPPPacketReader parser = new XMPPPacketReader(); factory.setNamespaceAware(true); parser.setXPPFactory(factory); return parser; } }; } // This map is used to transale exceptions from CS to OF private static final Map<String, String> exceptionMap; static { // Add a new exception map from CS to OF and it will be automatically translated. exceptionMap = new HashMap<String, String>(); exceptionMap.put("com.jivesoftware.base.UserNotFoundException", "org.jivesoftware.openfire.user.UserNotFoundException"); exceptionMap.put("com.jivesoftware.base.UserAlreadyExistsException", "org.jivesoftware.openfire.user.UserAlreadyExistsException"); exceptionMap.put("com.jivesoftware.base.GroupNotFoundException", "org.jivesoftware.openfire.group.GroupNotFoundException"); exceptionMap.put("com.jivesoftware.base.GroupAlreadyExistsException", "org.jivesoftware.openfire.group.GroupAlreadyExistsException"); exceptionMap.put("org.acegisecurity.BadCredentialsException", "org.jivesoftware.openfire.auth.UnauthorizedException"); } private static ClearspaceManager instance = new ClearspaceManager(); private Map<String, String> properties; private String uri; private String host; private int port; private String sharedSecret; private Map<String, Long> userIDCache; private Map<String, Long> groupIDCache; /** * Provides singleton access to an instance of the ClearspaceManager class. * * @return an ClearspaceManager instance. */ public static ClearspaceManager getInstance() { return instance; } /** * Constructs a new ClearspaceManager instance. Typically, {@link #getInstance()} should be * called instead of this method. ClearspaceManager instances should only be created directly * for testing purposes. * * @param properties the Map that contains properties used by the Clearspace manager, such as * Clearspace host and shared secret. */ public ClearspaceManager(Map<String, String> properties) { super("Clearspace integration module for testing only"); this.properties = properties; init(); } /** * Constructs a new ClearspaceManager instance. Typically, {@link #getInstance()} should be * called instead of this method. ClearspaceManager instances should only be created directly * for testing purposes. */ public ClearspaceManager() { super("Clearspace integration module"); // Create a special Map implementation to wrap XMLProperties. We only implement // the get, put, and remove operations, since those are the only ones used. Using a Map // makes it easier to perform LdapManager testing. this.properties = new Map<String, String>() { public String get(Object key) { return JiveGlobals.getXMLProperty((String) key); } public String put(String key, String value) { JiveGlobals.setXMLProperty(key, value); // Always return null since XMLProperties doesn't support the normal semantics. return null; } public String remove(Object key) { JiveGlobals.deleteXMLProperty((String) key); // Always return null since XMLProperties doesn't support the normal semantics. return null; } public int size() { return 0; } public boolean isEmpty() { return false; } public boolean containsKey(Object key) { return false; } public boolean containsValue(Object value) { return false; } public void putAll(Map<? extends String, ? extends String> t) { } public void clear() { } public Set<String> keySet() { return null; } public Collection<String> values() { return null; } public Set<Entry<String, String>> entrySet() { return null; } }; init(); } private void init() { this.uri = properties.get("clearspace.uri"); if (uri != null) { if (!this.uri.endsWith("/")) { this.uri = this.uri + "/"; } // Updates the host/port attributes based on the uri updateHostPort(); } sharedSecret = properties.get("clearspace.sharedSecret"); // Creates the cache maps userIDCache = new DefaultCache<String, Long>("clearspace.userid", 1000, JiveConstants.DAY); groupIDCache = new DefaultCache<String, Long>("clearspace.groupid", 1000, JiveConstants.DAY); if (Log.isDebugEnabled()) { StringBuilder buf = new StringBuilder(); buf.append("Created new ClearspaceManager() instance, fields:\n"); buf.append("\t URI: ").append(uri).append("\n"); buf.append("\t sharedSecret: ").append(sharedSecret).append("\n"); Log.debug("ClearspaceManager: " + buf.toString()); } } /** * Updates the host port attributes based on the URI. */ private void updateHostPort() { if (uri != null && !"".equals(uri.trim())) { try { URL url = new URL(uri); host = url.getHost(); port = url.getPort(); } catch (MalformedURLException e) { // this won't happen } } } /** * Check a username/password pair for valid authentication. * * @param username Username to authenticate against. * @param password Password to use for authentication. * @return True or false of the authentication succeeded. */ public Boolean checkAuthentication(String username, String password) { try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -