Skip to content

Commit 82a529f

Browse files
committed
Merge pull request #89 from surevine/local-access
Support 'local' access model
2 parents ed7169f + 16fccb6 commit 82a529f

19 files changed

Lines changed: 618 additions & 250 deletions

File tree

src/main/java/org/buddycloud/channelserver/channel/node/configuration/field/AccessModel.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ public class AccessModel extends Field
55
public static final String FIELD_NAME = "pubsub#access_model";
66
public static final String DEFAULT_VALUE = AccessModel.models.OPEN.toString();
77

8+
public static final models open = models.OPEN;
9+
public static final models authorize = models.AUTHORIZE;
10+
public static final models whitelist = models.WHITELIST;
11+
public static final models local = models.LOCAL;
12+
813
public enum models {
9-
OPEN("open"), WHITELIST("whitelist"), AUTHORIZE("authorize");
14+
OPEN("open"), WHITELIST("whitelist"), AUTHORIZE("authorize"), LOCAL("local");
1015
String model = null;
1116
private models(String model) {
1217
this.model = model;
@@ -26,6 +31,7 @@ public boolean isValid()
2631
return (getValue().equals(AccessModel.models.OPEN.toString())
2732
|| getValue().equals(AccessModel.models.WHITELIST.toString())
2833
|| getValue().equals(AccessModel.models.AUTHORIZE.toString())
34+
|| getValue().equals(AccessModel.models.LOCAL.toString())
2935
);
3036
}
3137
}

src/main/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/discoinfo/DiscoInfoGet.java

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.concurrent.BlockingQueue;
66
import org.apache.log4j.Logger;
77
import org.buddycloud.channelserver.channel.ChannelManager;
8+
import org.buddycloud.channelserver.channel.node.configuration.field.AccessModel;
9+
import org.buddycloud.channelserver.db.exception.NodeStoreException;
810
import org.buddycloud.channelserver.packetprocessor.PacketProcessor;
911
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub;
1012
import org.dom4j.Element;
@@ -15,6 +17,7 @@
1517
import org.xmpp.packet.JID;
1618
import org.xmpp.packet.Packet;
1719
import org.xmpp.packet.PacketError;
20+
import org.xmpp.packet.PacketError.Condition;
1821

1922
public class DiscoInfoGet implements PacketProcessor<IQ> {
2023

@@ -27,46 +30,53 @@ public class DiscoInfoGet implements PacketProcessor<IQ> {
2730
private IQ requestIq;
2831
private Element query;
2932
private Map<String, String> conf;
30-
31-
public DiscoInfoGet(BlockingQueue<Packet> outQueue, ChannelManager channelManager) {
33+
34+
public DiscoInfoGet(BlockingQueue<Packet> outQueue,
35+
ChannelManager channelManager) {
3236
this.outQueue = outQueue;
3337
this.channelManager = channelManager;
3438
}
3539

3640
@Override
3741
public void process(IQ reqIQ) throws Exception {
3842

39-
requestIq = reqIQ;
40-
result = IQ.createResultIQ(reqIQ);
41-
Element elm = reqIQ.getChildElement();
42-
node = elm.attributeValue("node");
43-
query = result.setChildElement(ELEMENT_NAME,
43+
requestIq = reqIQ;
44+
result = IQ.createResultIQ(reqIQ);
45+
Element elm = reqIQ.getChildElement();
46+
node = elm.attributeValue("node");
47+
query = result.setChildElement(ELEMENT_NAME,
4448
JabberDiscoInfo.NAMESPACE_URI);
45-
if (false == channelManager.isLocalJID(requestIq.getFrom())) {
46-
result.getElement().addAttribute("remote-server-discover", "false");
47-
}
49+
if (false == channelManager.isLocalJID(requestIq.getFrom())) {
50+
result.getElement().addAttribute("remote-server-discover", "false");
51+
}
4852
if ((node == null) || (true == node.equals(""))) {
4953
sendServerDiscoInfo();
5054
return;
5155
}
5256
if (false == channelManager.isLocalNode(node)
53-
&& (false == channelManager.isCachedNode(node))) {
57+
&& (false == channelManager.isCachedNode(node))) {
5458
logger.info("Node " + node + " is remote and not cached so "
55-
+ "we're going off to get disco#info");
59+
+ "we're going off to get disco#info");
5660
makeRemoteRequest();
57-
return;
61+
return;
5862
}
5963
conf = channelManager.getNodeConf(node);
6064
if (conf.isEmpty()) {
6165
nodeDoesntExistResponse();
6266
return;
6367
}
64-
sendNodeConfigurationInformation();
68+
try {
69+
sendNodeConfigurationInformation();
70+
} catch (NodeStoreException e) {
71+
logger.error(e);
72+
setErrorResponse(PacketError.Type.wait,
73+
PacketError.Condition.internal_server_error);
74+
}
6575
}
6676

67-
private void sendNodeConfigurationInformation() throws InterruptedException {
68-
69-
TreeMap<String, String> sorted_conf = new TreeMap<String, String>();
77+
private void sendNodeConfigurationInformation() throws Exception {
78+
79+
TreeMap<String, String> configuration = new TreeMap<String, String>();
7080

7181
DataForm x = new DataForm(DataForm.Type.result);
7282

@@ -75,25 +85,34 @@ private void sendNodeConfigurationInformation() throws InterruptedException {
7585
formType.setVariable("FORM_TYPE");
7686
formType.addValue("http://jabber.org/protocol/pubsub#meta-data");
7787

78-
sorted_conf.putAll(conf);
79-
for (String key : sorted_conf.keySet()) {
80-
x.addField(key, null, null).addValue(sorted_conf.get(key));
88+
String value;
89+
90+
configuration.putAll(conf);
91+
for (String key : configuration.keySet()) {
92+
value = configuration.get(key);
93+
if ((true == key.equals(AccessModel.FIELD_NAME))
94+
&& (value.equals(AccessModel.local.toString()))
95+
&& (false == channelManager.isLocalJID(requestIq.getFrom()))) {
96+
value = AccessModel.authorize.toString();
97+
}
98+
x.addField(key, null, null).addValue(value);
8199
}
82-
100+
83101
query.addAttribute("node", node);
84102
query.addElement("identity").addAttribute("category", "pubsub")
85103
.addAttribute("type", "leaf");
86104
query.addElement("feature").addAttribute("var",
87105
"http://jabber.org/protocol/pubsub");
88106

89107
query.add(x.getElement());
90-
91-
query.addElement("feature").addAttribute("var", JabberPubsub.NAMESPACE_URI);
108+
109+
query.addElement("feature").addAttribute("var",
110+
JabberPubsub.NAMESPACE_URI);
92111
Element identity = query.addElement("identity");
93112
identity.addAttribute("category", "pubsub");
94-
identity.addAttribute("type", "leaf");
95-
96-
logger.trace("Returning DISCO info for node: " + node);
113+
identity.addAttribute("type", "leaf");
114+
115+
logger.trace("Returning DISCO info for node: " + node);
97116
outQueue.put(result);
98117
}
99118

@@ -106,41 +125,41 @@ private void nodeDoesntExistResponse() throws InterruptedException {
106125
* <item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
107126
* </error> </iq>
108127
*/
128+
setErrorResponse(PacketError.Type.cancel,
129+
PacketError.Condition.item_not_found);
130+
}
109131

132+
private void setErrorResponse(PacketError.Type type, Condition condition)
133+
throws InterruptedException {
110134
result.setChildElement(result.getChildElement().createCopy());
111135
result.setType(Type.error);
112-
PacketError pe = new PacketError(
113-
org.xmpp.packet.PacketError.Condition.item_not_found,
114-
org.xmpp.packet.PacketError.Type.cancel);
136+
PacketError pe = new PacketError(condition, type);
115137
result.setError(pe);
116138
outQueue.put(result);
117139
}
118140

119-
private void sendServerDiscoInfo()
120-
throws InterruptedException {
141+
private void sendServerDiscoInfo() throws InterruptedException {
121142
query.addElement("identity").addAttribute("category", "pubsub")
122143
.addAttribute("type", "channels");
123144

124145
query.addElement("identity").addAttribute("category", "pubsub")
125146
.addAttribute("type", "inbox");
126147

127-
query.addElement("feature").addAttribute("var",
128-
"jabber:iq:register");
148+
query.addElement("feature").addAttribute("var", "jabber:iq:register");
129149

130150
query.addElement("feature").addAttribute("var",
131151
"http://jabber.org/protocol/disco#info");
132-
152+
133153
query.addElement("feature").addAttribute("var",
134154
"http://jabber.org/protocol/disco#items");
135-
136-
query.addElement("feature").addAttribute("var",
137-
"jabber:iq:search");
155+
156+
query.addElement("feature").addAttribute("var", "jabber:iq:search");
138157

139158
outQueue.put(result);
140159
}
141160

142161
private void makeRemoteRequest() throws InterruptedException {
143162
requestIq.setTo(new JID(node.split("/")[2]).getDomain());
144-
outQueue.put(requestIq);
163+
outQueue.put(requestIq);
145164
}
146165
}

src/main/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/pubsub/get/NodeConfigureGet.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import org.apache.log4j.Logger;
77
import org.buddycloud.channelserver.channel.ChannelManager;
8+
import org.buddycloud.channelserver.channel.node.configuration.field.AccessModel;
89
import org.buddycloud.channelserver.db.exception.NodeStoreException;
910
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub;
1011
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubElementProcessorAbstract;
@@ -22,7 +23,10 @@
2223
public class NodeConfigureGet extends PubSubElementProcessorAbstract {
2324
protected String node;
2425

25-
private static final Logger LOGGER = Logger.getLogger(NodeConfigureGet.class);
26+
public static final String NS_CONFIGURE = "http://jabber.org/protocol/pubsub#node_config";
27+
28+
private static final Logger LOGGER = Logger
29+
.getLogger(NodeConfigureGet.class);
2630

2731
public NodeConfigureGet(BlockingQueue<Packet> outQueue,
2832
ChannelManager channelManager) {
@@ -41,17 +45,17 @@ public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
4145
if (null == actor) {
4246
actor = request.getFrom();
4347
}
44-
48+
4549
if (!nodeProvided()) {
4650
outQueue.put(response);
4751
return;
4852
}
49-
53+
5054
if (!channelManager.isLocalNode(node)) {
5155
makeRemoteRequest();
5256
return;
5357
}
54-
58+
5559
try {
5660
if (!nodeExists()) {
5761
outQueue.put(response);
@@ -69,24 +73,35 @@ public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm)
6973

7074
private void getNodeConfiguration() throws Exception {
7175
Map<String, String> nodeConf = channelManager.getNodeConf(node);
72-
76+
7377
DataForm x = new DataForm(DataForm.Type.result);
7478

7579
FormField formType = x.addField();
7680
formType.setType(FormField.Type.hidden);
7781
formType.setVariable("FORM_TYPE");
78-
formType.addValue("http://jabber.org/protocol/pubsub#node_config");
82+
formType.addValue(NS_CONFIGURE);
83+
84+
String value;
7985

8086
for (String key : nodeConf.keySet()) {
81-
x.addField(key, null, null).addValue(nodeConf.get(key));
87+
// If access model is 'local' and its not a local user return
88+
// 'authorize'
89+
value = nodeConf.get(key);
90+
if ((true == key.equals(AccessModel.FIELD_NAME))
91+
&& (value.equals(AccessModel.local.toString()))
92+
&& (false == channelManager.isLocalJID(actor))) {
93+
value = AccessModel.authorize.toString();
94+
}
95+
x.addField(key, null, null).addValue(value);
8296
}
83-
Element pubsub = response.setChildElement(PubSubGet.ELEMENT_NAME, JabberPubsub.NS_PUBSUB_OWNER);
97+
Element pubsub = response.setChildElement(PubSubGet.ELEMENT_NAME,
98+
JabberPubsub.NS_PUBSUB_OWNER);
8499
Element configure = pubsub.addElement("configure");
85100
configure.addAttribute("node", node);
86101
configure.add(x.getElement());
87102
outQueue.put(response);
88103
}
89-
104+
90105
private boolean nodeExists() throws NodeStoreException {
91106
if (channelManager.nodeExists(node)) {
92107
return true;
@@ -113,14 +128,13 @@ private boolean nodeProvided() {
113128
response.setChildElement(error);
114129
return false;
115130
}
116-
131+
117132
private void makeRemoteRequest() throws InterruptedException {
118133
request.setTo(new JID(node.split("/")[2]).getDomain());
119-
Element actor = request.getElement()
120-
.element("pubsub")
121-
.addElement("actor", JabberPubsub.NS_BUDDYCLOUD);
134+
Element actor = request.getElement().element("pubsub")
135+
.addElement("actor", JabberPubsub.NS_BUDDYCLOUD);
122136
actor.addText(request.getFrom().toBareJID());
123-
outQueue.put(request);
137+
outQueue.put(request);
124138
}
125139

126140
public boolean accept(Element elm) {

0 commit comments

Comments
 (0)