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