*/
public void service(ServletRequest req1, ServletResponse res1)
throws ServletException {
final HttpServletRequest req = (HttpServletRequest) req1;
final HttpServletResponse res = (HttpServletResponse) res1;
final HTTPRequestHeader http_header = new HTTPRequestHeader();
if (req.getServletPath().equals("/admin")) try {
log.debug("Forwarding /admin request back to self");
req.getRequestDispatcher("WebMail/admin").forward(req1, res1);
return;
} catch (IOException ioe) {
log.fatal("Forward from '/admin' failed", ioe);
throw new ServletException(ioe.getMessage());
}
final Enumeration en = req.getHeaderNames();
while (en.hasMoreElements()) {
final String s = (String) en.nextElement();
http_header.setHeader(s, req.getHeader(s));
}
http_header.setPath(
req.getPathInfo() == null ? "/" : req.getPathInfo());
InetAddress addr;
try {
addr = InetAddress.getByName(req.getRemoteHost());
} catch (final UnknownHostException e) {
try {
addr = InetAddress.getByName(req.getRemoteAddr());
} catch (final Exception ex) {
throw new ServletException("Remote host must identify!");
}
}
HTMLDocument content = null;
final int err_code = 400;
HTTPSession sess = null;
/*
* Here we try to parse the MIME content that the Client sent in his
* POST since the JServ doesn't do that for us:-( At least we can use
* the functionality provided by the standalone server where we need to
* parse the content ourself anyway.
*/
try {
final BufferedOutputStream out =
new BufferedOutputStream(res.getOutputStream());
/*
* First we try to use the Servlet API's methods to parse the
* parameters. Unfortunately, it doesn't know how to handle MIME
* multipart POSTs, so we will have to handle that ourselves
*/
/*
* First get all the parameters and set their values into
* http_header
*/
Enumeration enum2 = req.getParameterNames();
while (enum2.hasMoreElements()) {
final String s = (String) enum2.nextElement();
http_header.setContent(s, req.getParameter(s));
// log.info("Parameter "+s);
}
/* Then we set all the headers in http_header */
enum2 = req.getHeaderNames();
while (enum2.hasMoreElements()) {
final String s = (String) enum2.nextElement();
http_header.setHeader(s, req.getHeader(s));
}
/*
* In Servlet API 2.2 we might want to fetch the attributes also,
* but this doesn't work in API 2.0, so we leave it commented out
*/
// enum2=req.getAttributeNames();
// while(enum2.hasMoreElements()) {
// String s=(String)enum2.nextElement();
// log.info("Attribute "+s);
// }
/* Now let's try to handle multipart/form-data posts */
if (req.getContentType() != null
&& req.getContentType().toUpperCase().
startsWith("MULTIPART/FORM-DATA")) {
final int size = Integer.parseInt(WebMailServer.
getStorage().getConfig("max attach size"));
final MultipartParser mparser = new MultipartParser(req, size);
Part p;
while ((p = mparser.readNextPart()) != null) {
if (p.isFile()) {
final ByteStore bs = ByteStore.getBinaryFromIS(
((FilePart) p).getInputStream(), size);
bs.setName(((FilePart) p).getFileName());
bs.setContentType(getStorage().getMimeType(
((FilePart) p).getFileName()));
http_header.setContent(p.getName(), bs);
log.info("File name " + bs.getName());
log.info("Type " + bs.getContentType());
} else if (p.isParam()) {
http_header.setContent(p.getName(),
((ParamPart) p).getStringValue());
}
// log.info("Parameter "+p.getName());
}
}
try {
final String url = http_header.getPath();
try {
/* Find out about the session id */
sess = req.getSession(false) == null
? null
: (HTTPSession) req.getSession(false).
getAttribute("webmail.session");
/*
* If the user was logging on, he doesn't have a session id,
* so generate one. If he already had one, all the better,
* we will take the old one
*/
if (sess == null && url.startsWith("/login")) {
sess = newSession(req, http_header);
} else if (sess == null && url.startsWith("/admin/login")) {
http_header.setHeader("LOGIN", "Administrator");
sess = newAdminSession(req, http_header);
}
if (sess == null && !url.equals("/")
&& !url.startsWith("/passthrough")
&& !url.startsWith("/admin")) {