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 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 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 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 222 synchronized protected static boolean IsConnectionAllowed() 223 { 224 return m_bAllowConnection; 225 } 226 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 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 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 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 299 protected static Protocol GetOwnHttps( int nPort ) 300 { 301 return new Protocol( "https", new WikiProtocolSocketFactory(), ( ( nPort < 0 ) ? 443 : nPort ) ); 302 } 303 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 699 protected static void ShowURLInBrowser( XComponentContext xContext, String sURL ) 700 { 701 if ( xContext != null && sURL != null && sURL.length() > 0 ) 702 { 703 try 704 { 705 Object oSystemShell = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.system.SystemShellExecute", xContext ); 706 XSystemShellExecute xSystemShell = (XSystemShellExecute)UnoRuntime.queryInterface( XSystemShellExecute.class, oSystemShell ); 707 if ( xSystemShell != null ) 708 xSystemShell.execute( sURL, "", SystemShellExecuteFlags.DEFAULTS ); 709 } 710 catch( Exception e ) 711 { 712 e.printStackTrace(); 713 } 714 } 715 } 716 717 protected static void ExecuteMethod( HttpMethodBase aMethod, HostConfiguration aHostConfig, URI aURI, XComponentContext xContext, boolean bSetHost ) 718 throws WikiCancelException, IOException, SSLException 719 { 720 if ( aMethod != null && aHostConfig != null && aURI != null && xContext != null ) 721 { 722 if ( bSetHost ) 723 { 724 aHostConfig.setHost( aURI ); 725 SetConfigurationProxy( aHostConfig, xContext ); 726 } 727 728 boolean bNoUnknownCertNotification = false; 729 if ( aHostConfig.getProtocol().getScheme().equals( "https" ) 730 && AllowUnknownCert( xContext, aURI.getHost() ) ) 731 { 732 // let unknown certificates be accepted 733 { 734 { 735 aHostConfig.setHost( aHostConfig.getHost(), ( aURI.getPort() < 0 ? 443 : aURI.getPort() ), Helper.GetOwnHttps( aURI.getPort() ) ); 736 Helper.GetHttpClient().executeMethod( aHostConfig, aMethod ); 737 } 738 } 739 } 740 else 741 { 742 Helper.GetHttpClient().executeMethod( aHostConfig, aMethod ); 743 } 744 } 745 } 746 747 static private class HTMLParse extends HTMLEditorKit 748 { 749 750 public HTMLEditorKit.Parser getParser() 751 { 752 return super.getParser(); 753 } 754 } 755 756 static protected HTMLEditorKit.Parser GetHTMLParser() 757 { 758 return new HTMLParse().getParser(); 759 } 760 761 static protected boolean LoginReportsError( String sRespond ) 762 { 763 boolean bResult = true; 764 if ( sRespond != null ) 765 { 766 try 767 { 768 StringReader aReader = new StringReader( sRespond ); 769 HTMLEditorKit.Parser aParser = GetHTMLParser(); 770 EditPageParser aCallback = new EditPageParser(); 771 772 aParser.parse( aReader, aCallback, true ); 773 bResult = ( aCallback.m_nErrorInd >= 0 ); 774 } 775 catch( Exception e ) 776 { 777 e.printStackTrace(); 778 } 779 } 780 781 return bResult; 782 } 783 784 static protected String GetLoginToken( String sLoginPage ) 785 { 786 String sResult = ""; 787 if ( sLoginPage != null && sLoginPage.length() > 0 ) 788 { 789 try 790 { 791 StringReader aReader = new StringReader( sLoginPage ); 792 HTMLEditorKit.Parser aParser = Helper.GetHTMLParser(); 793 EditPageParser aCallbacks = new EditPageParser(); 794 795 aParser.parse( aReader, aCallbacks, true ); 796 sResult = aCallbacks.m_sLoginToken; 797 } 798 catch( Exception e ) 799 { 800 e.printStackTrace(); 801 } 802 } 803 804 return sResult; 805 } 806 807 static protected HostConfiguration Login( URI aMainURL, String sWikiUser, String sWikiPass, XComponentContext xContext ) 808 throws com.sun.star.uno.Exception, java.io.IOException, WikiCancelException 809 { 810 HostConfiguration aHostConfig = null; 811 812 if ( sWikiUser != null && sWikiPass != null && xContext != null ) 813 { 814 HostConfiguration aNewHostConfig = new HostConfiguration(); 815 816 URI aURI = new URI( aMainURL.toString() + "index.php?title=Special:Userlogin" ); 817 GetMethod aGetCookie = new GetMethod( aURI.getEscapedPathQuery() ); 818 819 ExecuteMethod( aGetCookie, aNewHostConfig, aURI, xContext, true ); 820 821 int nResultCode = aGetCookie.getStatusCode(); 822 String sLoginPage = null; 823 if ( nResultCode == 200 ) 824 sLoginPage = aGetCookie.getResponseBodyAsString(); 825 826 aGetCookie.releaseConnection(); 827 828 if ( sLoginPage != null ) 829 { 830 String sLoginToken = GetLoginToken( sLoginPage ); 831 832 PostMethod aPost = new PostMethod(); 833 URI aPostURI = new URI( aMainURL.getPath() + "index.php?title=Special:Userlogin&action=submitlogin" ); 834 aPost.setPath( aPostURI.getEscapedPathQuery() ); 835 836 aPost.addParameter( "wpName", sWikiUser ); 837 aPost.addParameter( "wpRemember", "1" ); 838 aPost.addParameter( "wpPassword", sWikiPass ); 839 if ( sLoginToken.length() > 0 ) 840 aPost.addParameter( "wpLoginToken", sLoginToken ); 841 842 String[][] pArgs = GetSpecialArgs( xContext, aMainURL.getHost() ); 843 if ( pArgs != null ) 844 for ( int nArgInd = 0; nArgInd < pArgs.length; nArgInd++ ) 845 if ( pArgs[nArgInd].length == 2 && pArgs[nArgInd][0] != null && pArgs[nArgInd][1] != null ) 846 aPost.addParameter( pArgs[nArgInd][0], pArgs[nArgInd][1] ); 847 848 ExecuteMethod( aPost, aNewHostConfig, aPostURI, xContext, false ); 849 850 nResultCode = aPost.getStatusCode(); 851 852 while( nResultCode >= 301 && nResultCode <= 303 || nResultCode == 307 ) 853 { 854 String sRedirectURL = aPost.getResponseHeader( "Location" ).getValue(); 855 aPost.releaseConnection(); 856 857 aURI = new URI( sRedirectURL ); 858 aPost = new PostMethod(); 859 aPost.setPath( aURI.getEscapedPathQuery() ); 860 ExecuteMethod( aPost, aNewHostConfig, aURI, xContext, false ); 861 862 nResultCode = aPost.getStatusCode(); 863 } 864 865 if ( nResultCode == 200 ) 866 { 867 String sResult = aPost.getResponseBodyAsString(); 868 if ( !LoginReportsError( sResult ) ) 869 aHostConfig = aNewHostConfig; 870 } 871 872 aPost.releaseConnection(); 873 } 874 } 875 876 return aHostConfig; 877 } 878 879 private static XControl GetControlFromDialog( XDialog xDialog, String aControlName ) 880 { 881 XControl xResult = null; 882 XControlContainer xControlCont = (XControlContainer) UnoRuntime.queryInterface( XControlContainer.class, xDialog ); 883 884 if ( xControlCont != null ) 885 { 886 Object oControl = xControlCont.getControl( aControlName ); 887 xResult = ( XControl ) UnoRuntime.queryInterface( XControl.class, oControl ); 888 } 889 890 return xResult; 891 } 892 893 private static XPropertySet GetSubControlPropSet( XDialog xDialog, String aControlName ) 894 { 895 XControl xControl = GetControlFromDialog( xDialog, aControlName ); 896 if ( xControl != null ) 897 return ( XPropertySet ) UnoRuntime.queryInterface( XPropertySet.class, xControl.getModel() ); 898 899 return null; 900 } 901 902 protected static void SetControlPropInDialog( XDialog xDialog, String aControlName, String aPropName, Object aPropValue ) 903 { 904 if ( xDialog != null && aControlName != null && aPropName != null && aPropValue != null ) 905 { 906 try 907 { 908 XPropertySet xPropSet = GetSubControlPropSet( xDialog, aControlName ); 909 if ( xPropSet != null ) 910 xPropSet.setPropertyValue( aPropName, aPropValue ); 911 } 912 catch ( com.sun.star.uno.Exception e ) 913 { 914 e.printStackTrace(); 915 } 916 } 917 } 918 919 protected static String[] GetPasswordsForURLAndUser( XComponentContext xContext, String sURL, String sUserName ) 920 { 921 String[] aResult = null; 922 923 try 924 { 925 if ( xContext != null && sURL != null && sURL.length() > 0 && sUserName != null && sUserName.length() > 0 ) 926 { 927 UrlRecord aRec = GetPasswordContainer( xContext ).findForName( sURL, sUserName, GetInteractionHandler( xContext ) ); 928 if ( aRec != null && aRec.UserList != null && aRec.UserList.length > 0 929 && aRec.UserList[0].UserName.equals( sUserName ) ) 930 aResult = aRec.UserList[0].Passwords; 931 } 932 } 933 catch( Exception e ) 934 { 935 e.printStackTrace(); 936 } 937 938 return aResult; 939 } 940 941 protected static boolean PasswordStoringIsAllowed( XComponentContext xContext ) 942 { 943 boolean bResult = false; 944 try 945 { 946 XMasterPasswordHandling xMasterHdl = (XMasterPasswordHandling)UnoRuntime.queryInterface( XMasterPasswordHandling.class, GetPasswordContainer( xContext ) ); 947 if ( xMasterHdl != null ) 948 bResult = xMasterHdl.isPersistentStoringAllowed(); 949 } 950 catch( Exception e ) 951 { 952 e.printStackTrace(); 953 } 954 955 return bResult; 956 } 957 958 protected static void ShowError( XComponentContext xContext, XDialog xDialog, int nTitleID, int nErrorID, String sArg, boolean bQuery ) 959 { 960 XWindowPeer xPeer = null; 961 XControl xControl = (XControl)UnoRuntime.queryInterface( XControl.class, xDialog ); 962 if ( xControl != null ) 963 xPeer = xControl.getPeer(); 964 ShowError( xContext, xPeer, nTitleID, nErrorID, sArg, bQuery ); 965 } 966 967 protected static boolean ShowError( XComponentContext xContext, XWindowPeer xParentPeer, int nTitleID, int nErrorID, String sArg, boolean bQuery ) 968 { 969 boolean bResult = false; 970 971 if ( xContext != null && nErrorID >= 0 && nErrorID < STRINGS_NUM ) 972 { 973 boolean bShown = false; 974 975 String sError = null; 976 String sTitle = ""; 977 978 try 979 { 980 sError = GetLocalizedString( xContext, nErrorID ); 981 if ( sError != null && sArg != null ) 982 sError = sError.replaceAll( "\\$ARG1", sArg ); 983 984 sTitle = GetLocalizedString( xContext, nTitleID ); 985 } 986 catch( Exception e ) 987 { 988 e.printStackTrace(); 989 } 990 991 if ( sError == null ) 992 sError = "Error: " + nErrorID; 993 994 if ( xParentPeer != null ) 995 { 996 XMessageBoxFactory xMBFactory = null; 997 XMessageBox xMB = null; 998 try 999 { 1000 XMultiComponentFactory xFactory = xContext.getServiceManager(); 1001 if ( xFactory != null ) 1002 xMBFactory = (XMessageBoxFactory)UnoRuntime.queryInterface( 1003 XMessageBoxFactory.class, 1004 xFactory.createInstanceWithContext( "com.sun.star.awt.Toolkit", xContext ) ); 1005 1006 if ( xMBFactory != null ) 1007 { 1008 if ( bQuery ) 1009 { 1010 xMB = xMBFactory.createMessageBox( 1011 xParentPeer, 1012 MessageBoxType.QUERYBOX, 1013 MessageBoxButtons.BUTTONS_YES_NO | MessageBoxButtons.DEFAULT_BUTTON_NO, 1014 sTitle, 1015 sError ); 1016 } 1017 else 1018 { 1019 xMB = xMBFactory.createMessageBox( 1020 xParentPeer, 1021 MessageBoxType.ERRORBOX, 1022 MessageBoxButtons.BUTTONS_OK, 1023 sTitle, 1024 sError ); 1025 } 1026 if ( xMB != null ) 1027 { 1028 bResult = MainThreadDialogExecutor.Execute( xContext, xMB ); 1029 bShown = true; 1030 } 1031 } 1032 } 1033 catch( Exception e ) 1034 { 1035 e.printStackTrace(); 1036 } 1037 finally 1038 { 1039 if ( xMB != null ) 1040 Dispose( xMB ); 1041 } 1042 } 1043 } 1044 1045 return bResult; 1046 } 1047 1048 private static boolean AllowUnknownCert( XComponentContext xContext, String aURL ) 1049 { 1050 try 1051 { 1052 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/SpecialData" ); 1053 if ( xNameAccess.hasByName( aURL ) ) 1054 { 1055 XNameAccess xEntry = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class, xNameAccess.getByName( aURL ) ); 1056 if ( xEntry != null && xEntry.hasByName( "AllowUnknownCertificate" ) ) 1057 return AnyConverter.toBoolean( xEntry.getByName( "AllowUnknownCertificate" ) ); 1058 } 1059 } 1060 catch( Exception e ) 1061 { 1062 e.printStackTrace(); 1063 } 1064 1065 return false; 1066 } 1067 1068 private static String[][] GetSpecialArgs( XComponentContext xContext, String aURL ) 1069 { 1070 try 1071 { 1072 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/SpecialData" ); 1073 if ( xNameAccess.hasByName( aURL ) ) 1074 { 1075 XNameAccess xEntry = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class, xNameAccess.getByName( aURL ) ); 1076 if ( xEntry != null ) 1077 { 1078 XNameAccess xArgs = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class, xEntry.getByName( "AdditionalLoginArguments" ) ); 1079 if ( xArgs != null ) 1080 { 1081 String[] pNames = xArgs.getElementNames(); 1082 if ( pNames != null && pNames.length > 0 ) 1083 { 1084 String[][] pResult = new String[pNames.length][2]; 1085 for ( int nInd = 0; nInd < pNames.length; nInd++ ) 1086 { 1087 XNameAccess xArgument = (XNameAccess)UnoRuntime.queryInterface( XNameAccess.class, xArgs.getByName( pNames[nInd] ) ); 1088 if ( xArgument == null ) 1089 throw new com.sun.star.uno.RuntimeException(); 1090 1091 pResult[nInd][0] = pNames[nInd]; 1092 pResult[nInd][1] = AnyConverter.toString( xArgument.getByName( "Value" ) ); 1093 } 1094 1095 return pResult; 1096 } 1097 } 1098 } 1099 } 1100 } 1101 catch( Exception e ) 1102 { 1103 e.printStackTrace(); 1104 } 1105 1106 return null; 1107 } 1108 1109 protected static boolean AllowThreadUsage( XComponentContext xContext ) 1110 { 1111 if ( xContext != null ) 1112 { 1113 try 1114 { 1115 XMultiComponentFactory xFactory = xContext.getServiceManager(); 1116 if ( xFactory == null ) 1117 throw new com.sun.star.uno.RuntimeException(); 1118 1119 Object oCheckCallback = xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext ); 1120 return ( oCheckCallback != null ); 1121 } 1122 catch( Exception e ) 1123 { 1124 e.printStackTrace(); 1125 } 1126 } 1127 1128 return false; 1129 } 1130 1131 public static void Dispose( Object oObject ) 1132 { 1133 if ( oObject != null ) 1134 { 1135 try 1136 { 1137 XComponent xComp = (XComponent)UnoRuntime.queryInterface( XComponent.class, oObject ); 1138 if ( xComp != null ) 1139 xComp.dispose(); 1140 } 1141 catch( Exception e ) 1142 { 1143 e.printStackTrace(); 1144 } 1145 } 1146 } 1147 } 1148 1149