001: /**
002: * @author Tobias Schulz-Hess (sourceforge@schulz-hess.de)
003: * 12/04/2003 20:33:31
004: */package vqwiki.servlets;
005:
006: import java.io.IOException;
007: import java.io.OutputStream;
008: import java.io.StringReader;
009: import java.util.Collection;
010: import java.util.Iterator;
011:
012: import javax.servlet.ServletException;
013: import javax.servlet.http.HttpServlet;
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016:
017: import org.apache.log4j.Logger;
018:
019: import vqwiki.SearchEngine;
020: import vqwiki.WikiBase;
021: import vqwiki.utils.Utilities;
022:
023: /**
024: * This servlet exports the node file for the TGWikiBrowser.
025: * You can add a parameter "virutal-wiki", which then generates
026: * the node file on a particular virtual wiki.<p>
027: *
028: * For more details on TGWikiBrowser see:
029: * http://touchgraph.sourceforge.net
030: * <p>
031: *
032: * @author Tobias Schulz-Hess (sourceforge@schulz-hess.de)
033: *
034: * TODO: Create a zip containing the browser, a batch file plus the node file
035: */
036: public class ExportTGWikiBrowserServlet extends HttpServlet {
037:
038: /** Logging */
039: private static final Logger logger = Logger
040: .getLogger(ExportTGWikiBrowserServlet.class);
041:
042: /**
043: * Handle post request.
044: * Generate a node file and send it back as text.
045: *
046: * @param httpServletRequest The current http request
047: * @param httpServletResponse What the servlet will send back as response
048: *
049: * @throws ServletException If something goes wrong during servlet execution
050: * @throws IOException If the output stream cannot be accessed
051: *
052: */
053: protected void doPost(HttpServletRequest request,
054: HttpServletResponse response) throws ServletException,
055: IOException {
056: String virtualWiki = null;
057: try {
058: virtualWiki = (String) request.getAttribute("virtual-wiki");
059: if (virtualWiki == null || virtualWiki.length() < 1) {
060: virtualWiki = WikiBase.DEFAULT_VWIKI;
061: }
062: StringBuffer result = new StringBuffer();
063: WikiBase wb = WikiBase.getInstance();
064: SearchEngine sedb = wb.getSearchEngineInstance();
065: Collection all = sedb.getAllTopicNames(virtualWiki);
066: Iterator allIterator = all.iterator();
067: while (allIterator.hasNext()) {
068: StringBuffer oneline = new StringBuffer();
069: String topicname = (String) allIterator.next();
070: oneline.append(topicname);
071: String content = wb.readCooked(virtualWiki, topicname);
072: String searchfor = "href=\"Wiki?";
073: int iPos = content.indexOf(searchfor);
074: int iEndPos = content.indexOf(Utilities.resource(
075: "topic.ismentionedon", request.getLocale()));
076: if (iEndPos == -1)
077: iEndPos = Integer.MAX_VALUE;
078: while (iPos > -1 && iPos < iEndPos) {
079: String link = content.substring(iPos
080: + searchfor.length(), content.indexOf('"',
081: iPos + searchfor.length()));
082: if (link.indexOf('&') > -1) {
083: link = link.substring(0, link.indexOf('&'));
084: }
085: if (link.length() > 3 && !link.startsWith("topic=")
086: && !link.startsWith("action=")
087: && !topicname.equals(link)) {
088: oneline.append(" ").append(link);
089: }
090: iPos = content.indexOf(searchfor, iPos + 10);
091: }
092: logger.debug(oneline.toString());
093: result.append(oneline).append("\n");
094: }
095: response.setContentType("text/plain");
096: response.setHeader("Expires", "0");
097: response.setHeader("Pragma", "no-cache");
098: response.setHeader("Keep-Alive", "timeout=15, max=100");
099: response.setHeader("Connection", "Keep-Alive");
100: response.setContentLength(result.length());
101: OutputStream out = response.getOutputStream();
102: StringReader source = new StringReader(result.toString());
103: int copied;
104: while ((copied = source.read()) != -1) {
105: out.write(copied);
106: }
107: out.flush();
108: out.close();
109: } catch (Exception e) {
110: throw new ServletException(e.getMessage(), e);
111: }
112: }
113:
114: /**
115: * Handle get request.
116: * The request is handled the same way as the post request.
117: *
118: * @see doPost()
119: *
120: * @param httpServletRequest The current http request
121: * @param httpServletResponse What the servlet will send back as response
122: *
123: * @throws ServletException If something goes wrong during servlet execution
124: * @throws IOException If the output stream cannot be accessed
125: *
126: */
127: protected void doGet(HttpServletRequest httpServletRequest,
128: HttpServletResponse httpServletResponse)
129: throws ServletException, IOException {
130: this.doPost(httpServletRequest, httpServletResponse);
131: }
132: }
|