From 552540307a1b2862e82cbcbfd0fbbb8d1f7934b7 Mon Sep 17 00:00:00 2001 From: axelcl Date: Sat, 5 Feb 2005 11:53:10 +0000 Subject: [PATCH] Load proxy settings though external *.properties files --- .../prefs/default_linux.properties | 5 + .../prefs/default_macosx.properties | 5 + .../prefs/default_win32.properties | 5 + .../mediawiki/connect/MediaWikiConnector.java | 101 ++++++++++++++---- .../phpeclipse/wiki/editor/WikiEditorPlugin.java | 114 ++++++++++++++++++- 5 files changed, 202 insertions(+), 28 deletions(-) create mode 100644 archive/net.sourceforge.phpeclipse.wiki/prefs/default_linux.properties create mode 100644 archive/net.sourceforge.phpeclipse.wiki/prefs/default_macosx.properties create mode 100644 archive/net.sourceforge.phpeclipse.wiki/prefs/default_win32.properties diff --git a/archive/net.sourceforge.phpeclipse.wiki/prefs/default_linux.properties b/archive/net.sourceforge.phpeclipse.wiki/prefs/default_linux.properties new file mode 100644 index 0000000..858fde4 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/prefs/default_linux.properties @@ -0,0 +1,5 @@ +http.timeout=30 +http.proxyHost= +http.proxyPort= +http.proxyUserName= +http.proxyPassword= \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/prefs/default_macosx.properties b/archive/net.sourceforge.phpeclipse.wiki/prefs/default_macosx.properties new file mode 100644 index 0000000..858fde4 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/prefs/default_macosx.properties @@ -0,0 +1,5 @@ +http.timeout=30 +http.proxyHost= +http.proxyPort= +http.proxyUserName= +http.proxyPassword= \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/prefs/default_win32.properties b/archive/net.sourceforge.phpeclipse.wiki/prefs/default_win32.properties new file mode 100644 index 0000000..858fde4 --- /dev/null +++ b/archive/net.sourceforge.phpeclipse.wiki/prefs/default_win32.properties @@ -0,0 +1,5 @@ +http.timeout=30 +http.proxyHost= +http.proxyPort= +http.proxyUserName= +http.proxyPassword= \ No newline at end of file diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java index 07cc568..a9eba4a 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/mediawiki/connect/MediaWikiConnector.java @@ -21,6 +21,7 @@ import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAn import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import org.apache.commons.httpclient.ConnectMethod; +import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpConnection; import org.apache.commons.httpclient.HttpException; @@ -36,6 +37,7 @@ import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.protocol.Protocol; import org.apache.commons.httpclient.util.EncodingUtil; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.Preferences; /** * This class gets the wikitext from a wikipedia edit page @@ -91,6 +93,7 @@ public class MediaWikiConnector { manager.setConnectionStaleCheckingEnabled(true); // open the conversation client = new HttpClient(manager); + setHTTPClientParameters(client); //client.State.CookiePolicy = CookiePolicy.COMPATIBILITY; //client.HostConfiguration.setHost(LOGON_SITE, LOGON_PORT, "http"); } @@ -131,9 +134,9 @@ public class MediaWikiConnector { && responseBody.matches(config.getLoginNoUser())) { result = false; if (responseBody.matches(config.getLoginNoUser())) { - throw new UnexpectedAnswerException("login not successful: wrong user name: "+user); + throw new UnexpectedAnswerException("login not successful: wrong user name: " + user); } else if (responseBody.matches(config.getLoginWrongPw())) { - throw new UnexpectedAnswerException("login not successful: wrong password for user: "+user); + throw new UnexpectedAnswerException("login not successful: wrong password for user: " + user); } else { throw new UnexpectedAnswerException("logout not successful: responseCode == 200"); } @@ -251,15 +254,16 @@ public class MediaWikiConnector { return result; } - public ArrayList loadXML(IWikipedia config, String actionURL, String pages) throws UnexpectedAnswerException, MethodException , InterruptedException{ - storeThrottle.delay(); + public ArrayList loadXML(IWikipedia config, String actionURL, String pages) throws UnexpectedAnswerException, MethodException, + InterruptedException { + storeThrottle.delay(); PostMethod method = new PostMethod(actionURL); method.setFollowRedirects(false); method.addRequestHeader("User-Agent", userAgent); method.addRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE + "; charset=" + config.getCharSet()); - - NameValuePair[] params = new NameValuePair[] { - new NameValuePair("pages", pages), + + NameValuePair[] params = new NameValuePair[] { + new NameValuePair("pages", pages), new NameValuePair("curonly", "X"), new NameValuePair("action", "submit") }; method.addParameters(params); @@ -273,7 +277,7 @@ public class MediaWikiConnector { } else { throw new UnexpectedAnswerException("XML load not successful: expected 200 OK, got " + method.getStatusLine()); } - } catch(CoreException e) { + } catch (CoreException e) { throw new UnexpectedAnswerException("XML load method failed" + e.getMessage()); } catch (HttpException e) { throw new MethodException("XML load method failed", e); @@ -307,6 +311,7 @@ public class MediaWikiConnector { storeThrottle.delay(); PostMethod method = new PostMethod(actionUrl); + method.setFollowRedirects(false); method.addRequestHeader("User-Agent", userAgent); method.addRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE + "; charset=" + config.getCharSet()); @@ -378,22 +383,12 @@ public class MediaWikiConnector { } Protocol protocol = Protocol.getProtocol(schema); - HttpState state = new HttpState(); - method = new GetMethod(uri.toString()); String host = uri.getHost(); int port = uri.getPort(); HttpConnection connection = new HttpConnection(host, port, protocol); - // timeout after 30 seconds - connection.setConnectionTimeout(30000); - connection.setProxyHost(System.getProperty("http.proxyHost")); - connection.setProxyPort(Integer.parseInt(System.getProperty("http.proxyPort", "80"))); - - if (System.getProperty("http.proxyUserName") != null) { - state.setProxyCredentials(null, null, new UsernamePasswordCredentials(System.getProperty("http.proxyUserName"), System - .getProperty("http.proxyPassword"))); - } + HttpState state = setHTTPParameters(connection); if (connection.isProxied() && connection.isSecure()) { method = new ConnectMethod(method); @@ -491,11 +486,73 @@ public class MediaWikiConnector { // } // return null; // no success in getting wiki text // } - + + /** + * @param state + * @param connection + */ + private HttpState setHTTPParameters(HttpConnection connection) { + HttpState state = new HttpState(); + Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences(); + String timeout = prefs.getString(WikiEditorPlugin.HTTP_TIMEOUT); + String proxyHost = prefs.getString(WikiEditorPlugin.HTTP_PROXYHOST); + + try { + // timeout after xx seconds + connection.setConnectionTimeout(Integer.parseInt(timeout)); + + if (proxyHost.length() > 0) { + String proxyPort = prefs.getString(WikiEditorPlugin.HTTP_PROXYPORT); + connection.setProxyHost(proxyHost); + connection.setProxyPort(Integer.parseInt(proxyPort)); + + String proxyUserName = prefs.getString(WikiEditorPlugin.HTTP_PROXYUSERNAME); + if (proxyUserName.length() > 0) { + String proxyPassWord = prefs.getString(WikiEditorPlugin.HTTP_PROXYPASSWORD); + state.setProxyCredentials(null, null, new UsernamePasswordCredentials(proxyUserName, proxyPassWord)); + } + } + + } catch (Exception e) { + + } + return state; + } + + private void setHTTPClientParameters(HttpClient client) { + + Preferences prefs = WikiEditorPlugin.getDefault().getPluginPreferences(); + String timeout = prefs.getString(WikiEditorPlugin.HTTP_TIMEOUT); + String proxyHost = prefs.getString(WikiEditorPlugin.HTTP_PROXYHOST); + + try { + // timeout after xx seconds + client.setConnectionTimeout(Integer.parseInt(timeout)); + + if (proxyHost.length() > 0) { + String proxyPort = prefs.getString(WikiEditorPlugin.HTTP_PROXYPORT); + HostConfiguration conf = new HostConfiguration(); + client.setHostConfiguration(conf); + conf.setProxy(proxyHost, Integer.parseInt(proxyPort)); + + String proxyUserName = prefs.getString(WikiEditorPlugin.HTTP_PROXYUSERNAME); + if (proxyUserName.length() > 0) { + HttpState state = new HttpState(); + String proxyPassWord = prefs.getString(WikiEditorPlugin.HTTP_PROXYPASSWORD); + state.setProxyCredentials(null, null, new UsernamePasswordCredentials(proxyUserName, proxyPassWord)); + client.setState(state); + } + } + + } catch (Exception e) { + + } + + } public static void main(String[] args) { MediaWikiConnector mwc = new MediaWikiConnector(); try { - IWikipedia wp = null; + IWikipedia wp = null; ArrayList list = mwc.loadXML(wp, "http://www.plog4u.de/wiki/index.php/Spezial:Export", "Mechanisches Fernsehen\nSynästhesie"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).toString()); @@ -506,7 +563,7 @@ public class MediaWikiConnector { } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); - } + } } } diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java index 099922a..915af72 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/editor/WikiEditorPlugin.java @@ -1,6 +1,7 @@ package net.sourceforge.phpeclipse.wiki.editor; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.MalformedURLException; @@ -8,9 +9,11 @@ import java.net.URL; import java.sql.SQLException; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Enumeration; import java.util.Hashtable; import java.util.List; import java.util.MissingResourceException; +import java.util.PropertyResourceBundle; import java.util.ResourceBundle; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia; @@ -24,6 +27,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IExtension; import org.eclipse.core.runtime.IPluginDescriptor; import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.jface.dialogs.MessageDialog; @@ -48,10 +52,13 @@ public class WikiEditorPlugin extends AbstractUIPlugin { public final static String AUTOMATICALLY_CREATED = ""; public final static String WP_EXTENSION = "wp"; + public final static String XML_START_1 = "\n" - + "\n"; - public final static String XML_END ="\n"; + + public final static String XML_START_2 = "\" ?>\n" + "\n"; + + public final static String XML_END = "\n"; + public static final String PREFIX_LOAD = "Load "; public static final String PREFIX_STORE = "Store "; @@ -129,11 +136,21 @@ public class WikiEditorPlugin extends AbstractUIPlugin { public final static String LOCAL_CSS_URL = "__local_css_url"; public final static String EXPORT_CSS_URL = "__export_css_url"; - + public final static String PREF_STRING_CONFIGURATIONS = "__configurations4"; - + public final static String CONSOLE_OUTPUT = "__console_output"; + public final static String HTTP_TIMEOUT = "http.timeout"; + + public final static String HTTP_PROXYHOST = "http.proxyHost"; + + public final static String HTTP_PROXYPORT = "http.proxyPort"; + + public final static String HTTP_PROXYUSERNAME = "http.proxyUserName"; + + public final static String HTTP_PROXYPASSWORD = "http.proxyPassword"; + public final static String CONFIG_MEMENTO = "" + "" + "" + "" @@ -319,6 +336,17 @@ public class WikiEditorPlugin extends AbstractUIPlugin { return fTemplateStore; } + private static boolean isDefined(String test) { + if (test != null && test.length() > 0) { + for (int i = 0; i < test.length(); i++) { + if (!Character.isWhitespace(test.charAt(i))) { + return false; + } + } + } + return true; + } + /* * (non-Javadoc) * @@ -327,6 +355,80 @@ public class WikiEditorPlugin extends AbstractUIPlugin { protected void initializeDefaultPreferences(IPreferenceStore store) { store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO); store.setDefault(CONSOLE_OUTPUT, "true"); + + String operatingSystem = Platform.getOS(); + // operatingSystem ==> maxosx, linux, solaris, win32,... + try { + InputStream is = getDefault().openStream(new Path("prefs/default_" + operatingSystem + ".properties")); + PropertyResourceBundle resourceBundle = new PropertyResourceBundle(is); + + String key = HTTP_TIMEOUT; + try { + String timeout = resourceBundle.getString(key); + int val = Integer.parseInt(timeout); + if (val > 0) { + val *= 1000; + } else { + val = 30000; + } + store.setDefault(key, Integer.toString(val)); + } catch (Exception e) { + store.setDefault(key, "30000"); // 30 seconds timeout + } + + key = HTTP_PROXYHOST; + try { + String proxyHost = resourceBundle.getString(key); + if (isDefined(proxyHost)) { + store.setDefault(key, proxyHost); + } else { + store.setDefault(key, System.getProperty(key, "")); + } + } catch (Exception e) { + store.setDefault(key, System.getProperty(key, "")); + } + + key = HTTP_PROXYPORT; + try { + String proxyPort = resourceBundle.getString(key); + if (isDefined(proxyPort)) { + // check for integer parse exception: + Integer.parseInt(proxyPort); + store.setDefault(key, proxyPort); + } else { + store.setDefault(key, System.getProperty(key, "80")); + } + } catch (Exception e) { + store.setDefault(key, System.getProperty(key, "80")); + } + + key = HTTP_PROXYUSERNAME; + try { + String proxyUserName = resourceBundle.getString(key); + if (isDefined(proxyUserName)) { + store.setDefault(key, proxyUserName); + } else { + store.setDefault(key, System.getProperty(key, "")); + } + } catch (Exception e) { + store.setDefault(key, System.getProperty(key, "")); + } + + key = HTTP_PROXYPASSWORD; + try { + String proxyPassWord = resourceBundle.getString(key); + if (isDefined(proxyPassWord)) { + store.setDefault(key, proxyPassWord); + } else { + store.setDefault(key, System.getProperty(key, "")); + } + } catch (Exception e) { + store.setDefault(key, System.getProperty(key, "")); + } + + } catch (Exception e) { + } + } /* @@ -381,7 +483,7 @@ public class WikiEditorPlugin extends AbstractUIPlugin { public void reportError(String title, String message) { try { -// Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); + // Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); MessageDialog.openError(null, title, message); } catch (RuntimeException e) { log(e.getLocalizedMessage(), e); -- 1.7.1