001: package vqwiki.servlets;
002:
003: import org.apache.log4j.Logger;
004:
005: import vqwiki.PseudoTopicHandler;
006: import vqwiki.WikiBase;
007: import vqwiki.WikiMember;
008: import vqwiki.WikiMembers;
009: import vqwiki.users.Usergroup;
010: import vqwiki.utils.Utilities;
011:
012: import javax.servlet.ServletException;
013: import javax.servlet.http.Cookie;
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016: import java.io.IOException;
017:
018: /**
019: * @author garethc
020: * Date: Jan 8, 2003
021: */
022: public class MemberServlet extends VQWikiServlet {
023:
024: private static final Logger logger = Logger
025: .getLogger(MemberServlet.class);
026:
027: /**
028: *
029: */
030: protected void doGet(HttpServletRequest request,
031: HttpServletResponse response) throws ServletException,
032: IOException {
033: String user = null;
034: if (request.getParameter("userName") != null) {
035: if (!"".equals(request.getParameter("userName"))) {
036: user = request.getParameter("userName");
037: logger
038: .debug("retrieved username by parameter: "
039: + user);
040: }
041: }
042: if (user == null) {
043: user = Utilities.getUserFromRequest(request);
044: logger.debug("retrieved user from cookie: " + user);
045: }
046: logger.debug("setting user to: " + user);
047: request.setAttribute("title", "Wiki Membership");
048: request.setAttribute("user", user);
049: String virtualWiki = (String) request
050: .getAttribute("virtualWiki");
051: WikiMembers members = null;
052: Usergroup usergroup = null;
053: try {
054: members = WikiBase.getInstance().getWikiMembersInstance(
055: virtualWiki);
056: usergroup = WikiBase.getInstance().getUsergroupInstance();
057: } catch (Exception e) {
058: error(request, response, e);
059: return;
060: }
061: String email = request.getParameter("email");
062: String key = request.getParameter("key");
063: WikiMember member = null;
064: try {
065: if (members != null) {
066: member = members.findMemberByName(user);
067: }
068: } catch (Exception e) {
069: logger.warn("finding user name", e);
070: }
071: if (member == null) {
072: request.setAttribute("type", "newMember");
073: } else if (member.isPending()) {
074: request.setAttribute("type", "pendingMember");
075: } else if (member.isConfirmed()) {
076: request.setAttribute("type", "confirmedMember");
077: } else {
078: request.setAttribute("type", "newMember");
079: }
080: logger.debug("Set type to " + request.getAttribute("type"));
081: if (email != null) {
082: // request for membership - mail the user a key for confirmation
083: try {
084: if (usergroup.isEmailValidated()) {
085: logger.debug("confirming membership");
086: members.createMembershipWithoutRequest(user, email);
087: if (Utilities.getUserFromRequest(request) == null) {
088: // resend the username cookie
089: Cookie cookie = Utilities
090: .createUsernameCookie(user);
091: response.addCookie(cookie);
092: }
093: request.setAttribute("type", "confirmation");
094: request.setAttribute("valid", new Boolean(true));
095: } else {
096: logger.debug("requesting membership");
097: members.requestMembership(user, email, request);
098: request.setAttribute("type", "membershipRequested");
099: }
100: } catch (Exception e) {
101: error(request, response, e);
102: return;
103: }
104: } else if (key != null) {
105: // request for confirmation, check that key is valid
106: boolean isValid = false;
107: try {
108: logger.debug("confirming membership");
109: isValid = members.confirmMembership(user, key);
110: if (isValid) {
111: if (Utilities.getUserFromRequest(request) == null) {
112: // resend the username cookie
113: Cookie cookie = Utilities
114: .createUsernameCookie(user);
115: response.addCookie(cookie);
116: }
117: }
118: request.setAttribute("type", "confirmation");
119: } catch (Exception e) {
120: error(request, response, e);
121: return;
122: }
123: request.setAttribute("valid", new Boolean(isValid));
124: } else {
125: if (user == null) {
126: // force user to create a username first
127: logger.debug("user is null, username needs to be set");
128: request.setAttribute("userList", usergroup
129: .getListOfAllUsers());
130: dispatch(PseudoTopicHandler.getInstance()
131: .getRedirectURL("SetUsername"), request,
132: response);
133: return;
134: }
135: }
136: request.setAttribute("knownEmail", usergroup
137: .getKnownEmailById(user));
138: dispatch("/jsp/member.jsp", request, response);
139: }
140:
141: /**
142: *
143: */
144: protected void doPost(HttpServletRequest httpServletRequest,
145: HttpServletResponse httpServletResponse)
146: throws ServletException, IOException {
147: this.doGet(httpServletRequest, httpServletResponse);
148: }
149: }
|