103 lines
3.1 KiB
Java
103 lines
3.1 KiB
Java
package com.stephenschafer.email;
|
|
|
|
import java.io.IOException;
|
|
import java.io.PrintWriter;
|
|
import java.text.DateFormat;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
|
|
import javax.servlet.ServletConfig;
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.http.HttpServlet;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
public class PostMapping extends HttpServlet {
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
@Override
|
|
public void init(final ServletConfig config) throws ServletException {
|
|
try {
|
|
Configuration.INSTANCE.load();
|
|
GlobalCache.INSTANCE.initialize();
|
|
}
|
|
catch (final Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
super.init(config);
|
|
}
|
|
|
|
@Override
|
|
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
|
|
throws ServletException, IOException {
|
|
try {
|
|
final boolean newMapping = "true".equals(request.getParameter("new"));
|
|
final boolean mobile = "true".equals(request.getParameter("mobile"));
|
|
final String address = request.getParameter("email-address");
|
|
final String description = request.getParameter("description");
|
|
final String target = request.getParameter("target");
|
|
final boolean disabled = "true".equalsIgnoreCase(request.getParameter("disabled"));
|
|
if (newMapping) {
|
|
if (Util.addressExists(address)) {
|
|
response.sendError(404, "Address already exists");
|
|
return;
|
|
}
|
|
if (address == null || address.length() == 0) {
|
|
response.sendError(400, "Please supply an address");
|
|
return;
|
|
}
|
|
if (address.indexOf('@') < 0) {
|
|
response.sendError(400, "Not a valid email address");
|
|
return;
|
|
}
|
|
final Date date = new Date();
|
|
final Mapping mapping = new Mapping(address, target, description, date, disabled);
|
|
Util.addMapping(mapping);
|
|
Util.addHistory(mapping);
|
|
}
|
|
else {
|
|
if (!Util.addressExists(address)) {
|
|
response.sendError(404, "Address does not exist");
|
|
return;
|
|
}
|
|
final String dateString = request.getParameter("date");
|
|
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
Date date;
|
|
try {
|
|
date = dateString != null && dateString.trim().length() > 0
|
|
? df.parse(dateString)
|
|
: null;
|
|
}
|
|
catch (final ParseException e1) {
|
|
date = null;
|
|
}
|
|
final Mapping mapping = new Mapping(address, target, description, date, disabled);
|
|
Util.updateMapping(mapping);
|
|
Util.addHistory(mapping);
|
|
}
|
|
Util.SaveMappingsResult result;
|
|
try {
|
|
result = Util.saveMappings();
|
|
}
|
|
catch (final InterruptedException e) {
|
|
e.printStackTrace();
|
|
result = null;
|
|
}
|
|
if (result != null && result.getExitValue() != 0) {
|
|
final PrintWriter out = response.getWriter();
|
|
out.println("saveMappings returned " + result.getExitValue());
|
|
out.println("Error:");
|
|
out.println(result.getError());
|
|
out.println("Output:");
|
|
out.println(result.getOutput());
|
|
}
|
|
else {
|
|
response.sendRedirect(mobile ? "mapping.jsp?address=" + address : "index.jsp");
|
|
}
|
|
}
|
|
catch (final Exception e) {
|
|
throw new ServletException(e);
|
|
}
|
|
}
|
|
}
|