Commit dc8736f3 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Refactoring work.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@594 b35dd754-fafc-0310-a699-88a17e54d16e
parent 7ec010be
......@@ -67,17 +67,14 @@ public class OfflineMessageStrategy extends BasicModule {
public void storeOffline(Message message) throws UnauthorizedException, UserNotFoundException {
if (message != null) {
Session senderSession = null;
try {
senderSession = sessionManager.getSession(message.getFrom());
}
catch (SessionNotFoundException e) {
Log.error(e);
Session senderSession = sessionManager.getSession(message.getFrom());
if (senderSession == null) {
return;
}
JID sender = senderSession.getAddress();
// server messages and anonymous messages can be silently dropped
if (sender == null || sender.getNode() == null || senderSession == null) {
if (sender == null || sender.getNode() == null) {
// silently drop the server message
}
else {
......@@ -127,7 +124,7 @@ public class OfflineMessageStrategy extends BasicModule {
private void bounce(Message message) {
// Generate a rejection response to the sender
try {
Message response = packetFactory.getMessage();
Message response = new Message();
response.setTo(message.getFrom());
response.setFrom(xmppServer.createJID(null, null));
response.setBody("Message could not be delivered to " + message.getTo() + ". User is offline or unreachable.");
......@@ -136,7 +133,8 @@ public class OfflineMessageStrategy extends BasicModule {
session.getConnection().deliver(response);
Message errorResponse = message.createCopy();
errorResponse.setError(new PacketError(PacketError.Type.continue_processing, PacketError.Condition.item_not_found));
errorResponse.setError(new PacketError(PacketError.Condition.item_not_found,
PacketError.Type.continue_processing));
session.getConnection().deliver(errorResponse);
}
catch (Exception e) {
......
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger;
import org.xmpp.packet.Message;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Presence;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
/**
* A factory to produce packets in one of the three XMPP flavors:
* iq, presence, or message.
*
* @author Iain Shigeoka
*/
public interface PacketFactory {
/**
* Create an empty message.
*
* @return an empty message packet.
*/
Message getMessage();
/**
* Create a message parsed from the given stream.
*
* @param xpp the stream reader.
* @return a message produced from the reader.
* @throws XMLStreamException if there was trouble reading the stream.
*/
Message getMessage(XMLStreamReader xpp) throws XMLStreamException;
/**
* Create a message with the given body text.
*
* @param msgText the message body text.
* @return a message with body text.
* @throws XMLStreamException if there was trouble reading the stream.
*/
Message getMessage(String msgText) throws XMLStreamException;
/**
* Create an empty iq packet.
*
* @return an empty iq packet.
*/
IQ getIQ();
/**
* Create an IQ packet from the given stream.
*
* @param xpp the stream to read the iq packet from.
* @return the iq packet created.
* @throws XMLStreamException if there was trouble reading the stream.
*/
IQ getIQ(XMLStreamReader xpp) throws XMLStreamException;
/**
* Create an empty presence packet.
*
* @return an empty presence packet.
*/
Presence getPresence();
/**
* Create a presence packet from the given stream.
*
* @param xpp the stream to read the presence packet from.
* @return the packet created.
* @throws XMLStreamException if there was trouble reading the stream.
*/
Presence getPresence(XMLStreamReader xpp) throws XMLStreamException;
}
\ No newline at end of file
......@@ -15,6 +15,8 @@ import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.AuthToken;
import org.jivesoftware.messenger.auth.Permissions;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.xmpp.packet.Packet;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.xml.stream.XMLStreamException;
......@@ -116,7 +118,7 @@ public class ConnectionProxy implements Connection {
}
}
public void deliver(XMPPPacket packet) throws UnauthorizedException,
public void deliver(Packet packet) throws UnauthorizedException,
PacketException, XMLStreamException {
//if (permissions.hasPermission(Permissions.SYSTEM_ADMIN
// | Permissions.USER_ADMIN)) {
......
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.XMPPFragment;
import org.jivesoftware.messenger.auth.AuthToken;
import org.jivesoftware.messenger.auth.Permissions;
import java.util.Iterator;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
public class FragmentProxy implements XMPPFragment {
protected XMPPFragment fragment;
protected AuthToken authToken;
protected Permissions permissions;
public FragmentProxy(XMPPFragment fragment, AuthToken authToken, Permissions permissions) {
this.fragment = fragment;
this.authToken = authToken;
this.permissions = permissions;
}
public String getNamespace() {
return fragment.getNamespace();
}
public void setNamespace(String namespace) {
fragment.setNamespace(namespace);
}
public String getName() {
return fragment.getName();
}
public void setName(String name) {
fragment.setName(name);
}
public void send(XMLStreamWriter xmlSerializer, int version) throws
XMLStreamException {
fragment.send(xmlSerializer, version);
}
public XMPPFragment createDeepCopy() {
return fragment.createDeepCopy();
}
public void addFragment(XMPPFragment fragment) {
fragment.addFragment(fragment);
}
public Iterator getFragments() {
return fragment.getFragments();
}
public XMPPFragment getFragment(String name, String namespace) {
return fragment.getFragment(name, namespace);
}
public void clearFragments() {
fragment.clearFragments();
}
public int getSize() {
return fragment.getSize();
}
}
......@@ -22,6 +22,10 @@ import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.messenger.handler.IQHandler;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
import org.dom4j.Element;
/**
* Generic presence routing base class.
......@@ -47,9 +51,9 @@ public class IQRouterImpl extends BasicModule implements IQRouter {
if (packet == null) {
throw new NullPointerException();
}
if (packet.getOriginatingSession() == null
|| packet.getOriginatingSession().getStatus() == Session.STATUS_AUTHENTICATED
|| (isLocalServer(packet.getRecipient())
Session session = SessionManager.getInstance().getSession(packet.getFrom());
if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED
|| (isLocalServer(packet.getTo())
&& ("jabber:iq:auth".equals(packet.getChildNamespace())
|| "jabber:iq:register".equals(packet.getChildNamespace())))
) {
......@@ -68,20 +72,22 @@ public class IQRouterImpl extends BasicModule implements IQRouter {
}
}
private boolean isLocalServer(XMPPAddress recipientJID) {
return recipientJID == null || recipientJID.getHost() == null
|| "".equals(recipientJID.getHost()) || recipientJID.getResource() == null
private boolean isLocalServer(JID recipientJID) {
return recipientJID == null || recipientJID.getDomain() == null
|| "".equals(recipientJID.getDomain()) || recipientJID.getResource() == null
|| "".equals(recipientJID.getResource());
}
private void handle(IQ packet) {
XMPPAddress recipientJID = packet.getRecipient();
JID recipientJID = packet.getTo();
try {
if (isLocalServer(recipientJID)) {
String namespace = packet.getChildNamespace();
Element childElement = packet.getChildElement();
String namespace = null;
if (childElement != null) {
childElement.getNamespaceURI();
}
if (namespace == null) {
// Do nothing. We can't handle queries outside of a valid namespace
Log.warn("Unknown packet " + packet);
......@@ -90,8 +96,8 @@ public class IQRouterImpl extends BasicModule implements IQRouter {
IQHandler handler = getHandler(namespace);
if (handler == null) {
// Answer an error if JID is of the form <domain>
if (recipientJID.getName() == null || "".equals(recipientJID.getName())) {
packet.setError(XMPPError.Code.NOT_IMPLEMENTED);
if (recipientJID.getNode() == null || "".equals(recipientJID.getNode())) {
packet.setError(PacketError.Condition.feature_not_implemented);
}
else {
// JID is of the form <node@domain>
......@@ -109,7 +115,7 @@ public class IQRouterImpl extends BasicModule implements IQRouter {
// do nothing
}
// Answer an error since the server can't handle packets sent to a node
packet.setError(XMPPError.Code.SERVICE_UNAVAILABLE);
packet.setError(PacketError.Condition.service_unavailable);
}
Session session = packet.getOriginatingSession();
if (session != null) {
......
......@@ -16,6 +16,9 @@ import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.util.Log;
import org.xmpp.packet.Message;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
/**
* Generic message routing base class.
......@@ -38,9 +41,11 @@ public class MessageRouterImpl extends BasicModule implements MessageRouter {
if (packet == null) {
throw new NullPointerException();
}
if (packet.getOriginatingSession() == null
|| packet.getOriginatingSession().getStatus() == Session.STATUS_AUTHENTICATED) {
XMPPAddress recipientJID = packet.getRecipient();
Session session = SessionManager.getInstance().getSession(packet.getFrom());
if (session == null
|| session.getStatus() == Session.STATUS_AUTHENTICATED)
{
JID recipientJID = packet.getTo();
try {
routingTable.getBestRoute(recipientJID).process(packet);
......@@ -57,11 +62,11 @@ public class MessageRouterImpl extends BasicModule implements MessageRouter {
}
else {
packet.setRecipient(packet.getOriginatingSession().getAddress());
packet.setSender(null);
packet.setError(XMPPError.Code.UNAUTHORIZED);
packet.setTo(session.getAddress());
packet.setFrom((JID)null);
packet.setError(PacketError.Condition.not_authorized);
try {
packet.getOriginatingSession().process(packet);
session.process(packet);
}
catch (UnauthorizedException ue) {
Log.error(ue);
......
......@@ -16,6 +16,8 @@ import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.net.SocketPacketWriteHandler;
import org.xmpp.packet.Packet;
import javax.xml.stream.XMLStreamException;
/**
......@@ -37,7 +39,7 @@ public class PacketDelivererImpl extends BasicModule implements PacketDeliverer
super("Packet Delivery");
}
public void deliver(XMPPPacket packet) throws UnauthorizedException, PacketException, XMLStreamException {
public void deliver(Packet packet) throws UnauthorizedException, PacketException, XMLStreamException {
if (packet == null) {
throw new PacketException("Packet was null");
}
......
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.util.XPPReader;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.user.UserManager;
import org.jivesoftware.messenger.user.IQRoster;
import java.io.StringReader;
//import java.util.HashMap;
//import java.util.Iterator;
import java.util.LinkedList;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
public class PacketFactoryImpl extends BasicModule implements PacketFactory {
public LinkedList iqHandlers = new LinkedList();
public UserManager userManager;
private XMLInputFactory xppFactory;
public PacketFactoryImpl() {
super("Packet factory");
xppFactory = XMLInputFactory.newInstance();
xppFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
}
public Message getMessage() {
return new MessageImpl();
}
public Message getMessage(XMLStreamReader xpp) throws XMLStreamException {
Message msg = new MessageImpl();
parse(msg, xpp);
return msg;
}
public Message getMessage(String msgText) throws XMLStreamException {
Message msg = null;
XMLStreamReader xpp = null;
xpp = xppFactory.createXMLStreamReader(new StringReader(msgText));
xpp.next(); // move parser to start tag
msg = new MessageImpl();
parse(msg, xpp);
return msg;
}
public IQ getIQ() {
return new IQImpl();
}
public IQ getIQ(XMLStreamReader xpp) throws XMLStreamException {
IQ iq = null;
try {
Document doc = XPPReader.parseDocument(xpp);
Element query = doc.getRootElement().element("query");
if (query != null && "jabber:iq:roster".equals(query.getNamespaceURI())) {
iq = new IQRoster();
((IQImpl)iq).parse(doc);
}
else {
iq = new IQImpl();
((IQImpl)iq).parse(doc);
}
}
catch (DocumentException e) {
throw new XMLStreamException(e.getMessage());
}
return iq;
}
public Presence getPresence() {
return new PresenceImpl();
}
public Presence getPresence(XMLStreamReader xpp) throws XMLStreamException {
Presence presence = new PresenceImpl();
parse(presence, xpp);
return presence;
}
private void parse(XMPPPacket packet, XMLStreamReader xpp) throws
XMLStreamException {
packet.parse(xpp);
}
protected TrackInfo getTrackInfo() {
TrackInfo trackInfo = new TrackInfo();
trackInfo.getTrackerClasses().put(UserManager.class, "userManager");
return trackInfo;
}
}
\ No newline at end of file
......@@ -14,6 +14,10 @@ package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.container.BasicModule;
import org.jivesoftware.messenger.container.TrackInfo;
import org.jivesoftware.messenger.*;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Message;
import org.xmpp.packet.Presence;
import org.xmpp.packet.IQ;
/**
* Generic packet routing base class.
......@@ -49,7 +53,7 @@ public class PacketRouterImpl extends BasicModule implements PacketRouter {
* @param packet The packet to route
* @throws NullPointerException If the packet is null or the packet could not be routed
*/
public void route(XMPPPacket packet) {
public void route(Packet packet) {
if(hasRouted(packet)){
return;
}
......@@ -86,12 +90,12 @@ public class PacketRouterImpl extends BasicModule implements PacketRouter {
}
}
public boolean hasRouted(XMPPPacket packet){
if(packet.getRecipient() == null){
public boolean hasRouted(Packet packet){
if(packet.getTo() == null){
return false;
}
// Check for registered components
Component component = componentManager.getComponent(packet.getRecipient().toBareStringPrep());
Component component = componentManager.getComponent(packet.getTo().toBareJID());
if(component != null){
component.processPacket(packet);
return true;
......
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.AuthToken;
import org.jivesoftware.messenger.auth.Permissions;
/**
* Standard security proxy.
*
* @author Iain Shigeoka
*/
public class PacketRouterProxy implements PacketRouter {
PacketRouter router;
private org.jivesoftware.messenger.auth.AuthToken authToken;
private Permissions permissions;
public PacketRouterProxy(PacketRouter router, AuthToken authToken, Permissions permissions) {
this.router = router;
this.authToken = authToken;
this.permissions = permissions;
}
public void route(XMPPPacket packet) {
router.route(packet);
}
public void route(IQ packet) {
router.route(packet);
}
public void route(Message packet) {
router.route(packet);
}
public void route(Presence packet) {
router.route(packet);
}
}
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.Presence;
import org.jivesoftware.messenger.PresenceManager;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.auth.AuthToken;
import org.jivesoftware.messenger.auth.Permissions;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.User;
import java.util.Collection;
/**
* Enforces security constraints.
*
* @author Iain Shigeoka
*/
public class PresenceManagerProxy implements PresenceManager {
private PresenceManager manager;
private AuthToken authToken;
private Permissions permissions;
public PresenceManagerProxy(PresenceManager manager, AuthToken authToken,
Permissions permissions) {
this.manager = manager;
this.authToken = authToken;
this.permissions = permissions;
}
public boolean isAvailable(User user) {
return manager.isAvailable(user);
}
public Presence getPresence(User user) {
return manager.getPresence(user);
}
public Collection<Presence> getPresences(User user) {
return manager.getPresences(user);
}
public int getOnlineGuestCount() {
return manager.getOnlineGuestCount();
}
public Collection<User> getOnlineUsers() {
return manager.getOnlineUsers();
}
public Collection<User> getOnlineUsers(boolean ascending, int sortField) {
return manager.getOnlineUsers(ascending, sortField);
}
public Collection<User> getOnlineUsers(boolean ascending, int sortField, int numResults) {
return manager.getOnlineUsers(ascending, sortField, numResults);
}
public Presence createPresence(User user, String uid) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
return manager.createPresence(user, uid);
}
else {
throw new UnauthorizedException();
}
}
public void setOffline(Presence presence) throws UnauthorizedException {
if (presence.getUsername().equals(authToken.getUsername()) ||
permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
manager.setOffline(presence);
}
else {
throw new UnauthorizedException();
}
}
public void setOffline(XMPPAddress jid) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
manager.setOffline(jid);
}
else {
throw new UnauthorizedException();
}
}
public void probePresence(String prober, XMPPAddress probee) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
manager.probePresence(prober, probee);
}
else {
throw new UnauthorizedException();
}
}
public void probePresence(XMPPAddress prober, XMPPAddress probee) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
manager.probePresence(prober, probee);
}
else {
throw new UnauthorizedException();
}
}
}
......@@ -19,6 +19,9 @@ import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.handler.PresenceSubscribeHandler;
import org.jivesoftware.messenger.handler.PresenceUpdateHandler;
import org.xmpp.packet.Presence;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
/**
* Generic presence routing base class.
......@@ -43,16 +46,16 @@ public class PresenceRouterImpl extends BasicModule implements PresenceRouter {
if (packet == null) {
throw new NullPointerException();
}
if (packet.getOriginatingSession() == null
|| packet.getOriginatingSession().getStatus() == Session.STATUS_AUTHENTICATED) {
Session session = SessionManager.getInstance().getSession(packet.getFrom());
if (session == null || session.getStatus() == Session.STATUS_AUTHENTICATED) {
handle(packet);
}
else {
packet.setRecipient(packet.getOriginatingSession().getAddress());
packet.setSender(null);
packet.setError(XMPPError.Code.UNAUTHORIZED);
packet.setTo(session.getAddress());
packet.setFrom((JID)null);
packet.setError(PacketError.Condition.not_authorized);
try {
packet.getOriginatingSession().process(packet);
session.process(packet);
}
catch (UnauthorizedException ue) {
Log.error(ue);
......@@ -61,21 +64,16 @@ public class PresenceRouterImpl extends BasicModule implements PresenceRouter {
}
private void handle(Presence packet) {
XMPPAddress recipientJID = packet.getRecipient();
JID recipientJID = packet.getTo();
try {
XMPPPacket.Type type = packet.getType();
Presence.Type type = packet.getType();
// Presence updates (null is 'available')
if (type == null
|| Presence.UNAVAILABLE == type
|| Presence.INVISIBLE == type
|| Presence.AVAILABLE == type) {
// ridiculously long check for local server target
if (type == null || Presence.Type.unavailable == type) {
// check for local server target
if (recipientJID == null
|| recipientJID.getHost() == null
|| "".equals(recipientJID.getHost())
|| (recipientJID.getName() == null && recipientJID.getResource() == null)) {
|| recipientJID.getDomain() == null
|| "".equals(recipientJID.getDomain())
|| (recipientJID.getNode() == null && recipientJID.getResource() == null)) {
updateHandler.process(packet);
}
......@@ -88,11 +86,11 @@ public class PresenceRouterImpl extends BasicModule implements PresenceRouter {
}
}
else if (Presence.SUBSCRIBE == type // presence subscriptions
|| Presence.UNSUBSCRIBE == type
|| Presence.SUBSCRIBED == type
|| Presence.UNSUBSCRIBED == type) {
else if (Presence.Type.subscribe == type // presence subscriptions
|| Presence.Type.unsubscribe == type
|| Presence.Type.subscribed == type
|| Presence.Type.unsubscribed == type)
{
subscribeHandler.process(packet);
}
else {
......@@ -107,7 +105,7 @@ public class PresenceRouterImpl extends BasicModule implements PresenceRouter {
catch (Exception e) {
Log.error(LocaleUtils.getLocalizedString("admin.error.routing"), e);
try {
Session session = packet.getOriginatingSession();
Session session = SessionManager.getInstance().getSession(packet.getFrom());
if (session != null) {
Connection conn = session.getConnection();
if (conn != null) {
......
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.auth.AuthToken;
import org.jivesoftware.messenger.auth.Permissions;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.RosterItem;
import java.util.List;
/**
* Standard security proxy
*
* @author Iain Shigeoka
*/
public class RosterItemProxy implements RosterItem {
private RosterItem item;
private AuthToken authToken;
private Permissions permissions;
public RosterItemProxy(RosterItem item, AuthToken authToken, Permissions permissions) {
this.authToken = authToken;
this.permissions = permissions;
this.item = item;
}
public SubType getSubStatus() {
return item.getSubStatus();
}
public void setSubStatus(SubType subStatus) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
item.setSubStatus(subStatus);
}
else {
throw new org.jivesoftware.messenger.auth.UnauthorizedException();
}
}
public AskType getAskStatus() {
return item.getAskStatus();
}
public void setAskStatus(AskType askStatus) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
item.setAskStatus(askStatus);
}
else {
throw new UnauthorizedException();
}
}
public RecvType getRecvStatus() {
return item.getRecvStatus();
}
public void setRecvStatus(RecvType recvStatus) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
item.setRecvStatus(recvStatus);
}
else {
throw new UnauthorizedException();
}
}
public XMPPAddress getJid() {
return item.getJid();
}
public String getNickname() {
return item.getNickname();
}
public void setNickname(String nickname) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
item.setNickname(nickname);
}
else {
throw new UnauthorizedException();
}
}
public List getGroups() {
return item.getGroups();
}
public void setGroups(List groups) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
item.setGroups(groups);
}
else {
throw new UnauthorizedException();
}
}
}
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.auth.AuthToken;
import org.jivesoftware.messenger.auth.Permissions;
import org.jivesoftware.messenger.auth.UnauthorizedException;
import org.jivesoftware.messenger.user.Roster;
import org.jivesoftware.messenger.user.RosterItem;
import org.jivesoftware.messenger.user.UserAlreadyExistsException;
import org.jivesoftware.messenger.user.UserNotFoundException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Protection proxy for the Roster interface. It restricts access of certain
* methods to those that have the proper permissions to administer this object.
*
* @author Iain Shigeoka
*/
public class RosterProxy implements Roster {
protected Roster roster;
private AuthToken authToken;
private Permissions permissions;
public RosterProxy(Roster roster, AuthToken authToken, Permissions permissions) {
this.authToken = authToken;
this.permissions = permissions;
this.roster = roster;
}
public boolean isRosterItem(XMPPAddress user) {
return roster.isRosterItem(user);
}
public Iterator getRosterItems() throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
Iterator iter = roster.getRosterItems();
ArrayList items = new ArrayList();
while (iter.hasNext()) {
RosterItem item = (RosterItem)iter.next();
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
// check for ability to view online status generically
// rosteritem user .isAuthorized(Permissions.VIEW_ONLINE_STATUS)) {
if (!(item instanceof RosterItemProxy)) {
item = new RosterItemProxy(item, authToken, permissions);
}
items.add(item);
}
}
return items.iterator();
}
else {
throw new UnauthorizedException();
}
}
public int getTotalRosterItemCount() throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
return roster.getTotalRosterItemCount();
}
else {
throw new UnauthorizedException();
}
}
public RosterItem getRosterItem(XMPPAddress user) throws UnauthorizedException, UserNotFoundException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
RosterItem item = roster.getRosterItem(user);
if (!(item instanceof RosterItemProxy)) {
item = new RosterItemProxy(item, authToken, permissions);
}
return item;
}
else {
throw new UnauthorizedException();
}
}
public RosterItem createRosterItem(XMPPAddress user) throws UnauthorizedException, UserAlreadyExistsException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
return roster.createRosterItem(user);
}
else {
throw new UnauthorizedException();
}
}
public RosterItem createRosterItem(XMPPAddress user, String nickname, List groups) throws UnauthorizedException, UserAlreadyExistsException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
return roster.createRosterItem(user, nickname, groups);
}
else {
throw new UnauthorizedException();
}
}
public RosterItem createRosterItem(RosterItem item) throws UnauthorizedException, UserAlreadyExistsException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
return roster.createRosterItem(item);
}
else {
throw new UnauthorizedException();
}
}
public void updateRosterItem(RosterItem item) throws UnauthorizedException, UserNotFoundException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
roster.updateRosterItem(item);
}
else {
throw new UnauthorizedException();
}
}
public RosterItem deleteRosterItem(XMPPAddress user) throws UnauthorizedException {
if (permissions.hasPermission(Permissions.SYSTEM_ADMIN | Permissions.USER_ADMIN)) {
RosterItem item = roster.deleteRosterItem(user);
if (!(item instanceof RosterItemProxy)) {
item = new RosterItemProxy(item, authToken, permissions);
}
return item;
}
else {
throw new UnauthorizedException();
}
}
}
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.Message;
import org.jivesoftware.messenger.XMPPPacket;
/**
* <p>Implements a generic message packet without dealing with the underlying
* fragment storage.</p>
*
* @author Iain Shigeoka
*/
abstract public class XMPPAbstractMessagePacket implements Message {
/**
* <p>Implement the valid message types.</p>
*
* @param type The type of message or null for the default (normal) type
* @return The packet type
*/
public XMPPPacket.Type typeFromString(String type) {
if (CHAT.toString().equals(type)) {
return CHAT;
}
else if (GROUP_CHAT.toString().equals(type)) {
return GROUP_CHAT;
}
else if (HEADLINE.toString().equals(type)) {
return HEADLINE;
}
else if (ERROR.toString().equals(type)) {
return ERROR;
}
else {
return NORMAL;
}
}
}
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.Session;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.XMPPError;
import org.jivesoftware.messenger.XMPPPacket;
/**
* <p>Implementation of the common packet functionality to ease implementation of packet
* descendants.</p>
* <p>The abstract packet tracks the packet's known routing information but has no
* fragment representation.</p>
*
* @author Iain Shigeoka
*/
abstract public class XMPPAbstractPacket implements XMPPPacket {
/**
* <p>The packet's sender.</p>
*/
protected XMPPAddress sender;
/**
* <p>The packet's recipient.</p>
*/
protected XMPPAddress recipient;
/**
* <p>The packet's originating session.</p>
*/
protected Session session;
/**
* <p>The packet's type.</p>
*/
protected int packetType;
/**
* <p>The name of the server this packet originated on.</p>
*/
protected String serverName;
/**
* The error of the packet or null if no error set.
*/
protected XMPPError error;
/**
* <p>Create a packet with appropriate routing information.</p>
*
* @param sender The packet's sender
* @param recipient The packet's recipient
* @param session The packet's originating session
* @param packetType The type of packet
*/
public XMPPAbstractPacket(XMPPAddress sender, XMPPAddress recipient, Session session, int packetType) {
this.sender = sender;
this.recipient = recipient;
this.session = session;
this.packetType = packetType;
if (session != null) {
serverName = session.getServerName();
}
}
public int getPacketType() {
return packetType;
}
public XMPPAddress getRecipient() {
return recipient;
}
public XMPPAddress getSender() {
return sender;
}
public Session getOriginatingSession() {
return session;
}
public XMPPError getError() {
return error;
}
public XMPPPacket.Type typeFromString(String type) {
return ERROR;
}
}
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2004 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.
*/
package org.jivesoftware.messenger.spi;
import org.jivesoftware.messenger.container.Container;
import org.jivesoftware.messenger.Session;
import org.jivesoftware.messenger.XMPPAddress;
import org.jivesoftware.messenger.XMPPServer;
import org.jivesoftware.messenger.XMPPServerInfo;
import org.jivesoftware.messenger.auth.AuthToken;
import org.jivesoftware.messenger.auth.Permissions;
import org.jivesoftware.messenger.auth.UnauthorizedException;
/**
* Standard security wrapper for the XMPPServer class. This class also
* allows a simple mechanism for wrapping all server resources using
* a single security and resource control context without having to
* impose proxies for each resource.
*
* @author Iain Shigeoka
*/
public class XMPPServerProxy implements XMPPServer {
private XMPPServer server;
private AuthToken authToken;
private Permissions permissions;
/**
* <p>Create a new server proxy.</p>
*
* @param server The server to proxy
* @param auth The auth for the server
* @param permission The permissions for the server
*/
public XMPPServerProxy(XMPPServer server, AuthToken auth, Permissions permission) {
this.server = server;
this.authToken = auth;
this.permissions = permission;
}
public XMPPServerInfo getServerInfo() {
return server.getServerInfo();
}
public boolean isLocal(XMPPAddress jid) {
return server.isLocal(jid);
}
public XMPPAddress createJID(String username, String resource) {
return server.createJID(username, resource);
}
public Session getSession() throws UnauthorizedException {
return server.getSession();
}
public String getName() {
return server.getName();
}
public void initialize(Container container) {
server.initialize(container);
}
public void start() {
server.start();
}
public void stop() {
server.stop();
}
public void destroy() {
server.destroy();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment