001: package vqwiki.file;
002:
003: import JSX.ObjIn;
004: import JSX.ObjOut;
005: import org.apache.log4j.Logger;
006: import vqwiki.AbstractWikiMembers;
007: import vqwiki.Environment;
008: import vqwiki.WikiException;
009: import vqwiki.WikiMember;
010:
011: import javax.servlet.http.HttpServletRequest;
012: import java.io.File;
013: import java.io.FileReader;
014: import java.io.FileWriter;
015: import java.io.IOException;
016: import java.util.*;
017:
018: /**
019: * Stores a list of usernames and their registered email addresses so
020: * that users may set notifications and reminders per topic page. Users
021: * must set a canonical username and provide a valid email address.
022: * An email will then be sent to the supplied address with a hyperlink
023: * containing a validation key. The key is then checked against the
024: * list of registered names and confirmed, at which point the user
025: * is allowed to set notifications and reminders. This is a file-based
026: * implementation of the WikiMembers interface.
027: *
028: * @author Robert E Brewer
029: * @version 0.1
030: */
031: public class FileWikiMembers extends AbstractWikiMembers {
032:
033: private static final Logger logger = Logger
034: .getLogger(FileWikiMembers.class);
035: private File memberFile;
036: private Hashtable memberTable;
037:
038: /**
039: * Constructor for the members list.
040: *
041: * @exception java.lang.ClassNotFoundException if hashtable file is the wrong class
042: * @exception java.io.IOException if the members file could not be read
043: */
044: public FileWikiMembers(String virtualWiki)
045: throws ClassNotFoundException, IOException {
046: this .virtualWiki = virtualWiki;
047: this .memberFile = FileHandler.getPathFor(virtualWiki,
048: "member.xml");
049: if (!memberFile.exists()) {
050: File dirFile = new File(Environment.dir());
051: if (!dirFile.exists())
052: dirFile.mkdir();
053: memberTable = new Hashtable();
054: } else {
055: ObjIn in = new ObjIn(new FileReader(memberFile));
056: memberTable = (Hashtable) in.readObject();
057: in.close();
058: }
059: }
060:
061: /**
062: *
063: */
064: public boolean confirmMembership(String username, String key)
065: throws Exception {
066: boolean result = super .confirmMembership(username, key);
067: if (result == true)
068: return true;
069: WikiMember member = findMemberByName(username);
070: // Look up the username and check the keys
071: if (!member.checkKey(key))
072: return false;
073: member.confirm();
074: writeMemberTable();
075: return writeMemberTable();
076: }
077:
078: /**
079: * Finds a WikiMember object in the Member collection using the username.
080: *
081: * @param username the name of the user to find
082: * @return WikiMember the Member object for the specified user
083: */
084: public WikiMember findMemberByName(String username) {
085: WikiMember aMember;
086: if (memberTable.containsKey(username)) {
087: aMember = (WikiMember) memberTable.get(username);
088: } else {
089: aMember = new WikiMember(username);
090: }
091: return aMember;
092: }
093:
094: /**
095: *
096: */
097: public Collection getAllMembers() throws Exception {
098: return memberTable.values();
099: }
100:
101: /**
102: *
103: */
104: public void addMember(String username, String email, String key)
105: throws Exception {
106: WikiMember aMember = new WikiMember(username, email);
107: aMember.setKey(key);
108: memberTable.put(username, aMember);
109: writeMemberTable();
110: }
111:
112: /**
113: * Add a user account to the Members collection. A key will be generated and sent via
114: * email to the specified address in a hyperlink, which the user can then visit
115: * to confirm the membership request.
116: *
117: * @param username the name of the user for whom membership is requested
118: * @param email the email address of the user for whom membership is requested
119: * in the confirmation email. For example, http://www.mybogusdomain.com/vqwiki/jsp/confirm.jsp
120: * @return boolean true if the user account has been added, false if an account already exists for this username or the member file could not be written
121: * @exception vqwiki.WikiException if the mailer could not be instantiated
122: */
123: public synchronized boolean requestMembership(String username,
124: String email, HttpServletRequest request)
125: throws WikiException {
126: WikiMember aMember = createMember(username, email);
127: mailMember(username, request, aMember, email);
128: // Add the request to the members table
129: memberTable.put(username, aMember);
130: return writeMemberTable();
131: }
132:
133: /**
134: * Add a user account to the Members collection. It is assumed that the
135: * confirmation was done somewhere else e.g. in the LDAP directory.
136: *
137: * @param username the name of the user for whom membership is requested
138: * @param email the email address of the user for whom membership is requested
139: * in the confirmation email. For example, http://www.mybogusdomain.com/vqwiki/jsp/confirm.jsp
140: * @exception vqwiki.WikiException if the mailer could not be instantiated
141: */
142: public synchronized boolean createMembershipWithoutRequest(
143: String username, String email) throws WikiException {
144: WikiMember aMember = createMember(username, email);
145: aMember.confirm();
146: // Add the request to the members table
147: memberTable.put(username, aMember);
148: return writeMemberTable();
149: }
150:
151: /**
152: * Removes a WikiMember from the members list.
153: *
154: * @param username the name of the user to remove
155: * @return boolean true if the operation completed successfully, false if the member file could not be written
156: */
157: public synchronized boolean removeMember(String username) {
158: if (!memberTable.containsKey(username))
159: return false;
160: memberTable.remove(username);
161: return writeMemberTable();
162: }
163:
164: /**
165: *
166: */
167: private boolean writeMemberTable() {
168: try {
169: ObjOut out = new ObjOut(new FileWriter(memberFile));
170: out.writeObject(memberTable);
171: return true;
172: } catch (IOException e) {
173: logger.error(e);
174: return false;
175: }
176: }
177: }
|