it.infn.ct.security.actions.ReactivateUser.java Source code

Java tutorial

Introduction

Here is the source code for it.infn.ct.security.actions.ReactivateUser.java

Source

/***********************************************************************
 *  Copyright (c) 2011: 
 *  Istituto Nazionale di Fisica Nucleare (INFN), Italy
 *  Consorzio COMETA (COMETA), Italy
 * 
 *  See http://www.infn.it and and http://www.consorzio-cometa.it for details on
 *  the copyright holders.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 ***********************************************************************/

package it.infn.ct.security.actions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import it.infn.ct.security.entities.UserReActivateRequest;
import it.infn.ct.security.utilities.LDAPUser;
import java.io.UnsupportedEncodingException;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Marco Fargetta <marco.fargetta@ct.infn.it>
 */
public class ReactivateUser extends ActionSupport {
    private Log _log = LogFactory.getLog(ReactivateUser.class);
    private String mailFrom;
    private String mailTo;
    private String mailSubject;
    private String mailBody;
    private String username;

    public String getMailFrom() {
        return mailFrom;
    }

    public void setMailFrom(String mailFrom) {
        this.mailFrom = mailFrom;
    }

    public String getMailTo() {
        return mailTo;
    }

    public void setMailTo(String mailTo) {
        this.mailTo = mailTo;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getMailSubject() {
        return mailSubject;
    }

    public void setMailSubject(String mailSubject) {
        this.mailSubject = mailSubject;
    }

    public String getMailBody() {
        return mailBody;
    }

    public void setMailBody(String mailBody) {
        this.mailBody = mailBody;
    }

    @Override
    public String execute() throws Exception {
        if (ActionContext.getContext().getSession().containsKey("ldapUser")) {
            LDAPUser user = (LDAPUser) ActionContext.getContext().getSession().get("ldapUser");

            UserReActivateRequest urear = new UserReActivateRequest(user.getUsername());

            SessionFactory factory = (SessionFactory) ServletActionContext.getServletContext()
                    .getAttribute("IDPPublic.hibernatefactory");
            Session session = factory.openSession();
            session.beginTransaction();
            if (session.createCriteria(UserReActivateRequest.class)
                    .add(Restrictions.eq("username", user.getUsername())).add(Restrictions.eq("open", Boolean.TRUE))
                    .list().isEmpty()) {
                session.save(urear);
            }
            session.getTransaction().commit();
            session.close();

            sendMail(user);
            return SUCCESS;
        }

        return ERROR;
    }

    private void sendMail(LDAPUser user) throws MailException {
        javax.mail.Session session = null;
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            session = (javax.mail.Session) envCtx.lookup("mail/Users");

        } catch (Exception ex) {
            _log.error("Mail resource lookup error");
            _log.error(ex.getMessage());
            throw new MailException("Mail Resource not available");
        }

        Message mailMsg = new MimeMessage(session);
        try {
            mailMsg.setFrom(new InternetAddress(mailFrom, mailFrom));

            InternetAddress mailTos[] = new InternetAddress[1];
            mailTos[0] = new InternetAddress(mailTo);
            mailMsg.setRecipients(Message.RecipientType.TO, mailTos);

            mailMsg.setSubject(mailSubject);

            mailBody = mailBody.replaceAll("_USER_", user.getTitle() + " " + user.getGivenname() + " "
                    + user.getSurname() + " (" + user.getUsername() + ")");

            mailMsg.setText(mailBody);

            Transport.send(mailMsg);

        } catch (UnsupportedEncodingException ex) {
            _log.error(ex);
            throw new MailException("Mail address format not valid");
        } catch (MessagingException ex) {
            _log.error(ex);
            throw new MailException("Mail message has problems");
        }

    }

}