use of org.jivesoftware.openfire.session.Session in project Openfire by igniterealtime.
the class RemoteServerManager method setPermissionPolicy.
/**
* Sets the permission policy being used for new XMPP entities that are trying to
* connect to the server. There are two types of policies: 1) blacklist: where any entity
* is allowed to connect to the server except for those listed in the black list and
* 2) whitelist: where only the entities listed in the white list are allowed to connect to
* the server.
*
* @param policy the new PermissionPolicy to use.
*/
public static void setPermissionPolicy(PermissionPolicy policy) {
JiveGlobals.setProperty(ConnectionSettings.Server.PERMISSION_SETTINGS, policy.toString());
// Check if the connected servers can remain connected to the server
for (String hostname : SessionManager.getInstance().getIncomingServers()) {
if (!canAccess(hostname)) {
for (Session session : SessionManager.getInstance().getIncomingServerSessions(hostname)) {
Log.debug("Closing session for hostname '{}' as a changed permission policy is taken into effect. Affected session: {}", hostname, session);
session.close();
}
}
}
for (DomainPair domainPair : SessionManager.getInstance().getOutgoingDomainPairs()) {
if (!canAccess(domainPair.getRemote())) {
Session session = SessionManager.getInstance().getOutgoingServerSession(domainPair);
Log.debug("Closing session as a changed permission policy is taken into effect. Affected session: {}", session);
session.close();
// After the session has been close, inform all listeners as well.
ServerSessionEventDispatcher.dispatchEvent(session, ServerSessionEventDispatcher.EventType.session_destroyed);
}
}
}
use of org.jivesoftware.openfire.session.Session in project Openfire by igniterealtime.
the class IQMUCvCardHandler method handleIQ.
@Override
public IQ handleIQ(IQ packet) throws PacketException {
IQ result = IQ.createResultIQ(packet);
IQ.Type type = packet.getType();
if (type.equals(IQ.Type.set)) {
Log.debug("vCard update request received from: '{}', for: '{}'", packet.getFrom(), packet.getTo());
try {
String roomName = packet.getTo().getNode();
// If no TO was specified then return an error.
if (roomName == null) {
Log.debug("vCard update request from: '{}', for: '{}' is invalid: it does not refer to a specific room.", packet.getFrom(), packet.getTo());
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.not_acceptable);
result.getError().setText("Request 'to' attribute has no node-part. The request should be addressed to a room of a MUC service.");
} else {
final Lock lock = mucService.getChatRoomLock(roomName);
lock.lock();
try {
final MUCRoom room = mucService.getChatRoom(roomName);
Log.debug("vCard update request from: '{}', for: '{}' relates to room: {}", packet.getFrom(), packet.getTo(), room);
if (room == null || !room.getOwners().contains(packet.getFrom().asBareJID())) {
Log.debug("vCard update request from: '{}', for: '{}' is invalid: room does not exist, or sender is not allowed to discover the room.", packet.getFrom(), packet.getTo());
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.forbidden);
result.getError().setText("You are not an owner of this room.");
} else {
Element vcard = packet.getChildElement();
if (vcard != null) {
try {
VCardManager.getInstance().setVCard(room.getJID().toString(), vcard);
// This is what EJabberd does. Mimic it, for compatibility.
sendConfigChangeNotification(room);
// Mimic a client that broadcasts a vCard update. Converse seems to need this.
final String hash = calculatePhotoHash(vcard);
sendVCardUpdateNotification(room, hash);
Log.debug("vCard update request from: '{}', for: '{}' processed successfully.", packet.getFrom(), packet.getTo());
} catch (UnsupportedOperationException e) {
Log.debug("Entity '{}' tried to set VCard, but the configured VCard provider is read-only. An IQ error will be returned to sender.", packet.getFrom());
// VCards can include binary data. Let's not echo that back in the error.
// result.setChildElement( packet.getChildElement().createCopy() );
result.setError(PacketError.Condition.not_allowed);
// default to server locale.
Locale locale = JiveGlobals.getLocale();
final Session session = SessionManager.getInstance().getSession(result.getTo());
if (session != null && session.getLanguage() != null) {
// use client locale if one is available.
locale = session.getLanguage();
}
result.getError().setText(LocaleUtils.getLocalizedString("vcard.read_only", locale), locale.getLanguage());
}
}
}
// No need to ensure that other cluster nodes see the changes applied above, as this code does not apply changes.
// mucService.syncChatRoom(room);
} finally {
lock.unlock();
}
}
} catch (UserNotFoundException e) {
// VCards can include binary data. Let's not echo that back in the error.
// result.setChildElement( packet.getChildElement().createCopy() );
result.setError(PacketError.Condition.item_not_found);
} catch (Exception e) {
Log.error(e.getMessage(), e);
result.setError(PacketError.Condition.internal_server_error);
}
} else if (type.equals(IQ.Type.get)) {
Log.debug("vCard retrieve request received from: '{}', for: '{}'", packet.getFrom(), packet.getTo());
String roomName = packet.getTo().getNode();
// If no TO was specified then return an error.
if (roomName == null) {
Log.debug("vCard retrieve request from: '{}', for: '{}' is invalid: it does not refer to a specific room.", packet.getFrom(), packet.getTo());
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.not_acceptable);
result.getError().setText("Request 'to' attribute has no node-part. The request should be addressed to a room of a MUC service.");
} else {
// By default return an empty vCard
result.setChildElement(RESPONSE_ELEMENT_NAME, NAMESPACE);
// Only try to get the vCard values of rooms that can be discovered
// Answer the room occupants as items if that info is publicly available
final Lock lock = mucService.getChatRoomLock(roomName);
lock.lock();
try {
final MUCRoom room = mucService.getChatRoom(roomName);
Log.debug("vCard retrieve request from: '{}', for: '{}' relates to room: {}", packet.getFrom(), packet.getTo(), room);
if (room != null && mucService.canDiscoverRoom(room, packet.getFrom())) {
VCardManager vManager = VCardManager.getInstance();
Element userVCard = vManager.getVCard(room.getJID().toString());
if (userVCard != null) {
// Check if the requester wants to ignore some vCard's fields
Element filter = packet.getChildElement().element(QName.get("filter", "vcard-temp-filter"));
if (filter != null) {
// Create a copy so we don't modify the original vCard
userVCard = userVCard.createCopy();
// Ignore fields requested by the user
for (Iterator<Element> toFilter = filter.elementIterator(); toFilter.hasNext(); ) {
Element field = toFilter.next();
Element fieldToRemove = userVCard.element(field.getName());
if (fieldToRemove != null) {
fieldToRemove.detach();
}
}
}
result.setChildElement(userVCard);
Log.debug("vCard retrieve request from: '{}', for: '{}' processed successfully.", packet.getFrom(), packet.getTo());
}
} else {
Log.debug("vCard retrieve request from: '{}', for: '{}' is invalid: room does not exist, or sender is not allowed to discover the room.", packet.getFrom(), packet.getTo());
result = IQ.createResultIQ(packet);
result.setChildElement(packet.getChildElement().createCopy());
result.setError(PacketError.Condition.item_not_found);
result.getError().setText("Request 'to' references a room that cannot be found (or is not discoverable by you).");
}
// No need to ensure that other cluster nodes see the changes applied above, as this code does not apply changes.
// mucService.syncChatRoom(room);
} finally {
lock.unlock();
}
}
} else {
// Ignore non-request IQs
return null;
}
return result;
}
use of org.jivesoftware.openfire.session.Session in project Openfire by igniterealtime.
the class StatisticsModule method start.
public void start() {
// Retrieve instance of StatisticsManager
statisticsManager = StatisticsManager.getInstance();
// Register a packet listener so that we can track packet traffic.
packetInterceptor = new PacketInterceptor() {
public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed) {
// Only track processed packets so that we don't count them twice.
if (processed) {
packetCount.incrementAndGet();
}
}
};
InterceptorManager.getInstance().addInterceptor(packetInterceptor);
// Register all statistics.
addServerToServerStatistic();
addActiveSessionsStatistic();
addPacketStatistic();
}
use of org.jivesoftware.openfire.session.Session in project Openfire by igniterealtime.
the class TransportInstance method startInstance.
/**
* Starts the transport instance if it's enabled and not already running.
*/
public void startInstance() {
if (!enabled || running) {
return;
}
Log.info("Starting transport service: " + type.toString());
transport = null;
try {
transport = (BaseTransport) Class.forName(nameOfClass).newInstance();
transport.setup(this.type, this.description, sessionRouter);
} catch (ClassNotFoundException e) {
Log.error("Unable to find class: " + nameOfClass);
return;
} catch (InstantiationException e) {
Log.error("Unable to instantiate class: " + nameOfClass);
return;
} catch (IllegalAccessException e) {
Log.error("Unable to access class: " + nameOfClass);
return;
}
// Automatically kill any current s2s connections with the JID we want to use.
SessionManager sessionManager = SessionManager.getInstance();
String fullJID = this.subDomain + "." + XMPPServer.getInstance().getServerInfo().getXMPPDomain();
boolean pause = false;
try {
for (Session sess : sessionManager.getIncomingServerSessions(fullJID)) {
sess.close();
pause = true;
}
} catch (Exception ignored) {
// Session might have disappeared on its own
}
try {
Session sess = sessionManager.getOutgoingServerSession(fullJID);
if (sess != null) {
sess.close();
pause = true;
}
} catch (Exception ignored) {
// Session might have disappeared on its own
}
try {
// Wait one second if we closed something.
if (pause) {
Thread.sleep(1000L);
}
} catch (Exception ignored) {
// Hrm, interrupted? That's odd.
}
try {
componentManager.addComponent(this.subDomain, transport);
PropertyEventDispatcher.addListener(this);
running = true;
} catch (Exception e) {
Log.error("Error while adding component " + this.subDomain + ": ", e);
}
}
use of org.jivesoftware.openfire.session.Session in project Openfire by igniterealtime.
the class IQQueryHandler method handleIQ.
public IQ handleIQ(IQ packet) throws UnauthorizedException {
Session session = sessionManager.getSession(packet.getFrom());
// If no session was found then answer with an error (if possible)
if (session == null) {
Log.error("Error during resource binding. Session not found in " + sessionManager.getPreAuthenticatedKeys() + " for key " + packet.getFrom());
return buildErrorResponse(packet);
}
if (packet.getType().equals(IQ.Type.get)) {
return buildSupportedFieldsResult(packet, session);
}
// Default to user's own archive
JID archiveJid = packet.getTo();
if (archiveJid == null) {
archiveJid = packet.getFrom().asBareJID();
}
Log.debug("Archive requested is {}", archiveJid);
// Now decide the type.
boolean muc = false;
if (!XMPPServer.getInstance().isLocal(archiveJid)) {
Log.debug("Archive is not local (user)");
if (XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(archiveJid) == null) {
Log.debug("No chat service for this domain");
return buildErrorResponse(packet);
} else {
muc = true;
Log.debug("MUC");
}
}
JID requestor = packet.getFrom().asBareJID();
Log.debug("Requestor is {} for muc=={}", requestor, muc);
// Auth checking.
if (muc) {
MultiUserChatService service = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(archiveJid);
MUCRoom room = service.getChatRoom(archiveJid.getNode());
if (room == null) {
return buildErrorResponse(packet);
}
boolean pass = false;
if (service.isSysadmin(requestor)) {
pass = true;
}
MUCRole.Affiliation aff = room.getAffiliation(requestor);
if (aff != MUCRole.Affiliation.outcast) {
if (aff == MUCRole.Affiliation.owner || aff == MUCRole.Affiliation.admin) {
pass = true;
} else if (room.isMembersOnly()) {
if (aff == MUCRole.Affiliation.member) {
pass = true;
}
} else {
pass = true;
}
}
if (!pass) {
return buildForbiddenResponse(packet);
}
} else if (!archiveJid.equals(requestor)) {
// ... disallow unless admin.
if (!XMPPServer.getInstance().getAdmins().contains(requestor)) {
return buildForbiddenResponse(packet);
}
}
sendMidQuery(packet, session);
final QueryRequest queryRequest = new QueryRequest(packet.getChildElement(), archiveJid);
Collection<ArchivedMessage> archivedMessages = retrieveMessages(queryRequest);
for (ArchivedMessage archivedMessage : archivedMessages) {
sendMessageResult(session, queryRequest, archivedMessage);
}
sendEndQuery(packet, session, queryRequest);
return null;
}
Aggregations