alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

jforum example source code file (LostPassword.jsp)

This example jforum source code file (LostPassword.jsp) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - jforum tags/keywords

contact, contact, internal, jspwiki, jspwiki, nosuchprincipalexception, sendfailedexception, string, string, top, transitional//en, tried, tried, wikisession

The jforum LostPassword.jsp source code

<%@ page import="org.apache.log4j.*" %>
<%@ page import="com.ecyrd.jspwiki.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="com.ecyrd.jspwiki.auth.user.*" %>
<%@ page import="com.ecyrd.jspwiki.auth.*" %>
<%@ page import="com.ecyrd.jspwiki.util.*" %>
<%@ page errorPage="/Error.jsp" %>
<%@ taglib uri="/WEB-INF/jspwiki.tld" prefix="wiki" %>

<%! 
    public void jspInit()
    {
        wiki = WikiEngine.getInstance( getServletConfig() );
    }

    Logger log = Logger.getLogger("JSPWiki");
    WikiEngine wiki;
    
    String message = null;
    public boolean resetPassword(WikiEngine wiki, HttpServletRequest request) 
    {
        // Reset pw for account name
        String name = request.getParameter("name");
        UserDatabase userDatabase = wiki.getUserManager().getUserDatabase();
        boolean success = false;
        try 
        {
            UserProfile profile = null;
            try 
            {
                profile = userDatabase.find(name);
            }
            catch (NoSuchPrincipalException e) 
            {
                // Try email as well
            }
            if (profile == null) 
            {
                profile = userDatabase.findByEmail(name);
            }
			
			String email = profile.getEmail();

			String randomPassword = TextUtil.generateRandomPassword();

			// Try sending email first, as that is more likely to fail.
            
             String mailMessage = "As requested, your new password for login '"
                    + profile.getLoginName() + "' is '" + randomPassword + "'.\n\n" +
                    "You may log in at "
                    + wiki.getURLConstructor().makeURL(WikiContext.NONE, "Login.jsp", true, "") + ".\n\n"
                    + "--" + wiki.getApplicationName();
                    
			MailUtil.sendMessage( wiki.getWikiProperties(),
                                   email,
			                      "New password for " + wiki.getApplicationName(),
			                      mailMessage );
		
            log.info("User "+email+" requested and received a new password.");
            
			// Mail succeeded.  Now reset the password.
			// If this fails, we're kind of screwed, because we already emailed.
			profile.setPassword(randomPassword);
			userDatabase.save(profile);
			userDatabase.commit();
			success = true;
        }
        catch (NoSuchPrincipalException e) 
        {
            message = "No user or email '" + name + "' was found.";
            log.info("Tried to reset password for non-existent user '" + name + "'");
        }
        catch (SendFailedException e) 
        {
            message = "Internal error: couldn't send the email!  Contact the site administrator, please.";
            log.error("Tried to reset password and got SendFailedException: " + e);
        }
        catch (Exception e) 
        {
            message = "Internal error. Contact the site administrator, please.";
            log.error("Tried to reset password and got another exception: " + e);
        }
        return success;
    }
%>

<%
    // Create wiki context and check for authorization
    WikiContext wikiContext = wiki.createContext( request, WikiContext.VIEW );
    if(!wikiContext.hasAccess( response )) return;
    
    WikiSession wikiSession = wikiContext.getWikiSession(); 
    String action  = request.getParameter("action");

    response.setContentType("text/html; charset="+wiki.getContentEncoding() );
    response.setHeader( "Cache-control", "max-age=0" );
    response.setDateHeader( "Expires", new Date().getTime() );
    response.setDateHeader( "Last-Modified", new Date().getTime() );

%>


<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>
  <title>: 
  <wiki:Include page="commonheader.jsp"/>
</head>

<body class="view" bgcolor="#FFFFFF">
<a name="Top">

<div id="wikibody" >
  <wiki:Include page="Header.jsp" />

  <div id="applicationlogo">
    <a href="
        <img src="
<wiki:Include page="Footer.jsp" /> <div style="clear:both; height:0px;" > </div> <a name="Bottom"> </body> </html>

Other jforum examples (source code examples)

Here is a short list of links related to this jforum LostPassword.jsp source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.