Skip to content

Commit 91461e9

Browse files
committed
Refactoring Conf class and creating property configuration for channel default role and access mode. Related to issue #109 (almost finished)
1 parent 91a2211 commit 91461e9

File tree

6 files changed

+168
-200
lines changed

6 files changed

+168
-200
lines changed

configuration.properties.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
2626
# then whether to automatically approve the user.
2727
# NOTE: This will only work on local private channels
2828
channels.autosubscribe.autoapprove=false
29+
30+
# Default channels configuration related to access and permissions
31+
channels.default.accessmodel=open
32+
channels.default.role=follower+post

src/main/java/org/buddycloud/channelserver/Configuration.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class Configuration extends Properties {
2828

2929
public static final String CONFIGURATION_CHANNELS_AUTOSUBSCRIBE = "channels.autosubscribe";
3030
public static final String CONFIGURATION_CHANNELS_AUTOSUBSCRIBE_AUTOAPPROVE = "channels.autosubscribe.autoapprove";
31+
public static final String CONFIGURATION_CHANNELS_DEFAULT_ROLE = "channels.default.role";
32+
public static final String CONFIGURATION_CHANNELS_DEFAULT_ACCESSMODEL = "channels.default.accessmodel";
3133

3234
private static final String CONFIGURATION_FILE = "configuration.properties";
3335
private static Configuration instance = null;
@@ -40,22 +42,18 @@ public class Configuration extends Properties {
4042
private Configuration() {
4143
try {
4244
conf = new Properties();
43-
File f = new File(CONFIGURATION_FILE);
44-
45-
if (f.exists()) {
46-
LOGGER.info("Found " + CONFIGURATION_FILE
47-
+ " in working directory.");
48-
load(new FileInputStream(f));
45+
InputStream confFile = this.getClass().getClassLoader().getResourceAsStream(CONFIGURATION_FILE);
46+
if(confFile != null) {
47+
load(confFile);
48+
LOGGER.info("Loaded " + CONFIGURATION_FILE + " from classpath.");
4949
} else {
50-
// Otherwise attempt to load it from the classpath
51-
LOGGER.info("No "
52-
+ CONFIGURATION_FILE
53-
+ " found in working directory. Attempting to load from classpath.");
54-
load(this.getClass().getClassLoader()
55-
.getResourceAsStream(CONFIGURATION_FILE));
50+
File f = new File(CONFIGURATION_FILE);
51+
load(new FileInputStream(f));
52+
LOGGER.info("Loaded " + CONFIGURATION_FILE + " from working directory.");
5653
}
5754
} catch (Exception e) {
58-
System.out.println(e.getMessage());
55+
LOGGER.error("Could not load " + CONFIGURATION_FILE + "!");
56+
System.out.println(e.getLocalizedMessage());
5957
System.exit(1);
6058
}
6159
}

src/main/java/org/buddycloud/channelserver/channel/Conf.java

Lines changed: 123 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -2,193 +2,139 @@
22

33
import java.util.Date;
44
import java.util.HashMap;
5+
import org.buddycloud.channelserver.Configuration;
56

67
import org.buddycloud.channelserver.pubsub.accessmodel.AccessModels;
78
import org.buddycloud.channelserver.pubsub.affiliation.Affiliations;
89
import org.joda.time.format.DateTimeFormatter;
910
import org.joda.time.format.ISODateTimeFormat;
1011
import org.xmpp.packet.JID;
1112

12-
//TODO! Refactor this!
13-
// Lot's of duplicate code plus other mayhem (like the SimpleDateFormat.
13+
/*
14+
* Most of these are copied from here
15+
* https://github.com/buddycloud/buddycloud-server/blob/master/src/local/operations.coffee#L14
16+
*/
1417
public class Conf {
1518

16-
public static final String TYPE = "pubsub#type";
17-
public static final String TITLE = "pubsub#title";
18-
public static final String DESCRIPTION = "pubsub#description";
19-
public static final String PUBLISH_MODEL = "pubsub#publish_model";
20-
public static final String ACCESS_MODEL = "pubsub#access_model";
21-
public static final String CREATION_DATE = "pubsub#creation_date";
22-
public static final String OWNER = "pubsub#owner";
23-
public static final String DEFAULT_AFFILIATION = "buddycloud#default_affiliation";
24-
public static final String NUM_SUBSCRIBERS = "pubsub#num_subscribers";
25-
public static final String NOTIFY_CONFIG = "pubsub#notify_config";
26-
public static final String CHANNEL_TYPE = "buddycloud#channel_type";
27-
19+
public static final String TYPE = "pubsub#type";
20+
public static final String TITLE = "pubsub#title";
21+
public static final String DESCRIPTION = "pubsub#description";
22+
public static final String PUBLISH_MODEL = "pubsub#publish_model";
23+
public static final String ACCESS_MODEL = "pubsub#access_model";
24+
public static final String CREATION_DATE = "pubsub#creation_date";
25+
public static final String OWNER = "pubsub#owner";
26+
public static final String DEFAULT_AFFILIATION = "buddycloud#default_affiliation";
27+
public static final String NUM_SUBSCRIBERS = "pubsub#num_subscribers";
28+
public static final String NOTIFY_CONFIG = "pubsub#notify_config";
29+
public static final String CHANNEL_TYPE = "buddycloud#channel_type";
2830
private static final String PUBLISHERS = "publishers";
29-
3031
public static final DateTimeFormatter ISO_8601_PARSER = ISODateTimeFormat.dateTimeParser();
3132
public static final DateTimeFormatter ISO_8601_FORMATTER = ISODateTimeFormat.dateTime();
32-
33-
// Most of these are copied from here
34-
// https://github.com/buddycloud/buddycloud-server/blob/master/src/local/operations.coffee#L14
35-
36-
public static String getPostChannelNodename(JID channelJID) {
37-
return "/user/" + channelJID.toBareJID() + "/posts";
38-
}
39-
40-
/**
41-
* Parses a ISO 8601 to a string
42-
*
43-
* @param iso8601Str
44-
* @return
45-
* @throws IllegalArgumentException if the provided string is not ISO 8601
46-
*/
47-
public static Date parseDate(String iso8601Str) throws IllegalArgumentException {
48-
return ISO_8601_PARSER.parseDateTime(iso8601Str).toDate();
49-
}
50-
51-
public static String formatDate(Date date) {
52-
return ISO_8601_FORMATTER.print(date.getTime());
53-
}
54-
55-
public static HashMap<String, String> getDefaultChannelConf(JID channelJID, JID ownerJID) {
56-
HashMap<String, String> conf = new HashMap<String, String>();
57-
58-
conf.put(TYPE, "http://www.w3.org/2005/Atom");
59-
conf.put(TITLE, channelJID.toBareJID() + "'s title");
60-
conf.put(DESCRIPTION, channelJID.toBareJID() + "'s description");
61-
conf.put(PUBLISH_MODEL, PUBLISHERS);
62-
conf.put(ACCESS_MODEL, AccessModels.open.toString());
63-
conf.put(CREATION_DATE, formatDate(new Date()));
64-
conf.put(OWNER, ownerJID.toBareJID());
65-
conf.put(DEFAULT_AFFILIATION, Affiliations.member.toString());
66-
conf.put(NUM_SUBSCRIBERS, "1");
67-
conf.put(NOTIFY_CONFIG, "1");
68-
69-
return conf;
70-
}
71-
72-
public static HashMap<String, String> getDefaultPostChannelConf(JID channelJID) {
73-
HashMap<String, String> conf = new HashMap<String, String>();
74-
75-
conf.put(TYPE, "http://www.w3.org/2005/Atom");
76-
conf.put(TITLE, channelJID.toBareJID() + "'s very own buddycloud channel!");
77-
conf.put(DESCRIPTION, "This channel belongs to " + channelJID.toBareJID() + ". To nobody else!");
78-
conf.put(PUBLISH_MODEL, PUBLISHERS);
79-
conf.put(ACCESS_MODEL, AccessModels.open.toString());
80-
conf.put(CREATION_DATE, formatDate(new Date()));
81-
conf.put(OWNER, channelJID.toBareJID());
82-
conf.put(DEFAULT_AFFILIATION, Affiliations.member.toString());
83-
conf.put(NUM_SUBSCRIBERS, "1");
84-
conf.put(NOTIFY_CONFIG, "1");
85-
conf.put(CHANNEL_TYPE, "personal");
86-
87-
return conf;
88-
}
89-
90-
public static String getStatusChannelNodename(JID channelJID) {
91-
return "/user/" + channelJID.toBareJID() + "/status";
92-
}
93-
94-
public static HashMap<String, String> getDefaultStatusChannelConf(JID channelJID) {
95-
HashMap<String, String> conf = new HashMap<String, String>();
96-
97-
conf.put(TYPE, "http://www.w3.org/2005/Atom");
98-
conf.put(TITLE, channelJID.toBareJID() + "'s very own buddycloud status!");
99-
conf.put(DESCRIPTION, "This is " + channelJID.toBareJID() + "'s mood a.k.a status -channel. Depends how geek you are.");
100-
conf.put(PUBLISH_MODEL, PUBLISHERS);
101-
conf.put(ACCESS_MODEL, AccessModels.open.toString());
102-
conf.put(CREATION_DATE, formatDate(new Date()));
103-
conf.put(OWNER, channelJID.toBareJID());
104-
conf.put(DEFAULT_AFFILIATION, Affiliations.member.toString());
105-
conf.put(NUM_SUBSCRIBERS, "1");
106-
conf.put(NOTIFY_CONFIG, "1");
107-
108-
return conf;
109-
}
110-
111-
public static String getGeoPreviousChannelNodename(JID channelJID) {
112-
return "/user/" + channelJID.toBareJID() + "/geo/previous";
113-
}
114-
115-
public static HashMap<String, String> getDefaultGeoPreviousChannelConf(JID channelJID) {
116-
HashMap<String, String> conf = new HashMap<String, String>();
117-
118-
conf.put(TYPE, "http://www.w3.org/2005/Atom");
119-
conf.put(TITLE, channelJID.toBareJID() + "'s previous location.");
120-
conf.put(DESCRIPTION, "Where " + channelJID.toBareJID() + " has been before.");
121-
conf.put(PUBLISH_MODEL, PUBLISHERS);
122-
conf.put(ACCESS_MODEL, AccessModels.open.toString());
123-
conf.put(CREATION_DATE, formatDate(new Date()));
124-
conf.put(OWNER, channelJID.toBareJID());
125-
conf.put(DEFAULT_AFFILIATION, Affiliations.member.toString());
126-
conf.put(NUM_SUBSCRIBERS, "1");
127-
conf.put(NOTIFY_CONFIG, "1");
128-
129-
return conf;
130-
}
131-
132-
public static String getGeoCurrentChannelNodename(JID channelJID) {
133-
return "/user/" + channelJID.toBareJID() + "/geo/current";
134-
}
135-
136-
public static HashMap<String, String> getDefaultGeoCurrentChannelConf(JID channelJID) {
137-
HashMap<String, String> conf = new HashMap<String, String>();
138-
139-
conf.put(TYPE, "http://www.w3.org/2005/Atom");
140-
conf.put(TITLE, channelJID.toBareJID() + "'s current location.");
141-
conf.put(DESCRIPTION, "Where " + channelJID.toBareJID() + " is now.");
142-
conf.put(PUBLISH_MODEL, PUBLISHERS);
143-
conf.put(ACCESS_MODEL, AccessModels.open.toString());
144-
conf.put(CREATION_DATE, formatDate(new Date()));
145-
conf.put(OWNER, channelJID.toBareJID());
146-
conf.put(DEFAULT_AFFILIATION, Affiliations.member.toString());
147-
conf.put(NUM_SUBSCRIBERS, "1");
148-
conf.put(NOTIFY_CONFIG, "1");
149-
150-
return conf;
151-
}
152-
153-
public static String getGeoNextChannelNodename(JID channelJID) {
154-
return "/user/" + channelJID.toBareJID() + "/geo/next";
155-
}
156-
157-
public static HashMap<String, String> getDefaultGeoNextChannelConf(JID channelJID) {
158-
HashMap<String, String> conf = new HashMap<String, String>();
159-
160-
conf.put(TYPE, "http://www.w3.org/2005/Atom");
161-
conf.put(TITLE, channelJID.toBareJID() + "'s next location.");
162-
conf.put(DESCRIPTION, "Where " + channelJID.toBareJID() + " is going to go.");
163-
conf.put(PUBLISH_MODEL, PUBLISHERS);
164-
conf.put(ACCESS_MODEL, AccessModels.open.toString());
165-
conf.put(CREATION_DATE, formatDate(new Date()));
166-
conf.put(OWNER, channelJID.toBareJID());
167-
conf.put(DEFAULT_AFFILIATION, Affiliations.member.toString());
168-
conf.put(NUM_SUBSCRIBERS, "1");
169-
conf.put(NOTIFY_CONFIG, "1");
170-
171-
return conf;
172-
}
173-
174-
public static String getSubscriptionsChannelNodename(JID channelJID) {
175-
return "/user/" + channelJID.toBareJID() + "/subscriptions";
176-
}
177-
178-
public static HashMap<String, String> getDefaultSubscriptionsChannelConf(JID channelJID) {
179-
HashMap<String, String> conf = new HashMap<String, String>();
180-
181-
conf.put(TYPE, "http://www.w3.org/2005/Atom");
182-
conf.put(TITLE, channelJID.toBareJID() + "'s susbcriptions.");
183-
conf.put(DESCRIPTION, channelJID.toBareJID() + "'s subscriptions. ");
184-
conf.put(PUBLISH_MODEL, PUBLISHERS);
185-
conf.put(ACCESS_MODEL, AccessModels.open.toString());
186-
conf.put(CREATION_DATE, formatDate(new Date()));
187-
conf.put(OWNER, channelJID.toBareJID());
188-
conf.put(DEFAULT_AFFILIATION, Affiliations.member.toString());
189-
conf.put(NUM_SUBSCRIBERS, "1");
190-
conf.put(NOTIFY_CONFIG, "1");
191-
192-
return conf;
193-
}
33+
34+
public static String getPostChannelNodename(JID channelJID) {
35+
return "/user/" + channelJID.toBareJID() + "/posts";
36+
}
37+
38+
/**
39+
* Parses a ISO 8601 to a string
40+
*
41+
* @param iso8601Str
42+
* @return
43+
* @throws IllegalArgumentException if the provided string is not ISO 8601
44+
*/
45+
public static Date parseDate(String iso8601Str) throws IllegalArgumentException {
46+
return ISO_8601_PARSER.parseDateTime(iso8601Str).toDate();
47+
}
48+
49+
public static String formatDate(Date date) {
50+
return ISO_8601_FORMATTER.print(date.getTime());
51+
}
52+
53+
public static HashMap<String, String> getDefaultChannelConf(JID channelJID, JID ownerJID) {
54+
HashMap<String, String> conf = getDefaultConf(channelJID);
55+
conf.put(TITLE, channelJID.toBareJID() + "'s title");
56+
conf.put(DESCRIPTION, channelJID.toBareJID() + "'s description");
57+
conf.put(OWNER, ownerJID.toBareJID());
58+
return conf;
59+
}
60+
61+
public static HashMap<String, String> getDefaultPostChannelConf(JID channelJID) {
62+
HashMap<String, String> conf = getDefaultConf(channelJID);
63+
conf.put(TITLE, channelJID.toBareJID() + "'s very own buddycloud channel!");
64+
conf.put(DESCRIPTION, "This channel belongs to " + channelJID.toBareJID() + ". To nobody else!");
65+
conf.put(CHANNEL_TYPE, "personal");
66+
return conf;
67+
}
68+
69+
public static String getStatusChannelNodename(JID channelJID) {
70+
return "/user/" + channelJID.toBareJID() + "/status";
71+
}
72+
73+
public static HashMap<String, String> getDefaultStatusChannelConf(JID channelJID) {
74+
HashMap<String, String> conf = getDefaultConf(channelJID);
75+
conf.put(TITLE, channelJID.toBareJID() + "'s very own buddycloud status!");
76+
conf.put(DESCRIPTION, "This is " + channelJID.toBareJID() + "'s mood a.k.a status -channel. Depends how geek you are.");
77+
return conf;
78+
}
79+
80+
public static String getGeoPreviousChannelNodename(JID channelJID) {
81+
return "/user/" + channelJID.toBareJID() + "/geo/previous";
82+
}
83+
84+
public static HashMap<String, String> getDefaultGeoPreviousChannelConf(JID channelJID) {
85+
HashMap<String, String> conf = getDefaultConf(channelJID);
86+
conf.put(TITLE, channelJID.toBareJID() + "'s previous location.");
87+
conf.put(DESCRIPTION, "Where " + channelJID.toBareJID() + " has been before.");
88+
return conf;
89+
}
90+
91+
public static String getGeoCurrentChannelNodename(JID channelJID) {
92+
return "/user/" + channelJID.toBareJID() + "/geo/current";
93+
}
94+
95+
public static HashMap<String, String> getDefaultGeoCurrentChannelConf(JID channelJID) {
96+
HashMap<String, String> conf = getDefaultConf(channelJID);
97+
conf.put(TITLE, channelJID.toBareJID() + "'s current location.");
98+
conf.put(DESCRIPTION, "Where " + channelJID.toBareJID() + " is now.");
99+
return conf;
100+
}
101+
102+
public static String getGeoNextChannelNodename(JID channelJID) {
103+
return "/user/" + channelJID.toBareJID() + "/geo/next";
104+
}
105+
106+
public static HashMap<String, String> getDefaultGeoNextChannelConf(JID channelJID) {
107+
HashMap<String, String> conf = getDefaultConf(channelJID);
108+
conf.put(TITLE, channelJID.toBareJID() + "'s next location.");
109+
conf.put(DESCRIPTION, "Where " + channelJID.toBareJID() + " is going to go.");
110+
return conf;
111+
}
112+
113+
public static String getSubscriptionsChannelNodename(JID channelJID) {
114+
return "/user/" + channelJID.toBareJID() + "/subscriptions";
115+
}
116+
117+
public static HashMap<String, String> getDefaultSubscriptionsChannelConf(JID channelJID) {
118+
HashMap<String, String> conf = getDefaultConf(channelJID);
119+
conf.put(TITLE, channelJID.toBareJID() + "'s susbcriptions.");
120+
conf.put(DESCRIPTION, channelJID.toBareJID() + "'s subscriptions. ");
121+
return conf;
122+
}
123+
124+
private static HashMap<String, String> getDefaultConf(JID channelJID) {
125+
HashMap<String, String> conf = new HashMap<String, String>();
126+
Configuration projectConf = Configuration.getInstance();
127+
conf.put(TYPE, "http://www.w3.org/2005/Atom");
128+
conf.put(PUBLISH_MODEL, PUBLISHERS);
129+
conf.put(CREATION_DATE, formatDate(new Date()));
130+
conf.put(OWNER, channelJID.toBareJID());
131+
conf.put(ACCESS_MODEL, AccessModels.createFromString(projectConf.getProperty(
132+
Configuration.CONFIGURATION_CHANNELS_DEFAULT_ACCESSMODEL)).toString());
133+
conf.put(DEFAULT_AFFILIATION, Affiliations.createFromBuddycloudString(
134+
projectConf.getProperty(Configuration.CONFIGURATION_CHANNELS_DEFAULT_ROLE))
135+
.toString());
136+
conf.put(NUM_SUBSCRIBERS, "1");
137+
conf.put(NOTIFY_CONFIG, "1");
138+
return conf;
139+
}
194140
}

src/main/java/org/buddycloud/channelserver/pubsub/affiliation/Affiliations.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,25 @@ public static Affiliations createFromString(String asString) {
2121
}
2222
return none;
2323
}
24-
24+
25+
public static Affiliations createFromBuddycloudString(String bcString) {
26+
27+
if ("producer".equals(bcString)) {
28+
return owner;
29+
} else if ("follower+post".equals(bcString)) {
30+
return publisher;
31+
} else if ("moderator".equals(bcString)) {
32+
return moderator;
33+
} else if ("follower".equals(bcString)) {
34+
return member;
35+
} else if ("none".equals(bcString)) {
36+
return none;
37+
} else if ("banned".equals(bcString)) {
38+
return outcast;
39+
}
40+
return none;
41+
}
42+
2543
public boolean in(Affiliations... affiliations) {
2644
for (Affiliations a : affiliations) {
2745
if (a.equals(this)) return true;

0 commit comments

Comments
 (0)