view src/global/web/UserSites.jtp @ 41:f903ebdfa3b3

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 31 Jan 2021 15:35:37 -0700
parents 7ecd1a4ef557
children
line wrap: on
line source

<%
package global.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import fschmidt.util.mail.Mail;
import fschmidt.util.mail.MailAddress;
import fschmidt.util.mail.MailHome;
import fschmidt.util.mail.PlainTextContent;
import global.Site;
import global.HtmlGlobalUtils;


public final class UserSites extends HttpServlet {

	protected void service(HttpServletRequest request,HttpServletResponse response)
		throws IOException
	{
		PrintWriter out = response.getWriter();
		boolean isValid = true;
		boolean isSent = false;
		String email = request.getParameter("email");
		if( email == null ) {
			email = "";
		} else {
			email = email.trim();
			MailAddress to = new MailAddress(email);
			isValid = to.isValid();
			if( isValid ) {
				Mail mail = MailHome.newMail();
				mail.setTo(to);
				mail.setFrom( new MailAddress("no-reply@nabble.com","Nabble") );
				mail.setSubject("Your Nabble Apps");
				mail.setContent(new PlainTextContent(text(email)));
				MailHome.getDefaultSmtpServer().send(mail);
				isSent = true;
			}
		}
%>
<!DOCTYPE html>
<html lang="en">
	<head>
		<% HtmlGlobalUtils.head(request, response, "Your Apps"); %>
	</head>
	<body lato>
		<% HtmlGlobalUtils.header(request,response); %>
		<div content center paddingTop>
			<h1 oswald>Your Nabble Apps</h1>
			<p>
				Nabble can send you a list with all apps that you currently own.
			</p>
			<%
			if (!isSent) {
				%>
				<form action="UserSites.jtp">
					<p>
						<input name="email" type="text" value="<%=email%>" size="30" placeholder="Email address"/>
						<input type="submit" value="Submit"/>
					</p>
				</form>
				<%
				if( !isValid ) {
					%><p>invalid email address</p><%
				}
			} else {
				%><div class="info-message" style="padding:.5em">An email has been sent to you.</div><%
			}
			%>
		</div>
		<% HtmlGlobalUtils.footer(request,response); %>
	</body>
</html>
		<%
	}

    private static String text(String email) {
		try {
			IndexSearcher searcher = new IndexSearcher(Site.dir());
			Query q = new TermQuery(new Term(Site.OWNER_EMAIL_FLD,email));
			TopDocs hits = searcher.search( q, 500 );
			try {
				StringWriter buf = new StringWriter();
				PrintWriter out = new PrintWriter(buf);
%>
Dear Nabble user,
<% if( hits.totalHits == 0 ){ %>
You don't have any sites on Nabble.
<% } else { %>
Here are your Nabble apps:
<% for( ScoreDoc sd : hits.scoreDocs ) {
Site site = new Site( searcher.doc(sd.doc) );
%>
<%=site.url()%>
<%
}
}
%>
Regards,
The Nabble team
<%
				out.flush();
				return buf.toString();
			} finally {
				searcher.close();
			}
		} catch(IOException e) {
			throw new RuntimeException(e);
		}
	}

}
%>