0
+ − 1
+ − 2 package global.web;
+ − 3
+ − 4 import java.io.IOException;
+ − 5 import java.io.PrintWriter;
+ − 6 import java.io.StringWriter;
+ − 7 import javax.servlet.http.HttpServlet;
+ − 8 import javax.servlet.http.HttpServletRequest;
+ − 9 import javax.servlet.http.HttpServletResponse;
+ − 10 import org.apache.lucene.index.Term;
+ − 11 import org.apache.lucene.search.IndexSearcher;
+ − 12 import org.apache.lucene.search.TopDocs;
+ − 13 import org.apache.lucene.search.Query;
+ − 14 import org.apache.lucene.search.ScoreDoc;
+ − 15 import org.apache.lucene.search.TermQuery;
+ − 16 import fschmidt.util.mail.Mail;
+ − 17 import fschmidt.util.mail.MailAddress;
+ − 18 import fschmidt.util.mail.MailHome;
+ − 19 import fschmidt.util.mail.PlainTextContent;
+ − 20 import global.Site;
+ − 21 import global.HtmlGlobalUtils;
+ − 22
+ − 23
+ − 24 public final class UserSites extends HttpServlet {
+ − 25
+ − 26 protected void service(HttpServletRequest request,HttpServletResponse response)
+ − 27 throws IOException
+ − 28 {
+ − 29 PrintWriter out = response.getWriter();
+ − 30 boolean isValid = true;
+ − 31 boolean isSent = false;
+ − 32 String email = request.getParameter("email");
+ − 33 if( email == null ) {
+ − 34 email = "";
+ − 35 } else {
+ − 36 email = email.trim();
+ − 37 MailAddress to = new MailAddress(email);
+ − 38 isValid = to.isValid();
+ − 39 if( isValid ) {
+ − 40 Mail mail = MailHome.newMail();
+ − 41 mail.setTo(to);
+ − 42 mail.setFrom( new MailAddress("no-reply@nabble.com","Nabble") );
+ − 43 mail.setSubject("Your Nabble Apps");
+ − 44 mail.setContent(new PlainTextContent(text(email)));
+ − 45 MailHome.getDefaultSmtpServer().send(mail);
+ − 46 isSent = true;
+ − 47 }
+ − 48 }
+ − 49
+ − 50 out.print( "\r\n<!DOCTYPE html>\r\n<html lang=\"en\">\r\n <head>\r\n " );
+ − 51 HtmlGlobalUtils.head(request, response, "Your Apps");
+ − 52 out.print( "\r\n </head>\r\n <body lato>\r\n " );
+ − 53 HtmlGlobalUtils.header(request,response);
+ − 54 out.print( "\r\n <div content center paddingTop>\r\n <h1 oswald>Your Nabble Apps</h1>\r\n <p>\r\n Nabble can send you a list with all apps that you currently own.\r\n </p>\r\n " );
+ − 55
+ − 56 if (!isSent) {
+ − 57
+ − 58 out.print( "\r\n<form action=\"UserSites.jtp\">\r\n <p>\r\n <input name=\"email\" type=\"text\" value=\"" );
+ − 59 out.print( (email) );
+ − 60 out.print( "\" size=\"30\" placeholder=\"Email address\"/>\r\n <input type=\"submit\" value=\"Submit\"/>\r\n </p>\r\n</form>\r\n" );
+ − 61
+ − 62 if( !isValid ) {
+ − 63
+ − 64 out.print( "<p>invalid email address</p>" );
+ − 65
+ − 66 }
+ − 67 } else {
+ − 68
+ − 69 out.print( "<div class=\"info-message\" style=\"padding:.5em\">An email has been sent to you.</div>" );
+ − 70
+ − 71 }
+ − 72
+ − 73 out.print( "\r\n</div>\r\n" );
+ − 74 HtmlGlobalUtils.footer(request,response);
+ − 75 out.print( "\r\n</body>\r\n</html>\r\n" );
+ − 76
+ − 77 }
+ − 78
+ − 79 private static String text(String email) {
+ − 80 try {
+ − 81 IndexSearcher searcher = new IndexSearcher(Site.dir());
+ − 82 Query q = new TermQuery(new Term(Site.OWNER_EMAIL_FLD,email));
+ − 83 TopDocs hits = searcher.search( q, 500 );
+ − 84 try {
+ − 85 StringWriter buf = new StringWriter();
+ − 86 PrintWriter out = new PrintWriter(buf);
+ − 87
+ − 88 out.print( "\r\nDear Nabble user,\r\n" );
+ − 89 if( hits.totalHits == 0 ){
+ − 90 out.print( "\r\nYou don't have any sites on Nabble.\r\n" );
+ − 91 } else {
+ − 92 out.print( "\r\nHere are your Nabble apps:\r\n" );
+ − 93 for( ScoreDoc sd : hits.scoreDocs ) {
+ − 94 Site site = new Site( searcher.doc(sd.doc) );
+ − 95
+ − 96 out.print( "\r\n" );
+ − 97 out.print( (site.url()) );
+ − 98 out.print( "\r\n" );
+ − 99
+ − 100 }
+ − 101 }
+ − 102
+ − 103 out.print( "\r\nRegards,\r\nThe Nabble team\r\n" );
+ − 104
+ − 105 out.flush();
+ − 106 return buf.toString();
+ − 107 } finally {
+ − 108 searcher.close();
+ − 109 }
+ − 110 } catch(IOException e) {
+ − 111 throw new RuntimeException(e);
+ − 112 }
+ − 113 }
+ − 114
+ − 115 }
+ − 116