EditMailFactoryPropertiesAction.java

00001 /*
00002  * JOnAS: Java(TM) Open Application Server
00003  * Copyright (C) 1999 Bull S.A.
00004  * Contact: jonas-team@objectweb.org
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00019  * USA
00020  *
00021  * --------------------------------------------------------------------------
00022  * $Id: 
00023  * --------------------------------------------------------------------------
00024  */
00025 
00026 package org.objectweb.jonas.webapp.jonasadmin.resource;
00027 
00028 import java.io.IOException;
00029 import java.util.Enumeration;
00030 import java.util.Properties;
00031 
00032 import javax.servlet.ServletException;
00033 import javax.servlet.http.HttpServletRequest;
00034 import javax.servlet.http.HttpServletResponse;
00035 
00036 import org.apache.struts.action.ActionForm;
00037 import org.apache.struts.action.ActionForward;
00038 import org.apache.struts.action.ActionMapping;
00039 import org.objectweb.jonas.webapp.jonasadmin.deploy.BaseDeployAction;
00040 
00045 public class EditMailFactoryPropertiesAction extends BaseDeployAction {
00046 
00047     public ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
00048         , HttpServletRequest p_Request, HttpServletResponse p_Response)
00049         throws IOException, ServletException {
00050 
00051         String sAction = p_Request.getParameter("action");
00052         MailFactoryPropertiesForm oForm = new MailFactoryPropertiesForm();
00053         oForm.reset(p_Mapping, p_Request);
00054         m_Session.setAttribute("mailFactoryPropertiesForm", oForm);
00055         String name = "";
00056         Properties props = new Properties();
00057         try {
00058             populate(name, props, oForm);
00059         } catch (Throwable t) {
00060             addGlobalError(t);
00061             saveErrors(p_Request, m_Errors);
00062             return (p_Mapping.findForward("Global Error"));
00063         }
00064         
00065         // Forward to the jsp.
00066         return (p_Mapping.findForward("MailFactory Properties"));
00067     }
00075     void populate(String mailFactoryName, Properties props, MailFactoryPropertiesForm pForm) throws Exception { 
00076         // Set configuration attributes
00077         // - mail factory name
00078         pForm.setMailFactoryName(mailFactoryName);
00079         // - jndi name
00080         pForm.setJndiName(getStringAttribute(props, "mail.factory.name", pForm.getJndiName()));
00081         props.remove("mail.factory.name");
00082         // - type 
00083         pForm.setType(getStringAttribute(props, "mail.factory.type", pForm.getType()));
00084         props.remove("mail.factory.type");
00085 
00086         // Set authentication properties
00087         pForm.setUsername(getStringAttribute(props, "mail.authentication.username", pForm.getUsername()));
00088         props.remove("mail.authentication.username");
00089         pForm.setPassword(getStringAttribute(props, "mail.authentication.password", pForm.getPassword()));
00090         props.remove("mail.authentication.password");
00091         
00092         // Set mime part properties
00093         pForm.setTo(getStringAttribute(props, "mail.to", pForm.getTo()));
00094         pForm.setSubject(getStringAttribute(props, "mail.subject", pForm.getSubject()));
00095         pForm.setCc(getStringAttribute(props, "mail.cc", pForm.getCc()));
00096         pForm.setBcc(getStringAttribute(props, "mail.bcc", pForm.getBcc()));
00097         props.remove("mail.to");
00098         props.remove("mail.subject");
00099         props.remove("mail.cc");
00100         props.remove("mail.bcc");
00101 
00102         // Set the rest of the properties as session properties
00103         String aName = null;
00104         String aValue = null;
00105         StringBuffer bufSessionProps = new StringBuffer();
00106         for(Enumeration pNames = props.propertyNames(); pNames.hasMoreElements() ; ) {
00107             aName = (String)pNames.nextElement();
00108             aValue = (String)props.getProperty(aName);
00109             bufSessionProps.append(aName);
00110             bufSessionProps.append("=");
00111             bufSessionProps.append(aValue);
00112             bufSessionProps.append(",");
00113             bufSessionProps.append("\n");
00114         }
00115         if (!props.isEmpty()) {
00116             // delete the last 2 chars (, and \n)
00117             bufSessionProps.delete(bufSessionProps.length() - 2, bufSessionProps.length() - 1);
00118         } else {
00119             // Actually no chars in sessionPropsBuf
00120             //System.out.println("no session props, append mail.host= ");
00121             bufSessionProps.append("mail.host=");
00122         }
00123         String sSessionProps = new String(bufSessionProps);
00124         pForm.setSessionProps(sSessionProps);
00125     }
00126 
00131     Properties getPropsFromForm(MailFactoryPropertiesForm p_Form, boolean mimepart) throws Exception {
00132         Properties oProps = new Properties();
00133         Properties sProps = getPropsFromString(p_Form.getSessionProps());
00134         oProps.putAll(sProps);
00135         setStringAttribute(oProps, "mail.factory.name", p_Form.getJndiName());
00136         setStringAttribute(oProps, "mail.factory.type", p_Form.getType());
00137         setStringAttribute(oProps, "mail.authentication.username", p_Form.getUsername());
00138         setStringAttribute(oProps, "mail.authentication.password", p_Form.getPassword());
00139         String aName = null;
00140         String aValue = null;
00141         for(Enumeration pNames = oProps.propertyNames(); pNames.hasMoreElements() ; ) {
00142             aName = (String) pNames.nextElement();
00143             aValue = (String) oProps.getProperty(aName);
00144         }
00145         if (mimepart == true) {
00146             setStringAttribute(oProps, "mail.to", p_Form.getTo());
00147             setStringAttribute(oProps, "mail.subject", p_Form.getSubject());
00148             setStringAttribute(oProps, "mail.cc", p_Form.getCc());
00149             setStringAttribute(oProps, "mail.bcc", p_Form.getBcc());
00150         }
00151         return oProps;
00152     }
00153 }

Generated on Tue Feb 15 15:05:30 2005 for JOnAS by  doxygen 1.3.9.1