1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package com.sun.star.wiki; 29 30 import com.sun.star.awt.XDialog; 31 import com.sun.star.awt.XWindowPeer; 32 import com.sun.star.awt.XThrobber; 33 import com.sun.star.beans.XPropertySet; 34 import com.sun.star.uno.UnoRuntime; 35 import com.sun.star.uno.XComponentContext; 36 import com.sun.star.lang.EventObject; 37 import java.util.Hashtable; 38 import javax.net.ssl.SSLException; 39 40 import org.apache.commons.httpclient.*; 41 import org.apache.commons.httpclient.methods.*; 42 43 public class WikiEditSettingDialog extends WikiDialog 44 { 45 46 private final String sOKMethod = "OK"; 47 48 String[] Methods = 49 {sOKMethod }; 50 private Hashtable setting; 51 private boolean addMode; 52 private boolean m_bAllowURLChange = true; 53 54 public WikiEditSettingDialog( XComponentContext xContext, String DialogURL ) 55 { 56 super( xContext, DialogURL ); 57 super.setMethods( Methods ); 58 setting = new Hashtable(); 59 addMode = true; 60 61 InsertThrobber( 184, 20, 10, 10 ); 62 InitStrings( xContext ); 63 InitSaveCheckbox( xContext, false ); 64 } 65 66 public WikiEditSettingDialog( XComponentContext xContext, String DialogURL, Hashtable ht, boolean bAllowURLChange ) 67 { 68 super( xContext, DialogURL ); 69 super.setMethods( Methods ); 70 setting = ht; 71 72 boolean bInitSaveCheckBox = false; 73 74 try 75 { 76 XPropertySet xUrlField = GetPropSet( "UrlField" ); 77 78 xUrlField.setPropertyValue( "Text", ht.get( "Url" ) ); 79 80 GetPropSet( "UsernameField" ).setPropertyValue( "Text", ht.get( "Username" ) ); 81 82 if ( Helper.PasswordStoringIsAllowed( m_xContext ) ) 83 { 84 String[] pPasswords = Helper.GetPasswordsForURLAndUser( m_xContext, (String)ht.get( "Url" ), (String)ht.get( "Username" ) ); 85 bInitSaveCheckBox = ( pPasswords != null && pPasswords.length > 0 && pPasswords[0].equals( (String)ht.get( "Password" ) ) ); 86 } 87 88 // the password should be entered by the user or the Cancel should be pressed 89 // GetPropSet( "PasswordField" ).setPropertyValue( "Text", ht.get( "Password" )); 90 } 91 catch ( Exception ex ) 92 { 93 ex.printStackTrace(); 94 } 95 96 addMode = false; 97 m_bAllowURLChange = bAllowURLChange; 98 99 InsertThrobber( 184, 20, 10, 10 ); 100 InitStrings( xContext ); 101 InitSaveCheckbox( xContext, bInitSaveCheckBox ); 102 } 103 104 public boolean show( ) 105 { 106 SetThrobberVisible( false ); 107 EnableControls( true ); 108 boolean bResult = super.show(); 109 110 try 111 { 112 if ( bResult && Helper.PasswordStoringIsAllowed( m_xContext ) 113 && ( (Short)( GetPropSet( "SaveBox" ).getPropertyValue("State") ) ).shortValue() != (short)0 ) 114 { 115 String sURL = (String)setting.get( "Url" ); 116 String sUserName = (String)setting.get( "Username" ); 117 String sPassword = (String)setting.get( "Password" ); 118 119 if ( sURL != null && sURL.length() > 0 && sUserName != null && sUserName.length() > 0 && sPassword != null && sPassword.length() > 0 ) 120 { 121 String[] pPasswords = { sPassword }; 122 Helper.GetPasswordContainer( m_xContext ).addPersistent( sURL, sUserName, pPasswords, Helper.GetInteractionHandler( m_xContext ) ); 123 } 124 } 125 } 126 catch( Exception e ) 127 { 128 e.printStackTrace(); 129 } 130 131 return bResult; 132 } 133 134 public void EnableControls( boolean bEnable ) 135 { 136 if ( !bEnable ) 137 SetFocusTo( "CancelButton" ); 138 139 try 140 { 141 GetPropSet( "UsernameField" ).setPropertyValue( "Enabled", new Boolean( bEnable ) ); 142 GetPropSet( "PasswordField" ).setPropertyValue( "Enabled", new Boolean( bEnable ) ); 143 GetPropSet( "OkButton" ).setPropertyValue( "Enabled", new Boolean( bEnable ) ); 144 GetPropSet( "HelpButton" ).setPropertyValue( "Enabled", new Boolean( bEnable ) ); 145 146 if ( bEnable ) 147 { 148 GetPropSet( "UrlField" ).setPropertyValue( "Enabled", new Boolean( m_bAllowURLChange ) ); 149 GetPropSet( "SaveBox" ).setPropertyValue( "Enabled", new Boolean( Helper.PasswordStoringIsAllowed( m_xContext ) ) ); 150 if ( m_bAllowURLChange ) 151 SetFocusTo( "UrlField" ); 152 else 153 SetFocusTo( "UsernameField" ); 154 } 155 else 156 { 157 GetPropSet( "UrlField" ).setPropertyValue( "Enabled", Boolean.FALSE ); 158 GetPropSet( "SaveBox" ).setPropertyValue( "Enabled", Boolean.FALSE ); 159 } 160 } 161 catch ( Exception ex ) 162 { 163 ex.printStackTrace(); 164 } 165 } 166 167 private void InitStrings( XComponentContext xContext ) 168 { 169 try 170 { 171 SetTitle( Helper.GetLocalizedString( xContext, Helper.DLG_MEDIAWIKI_TITLE ) ); 172 GetPropSet( "UrlLabel" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_EDITSETTING_URLLABEL ) ); 173 GetPropSet( "UsernameLabel" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_EDITSETTING_USERNAMELABEL ) ); 174 GetPropSet( "PasswordLabel" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_EDITSETTING_PASSWORDLABEL ) ); 175 GetPropSet( "AccountLine" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_EDITSETTING_ACCOUNTLINE ) ); 176 GetPropSet( "WikiLine" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_EDITSETTING_WIKILINE ) ); 177 GetPropSet( "SaveBox" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_EDITSETTING_SAVEBOX ) ); 178 GetPropSet( "OkButton" ).setPropertyValue( "Label", Helper.GetLocalizedString( xContext, Helper.DLG_OK ) ); 179 } 180 catch( Exception e ) 181 { 182 e.printStackTrace(); 183 } 184 } 185 186 private void InitSaveCheckbox( XComponentContext xContext, boolean bInitSaveCheckBox ) 187 { 188 XPropertySet xSaveCheck = GetPropSet( "SaveBox" ); 189 try 190 { 191 xSaveCheck.setPropertyValue( "State", new Short( bInitSaveCheckBox ? (short)1 : (short)0 ) ); 192 xSaveCheck.setPropertyValue( "Enabled", new Boolean( Helper.PasswordStoringIsAllowed( xContext ) ) ); 193 } 194 catch( Exception e ) 195 { 196 e.printStackTrace(); 197 } 198 } 199 200 public void DoLogin( XDialog xDialog ) 201 { 202 String sRedirectURL = ""; 203 String sURL = ""; 204 try 205 { 206 sURL = ( String ) GetPropSet( "UrlField" ).getPropertyValue( "Text" ); 207 String sUserName = ( String ) GetPropSet( "UsernameField" ).getPropertyValue( "Text" ); 208 String sPassword = ( String ) GetPropSet( "PasswordField" ).getPropertyValue( "Text" ); 209 210 HostConfiguration aHostConfig = new HostConfiguration(); 211 boolean bInitHost = true; 212 boolean bAllowIndex = true; 213 214 do 215 { 216 if ( sRedirectURL.length() > 0 ) 217 { 218 sURL = sRedirectURL; 219 sRedirectURL = ""; 220 } 221 222 if ( sURL.length() > 0 ) 223 { 224 URI aURI = new URI( sURL ); 225 GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() ); 226 aRequest.setFollowRedirects( false ); 227 Helper.ExecuteMethod( aRequest, aHostConfig, aURI, m_xContext, bInitHost ); 228 bInitHost = false; 229 230 int nResultCode = aRequest.getStatusCode(); 231 String sWebPage = null; 232 if ( nResultCode == 200 ) 233 sWebPage = aRequest.getResponseBodyAsString(); 234 else if ( nResultCode >= 301 && nResultCode <= 303 || nResultCode == 307 ) 235 sRedirectURL = aRequest.getResponseHeader( "Location" ).getValue(); 236 237 aRequest.releaseConnection(); 238 239 if ( sWebPage != null && sWebPage.length() > 0 ) 240 { 241 //the URL is valid 242 String sMainURL = Helper.GetMainURL( sWebPage, sURL ); 243 244 if ( sMainURL.equals( "" ) ) 245 { 246 // TODO: 247 // it's not a Wiki Page, check first whether a redirect is requested 248 // happens usually in case of https 249 sRedirectURL = Helper.GetRedirectURL( sWebPage, sURL ); 250 if ( sRedirectURL.equals( "" ) ) 251 { 252 // show error 253 Helper.ShowError( m_xContext, 254 m_xDialog, 255 Helper.DLG_MEDIAWIKI_TITLE, 256 Helper.NOURLCONNECTION_ERROR, 257 sURL, 258 false ); 259 } 260 } 261 else 262 { 263 URI aMainURI = new URI( sMainURL, true ); // it must be an escaped URL, otherwise an exception should be thrown 264 265 if ( ( sUserName.length() > 0 || sPassword.length() > 0 ) 266 && Helper.Login( aMainURI, sUserName, sPassword, m_xContext ) == null ) 267 { 268 // a wrong login information is provided 269 // show error 270 Helper.ShowError( m_xContext, 271 m_xDialog, 272 Helper.DLG_MEDIAWIKI_TITLE, 273 Helper.WRONGLOGIN_ERROR, 274 null, 275 false ); 276 } 277 else 278 { 279 setting.put( "Url", aMainURI.getEscapedURI() ); 280 setting.put( "Username", sUserName ); 281 setting.put( "Password", sPassword ); 282 if ( addMode ) 283 { 284 // no cleaning of the settings is necessary 285 Settings.getSettings( m_xContext ).addWikiCon( setting ); 286 Settings.getSettings( m_xContext ).storeConfiguration(); 287 } 288 289 m_bAction = true; 290 } 291 } 292 } 293 else if ( sRedirectURL == null || sRedirectURL.length() == 0 ) 294 { 295 if ( sURL.length() > 0 && !sURL.endsWith( "index.php" ) && bAllowIndex ) 296 { 297 // the used MainURL is not alwais directly accessible 298 // add the suffix as workaround, but only once 299 sRedirectURL = sURL + "/index.php"; 300 bAllowIndex = false; 301 } 302 else 303 { 304 // URL invalid 305 // show error 306 Helper.ShowError( m_xContext, 307 m_xDialog, 308 Helper.DLG_MEDIAWIKI_TITLE, 309 Helper.INVALIDURL_ERROR, 310 null, 311 false ); 312 } 313 } 314 } 315 else 316 { 317 // URL field empty 318 // show error 319 Helper.ShowError( m_xContext, 320 m_xDialog, 321 Helper.DLG_MEDIAWIKI_TITLE, 322 Helper.NOURL_ERROR, 323 null, 324 false ); 325 } 326 } while ( sRedirectURL.length() > 0 ); 327 } 328 catch ( WikiCancelException ce ) 329 { 330 } 331 catch ( SSLException essl ) 332 { 333 if ( Helper.IsConnectionAllowed() ) 334 { 335 Helper.ShowError( m_xContext, 336 m_xDialog, 337 Helper.DLG_MEDIAWIKI_TITLE, 338 Helper.UNKNOWNCERT_ERROR, 339 null, 340 false ); 341 } 342 essl.printStackTrace(); 343 } 344 catch ( Exception ex ) 345 { 346 if ( Helper.IsConnectionAllowed() ) 347 { 348 Helper.ShowError( m_xContext, 349 m_xDialog, 350 Helper.DLG_MEDIAWIKI_TITLE, 351 Helper.NOURLCONNECTION_ERROR, 352 sURL, 353 false ); 354 } 355 ex.printStackTrace(); 356 } 357 } 358 359 public boolean callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName ) 360 { 361 if ( MethodName.equals( sOKMethod ) ) 362 { 363 EnableControls( false ); 364 SetThrobberVisible( true ); 365 SetThrobberActive( true ); 366 367 if ( Helper.AllowThreadUsage( m_xContext ) ) 368 { 369 final XDialog xDialogForThread = xDialog; 370 final XComponentContext xContext = m_xContext; 371 final WikiEditSettingDialog aThis = this; 372 373 // the thread name is used to allow the error dialogs 374 m_bThreadFinished = false; 375 m_aThread = new Thread( "com.sun.star.thread.WikiEditorSendingThread" ) 376 { 377 public void run() 378 { 379 try 380 { 381 Thread.yield(); 382 } catch( java.lang.Exception e ){} 383 384 DoLogin( xDialogForThread ); 385 aThis.EnableControls( true ); 386 aThis.SetThrobberActive( false ); 387 aThis.SetThrobberVisible( false ); 388 389 ThreadStop( true ); 390 391 if ( m_bAction ) 392 MainThreadDialogExecutor.Close( xContext, xDialogForThread ); 393 } 394 }; 395 396 m_aThread.start(); 397 } 398 else 399 { 400 try 401 { 402 DoLogin( xDialog ); 403 } catch( java.lang.Exception e ) 404 {} 405 finally 406 { 407 EnableControls( true ); 408 SetThrobberActive( false ); 409 SetThrobberVisible( false ); 410 411 if ( m_bAction ) 412 xDialog.endExecute(); 413 414 Helper.AllowConnection( true ); 415 } 416 } 417 418 return true; 419 } 420 421 return false; 422 } 423 424 public void windowClosed( EventObject e ) 425 { 426 ThreadStop( false ); 427 } 428 } 429 430