001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id:
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.webapp.jonasadmin.resource;
027:
028: import java.io.IOException;
029: import java.util.Enumeration;
030: import java.util.Properties;
031:
032: import javax.servlet.ServletException;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035:
036: import org.apache.struts.action.ActionForm;
037: import org.apache.struts.action.ActionForward;
038: import org.apache.struts.action.ActionMapping;
039: import org.objectweb.jonas.webapp.jonasadmin.deploy.BaseDeployAction;
040:
041: /**
042: * Helper for actions for the deployment of Mail Factories
043: * @author Adriana Danes
044: */
045: public class EditMailFactoryPropertiesAction extends BaseDeployAction {
046:
047: public ActionForward executeAction(ActionMapping p_Mapping,
048: ActionForm p_Form, HttpServletRequest p_Request,
049: HttpServletResponse p_Response) throws IOException,
050: ServletException {
051:
052: String sAction = p_Request.getParameter("action");
053: MailFactoryPropertiesForm oForm = new MailFactoryPropertiesForm();
054: oForm.reset(p_Mapping, p_Request);
055: m_Session.setAttribute("mailFactoryPropertiesForm", oForm);
056: String name = "";
057: Properties props = new Properties();
058: try {
059: populate(name, props, oForm);
060: } catch (Throwable t) {
061: addGlobalError(t);
062: saveErrors(p_Request, m_Errors);
063: return (p_Mapping.findForward("Global Error"));
064: }
065:
066: // Forward to the jsp.
067: return (p_Mapping.findForward("MailFactory Properties"));
068: }
069:
070: /**
071: * Populate a form with configuration properties read in a configuration file
072: *
073: * @param mailFactoryName the name of the mail factory
074: * @param props properties to configure the mail factory
075: * param pForm the form used to print-out the configuration properties
076: */
077: void populate(String mailFactoryName, Properties props,
078: MailFactoryPropertiesForm pForm) throws Exception {
079: // Set configuration attributes
080: // - mail factory name
081: pForm.setMailFactoryName(mailFactoryName);
082: // - jndi name
083: pForm.setJndiName(getStringAttribute(props,
084: "mail.factory.name", pForm.getJndiName()));
085: props.remove("mail.factory.name");
086: // - type
087: pForm.setType(getStringAttribute(props, "mail.factory.type",
088: pForm.getType()));
089: props.remove("mail.factory.type");
090:
091: // Set authentication properties
092: pForm.setUsername(getStringAttribute(props,
093: "mail.authentication.username", pForm.getUsername()));
094: props.remove("mail.authentication.username");
095: pForm.setPassword(getStringAttribute(props,
096: "mail.authentication.password", pForm.getPassword()));
097: props.remove("mail.authentication.password");
098:
099: // Set mime part properties
100: pForm
101: .setTo(getStringAttribute(props, "mail.to", pForm
102: .getTo()));
103: pForm.setSubject(getStringAttribute(props, "mail.subject",
104: pForm.getSubject()));
105: pForm
106: .setCc(getStringAttribute(props, "mail.cc", pForm
107: .getCc()));
108: pForm.setBcc(getStringAttribute(props, "mail.bcc", pForm
109: .getBcc()));
110: props.remove("mail.to");
111: props.remove("mail.subject");
112: props.remove("mail.cc");
113: props.remove("mail.bcc");
114:
115: // Set the rest of the properties as session properties
116: String aName = null;
117: String aValue = null;
118: StringBuffer bufSessionProps = new StringBuffer();
119: for (Enumeration pNames = props.propertyNames(); pNames
120: .hasMoreElements();) {
121: aName = (String) pNames.nextElement();
122: aValue = (String) props.getProperty(aName);
123: bufSessionProps.append(aName);
124: bufSessionProps.append("=");
125: bufSessionProps.append(aValue);
126: bufSessionProps.append(",");
127: bufSessionProps.append("\n");
128: }
129: if (!props.isEmpty()) {
130: // delete the last 2 chars (, and \n)
131: bufSessionProps.delete(bufSessionProps.length() - 2,
132: bufSessionProps.length() - 1);
133: } else {
134: // Actually no chars in sessionPropsBuf
135: //System.out.println("no session props, append mail.host= ");
136: bufSessionProps.append("mail.host=");
137: }
138: String sSessionProps = new String(bufSessionProps);
139: pForm.setSessionProps(sSessionProps);
140: }
141:
142: /**
143: * Use a form's content to create the Properties object needed to create and load a Mail factory
144: *
145: */
146: Properties getPropsFromForm(MailFactoryPropertiesForm p_Form,
147: boolean mimepart) throws Exception {
148: Properties oProps = new Properties();
149: Properties sProps = getPropsFromString(p_Form.getSessionProps());
150: oProps.putAll(sProps);
151: setStringAttribute(oProps, "mail.factory.name", p_Form
152: .getJndiName());
153: setStringAttribute(oProps, "mail.factory.type", p_Form
154: .getType());
155: setStringAttribute(oProps, "mail.authentication.username",
156: p_Form.getUsername());
157: setStringAttribute(oProps, "mail.authentication.password",
158: p_Form.getPassword());
159: String aName = null;
160: String aValue = null;
161: for (Enumeration pNames = oProps.propertyNames(); pNames
162: .hasMoreElements();) {
163: aName = (String) pNames.nextElement();
164: aValue = (String) oProps.getProperty(aName);
165: }
166: if (mimepart == true) {
167: setStringAttribute(oProps, "mail.to", p_Form.getTo());
168: setStringAttribute(oProps, "mail.subject", p_Form
169: .getSubject());
170: setStringAttribute(oProps, "mail.cc", p_Form.getCc());
171: setStringAttribute(oProps, "mail.bcc", p_Form.getBcc());
172: }
173: return oProps;
174: }
175: }
|