KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > EditProfileServlet


1 /*
2  * EditProfileServlet.java
3  *
4  * Version: $Revision: 1.5 $
5  *
6  * Date: $Date: 2005/04/20 14:22:36 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.servlet;
41
42 import java.io.IOException JavaDoc;
43 import java.sql.SQLException JavaDoc;
44
45 import javax.servlet.ServletException JavaDoc;
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47 import javax.servlet.http.HttpServletResponse JavaDoc;
48
49 import org.apache.log4j.Logger;
50 import org.dspace.app.webui.util.JSPManager;
51 import org.dspace.authorize.AuthorizeException;
52 import org.dspace.core.Context;
53 import org.dspace.core.LogManager;
54 import org.dspace.eperson.EPerson;
55
56 /**
57  * Servlet for handling editing user profiles
58  *
59  * @author Robert Tansley
60  * @version $Revision: 1.5 $
61  */

62 public class EditProfileServlet extends DSpaceServlet
63 {
64     /** Logger */
65     private static Logger log = Logger.getLogger(EditProfileServlet.class);
66
67     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
68             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
69             SQLException JavaDoc, AuthorizeException
70     {
71         // A GET displays the edit profile form. We assume the authentication
72
// filter means we have a user.
73
log.info(LogManager.getHeader(context, "view_profile", ""));
74
75         request.setAttribute("eperson", context.getCurrentUser());
76
77         JSPManager.showJSP(request, response, "/register/edit-profile.jsp");
78     }
79
80     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
81             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
82             SQLException JavaDoc, AuthorizeException
83     {
84         // Get the user - authentication should have happened
85
EPerson eperson = context.getCurrentUser();
86
87         // Find out if they're trying to set a new password
88
boolean settingPassword = false;
89
90         if ((eperson.getRequireCertificate() == false)
91                 && (request.getParameter("password") != null)
92                 && !request.getParameter("password").equals(""))
93         {
94             settingPassword = true;
95         }
96
97         // Set the user profile info
98
boolean ok = updateUserProfile(eperson, request);
99
100         if (!ok)
101         {
102             request.setAttribute("missing.fields", new Boolean JavaDoc(true));
103         }
104
105         String JavaDoc passwordProblem = null;
106
107         if (ok && settingPassword)
108         {
109             // They want to set a new password.
110
ok = confirmAndSetPassword(eperson, request);
111
112             if (!ok)
113             {
114                 request.setAttribute("password.problem", new Boolean JavaDoc(true));
115             }
116         }
117
118         if (ok)
119         {
120             // Update the DB
121
log.info(LogManager.getHeader(context, "edit_profile",
122                     "password_changed=" + settingPassword));
123             eperson.update();
124
125             // Show confirmation
126
request.setAttribute("password.updated", new Boolean JavaDoc(
127                     settingPassword));
128             JSPManager.showJSP(request, response,
129                     "/register/profile-updated.jsp");
130
131             context.complete();
132         }
133         else
134         {
135             log.info(LogManager.getHeader(context, "view_profile",
136                     "problem=true"));
137
138             request.setAttribute("eperson", eperson);
139
140             JSPManager.showJSP(request, response, "/register/edit-profile.jsp");
141         }
142     }
143
144     /**
145      * Update a user's profile information with the information in the given
146      * request. This assumes that authentication has occurred. This method
147      * doesn't write the changes to the database (i.e. doesn't call update.)
148      *
149      * @param eperson
150      * the e-person
151      * @param request
152      * the request to get values from
153      *
154      * @return true if the user supplied all the required information, false if
155      * they left something out.
156      */

157     public static boolean updateUserProfile(EPerson eperson,
158             HttpServletRequest JavaDoc request)
159     {
160         // Get the parameters from the form
161
String JavaDoc lastName = request.getParameter("last_name");
162         String JavaDoc firstName = request.getParameter("first_name");
163         String JavaDoc phone = request.getParameter("phone");
164
165         // Update the eperson
166
eperson.setFirstName(firstName);
167         eperson.setLastName(lastName);
168         eperson.setMetadata("phone", phone);
169
170         // Check all required fields are there
171
if ((lastName == null) || lastName.equals("") || (firstName == null)
172                 || firstName.equals(""))
173         {
174             return false;
175         }
176         else
177         {
178             return true;
179         }
180     }
181
182     /**
183      * Set an eperson's password, if the passwords they typed match and are
184      * acceptible. If all goes well and the password is set, null is returned.
185      * Otherwise the problem is returned as a String.
186      *
187      * @param eperson
188      * the eperson to set the new password for
189      * @param request
190      * the request containing the new password
191      *
192      * @return true if everything went OK, or false
193      */

194     public static boolean confirmAndSetPassword(EPerson eperson,
195             HttpServletRequest JavaDoc request)
196     {
197         // Get the passwords
198
String JavaDoc password = request.getParameter("password");
199         String JavaDoc passwordConfirm = request.getParameter("password_confirm");
200
201         // Check it's there and long enough
202
if ((password == null) || (password.length() < 6))
203         {
204             return false;
205         }
206
207         // Check the two passwords entered match
208
if (!password.equals(passwordConfirm))
209         {
210             return false;
211         }
212
213         // Everything OK so far, change the password
214
eperson.setPassword(password);
215
216         return true;
217     }
218 }
219
Popular Tags