KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > actions > system > ActionChangeMyPassword


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.system;
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.authentication.gui.*;
31 import org.openharmonise.him.context.StateHandler;
32 import org.openharmonise.him.harmonise.*;
33 import org.openharmonise.him.window.messages.*;
34 import org.openharmonise.vfs.*;
35 import org.openharmonise.vfs.context.*;
36 import org.openharmonise.vfs.gui.*;
37 import org.openharmonise.vfs.servers.*;
38 import org.openharmonise.webdav.client.*;
39
40
41 /**
42  * Action to change the current user's password.
43  *
44  * @author Matthew Large
45  * @version $Revision: 1.2 $
46  *
47  */

48 public class ActionChangeMyPassword
49     extends AbstractHIMAction
50     implements HIMAction {
51
52     /**
53      *
54      */

55     public ActionChangeMyPassword() {
56         super();
57     }
58
59     /**
60      * @param vfFile
61      */

62     public ActionChangeMyPassword(VirtualFile vfFile) {
63         super(vfFile);
64     }
65
66     /* (non-Javadoc)
67      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
68      */

69     public void actionPerformed(ActionEvent arg0) {
70         boolean bWorked = this.changePassword("Change your password.");
71         if(bWorked) {
72             MessageHandler.getInstance().fireMessageEvent("Your password has been changed.", MessageHandler.TYPE_CONFIRM);
73         } else {
74             MessageHandler.getInstance().fireMessageEvent("Your password has not been changed.", MessageHandler.TYPE_ERROR);
75         }
76     }
77
78     /* (non-Javadoc)
79      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getText()
80      */

81     public String JavaDoc getText() {
82         return "Change my password";
83     }
84
85     /* (non-Javadoc)
86      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getToolTip()
87      */

88     public String JavaDoc getToolTip() {
89         return "Change the password you use to login";
90     }
91
92     /* (non-Javadoc)
93      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getIcon()
94      */

95     public Icon getIcon() {
96         return IconManager.getInstance().getIcon("16-blank.gif");
97     }
98
99     /* (non-Javadoc)
100      * @see com.simulacramedia.contentmanager.actions.AbstractCMAction#getMnemonic()
101      */

102     public String JavaDoc getMnemonic() {
103         return "M";
104     }
105
106     /* (non-Javadoc)
107      * @see com.simulacramedia.contentmanager.actions.CMAction#getDescription()
108      */

109     public String JavaDoc getDescription() {
110         return this.getToolTip();
111     }
112
113     /* (non-Javadoc)
114      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorKeycode()
115      */

116     public int getAcceleratorKeycode() {
117         return 0;
118     }
119
120     /* (non-Javadoc)
121      * @see com.simulacramedia.contentmanager.actions.CMAction#getAcceleratorMask()
122      */

123     public int getAcceleratorMask() {
124         return 0;
125     }
126     
127     /**
128      * Opens a dialog to change the current user's password.
129      *
130      * @param sMessage Message to appear in dialog
131      * @return true if the method was successful
132      */

133     private boolean changePassword(String JavaDoc sMessage) {
134         boolean bWorked = false;
135         
136         Server server = ServerList.getInstance().getHarmoniseServer();
137         
138         URI uri = server.getURI();
139         
140         String JavaDoc sURI = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/webdav/services/HarmoniseService";
141         URL url = null;
142         try {
143             url = new URL(sURI);
144         } catch (MalformedURLException e2) {
145             e2.printStackTrace();
146             System.exit(1);
147         }
148         
149         WebDAVFileSystem m_wdvfs = (WebDAVFileSystem) server.getVFS();
150             
151         JFrame tempFrame = new JFrame();
152         tempFrame.setIconImage( ((ImageIcon)IconManager.getInstance().getIcon("32-sim-logo.gif")).getImage() );
153         ChangePasswordDialog dialog = new ChangePasswordDialog(tempFrame);
154         dialog.setUsername(m_wdvfs.getAuthentication().getUsername());
155         dialog.setPassword(m_wdvfs.getAuthentication().getPassword());
156         if(sMessage!=null && !sMessage.equals("")) {
157             dialog.setInformationText(sMessage);
158         }
159         
160         dialog.show();
161                         
162         String JavaDoc sNewPassword = dialog.getNewPassword();
163         if(!sNewPassword.equals("")) {
164             
165             try {
166                 int nRetn = -1;
167                 StateHandler.getInstance().addWait("CHANGE_MY_PASSWORD");
168                 try {
169                     nRetn = UserConfigClient.setPassword(url, dialog.getUsername(), dialog.getPassword(), dialog.getUsername(), dialog.getNewPassword());
170                 } catch(Exception JavaDoc e) {
171                     throw e;
172                 } finally {
173                     StateHandler.getInstance().removeWait("CHANGE_MY_PASSWORD");
174                 }
175                     
176                 if(nRetn==UserConfigClient.CODE_AUTHENTICATION_FAIL) {
177                     this.changePassword("Your current username/password information was incorrect");
178                 } else if(nRetn==UserConfigClient.CODE_INVALID_LENGTH) {
179                     this.changePassword("Your new password is not long enough");
180                 } else if(nRetn==UserConfigClient.CODE_NO_ALPHA_CHAR) {
181                     this.changePassword("Your new password must contain at least one letter");
182                 } else if(nRetn==UserConfigClient.CODE_NO_CASE_MIX) {
183                     this.changePassword("Your new password must contain mixed case letters");
184                 } else if(nRetn==UserConfigClient.CODE_NO_NUM_CHAR) {
185                     this.changePassword("Your new password must contain at least one number");
186                 } else if(nRetn==UserConfigClient.CODE_PWD_REPEAT) {
187                     this.changePassword("That password has been used too recently");
188                 } else if(nRetn==UserConfigClient.CODE_SUCCESS) {
189                     if(m_wdvfs.checkLoginDetails("/webdav", dialog.getUsername(), dialog.getNewPassword())) {
190                         m_wdvfs.changeLoginDetails(dialog.getUsername(), sNewPassword);
191                         bWorked = true;
192                     }
193                 }
194             } catch (RemoteException e) {
195                 e.printStackTrace();
196             } catch (ServiceException e) {
197                 e.printStackTrace();
198             } catch(Exception JavaDoc e) {
199                 e.printStackTrace();
200             }
201         }
202             
203         return bWorked;
204     }
205
206 }
207
Popular Tags