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