cid#1607171 Data race condition
[LibreOffice.git] / swext / mediawiki / src / com / sun / star / wiki / WikiEditSettingDialog.java
blobe1f1aac35da3581e844d19a859919051fe978fb9
1 /*
2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 package com.sun.star.wiki;
21 import java.util.HashMap;
22 import java.util.Map;
24 import java.net.URI;
25 import java.net.HttpURLConnection;
26 import javax.net.ssl.SSLException;
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;
33 public class WikiEditSettingDialog extends WikiDialog
36 private static final String sOKMethod = "OK";
38 private final String[] Methods = {sOKMethod };
39 private final Map<String,String> setting;
40 private final boolean addMode;
41 private boolean m_bAllowURLChange = true;
43 public WikiEditSettingDialog( XComponentContext xContext, String DialogURL )
45 super( xContext, DialogURL );
46 super.setMethods( Methods );
47 setting = new HashMap<String,String>();
48 addMode = true;
50 InsertThrobber( 184, 20, 10, 10 );
51 InitStrings( xContext );
52 InitSaveCheckbox( xContext, false );
55 public WikiEditSettingDialog( XComponentContext xContext, String DialogURL, Map<String,String> ht, boolean bAllowURLChange )
57 super( xContext, DialogURL );
58 super.setMethods( Methods );
59 setting = ht;
61 boolean bInitSaveCheckBox = false;
63 try
65 XPropertySet xUrlField = GetPropSet( "UrlField" );
67 xUrlField.setPropertyValue( "Text", ht.get( "Url" ) );
69 GetPropSet( "UsernameField" ).setPropertyValue( "Text", ht.get( "Username" ) );
71 if ( Helper.PasswordStoringIsAllowed( m_xContext ) )
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" ) ) );
77 // the password should be entered by the user or the Cancel should be pressed
78 // GetPropSet( "PasswordField" ).setPropertyValue( "Text", ht.get( "Password" ));
80 catch ( Exception ex )
82 ex.printStackTrace();
85 addMode = false;
86 m_bAllowURLChange = bAllowURLChange;
88 InsertThrobber( 184, 20, 10, 10 );
89 InitStrings( xContext );
90 InitSaveCheckbox( xContext, bInitSaveCheckBox );
93 @Override
94 public boolean show( )
96 SetThrobberVisible( false );
97 EnableControls( true );
98 boolean bResult = super.show();
102 if ( bResult && Helper.PasswordStoringIsAllowed( m_xContext )
103 && ( (Short)( GetPropSet( "SaveBox" ).getPropertyValue("State") ) ).shortValue() != (short)0 )
105 String sURL = setting.get( "Url" );
106 String sUserName = setting.get( "Username" );
107 String sPassword = setting.get( "Password" );
109 if ( sURL != null && sURL.length() > 0 && sUserName != null && sUserName.length() > 0 && sPassword != null && sPassword.length() > 0 )
111 String[] pPasswords = { sPassword };
112 Helper.GetPasswordContainer( m_xContext ).addPersistent( sURL, sUserName, pPasswords, Helper.GetInteractionHandler( m_xContext ) );
116 catch( Exception e )
118 e.printStackTrace();
121 return bResult;
124 private void EnableControls( boolean bEnable )
126 if ( !bEnable )
127 SetFocusTo( "CancelButton" );
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 ) );
136 if ( bEnable )
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" );
145 else
147 GetPropSet( "UrlField" ).setPropertyValue( "Enabled", Boolean.FALSE );
148 GetPropSet( "SaveBox" ).setPropertyValue( "Enabled", Boolean.FALSE );
151 catch ( Exception ex )
153 ex.printStackTrace();
157 private void InitStrings( XComponentContext xContext )
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 ) );
170 catch( Exception e )
172 e.printStackTrace();
176 private void InitSaveCheckbox( XComponentContext xContext, boolean bInitSaveCheckBox )
178 XPropertySet xSaveCheck = GetPropSet( "SaveBox" );
181 xSaveCheck.setPropertyValue( "State", Short.valueOf( bInitSaveCheckBox ? (short)1 : (short)0 ) );
182 xSaveCheck.setPropertyValue( "Enabled", Boolean.valueOf( Helper.PasswordStoringIsAllowed( xContext ) ) );
184 catch( Exception e )
186 e.printStackTrace();
190 private void DoLogin()
192 String sRedirectURL = "";
193 String sURL = "";
196 sURL = ( String ) GetPropSet( "UrlField" ).getPropertyValue( "Text" );
197 String sUserName = ( String ) GetPropSet( "UsernameField" ).getPropertyValue( "Text" );
198 String sPassword = ( String ) GetPropSet( "PasswordField" ).getPropertyValue( "Text" );
200 boolean bAllowIndex = true;
204 if ( sRedirectURL.length() > 0 )
206 sURL = sRedirectURL;
207 sRedirectURL = "";
210 if ( sURL.length() > 0 )
212 URI aURI = new URI(sURL);
213 HttpURLConnection connGet = Helper.PrepareMethod("GET", aURI, m_xContext);
214 connGet.setInstanceFollowRedirects(false);
215 connGet.connect();
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");
224 if ( sWebPage != null && sWebPage.length() > 0 )
226 //the URL is valid
227 String sMainURL = Helper.GetMainURL( sWebPage, sURL );
229 if ( sMainURL.length() == 0 )
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 )
237 // show error
238 Helper.ShowError( m_xContext,
239 m_xDialog,
240 Helper.DLG_MEDIAWIKI_TITLE,
241 Helper.NOURLCONNECTION_ERROR,
242 sURL,
243 false );
246 else
248 URI aMainURI = new URI(sMainURL);
250 if ( ( sUserName.length() > 0 || sPassword.length() > 0 )
251 && !Helper.Login(aMainURI, sUserName, sPassword, m_xContext))
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 );
262 else
264 setting.put( "Url", aMainURI.toASCIIString() );
265 setting.put( "Username", sUserName );
266 setting.put( "Password", sPassword );
267 if ( addMode )
269 // no cleaning of the settings is necessary
270 Settings.getSettings( m_xContext ).addWikiCon( setting );
271 Settings.getSettings( m_xContext ).storeConfiguration();
274 m_bAction = true;
278 else if ( sRedirectURL == null || sRedirectURL.length() == 0 )
280 if ( sURL.length() > 0 && !sURL.endsWith( "index.php" ) && bAllowIndex )
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;
287 else
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 );
300 else
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 );
311 } while (sRedirectURL != null && sRedirectURL.length() > 0);
313 catch ( WikiCancelException ce )
316 catch ( SSLException essl )
318 if ( Helper.IsConnectionAllowed() )
320 Helper.ShowError( m_xContext,
321 m_xDialog,
322 Helper.DLG_MEDIAWIKI_TITLE,
323 Helper.UNKNOWNCERT_ERROR,
324 null,
325 false );
327 essl.printStackTrace();
329 catch ( Exception ex )
331 if ( Helper.IsConnectionAllowed() )
333 Helper.ShowError( m_xContext,
334 m_xDialog,
335 Helper.DLG_MEDIAWIKI_TITLE,
336 Helper.NOURLCONNECTION_ERROR,
337 sURL,
338 false );
340 ex.printStackTrace();
344 @Override
345 public boolean callHandlerMethod( XDialog xDialog, Object EventObject, String MethodName )
347 if ( MethodName.equals( sOKMethod ) )
349 EnableControls( false );
350 SetThrobberVisible( true );
351 SetThrobberActive( true );
353 if ( Helper.AllowThreadUsage( m_xContext ) )
355 final XDialog xDialogForThread = xDialog;
356 final XComponentContext xContext = m_xContext;
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" )
362 @Override
363 public void run()
367 Thread.yield();
368 } catch( java.lang.Exception e ){}
370 DoLogin();
371 WikiEditSettingDialog.this.EnableControls( true );
372 WikiEditSettingDialog.this.SetThrobberActive( false );
373 WikiEditSettingDialog.this.SetThrobberVisible( false );
375 ThreadStop( true );
377 if ( m_bAction )
378 MainThreadDialogExecutor.Close( xContext, xDialogForThread );
382 m_aThread.start();
384 else
388 DoLogin();
389 } catch( java.lang.Exception e )
391 finally
393 EnableControls( true );
394 SetThrobberActive( false );
395 SetThrobberVisible( false );
397 if ( m_bAction )
398 xDialog.endExecute();
400 Helper.AllowConnection( true );
404 return true;
407 return false;
410 @Override
411 public void windowClosed( EventObject e )
413 ThreadStop( false );