package org.mcraig.cs445.refentry;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.ObjectInputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashSet;
import java.util.StringTokenizer;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Basic RefEntry Server application.
* @author <a href="mailto:mark@mcraig.org">Mark Craig</a>
*/
public class RefEntryServlet extends HttpServlet {
/** Default constructor. */
public RefEntryServlet() { return; }
/** Log that this method has been called. */
public void destroy() {
System.out.println("[RefEntry] destroy");
}
/** Get basic information. */
public String getServletInfo() {
return "Basic RefEntry Servlet";
}
/**
* Get the backend and tools for transformations.
* @param config Servlet configuration information.
* @throws ServletException when configuration information is null.
*/
public void init(ServletConfig config) throws ServletException {
try {
System.out.println("[RefEntry] init starting.");
// Get the context for real paths
ServletContext ctx = config.getServletContext();
// Read in the RefEntryBackEnd object and the serialized
// HashSet of common words
String sbe = config.getInitParameter("serializedBackend");
if (sbe != null) sbe = "/WEB-INF/" + sbe;
else throw new ServletException("sbe is null.");
ObjectInputStream inbe =
new ObjectInputStream(ctx.getResourceAsStream(sbe));
BackEnd = (RefEntryBackEnd)inbe.readObject();
inbe.close();
System.out.println("[RefEntry] got backend.");
String scw = config.getInitParameter("serializedCommonWords");
if (scw != null) scw = "/WEB-INF/" + scw;
else throw new ServletException("scw is null.");
ObjectInputStream incw =
new ObjectInputStream(ctx.getResourceAsStream(scw));
CommonWords = (HashSet)incw.readObject();
incw.close();
System.out.println("[RefEntry] got common words.");
// Get the default page to return when no search keywords
// are provided and no page is requested.
DefaultPage = config.getInitParameter("defaultPage");
if (DefaultPage == null)
throw new ServletException("DefaultPage is null");
System.out.println("[RefEntry] got default page.");
System.out.println("[RefEntry] init succeeded.");
} catch (Exception e) {
throw new ServletException(e.toString());
}
}
/**
* Form an XHTML response given a search request.
* @param req HTTP request from the servlet container.
* @param res HTTP response for the client application.
* @throws ServletException if the servlet cannot respond to the request.
*/
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// Based on the URL query field, set the RefEntry ID to return
// or the content of the search request.
String query = null;
if (req.getQueryString() != null)
query = new String(req.getQueryString());
String request = null;
String entryid = null;
HashSet search = new HashSet();
if (query != null) {
entryid = req.getParameter("RefEntry");
request = req.getParameter("SrchString");
if (request != null) {
try {
request = URLDecoder.decode(request, "UTF-8");
} catch (Exception e) {
throw new ServletException(e.toString());
}
search = new HashSet();
search = getTokens(request);
}
}
// Construct the response to the client
res.setContentType("text/html");
String base = new String((req.getRequestURL()).toString());
RefEntryTransformer tr = new RefEntryTransformer(base);
try {
PrintWriter pw = res.getWriter();
if (DefaultPage == null) {
throw new ServletException("[RefEntry] DefaultPage is null.");
} else if (entryid == null && request == null) {
// Nothing to look for? Return the default page.
pw.print(tr.transform(BackEnd.getEntry(DefaultPage), ""));
} else if (entryid != null) {
// Link to a RefEntry? Return the document.
pw.print(tr.transform(BackEnd.getEntry(entryid), ""));
} else {
// Search string? Return the results.
pw.print(tr.transform(BackEnd.getValues(search), request));
}
pw.close();
} catch (Exception e) {
ServletException se = new ServletException(e.toString());
res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
throw se;
}
}
/** Call doGet if some client tries doPut. */
public void doPut(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
/**
* Tokenize the search request, returning only useful words.
* @param request String to tokenize.
*/
HashSet getTokens(String request) {
StringTokenizer in = new StringTokenizer(request);
String tok = new String();
HashSet tokens = new HashSet();
while(in.hasMoreTokens()) {
tok = in.nextToken();
if (tok.matches("\\w+") &&
(tok.length() >= 3)) {
tokens.add(tok);
}
}
if (CommonWords != null) tokens.removeAll(CommonWords);
return tokens;
}
/** Backend used by this servlet. */
private RefEntryBackEnd BackEnd;
/** Common words to ignore when searching. */
private HashSet CommonWords;
/** ID of default page for this servlet. */
private String DefaultPage;
} |