2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com
.sun
.star
.wiki
;
21 import com
.sun
.star
.awt
.MessageBoxButtons
;
22 import com
.sun
.star
.awt
.MessageBoxType
;
23 import com
.sun
.star
.awt
.XControl
;
24 import com
.sun
.star
.awt
.XDialog
;
25 import com
.sun
.star
.awt
.XMessageBox
;
26 import com
.sun
.star
.awt
.XMessageBoxFactory
;
27 import com
.sun
.star
.awt
.XWindowPeer
;
28 import com
.sun
.star
.beans
.NamedValue
;
29 import com
.sun
.star
.beans
.PropertyValue
;
30 import com
.sun
.star
.beans
.XPropertySet
;
31 import com
.sun
.star
.container
.XContainerQuery
;
32 import com
.sun
.star
.container
.XEnumeration
;
33 import com
.sun
.star
.container
.XNameAccess
;
34 import com
.sun
.star
.container
.XNameContainer
;
35 import com
.sun
.star
.document
.XDocumentPropertiesSupplier
;
36 import com
.sun
.star
.document
.XDocumentProperties
;
37 import com
.sun
.star
.frame
.XModel
;
38 import com
.sun
.star
.frame
.XModuleManager
;
39 import com
.sun
.star
.io
.XInputStream
;
40 import com
.sun
.star
.io
.XOutputStream
;
41 import com
.sun
.star
.lang
.XMultiComponentFactory
;
42 import com
.sun
.star
.lang
.XMultiServiceFactory
;
43 import com
.sun
.star
.lang
.XComponent
;
44 import com
.sun
.star
.system
.SystemShellExecuteFlags
;
45 import com
.sun
.star
.system
.XSystemShellExecute
;
46 import com
.sun
.star
.task
.UrlRecord
;
47 import com
.sun
.star
.task
.XInteractionHandler
;
48 import com
.sun
.star
.task
.XMasterPasswordHandling
;
49 import com
.sun
.star
.task
.XPasswordContainer
;
50 import com
.sun
.star
.uno
.AnyConverter
;
51 import com
.sun
.star
.uno
.UnoRuntime
;
52 import com
.sun
.star
.uno
.XComponentContext
;
53 import com
.sun
.star
.util
.XChangesBatch
;
56 import javax
.net
.ssl
.SSLException
;
57 import javax
.swing
.text
.html
.HTMLEditorKit
;
59 import org
.apache
.commons
.httpclient
.HttpClient
;
60 import org
.apache
.commons
.httpclient
.HostConfiguration
;
61 import org
.apache
.commons
.httpclient
.HttpMethodBase
;
62 import org
.apache
.commons
.httpclient
.cookie
.CookiePolicy
;
63 import org
.apache
.commons
.httpclient
.MultiThreadedHttpConnectionManager
;
64 import org
.apache
.commons
.httpclient
.protocol
.Protocol
;
65 import org
.apache
.commons
.httpclient
.URI
;
66 import org
.apache
.commons
.httpclient
.methods
.GetMethod
;
67 import org
.apache
.commons
.httpclient
.methods
.PostMethod
;
71 public final static int GENERALSEND_ERROR
= 0;
72 public final static int NOWIKIFILTER_ERROR
= 1;
73 public final static int NOURLCONNECTION_ERROR
= 2;
74 public final static int WRONGLOGIN_ERROR
= 3;
75 public final static int INVALIDURL_ERROR
= 4;
76 public final static int NOURL_ERROR
= 5;
78 public final static int DLG_SENDTITLE
= 6;
79 public final static int DLG_WIKIARTICLE
= 7;
81 public final static int DLG_OK
= 9;
84 public final static int DLG_ADDBUTTON
= 12;
85 public final static int DLG_EDITBUTTON
= 13;
86 public final static int DLG_SENDBUTTON
= 14;
87 public final static int DLG_REMOVEBUTTON
= 15;
89 public final static int DLG_EDITSETTING_URLLABEL
= 16;
90 public final static int DLG_EDITSETTING_USERNAMELABEL
= 17;
91 public final static int DLG_EDITSETTING_PASSWORDLABEL
= 18;
93 public final static int DLG_SENDTOMEDIAWIKI_LABEL1
= 20;
94 public final static int DLG_SENDTOMEDIAWIKI_LABEL2
= 21;
95 public final static int DLG_SENDTOMEDIAWIKI_LABEL3
= 22;
96 public final static int DLG_SENDTOMEDIAWIKI_MINORCHECK
= 23;
97 public final static int DLG_SENDTOMEDIAWIKI_BROWSERCHECK
= 24;
98 public final static int UNKNOWNCERT_ERROR
= 25;
99 public final static int DLG_MEDIAWIKI_TITLE
= 26;
100 public final static int DLG_EDITSETTING_ACCOUNTLINE
= 27;
101 public final static int DLG_EDITSETTING_WIKILINE
= 28;
102 public final static int DLG_EDITSETTING_SAVEBOX
= 29;
103 public final static int CANCELSENDING_ERROR
= 30;
104 public final static int DLG_MEDIAWIKIEXTENSION_STRING
= 31;
105 public final static int DLG_WIKIPAGEEXISTS_LABEL1
= 32;
107 private final static int STRINGS_NUM
= 33;
109 private final static String
[] m_pEntryNames
= { "GeneralSendError",
125 "Dlg_EditSetting_UrlLabel",
126 "Dlg_EditSetting_UsernameLabel",
127 "Dlg_EditSetting_PasswordLabel",
128 "Dlg_NewWikiPage_Label1",
129 "Dlg_SendToMediaWiki_Label1",
130 "Dlg_SendToMediaWiki_Label2",
131 "Dlg_SendToMediaWiki_Label3",
132 "Dlg_SendToMediaWiki_MinorCheck",
133 "Dlg_SendToMediaWiki_BrowserCheck",
135 "Dlg_MediaWiki_Title",
136 "Dlg_EditSetting_AccountLine",
137 "Dlg_EditSetting_WikiLine",
138 "Dlg_EditSetting_SaveBox",
140 "Dlg_MediaWiki_Extension_String",
141 "Dlg_WikiPageExists_Label1" };
143 private static String
[] m_pConfigStrings
;
145 private static MultiThreadedHttpConnectionManager m_aConnectionManager
;
146 private static HttpClient m_aClient
;
147 private static boolean m_bAllowConnection
= true;
149 private static Boolean m_bShowInBrowser
= null;
151 private static XPasswordContainer m_xPasswordContainer
;
152 private static XInteractionHandler m_xInteractionHandler
;
154 synchronized protected static String
GetLocalizedString( XComponentContext xContext
, int nID
)
155 throws com
.sun
.star
.uno
.Exception
157 if ( nID
>= STRINGS_NUM
)
158 throw new com
.sun
.star
.uno
.RuntimeException();
160 if ( m_pConfigStrings
== null )
162 XNameAccess xNameAccess
= GetConfigNameAccess( xContext
, "org.openoffice.Office.Custom.WikiExtension/Strings" );
164 String
[] pStrings
= new String
[STRINGS_NUM
];
165 for ( int nInd
= 0; nInd
< STRINGS_NUM
; nInd
++ )
166 if ( m_pEntryNames
[nInd
] != null )
167 pStrings
[nInd
] = AnyConverter
.toString( xNameAccess
.getByName( m_pEntryNames
[nInd
] ) );
171 m_pConfigStrings
= pStrings
;
174 return m_pConfigStrings
[nID
];
177 synchronized private static HttpClient
GetHttpClient()
178 throws WikiCancelException
180 if ( !m_bAllowConnection
)
181 throw new WikiCancelException();
183 if ( m_aConnectionManager
== null )
184 m_aConnectionManager
= new MultiThreadedHttpConnectionManager();
186 if ( m_aClient
== null )
188 m_aClient
= new HttpClient( m_aConnectionManager
);
189 m_aClient
.getParams().setParameter( "http.protocol.cookie-policy", CookiePolicy
.BROWSER_COMPATIBILITY
);
190 m_aClient
.getParams().setParameter( "http.protocol.single-cookie-header", Boolean
.TRUE
);
191 m_aClient
.getParams().setParameter( "http.protocol.content-charset", "UTF-8" );
197 synchronized protected static void AllowConnection( boolean bAllow
)
199 m_bAllowConnection
= bAllow
;
200 if ( !bAllow
&& m_aConnectionManager
!= null )
203 m_aConnectionManager
.shutdown();
204 m_aConnectionManager
= null;
208 synchronized protected static boolean IsConnectionAllowed()
210 return m_bAllowConnection
;
213 synchronized protected static boolean GetShowInBrowserByDefault( XComponentContext xContext
)
215 if ( m_bShowInBrowser
== null )
219 XNameAccess xAccess
= Helper
.GetConfigNameAccess( xContext
, "org.openoffice.Office.Custom.WikiExtension/Settings" );
220 m_bShowInBrowser
= Boolean
.valueOf( AnyConverter
.toBoolean( xAccess
.getByName( "PreselectShowBrowser" ) ) );
222 catch( com
.sun
.star
.uno
.Exception e
)
228 return m_bShowInBrowser
.booleanValue();
231 synchronized protected static void SetShowInBrowserByDefault( XComponentContext xContext
, boolean bValue
)
235 m_bShowInBrowser
= Boolean
.valueOf( bValue
);
237 XPropertySet xProps
= Helper
.GetConfigProps( xContext
, "org.openoffice.Office.Custom.WikiExtension/Settings" );
238 xProps
.setPropertyValue( "PreselectShowBrowser", Boolean
.valueOf( bValue
) );
239 XChangesBatch xBatch
= UnoRuntime
.queryInterface( XChangesBatch
.class, xProps
);
240 if ( xBatch
!= null )
241 xBatch
.commitChanges();
249 synchronized protected static XPasswordContainer
GetPasswordContainer( XComponentContext xContext
)
250 throws com
.sun
.star
.uno
.Exception
252 if ( m_xPasswordContainer
== null && xContext
!= null )
254 XMultiComponentFactory xFactory
= xContext
.getServiceManager();
255 if ( xFactory
!= null )
256 m_xPasswordContainer
= UnoRuntime
.queryInterface(
257 XPasswordContainer
.class,
258 xFactory
.createInstanceWithContext( "com.sun.star.task.PasswordContainer", xContext
) );
261 if ( m_xPasswordContainer
== null )
262 throw new com
.sun
.star
.uno
.RuntimeException();
264 return m_xPasswordContainer
;
267 synchronized protected static XInteractionHandler
GetInteractionHandler( XComponentContext xContext
)
268 throws com
.sun
.star
.uno
.Exception
270 if ( m_xInteractionHandler
== null && xContext
!= null )
272 XMultiComponentFactory xFactory
= xContext
.getServiceManager();
273 if ( xFactory
!= null )
274 m_xInteractionHandler
= UnoRuntime
.queryInterface(
275 XInteractionHandler
.class,
276 xFactory
.createInstanceWithContext( "com.sun.star.task.InteractionHandler", xContext
) );
279 if ( m_xInteractionHandler
== null )
280 throw new com
.sun
.star
.uno
.RuntimeException();
282 return m_xInteractionHandler
;
285 private static Protocol
GetOwnHttps( int nPort
)
287 return new Protocol( "https", new WikiProtocolSocketFactory(), ( ( nPort
< 0 ) ?
443 : nPort
) );
290 protected static String
GetMainURL( String sWebPage
, String sVURL
)
292 String sResultURL
= "";
295 StringReader aReader
= new StringReader( sWebPage
);
296 HTMLEditorKit
.Parser aParser
= GetHTMLParser();
297 EditPageParser aCallback
= new EditPageParser();
299 aParser
.parse( aReader
, aCallback
, true );
300 sResultURL
= aCallback
.m_sMainURL
;
302 if ( !sResultURL
.startsWith( "http" ) )
304 //if the url is only relative then complete it
305 URL aURL
= new URL( sVURL
);
306 sResultURL
= aURL
.getProtocol() + "://" + aURL
.getHost() + sResultURL
;
314 if ( sResultURL
.length() == 0 )
316 // usually that should not happen
317 // workaround: try to get index.php from the provided URL
318 int nIndex
= sVURL
.indexOf( "index.php" );
320 sResultURL
= sVURL
.substring( 0, nIndex
);
326 protected static String
GetRedirectURL( String sWebPage
, String sURL
)
328 //scrape the HTML source and find the EditURL
329 // TODO/LATER: Use parser in future
331 int nInd
= sWebPage
.indexOf( "http-equiv=\"refresh\"" );
335 String sResultURL
= "";
336 int nContent
= sWebPage
.indexOf( "content=", nInd
);
339 int nURL
= sWebPage
.indexOf( "URL=", nContent
);
342 int nEndURL
= sWebPage
.indexOf('"', nURL
);
344 sResultURL
= sWebPage
.substring( nURL
+ 4, nEndURL
);
350 URL aURL
= new URL( sURL
);
351 if ( !sResultURL
.startsWith( aURL
.getProtocol() ))
353 //if the url is only relative then complete it
354 if ( sResultURL
.startsWith( "/" ) )
355 sResultURL
= aURL
.getProtocol() + "://" + aURL
.getHost() + sResultURL
;
357 sResultURL
= aURL
.getProtocol() + "://" + aURL
.getHost() + aURL
.getPath() + sResultURL
;
360 catch ( MalformedURLException ex
)
362 ex
.printStackTrace();
372 protected static String
CreateTempFile( XComponentContext xContext
)
377 Object oTempFile
= xContext
.getServiceManager().createInstanceWithContext( "com.sun.star.io.TempFile", xContext
);
378 XPropertySet xPropertySet
= UnoRuntime
.queryInterface( XPropertySet
.class, oTempFile
);
379 xPropertySet
.setPropertyValue( "RemoveFile", Boolean
.FALSE
);
380 sURL
= ( String
) xPropertySet
.getPropertyValue( "Uri" );
382 XInputStream xInputStream
= UnoRuntime
.queryInterface( XInputStream
.class, oTempFile
);
383 xInputStream
.closeInput();
384 XOutputStream xOutputStream
= UnoRuntime
.queryInterface( XOutputStream
.class, oTempFile
);
385 xOutputStream
.closeOutput();
386 } catch ( com
.sun
.star
.uno
.Exception ex
)
388 ex
.printStackTrace();
393 protected static String
EachLine( String sURL
)
398 URL aURL
= new URL( sURL
);
399 File aFile
= new File( aURL
.getFile() );
400 InputStreamReader aInputReader
= new InputStreamReader( new FileInputStream( aFile
), "UTF-8" );
401 BufferedReader aBufReader
= new BufferedReader( aInputReader
);
403 StringBuffer aBuf
= new StringBuffer();
404 String sEachLine
= aBufReader
.readLine();
406 while( sEachLine
!= null )
408 aBuf
.append( sEachLine
);
411 sEachLine
= aBufReader
.readLine();
413 sText
= aBuf
.toString();
416 } catch ( Exception e
)
423 protected static String
GetDocTitle( XModel xDoc
)
425 XDocumentPropertiesSupplier xDocPropSup
=
426 UnoRuntime
.queryInterface(XDocumentPropertiesSupplier
.class, xDoc
);
427 XDocumentProperties xDocProps
= xDocPropSup
.getDocumentProperties();
428 return xDocProps
.getTitle();
431 protected static void SetDocTitle( XModel xDoc
, String sTitle
)
433 XDocumentPropertiesSupplier xDocPropSup
=
434 UnoRuntime
.queryInterface(XDocumentPropertiesSupplier
.class, xDoc
);
435 XDocumentProperties xDocProps
= xDocPropSup
.getDocumentProperties();
436 xDocProps
.setTitle(sTitle
);
439 protected static String
GetDocServiceName( XComponentContext xContext
, XModel xModel
)
441 String aDocServiceName
= "";
442 if ( xModel
!= null && xContext
!= null )
446 XMultiComponentFactory xFactory
= xContext
.getServiceManager();
447 if ( xFactory
== null )
448 throw new com
.sun
.star
.uno
.RuntimeException();
450 Object oModuleManager
= xFactory
.createInstanceWithContext( "com.sun.star.frame.ModuleManager", xContext
);
451 XModuleManager xModuleManager
= UnoRuntime
.queryInterface( XModuleManager
.class, oModuleManager
);
452 if ( xModuleManager
!= null )
453 aDocServiceName
= xModuleManager
.identify( xModel
);
455 catch( java
.lang
.Exception e
)
461 return aDocServiceName
;
464 protected static String
GetFilterName( XComponentContext xContext
, String aTypeName
, String aDocServiceName
)
466 if ( xContext
== null || aTypeName
== null || aTypeName
.length() == 0
467 || aDocServiceName
== null || aDocServiceName
.length() == 0 ) {
470 String aFilterName
= "";
473 Object oFilterFactory
= xContext
.getServiceManager().createInstanceWithContext( "com.sun.star.document.FilterFactory", xContext
);
474 XContainerQuery xQuery
= UnoRuntime
.queryInterface( XContainerQuery
.class, oFilterFactory
);
475 if ( xQuery
!= null )
477 NamedValue
[] aRequest
= new NamedValue
[2];
478 aRequest
[0] = new NamedValue( "Type", aTypeName
);
479 aRequest
[1] = new NamedValue( "DocumentService", aDocServiceName
);
481 XEnumeration xSet
= xQuery
.createSubSetEnumerationByProperties( aRequest
);
484 boolean bAcceptable
= false;
485 while ( xSet
.hasMoreElements() && !bAcceptable
)
487 PropertyValue
[] pFilterProps
= ( PropertyValue
[] )AnyConverter
.toArray( xSet
.nextElement() );
488 if ( pFilterProps
!= null )
490 int nLen
= pFilterProps
.length
;
491 String aTmpFilter
= null;
493 for ( int nInd
= 0; nInd
< nLen
; nInd
++ )
495 if ( pFilterProps
[nInd
].Name
.equals( "Name" ) )
496 aTmpFilter
= AnyConverter
.toString( pFilterProps
[nInd
].Value
);
497 else if ( pFilterProps
[nInd
].Name
.equals( "Flags" ) )
498 bAcceptable
= ( ( AnyConverter
.toInt( pFilterProps
[nInd
].Value
) & 2 ) == 2 ); // must allow export
502 aFilterName
= aTmpFilter
;
508 catch( java
.lang
.Exception e
)
516 private static XMultiServiceFactory
GetConfigurationProvider( XComponentContext xContext
)
517 throws com
.sun
.star
.uno
.Exception
519 XMultiServiceFactory xConfigurationProvider
= null;
520 if ( xContext
!= null )
522 XMultiComponentFactory xFactory
= xContext
.getServiceManager();
523 Object oConfigProvider
= xFactory
.createInstanceWithContext( "com.sun.star.configuration.ConfigurationProvider", xContext
);
524 xConfigurationProvider
= UnoRuntime
.queryInterface( XMultiServiceFactory
.class, oConfigProvider
);
527 if ( xConfigurationProvider
== null )
528 throw new com
.sun
.star
.uno
.RuntimeException();
530 return xConfigurationProvider
;
533 private static Object
GetConfig( XComponentContext xContext
, String sNodepath
, boolean bWriteAccess
)
534 throws com
.sun
.star
.uno
.Exception
536 if ( xContext
== null || sNodepath
== null )
537 throw new com
.sun
.star
.uno
.RuntimeException();
539 PropertyValue aVal
= new PropertyValue();
540 aVal
.Name
= "nodepath";
541 aVal
.Value
= sNodepath
;
542 Object
[] aArgs
= new Object
[1];
545 return GetConfigurationProvider( xContext
).createInstanceWithArguments(
546 ( bWriteAccess ?
"com.sun.star.configuration.ConfigurationUpdateAccess"
547 : "com.sun.star.configuration.ConfigurationAccess" ),
551 private static XPropertySet
GetConfigProps( XComponentContext xContext
, String sNodepath
)
552 throws com
.sun
.star
.uno
.Exception
554 XPropertySet xProps
= UnoRuntime
.queryInterface( XPropertySet
.class, GetConfig( xContext
, sNodepath
, true ) );
555 if ( xProps
== null )
556 throw new com
.sun
.star
.uno
.RuntimeException();
562 protected static XNameContainer
GetConfigNameContainer( XComponentContext xContext
, String sNodepath
)
563 throws com
.sun
.star
.uno
.Exception
565 XNameContainer xContainer
= UnoRuntime
.queryInterface( XNameContainer
.class, GetConfig( xContext
, sNodepath
, true ) );
566 if ( xContainer
== null )
567 throw new com
.sun
.star
.uno
.RuntimeException();
572 protected static XNameAccess
GetConfigNameAccess( XComponentContext xContext
, String sNodepath
)
573 throws com
.sun
.star
.uno
.Exception
575 XNameAccess xNameAccess
= UnoRuntime
.queryInterface( XNameAccess
.class, GetConfig( xContext
, sNodepath
, false ) );
576 if ( xNameAccess
== null )
577 throw new com
.sun
.star
.uno
.RuntimeException();
582 private static void SetConfigurationProxy( HostConfiguration aHostConfig
, XComponentContext xContext
)
584 if ( aHostConfig
== null || xContext
== null )
589 XNameAccess xNameAccess
= GetConfigNameAccess( xContext
, "org.openoffice.Inet/Settings" );
591 int nProxyType
= AnyConverter
.toInt( xNameAccess
.getByName( "ooInetProxyType" ) );
592 if ( nProxyType
== 0 )
593 aHostConfig
.setProxyHost( null );
596 if ( nProxyType
== 1 )
600 else if ( nProxyType
== 2 )
602 String aProxyNameProp
= "ooInetHTTPProxyName";
603 String aProxyPortProp
= "ooInetHTTPProxyPort";
605 if ( aHostConfig
.getProtocol().getScheme().equals( "https" ) )
607 aProxyNameProp
= "ooInetHTTPSProxyName";
608 aProxyPortProp
= "ooInetHTTPSProxyPort";
611 String aProxyName
= AnyConverter
.toString( xNameAccess
.getByName( aProxyNameProp
) );
615 Object aPortNo
= xNameAccess
.getByName( aProxyPortProp
);
616 if ( !AnyConverter
.isVoid( aPortNo
) )
617 nProxyPort
= AnyConverter
.toInt( aPortNo
);
619 if ( nProxyPort
== -1 )
622 // TODO: check whether the URL is in the NoProxy list
623 aHostConfig
.setProxy( aProxyName
, nProxyPort
);
627 catch( java
.lang
.Exception e
)
633 protected static void ShowURLInBrowser( XComponentContext xContext
, String sURL
)
635 if ( xContext
!= null && sURL
!= null && sURL
.length() > 0 )
639 Object oSystemShell
= xContext
.getServiceManager().createInstanceWithContext( "com.sun.star.system.SystemShellExecute", xContext
);
640 XSystemShellExecute xSystemShell
= UnoRuntime
.queryInterface( XSystemShellExecute
.class, oSystemShell
);
641 if ( xSystemShell
!= null )
642 xSystemShell
.execute( sURL
, "", SystemShellExecuteFlags
.URIS_ONLY
);
651 protected static void ExecuteMethod( HttpMethodBase aMethod
, HostConfiguration aHostConfig
, URI aURI
, XComponentContext xContext
, boolean bSetHost
)
652 throws WikiCancelException
, IOException
, SSLException
654 if ( aMethod
!= null && aHostConfig
!= null && aURI
!= null && xContext
!= null )
658 aHostConfig
.setHost( aURI
);
659 SetConfigurationProxy( aHostConfig
, xContext
);
662 if ( aHostConfig
.getProtocol().getScheme().equals( "https" )
663 && AllowUnknownCert( xContext
, aURI
.getHost() ) )
665 // let unknown certificates be accepted
668 aHostConfig
.setHost( aHostConfig
.getHost(), ( aURI
.getPort() < 0 ?
443 : aURI
.getPort() ), Helper
.GetOwnHttps( aURI
.getPort() ) );
669 Helper
.GetHttpClient().executeMethod( aHostConfig
, aMethod
);
675 Helper
.GetHttpClient().executeMethod( aHostConfig
, aMethod
);
680 static private class HTMLParse
extends HTMLEditorKit
684 public HTMLEditorKit
.Parser
getParser()
686 return super.getParser();
690 static protected HTMLEditorKit
.Parser
GetHTMLParser()
692 return new HTMLParse().getParser();
695 static private boolean LoginReportsError( String sRespond
)
697 boolean bResult
= true;
698 if ( sRespond
!= null )
702 StringReader aReader
= new StringReader( sRespond
);
703 HTMLEditorKit
.Parser aParser
= GetHTMLParser();
704 EditPageParser aCallback
= new EditPageParser();
706 aParser
.parse( aReader
, aCallback
, true );
707 bResult
= ( aCallback
.m_nErrorInd
>= 0 );
718 static private String
GetLoginToken( String sLoginPage
)
721 if ( sLoginPage
!= null && sLoginPage
.length() > 0 )
725 StringReader aReader
= new StringReader( sLoginPage
);
726 HTMLEditorKit
.Parser aParser
= Helper
.GetHTMLParser();
727 EditPageParser aCallbacks
= new EditPageParser();
729 aParser
.parse( aReader
, aCallbacks
, true );
730 sResult
= aCallbacks
.m_sLoginToken
;
741 static protected HostConfiguration
Login( URI aMainURL
, String sWikiUser
, String sWikiPass
, XComponentContext xContext
)
742 throws java
.io
.IOException
, WikiCancelException
744 HostConfiguration aHostConfig
= null;
746 if ( sWikiUser
!= null && sWikiPass
!= null && xContext
!= null )
748 HostConfiguration aNewHostConfig
= new HostConfiguration();
750 URI aURI
= new URI( aMainURL
.toString() + "index.php?title=Special:Userlogin", false );
751 GetMethod aGetCookie
= new GetMethod( aURI
.getEscapedPathQuery() );
753 ExecuteMethod( aGetCookie
, aNewHostConfig
, aURI
, xContext
, true );
755 int nResultCode
= aGetCookie
.getStatusCode();
756 String sLoginPage
= null;
757 if ( nResultCode
== 200 )
758 sLoginPage
= aGetCookie
.getResponseBodyAsString();
760 aGetCookie
.releaseConnection();
762 if ( sLoginPage
!= null )
764 String sLoginToken
= GetLoginToken( sLoginPage
);
766 PostMethod aPost
= new PostMethod();
767 URI aPostURI
= new URI( aMainURL
.getPath() + "index.php?title=Special:Userlogin&action=submitlogin", false );
768 aPost
.setPath( aPostURI
.getEscapedPathQuery() );
770 aPost
.addParameter( "wpName", sWikiUser
);
771 aPost
.addParameter( "wpRemember", "1" );
772 aPost
.addParameter( "wpPassword", sWikiPass
);
773 if ( sLoginToken
.length() > 0 )
774 aPost
.addParameter( "wpLoginToken", sLoginToken
);
776 String
[][] pArgs
= GetSpecialArgs( xContext
, aMainURL
.getHost() );
778 for ( int nArgInd
= 0; nArgInd
< pArgs
.length
; nArgInd
++ )
779 if ( pArgs
[nArgInd
].length
== 2 && pArgs
[nArgInd
][0] != null && pArgs
[nArgInd
][1] != null )
780 aPost
.addParameter( pArgs
[nArgInd
][0], pArgs
[nArgInd
][1] );
782 ExecuteMethod( aPost
, aNewHostConfig
, aPostURI
, xContext
, false );
784 nResultCode
= aPost
.getStatusCode();
786 while( nResultCode
>= 301 && nResultCode
<= 303 || nResultCode
== 307 )
788 String sRedirectURL
= aPost
.getResponseHeader( "Location" ).getValue();
789 aPost
.releaseConnection();
791 aURI
= new URI( sRedirectURL
);
792 aPost
= new PostMethod();
793 aPost
.setPath( aURI
.getEscapedPathQuery() );
794 ExecuteMethod( aPost
, aNewHostConfig
, aURI
, xContext
, false );
796 nResultCode
= aPost
.getStatusCode();
799 if ( nResultCode
== 200 )
801 String sResult
= aPost
.getResponseBodyAsString();
802 if ( !LoginReportsError( sResult
) )
803 aHostConfig
= aNewHostConfig
;
806 aPost
.releaseConnection();
813 protected static String
[] GetPasswordsForURLAndUser( XComponentContext xContext
, String sURL
, String sUserName
)
815 String
[] aResult
= null;
819 if ( xContext
!= null && sURL
!= null && sURL
.length() > 0 && sUserName
!= null && sUserName
.length() > 0 )
821 UrlRecord aRec
= GetPasswordContainer( xContext
).findForName( sURL
, sUserName
, GetInteractionHandler( xContext
) );
822 if ( aRec
!= null && aRec
.UserList
!= null && aRec
.UserList
.length
> 0
823 && aRec
.UserList
[0].UserName
.equals( sUserName
) )
824 aResult
= aRec
.UserList
[0].Passwords
;
835 protected static boolean PasswordStoringIsAllowed( XComponentContext xContext
)
837 boolean bResult
= false;
840 XMasterPasswordHandling xMasterHdl
= UnoRuntime
.queryInterface( XMasterPasswordHandling
.class, GetPasswordContainer( xContext
) );
841 if ( xMasterHdl
!= null )
842 bResult
= xMasterHdl
.isPersistentStoringAllowed();
852 protected static void ShowError( XComponentContext xContext
, XDialog xDialog
, int nTitleID
, int nErrorID
, String sArg
, boolean bQuery
)
854 XWindowPeer xPeer
= null;
855 XControl xControl
= UnoRuntime
.queryInterface( XControl
.class, xDialog
);
856 if ( xControl
!= null )
857 xPeer
= xControl
.getPeer();
858 ShowError( xContext
, xPeer
, nTitleID
, nErrorID
, sArg
, bQuery
);
861 protected static boolean ShowError( XComponentContext xContext
, XWindowPeer xParentPeer
, int nTitleID
, int nErrorID
, String sArg
, boolean bQuery
)
863 boolean bResult
= false;
865 if ( xContext
!= null && nErrorID
>= 0 && nErrorID
< STRINGS_NUM
)
867 String sError
= null;
872 sError
= GetLocalizedString( xContext
, nErrorID
);
873 if ( sError
!= null && sArg
!= null )
874 sError
= sError
.replaceAll( "\\$ARG1", sArg
);
876 sTitle
= GetLocalizedString( xContext
, nTitleID
);
883 if ( sError
== null )
884 sError
= "Error: " + nErrorID
;
886 if ( xParentPeer
!= null )
888 XMessageBoxFactory xMBFactory
= null;
889 XMessageBox xMB
= null;
892 XMultiComponentFactory xFactory
= xContext
.getServiceManager();
893 if ( xFactory
!= null )
894 xMBFactory
= UnoRuntime
.queryInterface(
895 XMessageBoxFactory
.class,
896 xFactory
.createInstanceWithContext( "com.sun.star.awt.Toolkit", xContext
) );
898 if ( xMBFactory
!= null )
902 xMB
= xMBFactory
.createMessageBox(
904 MessageBoxType
.QUERYBOX
,
905 MessageBoxButtons
.BUTTONS_YES_NO
| MessageBoxButtons
.DEFAULT_BUTTON_NO
,
911 xMB
= xMBFactory
.createMessageBox(
913 MessageBoxType
.ERRORBOX
,
914 MessageBoxButtons
.BUTTONS_OK
,
920 bResult
= MainThreadDialogExecutor
.Execute( xContext
, xMB
);
939 private static boolean AllowUnknownCert( XComponentContext xContext
, String aURL
)
943 XNameAccess xNameAccess
= GetConfigNameAccess( xContext
, "org.openoffice.Office.Custom.WikiExtension/SpecialData" );
944 if ( xNameAccess
.hasByName( aURL
) )
946 XNameAccess xEntry
= UnoRuntime
.queryInterface( XNameAccess
.class, xNameAccess
.getByName( aURL
) );
947 if ( xEntry
!= null && xEntry
.hasByName( "AllowUnknownCertificate" ) )
948 return AnyConverter
.toBoolean( xEntry
.getByName( "AllowUnknownCertificate" ) );
959 private static String
[][] GetSpecialArgs( XComponentContext xContext
, String aURL
)
963 XNameAccess xNameAccess
= GetConfigNameAccess( xContext
, "org.openoffice.Office.Custom.WikiExtension/SpecialData" );
964 if ( xNameAccess
.hasByName( aURL
) )
966 XNameAccess xEntry
= UnoRuntime
.queryInterface( XNameAccess
.class, xNameAccess
.getByName( aURL
) );
967 if ( xEntry
!= null )
969 XNameAccess xArgs
= UnoRuntime
.queryInterface( XNameAccess
.class, xEntry
.getByName( "AdditionalLoginArguments" ) );
972 String
[] pNames
= xArgs
.getElementNames();
973 if ( pNames
!= null && pNames
.length
> 0 )
975 String
[][] pResult
= new String
[pNames
.length
][2];
976 for ( int nInd
= 0; nInd
< pNames
.length
; nInd
++ )
978 XNameAccess xArgument
= UnoRuntime
.queryInterface( XNameAccess
.class, xArgs
.getByName( pNames
[nInd
] ) );
979 if ( xArgument
== null )
980 throw new com
.sun
.star
.uno
.RuntimeException();
982 pResult
[nInd
][0] = pNames
[nInd
];
983 pResult
[nInd
][1] = AnyConverter
.toString( xArgument
.getByName( "Value" ) );
1000 protected static boolean AllowThreadUsage( XComponentContext xContext
)
1002 if ( xContext
!= null )
1006 XMultiComponentFactory xFactory
= xContext
.getServiceManager();
1007 if ( xFactory
== null )
1008 throw new com
.sun
.star
.uno
.RuntimeException();
1010 Object oCheckCallback
= xFactory
.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext
);
1011 return ( oCheckCallback
!= null );
1013 catch( Exception e
)
1015 e
.printStackTrace();
1022 public static void Dispose( Object oObject
)
1024 if ( oObject
!= null )
1028 XComponent xComp
= UnoRuntime
.queryInterface( XComponent
.class, oObject
);
1029 if ( xComp
!= null )
1032 catch( Exception e
)
1034 e
.printStackTrace();