package net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect; //Parts of this sources are copied and modified from the jEdit Wikipedia plugin: //http://www.djini.de/software/wikipedia/index.html // //The modified sources are available under the "Common Public License" //with permission from the original author: Daniel Wunsch import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sourceforge.phpeclipse.wiki.actions.OpenWikiLinkEditorAction; public class BookmarkletServer implements Runnable { // configuration private InetAddress ADDRESS; int PORT = 8009; ServerSocket serverSocket = null; Thread thread = null; public BookmarkletServer() { try { ADDRESS = InetAddress.getByName("127.0.0.1"); } catch (UnknownHostException e) { e.printStackTrace(); } } /** set the port the server listens on - must be called before start() */ public void setPort(int port) { PORT = port; } /** starts the server Thread */ public void start() { // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "listening on " + ADDRESS.CanonicalHostName + ":" + PORT); thread = new Thread(this); thread.start(); } /** stops the server Thread */ public void stop() throws IOException { thread.interrupt(); serverSocket.close(); // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "stopped"); } /** the server loop */ public void run() { try { serverSocket = new ServerSocket(PORT, 2, ADDRESS); for (;;) { Socket clientSocket = null; try { if (thread.isInterrupted()) break; clientSocket = serverSocket.accept(); clientSocket.setSoTimeout(1000); if (thread.isInterrupted()) break; handleClient(clientSocket); } catch (SocketException e) { //Log.Log(Log.NOTICE, plugin, "BookmarkletServerSocket closed"); System.err.println("WikipediaPlugin: BookmarkletServer: Socket closed"); } catch (Exception e) { // Log.log(Log.ERROR, plugin, BshUtil.extractDescription(e)); } finally { if (clientSocket != null) clientSocket.close(); } } serverSocket.close(); } catch (IOException e) { } } /** called when an incoming Connection is accepted */ private void handleClient(Socket clientSocket) throws IOException { if (!clientSocket.getInetAddress().equals(ADDRESS)) { // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "illegal remote address: " + clientSocket.InetAddress); return; } BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "UTF-8")); String line = reader.readLine(); if (line == null) { // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "illegal request: " + line); return; } /* * javascript:window.location.href='http://127.0.0.1:8009/open' Pattern pattern = Pattern.compile("^GET /(.*?) HTTP/1.[0-9]$", * Pattern.DOTALL); */ Pattern pattern = Pattern.compile("^GET /(.*?)/\\?(.*) HTTP/1.[0-9]$", Pattern.DOTALL); Matcher matcher = pattern.matcher(line); if (!matcher.matches()) { // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "illegal request: " + line); return; } String action = matcher.group(1); String url = matcher.group(2); /* * BookmarkletServer.bsh: Host: 127.0.0.1:8009 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.2) Gecko/20040810 * Debian/1.7.2-2 User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90) Referer: * http://kamelopedia.mormo.org/index.php/V%F6geln */ boolean respond = true; for (;;) { line = reader.readLine(); if (line == null) break; if (line.length() == 0) break; String lower = line.toLowerCase(); /* * // find out referer if (lower.startsWith("referer: ")) { url = line.substring("referer: ".length()); Log.log(Log.NOTICE, * plugin, "BookmarkletServer: " + "got URL: " + url); } */ // find out user-agent if (lower.startsWith("user-agent: ") && lower.indexOf("mozilla") >= 0 && lower.indexOf("gecko") >= 0 && lower.indexOf("msie") < 0 && lower.indexOf("opera") < 0 && lower.indexOf("safari") < 0) { respond = false; // Log.log(Log.DEBUG, plugin, "BookmarkletServer: " + "mozilla detected: skipping response"); } } /* * if (url == null) { Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "missing referer header"); return; } */ handleAction(action, url); if (respond) { Writer writer = new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8"); writer.write("HTTP/1.0 301 Moved Permanently\r\n"); writer.write("Cache-Control: no-cache\r\n"); writer.write("Pragma: no-cache\r\n"); writer.write("Expires: -1\r\n"); writer.write("Location: " + url + "\r\n"); writer.write("\r\n"); writer.write(""); writer.write("goto " + url + ""); writer.write(""); writer.write("\r\n"); writer.flush(); writer.close(); } reader.close(); } /** called when the Connection sent a Command */ private void handleAction(String action, String url) { if (!"open".equals(action)) { // Log.log(Log.NOTICE, plugin, "BookmarkletServer: " + "unknown action: " + action); return; } // TODO determine the global project and open editor for URL // OpenWikiLinkEditorAction.openWikiUrl(null, url); // wikipedia.openURL(jEdit.getActiveView(), new URL(url), true); } }