Kaydet (Commit) 81d31dc3 authored tarafından Michael Stahl's avatar Michael Stahl Kaydeden (comit) Andras Timar

swext: remove commons-httpclient dependency from Wiki Publisher

JRE 6 has sufficient HttpURLConnection etc. stuff to make this work
without bundling external libraries.

Change-Id: I6c71980c718169024006f02a96c442a71d798d55
(cherry picked from commit f7d149a7)
üst c45c2c8f
...@@ -19,13 +19,11 @@ $(eval $(call gb_Extension_use_default_license,wiki-publisher)) ...@@ -19,13 +19,11 @@ $(eval $(call gb_Extension_use_default_license,wiki-publisher))
ifeq ($(SYSTEM_APACHE_COMMONS),) ifeq ($(SYSTEM_APACHE_COMMONS),)
$(eval $(call gb_Extension_use_external_project,wiki-publisher,apache_commons_logging)) $(eval $(call gb_Extension_use_external_project,wiki-publisher,apache_commons_logging))
$(eval $(call gb_Extension_use_external_project,wiki-publisher,apache_commons_codec)) $(eval $(call gb_Extension_use_external_project,wiki-publisher,apache_commons_codec))
$(eval $(call gb_Extension_use_external_project,wiki-publisher,apache_commons_httpclient))
$(eval $(call gb_Extension_use_external_project,wiki-publisher,apache_commons_lang)) $(eval $(call gb_Extension_use_external_project,wiki-publisher,apache_commons_lang))
$(eval $(call gb_Extension_add_file,wiki-publisher,$(if $(filter TRUE,$(HAVE_JAVA6)),commons-codec-1.9.jar,commons-codec-1.6.jar),\ $(eval $(call gb_Extension_add_file,wiki-publisher,$(if $(filter TRUE,$(HAVE_JAVA6)),commons-codec-1.9.jar,commons-codec-1.6.jar),\
$(call gb_UnpackedTarball_get_dir,apache_commons_codec)$(if $(filter TRUE,$(HAVE_JAVA6)),/dist/commons-codec-1.9.jar,/dist/commons-codec-1.6-SNAPSHOT.jar)\ $(call gb_UnpackedTarball_get_dir,apache_commons_codec)$(if $(filter TRUE,$(HAVE_JAVA6)),/dist/commons-codec-1.9.jar,/dist/commons-codec-1.6-SNAPSHOT.jar)\
)) ))
$(eval $(call gb_Extension_add_file,wiki-publisher,commons-httpclient-3.1.jar,$(call gb_UnpackedTarball_get_dir,apache_commons_httpclient)/dist/commons-httpclient.jar))
$(eval $(call gb_Extension_add_file,wiki-publisher,$(if $(filter TRUE,$(HAVE_JAVA6)),commons-lang3-3.3.1.jar,commons-lang-2.4.jar),\ $(eval $(call gb_Extension_add_file,wiki-publisher,$(if $(filter TRUE,$(HAVE_JAVA6)),commons-lang3-3.3.1.jar,commons-lang-2.4.jar),\
$(call gb_UnpackedTarball_get_dir,apache_commons_lang)$(if $(filter TRUE,$(HAVE_JAVA6)),/target/commons-lang3-3.3.1.jar,/dist/commons-lang-2.4.jar)\ $(call gb_UnpackedTarball_get_dir,apache_commons_lang)$(if $(filter TRUE,$(HAVE_JAVA6)),/target/commons-lang3-3.3.1.jar,/dist/commons-lang-2.4.jar)\
)) ))
......
...@@ -17,7 +17,6 @@ $(eval $(call gb_Jar_set_manifest,mediawiki,$(SRCDIR)/swext/mediawiki/src/com/su ...@@ -17,7 +17,6 @@ $(eval $(call gb_Jar_set_manifest,mediawiki,$(SRCDIR)/swext/mediawiki/src/com/su
$(eval $(call gb_Jar_use_externals,mediawiki,\ $(eval $(call gb_Jar_use_externals,mediawiki,\
commons-codec \ commons-codec \
commons-lang \ commons-lang \
commons-httpclient \
commons-logging \ commons-logging \
)) ))
......
...@@ -53,18 +53,10 @@ import com.sun.star.uno.XComponentContext; ...@@ -53,18 +53,10 @@ import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XChangesBatch; import com.sun.star.util.XChangesBatch;
import java.net.*; import java.net.*;
import java.io.*; import java.io.*;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.html.HTMLEditorKit;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class Helper public class Helper
{ {
...@@ -142,8 +134,6 @@ public class Helper ...@@ -142,8 +134,6 @@ public class Helper
private static String[] m_pConfigStrings; private static String[] m_pConfigStrings;
private static MultiThreadedHttpConnectionManager m_aConnectionManager;
private static HttpClient m_aClient;
private static boolean m_bAllowConnection = true; private static boolean m_bAllowConnection = true;
private static Boolean m_bShowInBrowser = null; private static Boolean m_bShowInBrowser = null;
...@@ -174,35 +164,11 @@ public class Helper ...@@ -174,35 +164,11 @@ public class Helper
return m_pConfigStrings[nID]; return m_pConfigStrings[nID];
} }
synchronized private static HttpClient GetHttpClient()
throws WikiCancelException
{
if ( !m_bAllowConnection )
throw new WikiCancelException();
if ( m_aConnectionManager == null )
m_aConnectionManager = new MultiThreadedHttpConnectionManager();
if ( m_aClient == null )
{
m_aClient = new HttpClient( m_aConnectionManager );
m_aClient.getParams().setParameter( "http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY );
m_aClient.getParams().setParameter( "http.protocol.single-cookie-header", Boolean.TRUE );
m_aClient.getParams().setParameter( "http.protocol.content-charset", "UTF-8" );
}
return m_aClient;
}
synchronized protected static void AllowConnection( boolean bAllow ) synchronized protected static void AllowConnection( boolean bAllow )
{ {
m_bAllowConnection = bAllow; m_bAllowConnection = bAllow;
if ( !bAllow && m_aConnectionManager != null ) // TODO: how to shut down any pending connections?
{ // hope it doesn't matter?
m_aClient = null;
m_aConnectionManager.shutdown();
m_aConnectionManager = null;
}
} }
synchronized protected static boolean IsConnectionAllowed() synchronized protected static boolean IsConnectionAllowed()
...@@ -282,11 +248,6 @@ public class Helper ...@@ -282,11 +248,6 @@ public class Helper
return m_xInteractionHandler; return m_xInteractionHandler;
} }
private static Protocol GetOwnHttps( int nPort )
{
return new Protocol( "https", new WikiProtocolSocketFactory(), ( ( nPort < 0 ) ? 443 : nPort ) );
}
protected static String GetMainURL( String sWebPage, String sVURL ) protected static String GetMainURL( String sWebPage, String sVURL )
{ {
String sResultURL = ""; String sResultURL = "";
...@@ -366,9 +327,6 @@ public class Helper ...@@ -366,9 +327,6 @@ public class Helper
} }
protected static String CreateTempFile( XComponentContext xContext ) protected static String CreateTempFile( XComponentContext xContext )
{ {
String sURL = ""; String sURL = "";
...@@ -579,30 +537,30 @@ public class Helper ...@@ -579,30 +537,30 @@ public class Helper
return xNameAccess; return xNameAccess;
} }
private static void SetConfigurationProxy( HostConfiguration aHostConfig, XComponentContext xContext ) private static Proxy GetConfigurationProxy(URI uri, XComponentContext xContext)
{ {
if ( aHostConfig == null || xContext == null ) assert(uri != null);
return; assert(xContext != null);
try try
{ {
XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Inet/Settings" ); XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Inet/Settings" );
int nProxyType = AnyConverter.toInt( xNameAccess.getByName( "ooInetProxyType" ) ); int nProxyType = AnyConverter.toInt( xNameAccess.getByName( "ooInetProxyType" ) );
if ( nProxyType == 0 ) if ( nProxyType == 0 ) {
aHostConfig.setProxyHost( null ); return Proxy.NO_PROXY;
else } else {
{
if ( nProxyType == 1 ) if ( nProxyType == 1 )
{ {
// system proxy // system proxy
return null;
} }
else if ( nProxyType == 2 ) else if ( nProxyType == 2 )
{ {
String aProxyNameProp = "ooInetHTTPProxyName"; String aProxyNameProp = "ooInetHTTPProxyName";
String aProxyPortProp = "ooInetHTTPProxyPort"; String aProxyPortProp = "ooInetHTTPProxyPort";
if ( aHostConfig.getProtocol().getScheme().equals( "https" ) ) if (uri.getScheme().equals("https"))
{ {
aProxyNameProp = "ooInetHTTPSProxyName"; aProxyNameProp = "ooInetHTTPSProxyName";
aProxyPortProp = "ooInetHTTPSProxyPort"; aProxyPortProp = "ooInetHTTPSProxyPort";
...@@ -620,7 +578,8 @@ public class Helper ...@@ -620,7 +578,8 @@ public class Helper
nProxyPort = 80; nProxyPort = 80;
// TODO: check whether the URL is in the NoProxy list // TODO: check whether the URL is in the NoProxy list
aHostConfig.setProxy( aProxyName, nProxyPort ); InetSocketAddress address = new InetSocketAddress(aProxyName, nProxyPort);
return new Proxy(Proxy.Type.HTTP, address);
} }
} }
} }
...@@ -628,6 +587,7 @@ public class Helper ...@@ -628,6 +587,7 @@ public class Helper
{ {
e.printStackTrace(); e.printStackTrace();
} }
return null; // invalid configuration value?
} }
protected static void ShowURLInBrowser( XComponentContext xContext, String sURL ) protected static void ShowURLInBrowser( XComponentContext xContext, String sURL )
...@@ -648,33 +608,70 @@ public class Helper ...@@ -648,33 +608,70 @@ public class Helper
} }
} }
protected static void ExecuteMethod( HttpMethodBase aMethod, HostConfiguration aHostConfig, URI aURI, XComponentContext xContext, boolean bSetHost ) protected static HttpURLConnection PrepareMethod(String method, URI uri, XComponentContext xContext)
throws WikiCancelException, IOException, SSLException throws WikiCancelException, IOException, SSLException
{ {
if ( aMethod != null && aHostConfig != null && aURI != null && xContext != null ) assert(method != null);
{ assert(uri != null);
if ( bSetHost ) assert(xContext != null);
{
aHostConfig.setHost( aURI ); if (!IsConnectionAllowed()) {
SetConfigurationProxy( aHostConfig, xContext ); throw new WikiCancelException();
}
if (java.net.CookieHandler.getDefault() == null) {
// set a cookie manager so cookies don't get lost
// apparently it's not possible to do that on a per-connection
// basis but only globally?
java.net.CookieHandler.setDefault(new java.net.CookieManager());
} }
if ( aHostConfig.getProtocol().getScheme().equals( "https" ) Proxy proxy = GetConfigurationProxy(uri, xContext);
&& AllowUnknownCert( xContext, aURI.getHost() ) ) HttpURLConnection conn = null;
if (proxy != null) {
conn = (HttpURLConnection) uri.toURL().openConnection(proxy);
} else {
conn = (HttpURLConnection) uri.toURL().openConnection();
}
if (uri.getScheme().equals("https") && AllowUnknownCert(xContext, uri.getHost()))
{ {
// let unknown certificates be accepted // let unknown certificates be accepted
{ ((HttpsURLConnection) conn).setSSLSocketFactory(new WikiProtocolSocketFactory());
{
aHostConfig.setHost( aHostConfig.getHost(), ( aURI.getPort() < 0 ? 443 : aURI.getPort() ), Helper.GetOwnHttps( aURI.getPort() ) );
Helper.GetHttpClient().executeMethod( aHostConfig, aMethod );
}
} }
conn.setRequestMethod(method);
// note: don't connect yet so that the caller can do some further setup
return conn;
} }
else
protected static String ReadResponseBody(HttpURLConnection conn)
throws IOException
{ {
Helper.GetHttpClient().executeMethod( aHostConfig, aMethod ); String ret = null;
InputStream stream = conn.getInputStream();
try {
// there doesn't seem to be an easier way get the content encoding
String type = conn.getContentType();
String charset = "ISO-8859-1"; // default in RFC2616
for (String param : type.split(";")) {
if (param.trim().toLowerCase().startsWith("charset=")) {
charset = param.trim().substring("charset=".length());
break;
}
} }
BufferedReader br =
new BufferedReader(new InputStreamReader(stream, charset));
StringBuilder buf = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
buf.append(line);
} }
ret = buf.toString();
} finally {
stream.close();
}
return ret;
} }
private static class HTMLParse extends HTMLEditorKit private static class HTMLParse extends HTMLEditorKit
...@@ -738,76 +735,74 @@ public class Helper ...@@ -738,76 +735,74 @@ public class Helper
return sResult; return sResult;
} }
protected static HostConfiguration Login( URI aMainURL, String sWikiUser, String sWikiPass, XComponentContext xContext ) protected static boolean Login(URI aMainURL, String sWikiUser, String sWikiPass, XComponentContext xContext)
throws java.io.IOException, WikiCancelException throws java.io.IOException, WikiCancelException, URISyntaxException
{ {
HostConfiguration aHostConfig = null; boolean success = false;
if ( sWikiUser != null && sWikiPass != null && xContext != null ) if ( sWikiUser != null && sWikiPass != null && xContext != null )
{ {
HostConfiguration aNewHostConfig = new HostConfiguration();
URI aURI = new URI( aMainURL.toString() + "index.php?title=Special:Userlogin", false );
GetMethod aGetCookie = new GetMethod( aURI.getEscapedPathQuery() );
ExecuteMethod( aGetCookie, aNewHostConfig, aURI, xContext, true );
int nResultCode = aGetCookie.getStatusCode();
String sLoginPage = null; String sLoginPage = null;
if ( nResultCode == 200 ) URI aURI = new URI(aMainURL.toString() + "index.php?title=Special:Userlogin");
sLoginPage = aGetCookie.getResponseBodyAsString(); HttpURLConnection connGet = PrepareMethod("GET", aURI, xContext);
connGet.setInstanceFollowRedirects(true);
aGetCookie.releaseConnection(); connGet.connect();
int nResultCode = connGet.getResponseCode();
if (nResultCode == 200) {
sLoginPage = ReadResponseBody(connGet);
}
if ( sLoginPage != null ) if ( sLoginPage != null )
{ {
String sLoginToken = GetLoginToken( sLoginPage ); String sLoginToken = GetLoginToken( sLoginPage );
PostMethod aPost = new PostMethod(); URI aPostURI = new URI(aMainURL.toString() + "index.php?title=Special:Userlogin&action=submitlogin");
URI aPostURI = new URI( aMainURL.getPath() + "index.php?title=Special:Userlogin&action=submitlogin", false );
aPost.setPath( aPostURI.getEscapedPathQuery() );
aPost.addParameter( "wpName", sWikiUser ); HttpURLConnection connPost = PrepareMethod("POST", aPostURI, xContext);
aPost.addParameter( "wpRemember", "1" ); connPost.setInstanceFollowRedirects(true);
aPost.addParameter( "wpPassword", sWikiPass ); connPost.setDoInput(true);
if ( sLoginToken.length() > 0 ) connPost.setDoOutput(true);
aPost.addParameter( "wpLoginToken", sLoginToken ); connPost.connect();
OutputStreamWriter post = new OutputStreamWriter(connPost.getOutputStream());
post.write("wpName=");
post.write(URLEncoder.encode(sWikiUser, "UTF-8"));
post.write("&wpRemember=1");
post.write("&wpPassword=");
post.write(URLEncoder.encode(sWikiPass, "UTF-8"));
if (sLoginToken.length() > 0) {
post.write("&wpLoginToken=");
post.write(URLEncoder.encode(sLoginToken, "UTF-8"));
}
String[][] pArgs = GetSpecialArgs( xContext, aMainURL.getHost() ); String[][] pArgs = GetSpecialArgs( xContext, aMainURL.getHost() );
if ( pArgs != null ) if ( pArgs != null )
for ( int nArgInd = 0; nArgInd < pArgs.length; nArgInd++ ) for ( int nArgInd = 0; nArgInd < pArgs.length; nArgInd++ )
if ( pArgs[nArgInd].length == 2 && pArgs[nArgInd][0] != null && pArgs[nArgInd][1] != null ) if ( pArgs[nArgInd].length == 2 && pArgs[nArgInd][0] != null && pArgs[nArgInd][1] != null )
aPost.addParameter( pArgs[nArgInd][0], pArgs[nArgInd][1] );
ExecuteMethod( aPost, aNewHostConfig, aPostURI, xContext, false );
nResultCode = aPost.getStatusCode();
while( nResultCode >= 301 && nResultCode <= 303 || nResultCode == 307 )
{ {
String sRedirectURL = aPost.getResponseHeader( "Location" ).getValue(); post.write("&");
aPost.releaseConnection(); post.write(URLEncoder.encode(pArgs[nArgInd][0], "UTF-8"));
post.write("=");
post.write(URLEncoder.encode(pArgs[nArgInd][0], "UTF-8"));
}
aURI = new URI( sRedirectURL ); post.flush();
aPost = new PostMethod(); post.close();
aPost.setPath( aURI.getEscapedPathQuery() );
ExecuteMethod( aPost, aNewHostConfig, aURI, xContext, false );
nResultCode = aPost.getStatusCode(); nResultCode = connPost.getResponseCode();
}
if ( nResultCode == 200 ) if ( nResultCode == 200 )
{ {
String sResult = aPost.getResponseBodyAsString(); String sResult = ReadResponseBody(connPost);
if ( !LoginReportsError( sResult ) ) if ( !LoginReportsError( sResult ) )
aHostConfig = aNewHostConfig; success = true;
} }
aPost.releaseConnection();
} }
} }
return aHostConfig; return success;
} }
protected static String[] GetPasswordsForURLAndUser( XComponentContext xContext, String sURL, String sUserName ) protected static String[] GetPasswordsForURLAndUser( XComponentContext xContext, String sURL, String sUserName )
......
...@@ -19,15 +19,14 @@ ...@@ -19,15 +19,14 @@
package com.sun.star.wiki; package com.sun.star.wiki;
import java.io.StringReader; import java.io.StringReader;
import java.io.OutputStreamWriter;
import java.util.Map; import java.util.Map;
import java.net.URLEncoder;
import java.net.URI;
import java.net.HttpURLConnection;
import javax.swing.text.html.HTMLEditorKit; import javax.swing.text.html.HTMLEditorKit;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import com.sun.star.uno.XComponentContext; import com.sun.star.uno.XComponentContext;
...@@ -47,12 +46,11 @@ public class WikiArticle ...@@ -47,12 +46,11 @@ public class WikiArticle
private final String m_sTitle; private final String m_sTitle;
private final URI m_aMainURI; private final URI m_aMainURI;
private HostConfiguration m_aHostConfig; private boolean m_isLoggedIn = false;
/** Creates a new instance of WikiArticle */ /** Creates a new instance of WikiArticle */
public WikiArticle( XComponentContext xContext, String sTitle, Map<String,String> wikiSettings, boolean bLogin, WikiPropDialog aPropDialog ) public WikiArticle( XComponentContext xContext, String sTitle, Map<String,String> wikiSettings, boolean bLogin, WikiPropDialog aPropDialog )
throws java.net.MalformedURLException, java.io.IOException, WikiCancelException throws java.net.URISyntaxException, java.io.IOException, WikiCancelException
{ {
m_xContext = xContext; m_xContext = xContext;
...@@ -61,7 +59,7 @@ public class WikiArticle ...@@ -61,7 +59,7 @@ public class WikiArticle
m_sWikiPass = wikiSettings.get("Password"); m_sWikiPass = wikiSettings.get("Password");
m_sTitle = sTitle; m_sTitle = sTitle;
m_aMainURI = new URI( sMainUrl, false ); m_aMainURI = new URI(sMainUrl);
if ( bLogin ) if ( bLogin )
{ {
...@@ -112,23 +110,21 @@ public class WikiArticle ...@@ -112,23 +110,21 @@ public class WikiArticle
private String getArticleWiki() private String getArticleWiki()
throws java.io.IOException, WikiCancelException throws java.net.URISyntaxException, java.io.IOException, WikiCancelException
{ {
String sWikiCode = null; String sWikiCode = null;
if ( m_aHostConfig != null ) if (m_isLoggedIn)
{ {
URI aURI = new URI( m_aMainURI.toString() + "index.php?title=" + m_sTitle + "&action=edit", false ); URI aURI = new URI(m_aMainURI.toString() + "index.php?title=" + m_sTitle + "&action=edit");
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() ); HttpURLConnection connGet = Helper.PrepareMethod("GET", aURI, m_xContext);
connGet.connect();
Helper.ExecuteMethod( aRequest, m_aHostConfig, aURI, m_xContext, false );
int nResultCode = aRequest.getStatusCode(); int nResultCode = connGet.getResponseCode();
String sWebPage = null; String sWebPage = null;
if ( nResultCode == 200 ) if (nResultCode == 200) {
sWebPage = aRequest.getResponseBodyAsString(); sWebPage = Helper.ReadResponseBody(connGet);
}
aRequest.releaseConnection();
if ( sWebPage != null ) if ( sWebPage != null )
{ {
...@@ -156,19 +152,19 @@ public class WikiArticle ...@@ -156,19 +152,19 @@ public class WikiArticle
} }
private void InitArticleHTML() private void InitArticleHTML()
throws java.io.IOException, WikiCancelException throws java.net.URISyntaxException, java.io.IOException, WikiCancelException
{ {
if ( m_aHostConfig != null ) if (m_isLoggedIn)
{ {
URI aURI = new URI( m_aMainURI.toString() + "index.php?title=" + m_sTitle, false ); URI uri = new URI(m_aMainURI.toString() + "index.php?title=" + m_sTitle);
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() ); HttpURLConnection connGet = Helper.PrepareMethod("GET", uri, m_xContext);
connGet.connect();
Helper.ExecuteMethod( aRequest, m_aHostConfig, aURI, m_xContext, false );
int nResultCode = aRequest.getStatusCode(); int nResultCode = connGet.getResponseCode();
String sWebPage = null; String sWebPage = null;
if ( nResultCode == 200 ) if (nResultCode == 200) {
sWebPage = aRequest.getResponseBodyAsString(); sWebPage = Helper.ReadResponseBody(connGet);
}
if ( sWebPage != null ) if ( sWebPage != null )
{ {
...@@ -192,36 +188,46 @@ public class WikiArticle ...@@ -192,36 +188,46 @@ public class WikiArticle
} }
protected boolean setArticle( String sWikiCode, String sWikiComment, boolean bMinorEdit ) protected boolean setArticle( String sWikiCode, String sWikiComment, boolean bMinorEdit )
throws java.io.IOException, WikiCancelException throws java.net.URISyntaxException, java.io.IOException, WikiCancelException
{ {
boolean bResult = false; boolean bResult = false;
if ( m_aHostConfig != null && sWikiCode != null && sWikiComment != null ) if (m_isLoggedIn && sWikiCode != null && sWikiComment != null)
{ {
// get the edit time and token // get the edit time and token
getArticleWiki(); getArticleWiki();
URI aURI = new URI( m_aMainURI.getPath() + "index.php?title=" + m_sTitle + "&action=submit", false ); URI uri = new URI(m_aMainURI.toString() + "index.php?title=" + m_sTitle + "&action=submit");
PostMethod aPost = new PostMethod();
aPost.setPath( aURI.getEscapedPathQuery() ); HttpURLConnection connPost = Helper.PrepareMethod("POST", uri, m_xContext);
connPost.setDoInput(true);
connPost.setDoOutput(true);
connPost.connect();
aPost.addParameter( "wpTextbox1", sWikiCode ); OutputStreamWriter post = new OutputStreamWriter(connPost.getOutputStream());
aPost.addParameter( "wpSummary", sWikiComment ); post.write("wpTextbox1=");
aPost.addParameter( "wpSection", "" ); post.write(URLEncoder.encode(sWikiCode, "UTF-8"));
aPost.addParameter( "wpEdittime", m_sEditTime ); post.write("&wpSummary=");
aPost.addParameter( "wpSave", "Save page" ); post.write(URLEncoder.encode(sWikiComment, "UTF-8"));
aPost.addParameter( "wpEditToken", m_sEditToken ); post.write("&wpSection=");
post.write("&wpEdittime=");
post.write(URLEncoder.encode(m_sEditTime, "UTF-8"));
post.write("&wpSave=Save%20page");
post.write("&wpEditToken=");
post.write(URLEncoder.encode(m_sEditToken, "UTF-8"));
if ( bMinorEdit ) if (bMinorEdit) {
aPost.addParameter( "wpMinoredit", "1" ); post.write("&wpMinoredit=1");
}
Helper.ExecuteMethod( aPost, m_aHostConfig, aURI, m_xContext, false ); post.flush();
post.close();
int nResultCode = aPost.getStatusCode(); int nResultCode = connPost.getResponseCode();
if ( nResultCode < 400 ) if ( nResultCode < 400 )
bResult = true; bResult = true;
String aResult = aPost.getResponseBodyAsString(); String aResult = Helper.ReadResponseBody(connPost);
// TODO: remove the debug printing, try to detect the error // TODO: remove the debug printing, try to detect the error
System.out.print( "nSubmitCode = " + nResultCode + "\n===\n" + aResult ); System.out.print( "nSubmitCode = " + nResultCode + "\n===\n" + aResult );
...@@ -231,15 +237,12 @@ public class WikiArticle ...@@ -231,15 +237,12 @@ public class WikiArticle
} }
private boolean Login() private boolean Login()
throws java.io.IOException, WikiCancelException throws java.net.URISyntaxException, java.io.IOException, WikiCancelException
{ {
m_aHostConfig = Helper.Login( m_aMainURI, m_sWikiUser, m_sWikiPass, m_xContext ); m_isLoggedIn = Helper.Login( m_aMainURI, m_sWikiUser, m_sWikiPass, m_xContext );
return ( m_aHostConfig != null ); return m_isLoggedIn;
} }
protected boolean NotExist() protected boolean NotExist()
{ {
boolean bResult = true; boolean bResult = true;
......
...@@ -21,12 +21,10 @@ package com.sun.star.wiki; ...@@ -21,12 +21,10 @@ package com.sun.star.wiki;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.net.URI;
import java.net.HttpURLConnection;
import javax.net.ssl.SSLException; import javax.net.ssl.SSLException;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import com.sun.star.awt.XDialog; import com.sun.star.awt.XDialog;
import com.sun.star.beans.XPropertySet; import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.EventObject; import com.sun.star.lang.EventObject;
...@@ -199,8 +197,6 @@ public class WikiEditSettingDialog extends WikiDialog ...@@ -199,8 +197,6 @@ public class WikiEditSettingDialog extends WikiDialog
String sUserName = ( String ) GetPropSet( "UsernameField" ).getPropertyValue( "Text" ); String sUserName = ( String ) GetPropSet( "UsernameField" ).getPropertyValue( "Text" );
String sPassword = ( String ) GetPropSet( "PasswordField" ).getPropertyValue( "Text" ); String sPassword = ( String ) GetPropSet( "PasswordField" ).getPropertyValue( "Text" );
HostConfiguration aHostConfig = new HostConfiguration();
boolean bInitHost = true;
boolean bAllowIndex = true; boolean bAllowIndex = true;
do do
...@@ -213,20 +209,17 @@ public class WikiEditSettingDialog extends WikiDialog ...@@ -213,20 +209,17 @@ public class WikiEditSettingDialog extends WikiDialog
if ( sURL.length() > 0 ) if ( sURL.length() > 0 )
{ {
URI aURI = new URI( sURL, false ); URI aURI = new URI(sURL);
GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() ); HttpURLConnection connGet = Helper.PrepareMethod("GET", aURI, m_xContext);
aRequest.setFollowRedirects( false ); connGet.setInstanceFollowRedirects(false);
Helper.ExecuteMethod( aRequest, aHostConfig, aURI, m_xContext, bInitHost ); connGet.connect();
bInitHost = false;
int nResultCode = aRequest.getStatusCode(); int nResultCode = connGet.getResponseCode();
String sWebPage = null; String sWebPage = null;
if ( nResultCode == 200 ) if ( nResultCode == 200 )
sWebPage = aRequest.getResponseBodyAsString(); sWebPage = Helper.ReadResponseBody(connGet);
else if ( nResultCode >= 301 && nResultCode <= 303 || nResultCode == 307 ) else if ( nResultCode >= 301 && nResultCode <= 303 || nResultCode == 307 )
sRedirectURL = aRequest.getResponseHeader( "Location" ).getValue(); sRedirectURL = connGet.getHeaderField("Location");
aRequest.releaseConnection();
if ( sWebPage != null && sWebPage.length() > 0 ) if ( sWebPage != null && sWebPage.length() > 0 )
{ {
...@@ -252,10 +245,10 @@ public class WikiEditSettingDialog extends WikiDialog ...@@ -252,10 +245,10 @@ public class WikiEditSettingDialog extends WikiDialog
} }
else else
{ {
URI aMainURI = new URI( sMainURL, true ); // it must be an escaped URL, otherwise an exception should be thrown URI aMainURI = new URI(sMainURL);
if ( ( sUserName.length() > 0 || sPassword.length() > 0 ) if ( ( sUserName.length() > 0 || sPassword.length() > 0 )
&& Helper.Login( aMainURI, sUserName, sPassword, m_xContext ) == null ) && !Helper.Login(aMainURI, sUserName, sPassword, m_xContext))
{ {
// a wrong login information is provided // a wrong login information is provided
// show error // show error
...@@ -268,7 +261,7 @@ public class WikiEditSettingDialog extends WikiDialog ...@@ -268,7 +261,7 @@ public class WikiEditSettingDialog extends WikiDialog
} }
else else
{ {
setting.put( "Url", aMainURI.getEscapedURI() ); setting.put( "Url", aMainURI.toASCIIString() );
setting.put( "Username", sUserName ); setting.put( "Username", sUserName );
setting.put( "Password", sPassword ); setting.put( "Password", sPassword );
if ( addMode ) if ( addMode )
......
...@@ -25,17 +25,14 @@ import java.net.Socket; ...@@ -25,17 +25,14 @@ import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.security.KeyStore; import java.security.KeyStore;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.HttpClientError;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
class WikiProtocolSocketFactory implements SecureProtocolSocketFactory class WikiProtocolSocketFactory extends SSLSocketFactory
{ {
private SSLContext m_aSSLContext; private SSLContext m_aSSLContext;
...@@ -105,42 +102,58 @@ class WikiProtocolSocketFactory implements SecureProtocolSocketFactory ...@@ -105,42 +102,58 @@ class WikiProtocolSocketFactory implements SecureProtocolSocketFactory
} }
if ( m_aSSLContext == null ) if ( m_aSSLContext == null )
throw new HttpClientError(); throw new RuntimeException("failed to create SSLContext");
return m_aSSLContext; return m_aSSLContext;
} }
public Socket createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort ) @Override
throws IOException, UnknownHostException public Socket createSocket(InetAddress address, int port)
throws IOException
{ {
return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort, clientHost, clientPort ); return GetNotSoSecureSSLContext().getSocketFactory().createSocket(address, port);
} }
public Socket createSocket( final String sHost, final int nPort, final InetAddress aLocalAddress, final int nLocalPort, final HttpConnectionParams params ) @Override
throws IOException, UnknownHostException, ConnectTimeoutException public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
throws IOException
{ {
if ( params == null ) return GetNotSoSecureSSLContext().getSocketFactory().createSocket(address, port, localAddress, localPort);
return createSocket( sHost, nPort, aLocalAddress, nLocalPort ); }
int nTimeout = params.getConnectionTimeout(); @Override
Socket aSocket = GetNotSoSecureSSLContext().getSocketFactory().createSocket(); public Socket createSocket( String sHost, int nPort, InetAddress clientHost, int clientPort )
aSocket.bind( new InetSocketAddress( aLocalAddress, nLocalPort ) ); throws IOException, UnknownHostException
aSocket.connect( new InetSocketAddress( sHost, nPort ), nTimeout ); {
return aSocket; return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort, clientHost, clientPort );
} }
@Override
public Socket createSocket( String sHost, int nPort ) public Socket createSocket( String sHost, int nPort )
throws IOException, UnknownHostException throws IOException, UnknownHostException
{ {
return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort ); return GetNotSoSecureSSLContext().getSocketFactory().createSocket( sHost, nPort );
} }
@Override
public Socket createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose ) public Socket createSocket( Socket aSocket, String sHost, int nPort, boolean bAutoClose )
throws IOException, UnknownHostException throws IOException
{ {
return GetNotSoSecureSSLContext().getSocketFactory().createSocket( aSocket, sHost, nPort, bAutoClose ); return GetNotSoSecureSSLContext().getSocketFactory().createSocket( aSocket, sHost, nPort, bAutoClose );
} }
@Override
public String[] getDefaultCipherSuites()
{
return GetNotSoSecureSSLContext().getSocketFactory().getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites()
{
return GetNotSoSecureSSLContext().getSocketFactory().getSupportedCipherSuites();
}
@Override @Override
public boolean equals(Object obj) public boolean equals(Object obj)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment