import java.util.regex.Pattern;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
-import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.WikipediaDE;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.PageNotEditableException;
import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
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;
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
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");
}
} else if (responseCode == 200 && responseBody.matches(config.getLoginWrongPw()) || responseCode == 200
&& responseBody.matches(config.getLoginNoUser())) {
result = false;
+ if (responseBody.matches(config.getLoginNoUser())) {
+ 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);
+ } else {
+ throw new UnexpectedAnswerException("logout not successful: responseCode == 200");
+ }
} else {
throw new UnexpectedAnswerException("login not successful: " + method.getStatusLine());
}
} else if (responseCode == 200) {
//### should check for a failure message
result = false;
+ throw new UnexpectedAnswerException("logout not successful: responseCode == 200");
} else {
throw new UnexpectedAnswerException("logout not successful: " + method.getStatusLine());
}
return result;
}
- public ArrayList loadXML(IWikipedia config, String actionURL, String pages) throws UnexpectedAnswerException, MethodException {
+ 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);
} 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);
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());
}
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);
// }
// 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());
} catch (UnexpectedAnswerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
- } catch (MethodException e) {
+ } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
- }
+ }
}
}