1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.wiki; 25 26 import com.sun.star.awt.MessageBoxButtons; 27 import com.sun.star.awt.MessageBoxType; 28 import com.sun.star.awt.XControl; 29 import com.sun.star.awt.XControlContainer; 30 import com.sun.star.awt.XDialog; 31 import com.sun.star.awt.XMessageBox; 32 import com.sun.star.awt.XMessageBoxFactory; 33 import com.sun.star.awt.XWindowPeer; 34 import com.sun.star.beans.NamedValue; 35 import com.sun.star.beans.PropertyValue; 36 import com.sun.star.beans.XPropertySet; 37 import com.sun.star.container.XContainerQuery; 38 import com.sun.star.container.XEnumeration; 39 import com.sun.star.container.XNameAccess; 40 import com.sun.star.container.XNameContainer; 41 import com.sun.star.document.XDocumentInfoSupplier; 42 import com.sun.star.frame.XModel; 43 import com.sun.star.frame.XModuleManager; 44 import com.sun.star.io.XInputStream; 45 import com.sun.star.io.XOutputStream; 46 import com.sun.star.io.XSeekable; 47 import com.sun.star.io.XStream; 48 import com.sun.star.lang.XMultiComponentFactory; 49 import com.sun.star.lang.XMultiServiceFactory; 50 import com.sun.star.lang.XComponent; 51 import com.sun.star.system.SystemShellExecuteFlags; 52 import com.sun.star.system.XSystemShellExecute; 53 import com.sun.star.task.UrlRecord; 54 import com.sun.star.task.XInteractionHandler; 55 import com.sun.star.task.XMasterPasswordHandling; 56 import com.sun.star.task.XPasswordContainer; 57 import com.sun.star.uno.AnyConverter; 58 import com.sun.star.uno.UnoRuntime; 59 import com.sun.star.uno.XComponentContext; 60 import com.sun.star.util.XChangesBatch; 61 import java.net.*; 62 import java.io.*; 63 import java.util.Hashtable; 64 import java.util.Random; 65 import javax.net.ssl.SSLException; 66 import javax.swing.text.html.HTMLEditorKit; 67 68 import org.apache.commons.httpclient.HttpClient; 69 import org.apache.commons.httpclient.HostConfiguration; 70 import org.apache.commons.httpclient.HttpMethodBase; 71 import org.apache.commons.httpclient.cookie.CookiePolicy; 72 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 73 import org.apache.commons.httpclient.protocol.Protocol; 74 import org.apache.commons.httpclient.URI; 75 import org.apache.commons.httpclient.methods.GetMethod; 76 import org.apache.commons.httpclient.methods.PostMethod; 77 78 public class Helper 79 { 80 public final static int GENERALSEND_ERROR = 0; 81 public final static int NOWIKIFILTER_ERROR = 1; 82 public final static int NOURLCONNECTION_ERROR = 2; 83 public final static int WRONGLOGIN_ERROR = 3; 84 public final static int INVALIDURL_ERROR = 4; 85 public final static int NOURL_ERROR = 5; 86 87 public final static int DLG_SENDTITLE = 6; 88 public final static int DLG_WIKIARTICLE = 7; 89 public final static int DLG_NO = 8; 90 public final static int DLG_OK = 9; 91 public final static int DLG_YES = 10; 92 // 11 is reserved 93 public final static int DLG_ADDBUTTON = 12; 94 public final static int DLG_EDITBUTTON = 13; 95 public final static int DLG_SENDBUTTON = 14; 96 public final static int DLG_REMOVEBUTTON = 15; 97 98 public final static int DLG_EDITSETTING_URLLABEL = 16; 99 public final static int DLG_EDITSETTING_USERNAMELABEL = 17; 100 public final static int DLG_EDITSETTING_PASSWORDLABEL = 18; 101 public final static int DLG_NEWWIKIPAGE_LABEL1 = 19; 102 public final static int DLG_SENDTOMEDIAWIKI_LABEL1 = 20; 103 public final static int DLG_SENDTOMEDIAWIKI_LABEL2 = 21; 104 public final static int DLG_SENDTOMEDIAWIKI_LABEL3 = 22; 105 public final static int DLG_SENDTOMEDIAWIKI_MINORCHECK = 23; 106 public final static int DLG_SENDTOMEDIAWIKI_BROWSERCHECK = 24; 107 public final static int UNKNOWNCERT_ERROR = 25; 108 public final static int DLG_MEDIAWIKI_TITLE = 26; 109 public final static int DLG_EDITSETTING_ACCOUNTLINE = 27; 110 public final static int DLG_EDITSETTING_WIKILINE = 28; 111 public final static int DLG_EDITSETTING_SAVEBOX = 29; 112 public final static int CANCELSENDING_ERROR = 30; 113 public final static int DLG_MEDIAWIKIEXTENSION_STRING = 31; 114 public final static int DLG_WIKIPAGEEXISTS_LABEL1 = 32; 115 116 public final static int STRINGS_NUM = 33; 117 118 private final static String[] m_pEntryNames = { "GeneralSendError", 119 "NoWikiFilter", 120 "NoConnectionToURL", 121 "WrongLogin", 122 "InvalidURL", 123 "NoURL", 124 "Dlg_SendTitle", 125 "Dlg_WikiArticle", 126 "Dlg_No", 127 "Dlg_OK", 128 "Dlg_Yes", 129 null, // reserved 130 "Dlg_AddButton", 131 "Dlg_EditButton", 132 "Dlg_SendButton", 133 "Dlg_RemoveButton", 134 "Dlg_EditSetting_UrlLabel", 135 "Dlg_EditSetting_UsernameLabel", 136 "Dlg_EditSetting_PasswordLabel", 137 "Dlg_NewWikiPage_Label1", 138 "Dlg_SendToMediaWiki_Label1", 139 "Dlg_SendToMediaWiki_Label2", 140 "Dlg_SendToMediaWiki_Label3", 141 "Dlg_SendToMediaWiki_MinorCheck", 142 "Dlg_SendToMediaWiki_BrowserCheck", 143 "UnknownCert", 144 "Dlg_MediaWiki_Title", 145 "Dlg_EditSetting_AccountLine", 146 "Dlg_EditSetting_WikiLine", 147 "Dlg_EditSetting_SaveBox", 148 "CancelSending", 149 "Dlg_MediaWiki_Extension_String", 150 "Dlg_WikiPageExists_Label1" }; 151 152 private static String[] m_pConfigStrings; 153 154 private static final String sHTMLHeader = "<HTML><HEAD><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><TITLE></TITLE></HEAD><BODY>"; 155 private static final String sHTMLFooter = "</BODY></HTML>"; 156 157 private static Random m_aRandom; 158 private static MultiThreadedHttpConnectionManager m_aConnectionManager; 159 private static HttpClient m_aClient; 160 private static boolean m_bAllowConnection = true; 161 private static Hashtable m_aAcceptedUnknownCerts; 162 163 private static Boolean m_bShowInBrowser = null; 164 165 private static XPasswordContainer m_xPasswordContainer; 166 private static XInteractionHandler m_xInteractionHandler; 167 GetLocalizedString( XComponentContext xContext, int nID )168 synchronized protected static String GetLocalizedString( XComponentContext xContext, int nID ) 169 throws com.sun.star.uno.Exception 170 { 171 if ( nID >= STRINGS_NUM ) 172 throw new com.sun.star.uno.RuntimeException(); 173 174 if ( m_pConfigStrings == null ) 175 { 176 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/Strings" ); 177 178 String[] pStrings = new String[STRINGS_NUM]; 179 for ( int nInd = 0; nInd < STRINGS_NUM; nInd++ ) 180 if ( m_pEntryNames[nInd] != null ) 181 pStrings[nInd] = AnyConverter.toString( xNameAccess.getByName( m_pEntryNames[nInd] ) ); 182 else 183 pStrings[nInd] = ""; 184 185 m_pConfigStrings = pStrings; 186 } 187 188 return m_pConfigStrings[nID]; 189 } 190 GetHttpClient()191 synchronized protected static HttpClient GetHttpClient() 192 throws WikiCancelException 193 { 194 if ( !m_bAllowConnection ) 195 throw new WikiCancelException(); 196 197 if ( m_aConnectionManager == null ) 198 m_aConnectionManager = new MultiThreadedHttpConnectionManager(); 199 200 if ( m_aClient == null ) 201 { 202 m_aClient = new HttpClient( m_aConnectionManager ); 203 m_aClient.getParams().setParameter( "http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY ); 204 m_aClient.getParams().setParameter( "http.protocol.single-cookie-header", Boolean.TRUE ); 205 m_aClient.getParams().setParameter( "http.protocol.content-charset", "UTF-8" ); 206 } 207 208 return m_aClient; 209 } 210 AllowConnection( boolean bAllow )211 synchronized protected static void AllowConnection( boolean bAllow ) 212 { 213 m_bAllowConnection = bAllow; 214 if ( !bAllow && m_aConnectionManager != null ) 215 { 216 m_aClient = null; 217 m_aConnectionManager.shutdown(); 218 m_aConnectionManager = null; 219 } 220 } 221 IsConnectionAllowed()222 synchronized protected static boolean IsConnectionAllowed() 223 { 224 return m_bAllowConnection; 225 } 226 GetShowInBrowserByDefault( XComponentContext xContext )227 synchronized protected static boolean GetShowInBrowserByDefault( XComponentContext xContext ) 228 { 229 if ( m_bShowInBrowser == null ) 230 { 231 try 232 { 233 XNameAccess xAccess = Helper.GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/Settings" ); 234 m_bShowInBrowser = new Boolean( AnyConverter.toBoolean( xAccess.getByName( "PreselectShowBrowser" ) ) ); 235 } 236 catch( com.sun.star.uno.Exception e ) 237 { 238 e.printStackTrace(); 239 } 240 } 241 242 return m_bShowInBrowser.booleanValue(); 243 } 244 SetShowInBrowserByDefault( XComponentContext xContext, boolean bValue )245 synchronized protected static void SetShowInBrowserByDefault( XComponentContext xContext, boolean bValue ) 246 { 247 try 248 { 249 m_bShowInBrowser = new Boolean( bValue ); 250 251 XPropertySet xProps = Helper.GetConfigProps( xContext, "org.openoffice.Office.Custom.WikiExtension/Settings" ); 252 xProps.setPropertyValue( "PreselectShowBrowser", new Boolean( bValue ) ); 253 XChangesBatch xBatch = ( XChangesBatch ) UnoRuntime.queryInterface( XChangesBatch.class, xProps ); 254 if ( xBatch != null ) 255 xBatch.commitChanges(); 256 } 257 catch( Exception e ) 258 { 259 e.printStackTrace(); 260 } 261 } 262 GetPasswordContainer( XComponentContext xContext )263 synchronized protected static XPasswordContainer GetPasswordContainer( XComponentContext xContext ) 264 throws com.sun.star.uno.Exception 265 { 266 if ( m_xPasswordContainer == null && xContext != null ) 267 { 268 XMultiComponentFactory xFactory = xContext.getServiceManager(); 269 if ( xFactory != null ) 270 m_xPasswordContainer = (XPasswordContainer)UnoRuntime.queryInterface( 271 XPasswordContainer.class, 272 xFactory.createInstanceWithContext( "com.sun.star.task.PasswordContainer", xContext ) ); 273 } 274 275 if ( m_xPasswordContainer == null ) 276 throw new com.sun.star.uno.RuntimeException(); 277 278 return m_xPasswordContainer; 279 } 280 GetInteractionHandler( XComponentContext xContext )281 synchronized protected static XInteractionHandler GetInteractionHandler( XComponentContext xContext ) 282 throws com.sun.star.uno.Exception 283 { 284 if ( m_xInteractionHandler == null && xContext != null ) 285 { 286 XMultiComponentFactory xFactory = xContext.getServiceManager(); 287 if ( xFactory != null ) 288 m_xInteractionHandler = ( XInteractionHandler )UnoRuntime.queryInterface( 289 XInteractionHandler.class, 290 xFactory.createInstanceWithContext( "com.sun.star.task.InteractionHandler", xContext ) ); 291 } 292 293 if ( m_xInteractionHandler == null ) 294 throw new com.sun.star.uno.RuntimeException(); 295 296 return m_xInteractionHandler; 297 } 298 GetOwnHttps( int nPort )299 protected static Protocol GetOwnHttps( int nPort ) 300 { 301 return new Protocol( "https", new WikiProtocolSocketFactory(), ( ( nPort < 0 ) ? 443 : nPort ) ); 302 } 303 GetMainURL( String sWebPage, String sVURL )304 protected static String GetMainURL( String sWebPage, String sVURL ) 305 { 306 String sResultURL = ""; 307 try 308 { 309 StringReader aReader = new StringReader( sWebPage ); 310 HTMLEditorKit.Parser aParser = GetHTMLParser(); 311 EditPageParser aCallback = new EditPageParser(); 312 313 aParser.parse( aReader, aCallback, true ); 314 sResultURL = aCallback.m_sMainURL; 315 316 if ( !sResultURL.startsWith( "http" ) ) 317 { 318 //if the url is only relative then complete it 319 URL aURL = new URL( sVURL ); 320 sResultURL = aURL.getProtocol() + "://" + aURL.getHost() + sResultURL; 321 } 322 } 323 catch( Exception e ) 324 { 325 e.printStackTrace(); 326 } 327 328 if ( sResultURL.length() == 0 ) 329 { 330 // usually that should not happen 331 // workaround: try to get index.php from the provided URL 332 int nIndex = sVURL.indexOf( "index.php" ); 333 if ( nIndex >= 0 ) 334 sResultURL = sVURL.substring( 0, nIndex ); 335 } 336 337 return sResultURL; 338 } 339 GetRedirectURL( String sWebPage, String sURL )340 protected static String GetRedirectURL( String sWebPage, String sURL ) 341 { 342 //scrape the HTML source and find the EditURL 343 // TODO/LATER: Use parser in future 344 345 String sResultURL = ""; 346 int nInd = sWebPage.indexOf( "http-equiv=\"refresh\"" ); 347 if ( nInd != -1 ) 348 { 349 int nContent = sWebPage.indexOf( "content=", nInd ); 350 if ( nContent > 0 ) 351 { 352 int nURL = sWebPage.indexOf( "URL=", nContent ); 353 if ( nURL > 0 ) 354 { 355 int nEndURL = sWebPage.indexOf( "\"", nURL ); 356 if ( nEndURL > 0 ) 357 sResultURL = sWebPage.substring( nURL + 4, nEndURL ); 358 } 359 } 360 361 try 362 { 363 URL aURL = new URL( sURL ); 364 if ( !sResultURL.startsWith( aURL.getProtocol() )) 365 { 366 //if the url is only relative then complete it 367 if ( sResultURL.startsWith( "/" ) ) 368 sResultURL = aURL.getProtocol() + "://" + aURL.getHost() + sResultURL; 369 else 370 sResultURL = aURL.getProtocol() + "://" + aURL.getHost() + aURL.getPath() + sResultURL; 371 } 372 } 373 catch ( MalformedURLException ex ) 374 { 375 ex.printStackTrace(); 376 } 377 } 378 379 return sResultURL; 380 381 } 382 SaveHTMLTemp( XComponentContext xContext, String sArticle )383 protected static XInputStream SaveHTMLTemp( XComponentContext xContext, String sArticle ) 384 { 385 XInputStream xResult = null; 386 387 if ( xContext != null ) 388 { 389 try 390 { 391 Object oTempFile = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.io.TempFile", xContext ); 392 XStream xStream = ( XStream ) UnoRuntime.queryInterface( XStream.class, oTempFile ); 393 XSeekable xSeekable = ( XSeekable ) UnoRuntime.queryInterface( XSeekable.class, oTempFile ); 394 if ( xStream != null && xSeekable != null ) 395 { 396 XOutputStream xOutputStream = xStream.getOutputStream(); 397 XInputStream xInputStream = xStream.getInputStream(); 398 if ( xOutputStream != null && xInputStream != null ) 399 { 400 String sHTML = sHTMLHeader.concat( sArticle ); 401 sHTML = sHTML.concat( sHTMLFooter ); 402 xOutputStream.writeBytes( sHTML.getBytes( "UTF-8" ) ); 403 // xOutputStream.closeOutput(); 404 xSeekable.seek( 0 ); 405 406 xResult = xInputStream; 407 } 408 } 409 } 410 catch ( Exception ex ) 411 { 412 ex.printStackTrace(); 413 } 414 } 415 416 return xResult; 417 } 418 419 CreateTempFile( XComponentContext xContext )420 protected static String CreateTempFile( XComponentContext xContext ) 421 { 422 String sURL = ""; 423 try 424 { 425 Object oTempFile = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.io.TempFile", xContext ); 426 XPropertySet xPropertySet = ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, oTempFile ); 427 xPropertySet.setPropertyValue( "RemoveFile", Boolean.FALSE ); 428 sURL = ( String ) xPropertySet.getPropertyValue( "Uri" ); 429 430 XInputStream xInputStream = ( XInputStream ) UnoRuntime.queryInterface( XInputStream.class, oTempFile ); 431 xInputStream.closeInput(); 432 XOutputStream xOutputStream = ( XOutputStream ) UnoRuntime.queryInterface( XOutputStream.class, oTempFile ); 433 xOutputStream.closeOutput(); 434 } catch ( com.sun.star.uno.Exception ex ) 435 { 436 ex.printStackTrace(); 437 } 438 return sURL; 439 } 440 EachLine( String sURL )441 protected static String EachLine( String sURL ) 442 { 443 String sText = ""; 444 try 445 { 446 URL aURL = new URL( sURL ); 447 File aFile = new File( aURL.getFile() ); 448 InputStreamReader aInputReader = new InputStreamReader( new FileInputStream( aFile ), "UTF-8" ); 449 BufferedReader aBufReader = new BufferedReader( aInputReader ); 450 451 StringBuffer aBuf = new StringBuffer(); 452 String sEachLine = aBufReader.readLine(); 453 454 while( sEachLine != null ) 455 { 456 aBuf.append( sEachLine ); 457 aBuf.append( "\n" ); 458 459 sEachLine = aBufReader.readLine(); 460 } 461 sText = aBuf.toString(); 462 } catch ( Exception e ) 463 { 464 e.printStackTrace(); 465 } 466 return sText; 467 } 468 GetDocTitle( XModel xDoc )469 protected static String GetDocTitle( XModel xDoc ) 470 { 471 String sTitle = ""; 472 XDocumentInfoSupplier xDocInfoSup = ( XDocumentInfoSupplier ) UnoRuntime.queryInterface( XDocumentInfoSupplier.class, xDoc ); 473 XPropertySet xPropSet = ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, xDocInfoSup.getDocumentInfo() ); 474 try 475 { 476 sTitle = ( String ) xPropSet.getPropertyValue( "Title" ); 477 } catch ( Exception ex ) 478 { 479 ex.printStackTrace(); 480 } 481 return sTitle; 482 } 483 SetDocTitle( XModel xDoc, String sTitle )484 protected static void SetDocTitle( XModel xDoc, String sTitle ) 485 { 486 XDocumentInfoSupplier xDocInfoSup = ( XDocumentInfoSupplier ) UnoRuntime.queryInterface( XDocumentInfoSupplier.class, xDoc ); 487 if ( xDocInfoSup != null ) 488 { 489 XPropertySet xPropSet = ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, xDocInfoSup.getDocumentInfo() ); 490 if ( xPropSet != null ) 491 { 492 try 493 { 494 xPropSet.setPropertyValue( "Title", sTitle ); 495 } 496 catch ( Exception ex ) 497 { 498 ex.printStackTrace(); 499 } 500 } 501 } 502 } 503 GetDocServiceName( XComponentContext xContext, XModel xModel )504 protected static String GetDocServiceName( XComponentContext xContext, XModel xModel ) 505 { 506 String aDocServiceName = ""; 507 if ( xModel != null && xContext != null ) 508 { 509 try 510 { 511 XMultiComponentFactory xFactory = xContext.getServiceManager(); 512 if ( xFactory == null ) 513 throw new com.sun.star.uno.RuntimeException(); 514 515 Object oModuleManager = xFactory.createInstanceWithContext( "com.sun.star.frame.ModuleManager", xContext ); 516 XModuleManager xModuleManager = ( XModuleManager ) UnoRuntime.queryInterface( XModuleManager.class, oModuleManager ); 517 if ( xModuleManager != null ) 518 aDocServiceName = xModuleManager.identify( xModel ); 519 } 520 catch( java.lang.Exception e ) 521 { 522 e.printStackTrace(); 523 } 524 } 525 526 return aDocServiceName; 527 } 528 GetFilterName( XComponentContext xContext, String aTypeName, String aDocServiceName )529 protected static String GetFilterName( XComponentContext xContext, String aTypeName, String aDocServiceName ) 530 { 531 String aFilterName = ""; 532 if ( xContext != null && aTypeName != null && aTypeName.length() != 0 533 && aDocServiceName != null && aDocServiceName.length() != 0 ) 534 { 535 try 536 { 537 Object oFilterFactory = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.document.FilterFactory", xContext ); 538 XContainerQuery xQuery = ( XContainerQuery )UnoRuntime.queryInterface( XContainerQuery.class, oFilterFactory ); 539 if ( xQuery != null ) 540 { 541 NamedValue[] aRequest = new NamedValue[2]; 542 aRequest[0] = new NamedValue( "Type", aTypeName ); 543 aRequest[1] = new NamedValue( "DocumentService", aDocServiceName ); 544 545 XEnumeration xSet = xQuery.createSubSetEnumerationByProperties( aRequest ); 546 if ( xSet != null ) 547 { 548 boolean bAcceptable = false; 549 while ( xSet.hasMoreElements() && !bAcceptable ) 550 { 551 PropertyValue[] pFilterProps = ( PropertyValue[] )AnyConverter.toArray( xSet.nextElement() ); 552 if ( pFilterProps != null ) 553 { 554 int nLen = pFilterProps.length; 555 String aTmpFilter = null; 556 557 for ( int nInd = 0; nInd < nLen; nInd++ ) 558 { 559 if ( pFilterProps[nInd].Name.equals( "Name" ) ) 560 aTmpFilter = AnyConverter.toString( pFilterProps[nInd].Value ); 561 else if ( pFilterProps[nInd].Name.equals( "Flags" ) ) 562 bAcceptable = ( ( AnyConverter.toInt( pFilterProps[nInd].Value ) & 2 ) == 2 ); // must allow export 563 } 564 565 if ( bAcceptable ) 566 aFilterName = aTmpFilter; 567 } 568 } 569 } 570 } 571 } 572 catch( java.lang.Exception e ) 573 { 574 e.printStackTrace(); 575 } 576 } 577 578 return aFilterName; 579 } 580 GetConfigurationProvider( XComponentContext xContext )581 protected static XMultiServiceFactory GetConfigurationProvider( XComponentContext xContext ) 582 throws com.sun.star.uno.Exception 583 { 584 XMultiServiceFactory xConfigurationProvider = null; 585 if ( xContext != null ) 586 { 587 XMultiComponentFactory xFactory = xContext.getServiceManager(); 588 Object oConfigProvider = xFactory.createInstanceWithContext( "com.sun.star.configuration.ConfigurationProvider", xContext ); 589 xConfigurationProvider = ( XMultiServiceFactory ) UnoRuntime.queryInterface( XMultiServiceFactory.class, oConfigProvider ); 590 } 591 592 if ( xConfigurationProvider == null ) 593 throw new com.sun.star.uno.RuntimeException(); 594 595 return xConfigurationProvider; 596 } 597 GetConfig( XComponentContext xContext, String sNodepath, boolean bWriteAccess )598 protected static Object GetConfig( XComponentContext xContext, String sNodepath, boolean bWriteAccess ) 599 throws com.sun.star.uno.Exception 600 { 601 if ( xContext == null || sNodepath == null ) 602 throw new com.sun.star.uno.RuntimeException(); 603 604 PropertyValue aVal = new PropertyValue(); 605 aVal.Name = "nodepath"; 606 aVal.Value = sNodepath; 607 Object[] aArgs = new Object[1]; 608 aArgs[0] = aVal; 609 610 return GetConfigurationProvider( xContext ).createInstanceWithArguments( 611 ( bWriteAccess ? "com.sun.star.configuration.ConfigurationUpdateAccess" 612 : "com.sun.star.configuration.ConfigurationAccess" ), 613 aArgs ); 614 } 615 GetConfigProps( XComponentContext xContext, String sNodepath )616 protected static XPropertySet GetConfigProps( XComponentContext xContext, String sNodepath ) 617 throws com.sun.star.uno.Exception 618 { 619 XPropertySet xProps = ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, GetConfig( xContext, sNodepath, true ) ); 620 if ( xProps == null ) 621 throw new com.sun.star.uno.RuntimeException(); 622 623 return xProps; 624 } 625 626 GetConfigNameContainer( XComponentContext xContext, String sNodepath )627 protected static XNameContainer GetConfigNameContainer( XComponentContext xContext, String sNodepath ) 628 throws com.sun.star.uno.Exception 629 { 630 XNameContainer xContainer = ( XNameContainer ) UnoRuntime.queryInterface( XNameContainer.class, GetConfig( xContext, sNodepath, true ) ); 631 if ( xContainer == null ) 632 throw new com.sun.star.uno.RuntimeException(); 633 634 return xContainer; 635 } 636 GetConfigNameAccess( XComponentContext xContext, String sNodepath )637 protected static XNameAccess GetConfigNameAccess( XComponentContext xContext, String sNodepath ) 638 throws com.sun.star.uno.Exception 639 { 640 XNameAccess xNameAccess = ( XNameAccess ) UnoRuntime.queryInterface( XNameAccess.class, GetConfig( xContext, sNodepath, false ) ); 641 if ( xNameAccess == null ) 642 throw new com.sun.star.uno.RuntimeException(); 643 644 return xNameAccess; 645 } 646 SetConfigurationProxy( HostConfiguration aHostConfig, XComponentContext xContext )647 protected static void SetConfigurationProxy( HostConfiguration aHostConfig, XComponentContext xContext ) 648 { 649 if ( aHostConfig == null || xContext == null ) 650 return; 651 652 try 653 { 654 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Inet/Settings" ); 655 656 int nProxyType = AnyConverter.toInt( xNameAccess.getByName( "ooInetProxyType" ) ); 657 if ( nProxyType == 0 ) 658 aHostConfig.setProxyHost( null ); 659 else 660 { 661 if ( nProxyType == 1 ) 662 { 663 // system proxy 664 } 665 else if ( nProxyType == 2 ) 666 { 667 String aProxyNameProp = "ooInetHTTPProxyName"; 668 String aProxyPortProp = "ooInetHTTPProxyPort"; 669 670 if ( aHostConfig.getProtocol().getScheme().equals( "https" ) ) 671 { 672 aProxyNameProp = "ooInetHTTPSProxyName"; 673 aProxyPortProp = "ooInetHTTPSProxyPort"; 674 } 675 676 String aNoProxyList = AnyConverter.toString( xNameAccess.getByName( "ooInetNoProxy" ) ); 677 String aProxyName = AnyConverter.toString( xNameAccess.getByName( aProxyNameProp ) ); 678 679 int nProxyPort = 80; 680 681 Object aPortNo = xNameAccess.getByName( aProxyPortProp ); 682 if ( !AnyConverter.isVoid( aPortNo ) ) 683 nProxyPort = AnyConverter.toInt( aPortNo ); 684 685 if ( nProxyPort == -1 ) 686 nProxyPort = 80; 687 688 // TODO: check whether the URL is in the NoProxy list 689 aHostConfig.setProxy( aProxyName, nProxyPort ); 690 } 691 } 692 } 693 catch( java.lang.Exception e ) 694 { 695 e.printStackTrace(); 696 } 697 } 698 ShowURLInBrowser( XComponentContext xContext, String sURL )699 protected static void ShowURLInBrowser( XComponentContext xContext, String sURL ) 700 { 701 if ( xContext != null && sURL != null && sURL.length() > 0 ) 702 { 703 try 704 { 705 XSystemShellExecute xSystemShell = com.sun.star.system.SystemShellExecute.create( xContext ); 706 if ( xSystemShell != null ) 707 xSystemShell.execute( sURL, "", SystemShellExecuteFlags.DEFAULTS ); 708 } 709 catch( Exception e ) 710 { 711 e.printStackTrace(); 712 } 713 } 714 } 715 ExecuteMethod( HttpMethodBase aMethod, HostConfiguration aHostConfig, URI aURI, XComponentContext xContext, boolean bSetHost )716 protected static void ExecuteMethod( HttpMethodBase aMethod, HostConfiguration aHostConfig, URI aURI, XComponentContext xContext, boolean bSetHost ) 717 throws WikiCancelException, IOException, SSLException 718 { 719 if ( aMethod != null && aHostConfig != null && aURI != null && xContext != null ) 720 { 721 if ( bSetHost ) 722 { 723 aHostConfig.setHost( aURI ); 724 SetConfigurationProxy( aHostConfig, xContext ); 725 } 726 727 boolean bNoUnknownCertNotification = false; 728 if ( aHostConfig.getProtocol().getScheme().equals( "https" ) 729 && AllowUnknownCert( xContext, aURI.getHost() ) ) 730 { 731 // let unknown certificates be accepted 732 { 733 { 734 aHostConfig.setHost( aHostConfig.getHost(), ( aURI.getPort() < 0 ? 443 : aURI.getPort() ), Helper.GetOwnHttps( aURI.getPort() ) ); 735 Helper.GetHttpClient().executeMethod( aHostConfig, aMethod ); 736 } 737 } 738 } 739 else 740 { 741 Helper.GetHttpClient().executeMethod( aHostConfig, aMethod ); 742 } 743 } 744 } 745 746 static private class HTMLParse extends HTMLEditorKit 747 { 748 749 public HTMLEditorKit.Parser getParser() 750 { 751 return super.getParser(); 752 } 753 } 754 755 static protected HTMLEditorKit.Parser GetHTMLParser() 756 { 757 return new HTMLParse().getParser(); 758 } 759 760 static protected boolean LoginReportsError( String sRespond ) 761 { 762 boolean bResult = true; 763 if ( sRespond != null ) 764 { 765 try 766 { 767 StringReader aReader = new StringReader( sRespond ); 768 HTMLEditorKit.Parser aParser = GetHTMLParser(); 769 EditPageParser aCallback = new EditPageParser(); 770 771 aParser.parse( aReader, aCallback, true ); 772 bResult = ( aCallback.m_nErrorInd >= 0 ); 773 } 774 catch( Exception e ) 775 { 776 e.printStackTrace(); 777 } 778 } 779 780 return bResult; 781 } 782 GetLoginToken( String sLoginPage )783 static protected String GetLoginToken( String sLoginPage ) 784 { 785 String sResult = ""; 786 if ( sLoginPage != null && sLoginPage.length() > 0 ) 787 { 788 try 789 { 790 StringReader aReader = new StringReader( sLoginPage ); 791 HTMLEditorKit.Parser aParser = Helper.GetHTMLParser(); 792 EditPageParser aCallbacks = new EditPageParser(); 793 794 aParser.parse( aReader, aCallbacks, true ); 795 sResult = aCallbacks.m_sLoginToken; 796 } 797 catch( Exception e ) 798 { 799 e.printStackTrace(); 800 } 801 } 802 803 return sResult; 804 } 805 Login( URI aMainURL, String sWikiUser, String sWikiPass, XComponentContext xContext )806 static protected HostConfiguration Login( URI aMainURL, String sWikiUser, String sWikiPass, XComponentContext xContext ) 807 throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException 808 { 809 HostConfiguration aHostConfig = null; 810 811 if ( sWikiUser != null && sWikiPass != null && xContext != null ) 812 { 813 HostConfiguration aNewHostConfig = new HostConfiguration(); 814 815 URI aURI = new URI( aMainURL.toString() + "index.php?title=Special:Userlogin" ); 816 GetMethod aGetCookie = new GetMethod( aURI.getEscapedPathQuery() ); 817 818 ExecuteMethod( aGetCookie, aNewHostConfig, aURI, xContext, true ); 819 820 int nResultCode = aGetCookie.getStatusCode(); 821 String sLoginPage = null; 822 if ( nResultCode == 200 ) 823 sLoginPage = aGetCookie.getResponseBodyAsString(); 824 825 aGetCookie.releaseConnection(); 826 827 if ( sLoginPage != null ) 828 { 829 String sLoginToken = GetLoginToken( sLoginPage ); 830 831 PostMethod aPost = new PostMethod(); 832 URI aPostURI = new URI( aMainURL.getPath() + "index.php?title=Special:Userlogin&action=submitlogin" ); 833 aPost.setPath( aPostURI.getEscapedPathQuery() ); 834 835 aPost.addParameter( "wpName", sWikiUser ); 836 aPost.addParameter( "wpRemember", "1" ); 837 aPost.addParameter( "wpPassword", sWikiPass ); 838 if ( sLoginToken.length() > 0 ) 839 aPost.addParameter( "wpLoginToken", sLoginToken ); 840 841 String[][] pArgs = GetSpecialArgs( xContext, aMainURL.getHost() ); 842 if ( pArgs != null ) 843 for ( int nArgInd = 0; nArgInd < pArgs.length; nArgInd++ ) 844 if ( pArgs[nArgInd].length == 2 && pArgs[nArgInd][0] != null && pArgs[nArgInd][1] != null ) 845 aPost.addParameter( pArgs[nArgInd][0], pArgs[nArgInd][1] ); 846 847 ExecuteMethod( aPost, aNewHostConfig, aPostURI, xContext, false ); 848 849 nResultCode = aPost.getStatusCode(); 850 851 while( nResultCode >= 301 && nResultCode <= 303 || nResultCode == 307 ) 852 { 853 String sRedirectURL = aPost.getResponseHeader( "Location" ).getValue(); 854 aPost.releaseConnection(); 855 856 aURI = new URI( sRedirectURL ); 857 aPost = new PostMethod(); 858 aPost.setPath( aURI.getEscapedPathQuery() ); 859 ExecuteMethod( aPost, aNewHostConfig, aURI, xContext, false ); 860 861 nResultCode = aPost.getStatusCode(); 862 } 863 864 if ( nResultCode == 200 ) 865 { 866 String sResult = aPost.getResponseBodyAsString(); 867 if ( !LoginReportsError( sResult ) ) 868 aHostConfig = aNewHostConfig; 869 } 870 871 aPost.releaseConnection(); 872 } 873 } 874 875 return aHostConfig; 876 } 877 GetControlFromDialog( XDialog xDialog, String aControlName )878 private static XControl GetControlFromDialog( XDialog xDialog, String aControlName ) 879 { 880 XControl xResult = null; 881 XControlContainer xControlCont = (XControlContainer) UnoRuntime.queryInterface( XControlContainer.class, xDialog ); 882 883 if ( xControlCont != null ) 884 { 885 Object oControl = xControlCont.getControl( aControlName ); 886 xResult = ( XControl ) UnoRuntime.queryInterface( XControl.class, oControl ); 887 } 888 889 return xResult; 890 } 891 GetSubControlPropSet( XDialog xDialog, String aControlName )892 private static XPropertySet GetSubControlPropSet( XDialog xDialog, String aControlName ) 893 { 894 XControl xControl = GetControlFromDialog( xDialog, aControlName ); 895 if ( xControl != null ) 896 return ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, xControl.getModel() ); 897 898 return null; 899 } 900 SetControlPropInDialog( XDialog xDialog, String aControlName, String aPropName, Object aPropValue )901 protected static void SetControlPropInDialog( XDialog xDialog, String aControlName, String aPropName, Object aPropValue ) 902 { 903 if ( xDialog != null && aControlName != null && aPropName != null && aPropValue != null ) 904 { 905 try 906 { 907 XPropertySet xPropSet = GetSubControlPropSet( xDialog, aControlName ); 908 if ( xPropSet != null ) 909 xPropSet.setPropertyValue( aPropName, aPropValue ); 910 } 911 catch ( com.sun.star.uno.Exception e ) 912 { 913 e.printStackTrace(); 914 } 915 } 916 } 917 GetPasswordsForURLAndUser( XComponentContext xContext, String sURL, String sUserName )918 protected static String[] GetPasswordsForURLAndUser( XComponentContext xContext, String sURL, String sUserName ) 919 { 920 String[] aResult = null; 921 922 try 923 { 924 if ( xContext != null && sURL != null && sURL.length() > 0 && sUserName != null && sUserName.length() > 0 ) 925 { 926 UrlRecord aRec = GetPasswordContainer( xContext ).findForName( sURL, sUserName, GetInteractionHandler( xContext ) ); 927 if ( aRec != null && aRec.UserList != null && aRec.UserList.length > 0 928 && aRec.UserList[0].UserName.equals( sUserName ) ) 929 aResult = aRec.UserList[0].Passwords; 930 } 931 } 932 catch( Exception e ) 933 { 934 e.printStackTrace(); 935 } 936 937 return aResult; 938 } 939 PasswordStoringIsAllowed( XComponentContext xContext )940 protected static boolean PasswordStoringIsAllowed( XComponentContext xContext ) 941 { 942 boolean bResult = false; 943 try 944 { 945 XMasterPasswordHandling xMasterHdl = (XMasterPasswordHandling)UnoRuntime.queryInterface( XMasterPasswordHandling.class, GetPasswordContainer( xContext ) ); 946 if ( xMasterHdl != null ) 947 bResult = xMasterHdl.isPersistentStoringAllowed(); 948 } 949 catch( Exception e ) 950 { 951 e.printStackTrace(); 952 } 953 954 return bResult; 955 } 956 ShowError( XComponentContext xContext, XDialog xDialog, int nTitleID, int nErrorID, String sArg, boolean bQuery )957 protected static void ShowError( XComponentContext xContext, XDialog xDialog, int nTitleID, int nErrorID, String sArg, boolean bQuery ) 958 { 959 XWindowPeer xPeer = null; 960 XControl xControl = (XControl)UnoRuntime.queryInterface( XControl.class, xDialog ); 961 if ( xControl != null ) 962 xPeer = xControl.getPeer(); 963 ShowError( xContext, xPeer, nTitleID, nErrorID, sArg, bQuery ); 964 } 965 ShowError( XComponentContext xContext, XWindowPeer xParentPeer, int nTitleID, int nErrorID, String sArg, boolean bQuery )966 protected static boolean ShowError( XComponentContext xContext, XWindowPeer xParentPeer, int nTitleID, int nErrorID, String sArg, boolean bQuery ) 967 { 968 boolean bResult = false; 969 970 if ( xContext != null && nErrorID >= 0 && nErrorID < STRINGS_NUM ) 971 { 972 boolean bShown = false; 973 974 String sError = null; 975 String sTitle = ""; 976 977 try 978 { 979 sError = GetLocalizedString( xContext, nErrorID ); 980 if ( sError != null && sArg != null ) 981 sError = sError.replaceAll( "\\$ARG1", sArg ); 982 983 sTitle = GetLocalizedString( xContext, nTitleID ); 984 } 985 catch( Exception e ) 986 { 987 e.printStackTrace(); 988 } 989 990 if ( sError == null ) 991 sError = "Error: " + nErrorID; 992 993 if ( xParentPeer != null ) 994 { 995 XMessageBoxFactory xMBFactory = null; 996 XMessageBox xMB = null; 997 try 998 { 999 XMultiComponentFactory xFactory = xContext.getServiceManager(); 1000 if ( xFactory != null ) 1001 xMBFactory = (XMessageBoxFactory)UnoRuntime.queryInterface( 1002 XMessageBoxFactory.class, 1003 xFactory.createInstanceWithContext( "com.sun.star.awt.Toolkit", xContext ) ); 1004 1005 if ( xMBFactory != null ) 1006 { 1007 if ( bQuery ) 1008 { 1009 xMB = xMBFactory.createMessageBox( 1010 xParentPeer, 1011 MessageBoxType.QUERYBOX, 1012 MessageBoxButtons.BUTTONS_YES_NO | MessageBoxButtons.DEFAULT_BUTTON_NO, 1013 sTitle, 1014 sError ); 1015 } 1016 else 1017 { 1018 xMB = xMBFactory.createMessageBox( 1019 xParentPeer, 1020 MessageBoxType.ERRORBOX, 1021 MessageBoxButtons.BUTTONS_OK, 1022 sTitle, 1023 sError ); 1024 } 1025 if ( xMB != null ) 1026 { 1027 bResult = MainThreadDialogExecutor.Execute( xContext, xMB ); 1028 bShown = true; 1029 } 1030 } 1031 } 1032 catch( Exception e ) 1033 { 1034 e.printStackTrace(); 1035 } 1036 finally 1037 { 1038 if ( xMB != null ) 1039 Dispose( xMB ); 1040 } 1041 } 1042 } 1043 1044 return bResult; 1045 } 1046 AllowUnknownCert( XComponentContext xContext, String aURL )1047 private static boolean AllowUnknownCert( XComponentContext xContext, String aURL ) 1048 { 1049 try 1050 { 1051 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/SpecialData" ); 1052 if ( xNameAccess.hasByName( aURL ) ) 1053 { 1054 XNameAccess xEntry = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class, xNameAccess.getByName( aURL ) ); 1055 if ( xEntry != null && xEntry.hasByName( "AllowUnknownCertificate" ) ) 1056 return AnyConverter.toBoolean( xEntry.getByName( "AllowUnknownCertificate" ) ); 1057 } 1058 } 1059 catch( Exception e ) 1060 { 1061 e.printStackTrace(); 1062 } 1063 1064 return false; 1065 } 1066 GetSpecialArgs( XComponentContext xContext, String aURL )1067 private static String[][] GetSpecialArgs( XComponentContext xContext, String aURL ) 1068 { 1069 try 1070 { 1071 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/SpecialData" ); 1072 if ( xNameAccess.hasByName( aURL ) ) 1073 { 1074 XNameAccess xEntry = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class, xNameAccess.getByName( aURL ) ); 1075 if ( xEntry != null ) 1076 { 1077 XNameAccess xArgs = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class, xEntry.getByName( "AdditionalLoginArguments" ) ); 1078 if ( xArgs != null ) 1079 { 1080 String[] pNames = xArgs.getElementNames(); 1081 if ( pNames != null && pNames.length > 0 ) 1082 { 1083 String[][] pResult = new String[pNames.length][2]; 1084 for ( int nInd = 0; nInd < pNames.length; nInd++ ) 1085 { 1086 XNameAccess xArgument = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class, xArgs.getByName( pNames[nInd] ) ); 1087 if ( xArgument == null ) 1088 throw new com.sun.star.uno.RuntimeException(); 1089 1090 pResult[nInd][0] = pNames[nInd]; 1091 pResult[nInd][1] = AnyConverter.toString( xArgument.getByName( "Value" ) ); 1092 } 1093 1094 return pResult; 1095 } 1096 } 1097 } 1098 } 1099 } 1100 catch( Exception e ) 1101 { 1102 e.printStackTrace(); 1103 } 1104 1105 return null; 1106 } 1107 AllowThreadUsage( XComponentContext xContext )1108 protected static boolean AllowThreadUsage( XComponentContext xContext ) 1109 { 1110 if ( xContext != null ) 1111 { 1112 try 1113 { 1114 XMultiComponentFactory xFactory = xContext.getServiceManager(); 1115 if ( xFactory == null ) 1116 throw new com.sun.star.uno.RuntimeException(); 1117 1118 Object oCheckCallback = xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext ); 1119 return ( oCheckCallback != null ); 1120 } 1121 catch( Exception e ) 1122 { 1123 e.printStackTrace(); 1124 } 1125 } 1126 1127 return false; 1128 } 1129 Dispose( Object oObject )1130 public static void Dispose( Object oObject ) 1131 { 1132 if ( oObject != null ) 1133 { 1134 try 1135 { 1136 XComponent xComp = (XComponent)UnoRuntime.queryInterface( XComponent.class, oObject ); 1137 if ( xComp != null ) 1138 xComp.dispose(); 1139 } 1140 catch( Exception e ) 1141 { 1142 e.printStackTrace(); 1143 } 1144 } 1145 } 1146 } 1147 1148