Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix formatting and a couple warnings.

  • Loading branch information...
commit 1439266ade321f95af1098f64dce476af5f32027 1 parent 3839d3a
@GraylinKim GraylinKim authored
View
126 BBSignup/src/main/java/bbsignup/src/Controller.java
@@ -18,19 +18,23 @@
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
-import bbsignup.model.*;
+import bbsignup.model.Senator;
+import bbsignup.model.User;
+import bbsignup.model.UserAuth;
+import bbsignup.src.PMF;
+import bbsignup.src.Resource;
public class Controller {
-
+
public String WEBLINK = "http://billbuzz.nysenate.gov/";
-
+
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String in = "";
-
+
System.out.print("> ");
while(!(in = br.readLine()).equals("exit")) {
-
+
if(in.startsWith("add user")) {
Pattern p = Pattern.compile("add user (.+?) (.+?) (.+?) (true|false)$");
Matcher m = p.matcher(in);
@@ -60,15 +64,15 @@ else if(in.startsWith("delete user")) {
else if(in.startsWith("set other")) {
PersistenceManager pm = PMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
-
+
try {
tx.begin();
-
+
List<User> users = (List<User>)PMF.getObjects(pm, User.class);
for(User user:users) {
user.setOtherData(true);
}
-
+
tx.commit();
}
finally {
@@ -81,32 +85,32 @@ else if(in.startsWith("set other")) {
System.out.print("> ");
}
}
-
+
public Controller() {
}
-
+
@SuppressWarnings("unchecked")
public List<Senator> getSenators() {
return (List<Senator>) PMF.getDetachedObjects(Senator.class);
-
+
}
-
+
public String tryDelete(String email) {
-
+
User u = (User) PMF.getDetachedObject(User.class, "email", email);
-
+
if(u != null) {
UserAuth ua = this.getUserAuth(email);
if(ua == null) {
ua = new UserAuth(email);
}
-
+
String message = "Hello!<br/><br/>To remove yourself from BillBuzz please click the following link:";
-
+
message += "<br/><br/>" + WEBLINK + "delete.jsp?email=" + email + "&key=" + ua.getHash();
-
+
message += "<br/><br/>If you have any questions please contact us at <a href=\"mailto:billbuzz@nysenate.gov\">billbuzz@nysenate.gov</a> or reply to this email.";
-
+
try {
sendMail(email,"Finalize your BillBuzz cancellation", message, "billbuzz@nysenate.gov", "BillBuzz");
PMF.persistObject(ua);
@@ -114,66 +118,66 @@ public String tryDelete(String email) {
e.printStackTrace();
}
return null;
-
+
}
return "errno4";
}
-
+
public boolean deleteUser(String email, String key) {
UserAuth ua = getUserAuth(email);
-
- if(ua != null && ua.getHash() != null && ua.isHashCorrect(key)) {
- PMF.deleteObjects(new Class[] {User.class,UserAuth.class},
- new String[] {"email","email"},
+
+ if(ua != null && ua.getHash() != null && ua.isHashCorrect(key)) {
+ PMF.deleteObjects(new Class[] {User.class,UserAuth.class},
+ new String[] {"email","email"},
new String[] {email,email});
- return true;
+ return true;
}
return false;
}
-
+
public User getUser(String email) {
return (User) PMF.getDetachedObject(User.class, "email", email);
}
-
+
public UserAuth getUserAuth(String email) {
return (UserAuth) PMF.getDetachedObject(UserAuth.class, "email", email);
}
-
-
+
+
public void newUserEmail(String email, String hash) {
String message = "Hello!<br/><br/>Thanks for signing up for BillBuzz. To finalize your subscription please click the following link:";
-
+
message += "<br/><br/>" + WEBLINK + "authenticate.jsp?email=" + email + "&key=" + hash;
-
+
message += "<br/><br/>If you have any questions please contact us at <a href=\"mailto:billbuzz@nysenate.gov\">billbuzz@nysenate.gov</a> or reply to this email.";
-
+
try {
sendMail(email,"Finalize your BillBuzz subscription", message, "billbuzz@nysenate.gov", "BillBuzz");
} catch (Exception e) {
e.printStackTrace();
- }
+ }
}
-
+
public String updateEmail(String email) {
-
+
User u = getUser(email);
-
+
if(u == null) {
return "errno5";
}
-
+
UserAuth ua = getUserAuth(email);
if(ua == null) {
- ua = new UserAuth(email);
+ ua = new UserAuth(email);
}
-
+
String message = "Hello!<br/><br/>It looks like you're trying to update your subscription, to do so please click the link below and follow the instructions:";
-
+
message += "<br/><br/>" + WEBLINK + "update.jsp?uemail=" + email + "&key=" + ua.getHash();
-
+
message += "<br/><br/>If you have any questions please contact us at <a href=\"mailto:billbuzz@nysenate.gov\">billbuzz@nysenate.gov</a> or reply to this email.";
-
+
try {
sendMail(email,"Update your BillBuzz subscription", message, "billbuzz@nysenate.gov", "BillBuzz");
PMF.deleteObjectById(UserAuth.class, "email", email);
@@ -183,22 +187,22 @@ public String updateEmail(String email) {
}
return null;
}
-
+
public boolean authenticateUser(String email, String key){
UserAuth ua = getUserAuth(email);
-
+
if(ua != null && ua.isHashCorrect(key)) {
-
+
PersistenceManager pm = PMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
-
+
User u = null;
-
+
try {
tx.begin();
u = (User)PMF.getObject(pm, User.class, "email", email);
-
- if(u != null) {
+
+ if(u != null) {
u.setAuth("y");
}
tx.commit();
@@ -208,13 +212,13 @@ public boolean authenticateUser(String email, String key){
tx.rollback();
}
pm.close();
-
+
PMF.deleteObjectById(UserAuth.class, "email", email);
}
-
+
if(u != null) {
return true;
- }
+ }
}
return false;
}
@@ -238,25 +242,25 @@ protected PasswordAuthentication getPasswordAuthentication() {
InternetAddress addressFrom = new InternetAddress(from);
addressFrom.setPersonal(fromDisplay);
msg.setFrom(addressFrom);
-
-
+
+
StringTokenizer st = new StringTokenizer (to,",");
-
+
InternetAddress[] rcps = new InternetAddress[st.countTokens()];
int idx = 0;
-
+
while (st.hasMoreTokens())
{
InternetAddress addressTo = new InternetAddress(st.nextToken());
rcps[idx++] = addressTo;
-
+
}
-
+
msg.setRecipients(Message.RecipientType.TO,rcps);
-
+
msg.setSubject(subject);
msg.setContent(message, "text/html");
Transport.send(msg);
- }
-
+ }
+
}
View
38 BBSignup/src/main/java/bbsignup/src/Dump.java
@@ -11,41 +11,43 @@
import bbsignup.model.User;
public class Dump {
-
-
+
+
public static void main(String[] args) throws IOException {
-
+
if(args.length == 0) {
System.out.println("filename must be provided");
return;
}
-
+
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(args[0])));
-
-
+
+
@SuppressWarnings("unchecked")
Collection<User> users = (Collection<User>) PMF.getDetachedObjects(User.class);
-
-
+
+
for(User user:users) {
-
+
if(user.getAuth().equals("y")) {
-
+
bw.write(user.getFirstName() + ":" + user.getEmail()+":");
-
+
List<String> subs = user.getSubscriptions();
-
+
for(Iterator<String> itr = subs.iterator(); itr.hasNext();) {
bw.write(itr.next() + ((itr.hasNext()) ? ",":""));
}
-
+
bw.newLine();
-
-
+
+
}
-
+
}
-
+
+ bw.close();
+
}
-
+
}
View
51 BBSignup/src/main/java/servlets/MainServlet.java
@@ -15,56 +15,55 @@
import bbsignup.model.UserAuth;
import bbsignup.src.Controller;
import bbsignup.src.PMF;
-import bbsignup.src.Resource;
/**
* Servlet implementation class MainServlet
*/
public class MainServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
-
+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// will store selected subscriptions
List<String> subs = new ArrayList<String>();
-
+
HttpSession session = request.getSession();
Object o = SenatorContext.getSenators(this.getServletContext());
-
+
String fname = null;
String lname = null;
String email = null;
String otherData = null;
-
+
// if update exists in the session then the user already xists */
String update = (String)session.getAttribute("update");
-
+
if(o != null) {
- fname = (String)request.getParameter("firstname");
- lname = (String)request.getParameter("lastname");
- email = (String)request.getParameter("email1");
- otherData = (String)request.getParameter("otherData");
-
+ fname = request.getParameter("firstname");
+ lname = request.getParameter("lastname");
+ email = request.getParameter("email1");
+ otherData = request.getParameter("otherData");
+
/* this is a session variable stored from index.jsp, verifies
* that the user is following proper workflow */
@SuppressWarnings("unchecked")
List<Senator> senators = (List<Senator>)o;
-
- // if the button to select all senators has been selected
+
+ // if the button to select all senators has been selected
if(request.getParameter("cb_all") != null) {
subs.add("all");
}
else {
for(Senator senator:senators) {
String[] params = request.getParameterValues(senator.getOpenLegName());
-
+
if(params != null) {
//it has been selected
subs.add(senator.getOpenLegName());
}
}
}
-
+
//they didn't select anything, return back to index.jsp
if(subs.size() == 0) {
session.setAttribute("fn", fname);
@@ -74,24 +73,24 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
response.sendRedirect("index.jsp");
return;
}
-
+
//they selected everything, change to 'all'
if(subs.size() == 62) {
subs.clear();
subs.add("all");
}
-
+
Controller c = new Controller();
String message = null;
User u = new User(fname, lname, email, "n", (otherData.equals("yes") ? true:false));
u.setSubscriptions(subs);
-
+
if(update == null) {
User cur = (User)PMF.getDetachedObject(User.class, "email", email);
-
+
if(cur == null) {
UserAuth ua = new UserAuth(email);
-
+
c.newUserEmail(email, ua.getHash());
PMF.persistObject(u, ua);
message = "<div class=\"good\" style=\"width:500px;\">" +
@@ -108,18 +107,18 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
"<br/><br/>" +
"</div>";
}
-
-
+
+
}
else {
String oldEmail = (String) session.getAttribute("oldemail");
u.setAuth("y");
-
+
PMF.deleteObjects(new Class[] {UserAuth.class, User.class},
new String[] {"email","email"},
new String[] {oldEmail,oldEmail});
PMF.persistObject(u);
-
+
message = "<div class=\"good\" style=\"width:500px;\">" +
"Your account has been successfully updated! Thanks for using BillBuzz." +
"<br/><br/>" +
@@ -134,7 +133,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
session.setAttribute("e", null);
session.setAttribute("oldemail", null);
session.setAttribute("update", null);
-
+
session.setAttribute("message", message);
response.sendRedirect("message.jsp");
}
@@ -142,7 +141,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
response.sendRedirect("index.jsp");
}
}
-
+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
Please sign in to comment.
Something went wrong with that request. Please try again.