KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > actions > file > ActionChangeUserPassword


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.actions.file;
20
21 import java.awt.event.*;
22 import java.net.*;
23 import java.rmi.*;
24
25 import javax.swing.*;
26 import javax.xml.rpc.*;
27
28 import org.openharmonise.him.*;
29 import org.openharmonise.him.actions.*;
30 import org.openharmonise.him.actions.rules.*;
31 import org.openharmonise.him.authentication.gui.*;
32 import org.openharmonise.him.context.StateHandler;
33 import org.openharmonise.him.harmonise.*;
34 import org.openharmonise.him.window.messages.*;
35 import org.openharmonise.vfs.*;
36 import org.openharmonise.vfs.authentication.*;
37 import org.openharmonise.vfs.context.*;
38 import org.openharmonise.vfs.gui.*;
39 import org.openharmonise.vfs.servers.*;
40 import org.openharmonise.webdav.client.*;
41
42
43 /**
44  * Action for changing a user's password.
45  *
46  * @author Matthew Large
47  * @version $Revision: 1.3 $
48  *
49  */

50 public class ActionChangeUserPassword
51     extends AbstractHIMAction
52     implements HIMAction {
53
54
55     /**
56      *
57      */

58     public ActionChangeUserPassword() {
59         super();
60         this.setup();
61     }
62
63     /**
64      * @param vfFile
65      */

66     public ActionChangeUserPassword(VirtualFile vfFile) {
67         super(vfFile);
68     }
69     
70     /**
71      * Configures this action.
72      *
73      */

74     private void setup() {
75         RuleGroup andGroup = new RuleGroup();
76         andGroup.setGroupType(RuleGroup.GROUPTYPE_AND);
77         
78         andGroup.addEnableRule( new PathRule("/webdav/Users") );
79         IsDirectoryRule dirRule = new IsDirectoryRule();
80         dirRule.setResultComparator(false);
81         andGroup.addEnableRule( dirRule );
82         
83         super.addEnableRule(andGroup);
84     }
85
86     /* (non-Javadoc)
87      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
88      */

89     public void actionPerformed(ActionEvent arg0) {
90         VirtualFile vfPrincipal = super.getLastContextFile();
91         this.changePassword("Change " + vfPrincipal.getFileName() + "'s password.", vfPrincipal);
92     }
93
94     /* (non-Javadoc)
95      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getText()
96      */

97     public String JavaDoc getText() {
98         return "Change user's password";
99     }
100
101     /* (non-Javadoc)
102      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getToolTip()
103      */

104     public String JavaDoc getToolTip() {
105         return "Change the user's login password";
106     }
107
108     /* (non-Javadoc)
109      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getIcon()
110      */

111     public Icon getIcon() {
112         return IconManager.getInstance().getIcon("16-blank.gif");
113     }
114
115     /* (non-Javadoc)
116      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getMnemonic()
117      */

118     public String JavaDoc getMnemonic() {
119         return "p";
120     }
121
122     /* (non-Javadoc)
123      * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
124      */

125     public String JavaDoc getDescription() {
126         return this.getToolTip();
127     }
128
129     /* (non-Javadoc)
130      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
131      */

132     public int getAcceleratorKeycode() {
133         return 0;
134     }
135
136     /* (non-Javadoc)
137      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
138      */

139     public int getAcceleratorMask() {
140         return 0;
141     }
142
143     /* (non-Javadoc)
144      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#isEnabled(com.simulacramedia.contentmanager.context.ContextEvent)
145      */

146     public boolean isEnabled(ContextEvent ce) {
147         if(!this.m_bUserChecked) {
148             this.checkUser();
149         }
150         this.setEnabled(super.isEnabled(ce) && this.m_bShow);
151         return this.m_bShow;
152     }
153     
154     /**
155      * Changes a user's password.
156      *
157      * @param sMessage Message to go in dialog box
158      * @param vfPrincipal Virtual file for the principal that represents the user
159      * @return true if the method was successful
160      */

161     public boolean changePassword(String JavaDoc sMessage, VirtualFile vfPrincipal) {
162         boolean bWorked = false;
163         
164         Server server = ServerList.getInstance().getHarmoniseServer();
165         
166         URI uri = server.getURI();
167         
168         String JavaDoc sURI = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/webdav/services/HarmoniseService";
169         URL url = null;
170         try {
171             url = new URL(sURI);
172         } catch (MalformedURLException e2) {
173             e2.printStackTrace();
174             System.exit(1);
175         }
176         
177         WebDAVFileSystem m_wdvfs = (WebDAVFileSystem) server.getVFS();
178             
179         JFrame tempFrame = new JFrame();
180         tempFrame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
181         ChangePasswordDialog dialog = new ChangePasswordDialog(tempFrame);
182         dialog.setUsername(m_wdvfs.getAuthentication().getUsername());
183         dialog.setPassword(m_wdvfs.getAuthentication().getPassword());
184         if(sMessage!=null && !sMessage.equals("")) {
185             dialog.setInformationText(sMessage);
186         }
187         
188         dialog.show();
189                         
190         String JavaDoc sNewPassword = dialog.getNewPassword();
191         if(!sNewPassword.equals("")) {
192             
193             try {
194                 int nRetn = -1;
195                 StateHandler.getInstance().addWait("CHANGE_USER_PASSWORD");
196                 try {
197                     nRetn = UserConfigClient.setPassword(url, dialog.getUsername(), dialog.getPassword(), vfPrincipal.getFileName(), dialog.getNewPassword());
198                 } catch(Exception JavaDoc e) {
199                     throw e;
200                 } finally {
201                     StateHandler.getInstance().removeWait("CHANGE_USER_PASSWORD");
202                 }
203                     
204                 if(nRetn==UserConfigClient.CODE_AUTHENTICATION_FAIL) {
205                     this.changePassword("Your username/password information was incorrect", vfPrincipal);
206                 } else if(nRetn==UserConfigClient.CODE_INVALID_LENGTH) {
207                     this.changePassword("The new password is not long enough", vfPrincipal);
208                 } else if(nRetn==UserConfigClient.CODE_NO_ALPHA_CHAR) {
209                     this.changePassword("The new password must contain at least one letter", vfPrincipal);
210                 } else if(nRetn==UserConfigClient.CODE_NO_CASE_MIX) {
211                     this.changePassword("The new password must contain mixed case letters", vfPrincipal);
212                 } else if(nRetn==UserConfigClient.CODE_NO_NUM_CHAR) {
213                     this.changePassword("The new password must contain at least one number", vfPrincipal);
214                 } else if(nRetn==UserConfigClient.CODE_SUCCESS) {
215                     bWorked = true;
216                 }
217             } catch (RemoteException e) {
218                 e.printStackTrace();
219             } catch (ServiceException e) {
220                 e.printStackTrace();
221             } catch(Exception JavaDoc e) {
222                 e.printStackTrace();
223             }
224         } else {
225             MessageHandler.getInstance().fireMessageEvent("The user's password not changed.", MessageHandler.TYPE_ERROR);
226         }
227             
228         return bWorked;
229     }
230
231 }
232
Popular Tags