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