/**
* OLAT - Online Learning and Training
* http://www.olat.org
*
* 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.
*
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),
* University of Zurich, Switzerland.
*
*/ package org.olat.modules.co; import java.io.File; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.mail.Address; import javax.mail.AuthenticationFailedException; import javax.mail.MessagingException; import javax.mail.SendFailedException; import javax.mail.internet.AddressException; import org.olat.core.gui.UserRequest; import org.olat.core.gui.components.Component; import org.olat.core.gui.components.panel.Panel; import org.olat.core.gui.components.velocity.VelocityContainer; import org.olat.core.gui.control.Controller; import org.olat.core.gui.control.Event; import org.olat.core.gui.control.WindowControl; import org.olat.core.gui.control.controller.BasicController; import org.olat.core.gui.control.generic.messages.MessageUIFactory; import org.olat.core.gui.control.generic.modal.DialogBoxController; import org.olat.core.gui.control.generic.modal.DialogBoxUIFactory; import org.olat.core.id.Identity; import org.olat.core.logging.OLATRuntimeException; import org.olat.core.logging.OLog; import org.olat.core.logging.Tracing; import org.olat.core.logging.activity.ThreadLocalUserActivityLogger; import org.olat.core.util.WebappHelper; import org.olat.core.util.mail.ContactList; import org.olat.core.util.mail.ContactMessage; import org.olat.core.util.mail.Emailer; import org.olat.core.util.mail.MailHelper; import org.olat.core.util.mail.MailLoggingAction; /** * Fires Event: *
* Consumes Events from: *
* Main Purpose: is to provide an easy interface for contact message * creation and sending from within different OLAT bulding blocks. *
* Responsabilites:
*
* TODO:pb:b refactor ContactFormController and ContactForm to extract a ContactMessageManager,
* setSubject(..) setRecipients.. etc. should not be in the controller. Refactor to use ContactMessage!
* @see org.olat.modules.co.ContactList
* Initial Date: Jul 19, 2004
* @author patrick
*/
public class ContactFormController extends BasicController {
OLog log = Tracing.createLoggerFor(this.getClass());
//
private Identity emailFrom;
private ContactForm cntctForm;
private VelocityContainer vcCreateContactMsg;
private DialogBoxController noUsersErrorCtr;
private ArrayList
*
* @param ureq
* @param source
* @param event
*/
public void event(UserRequest ureq, Component source, Event event) {
//
}
/**
*/
private void handleMessagingException() {
String infoMessage = translate("error.msg.send.nok");
infoMessage += " generates an infoMessage
*
* @param e
* @throws OLATRuntimeException
* return boolean true: handling was successful, exception can be ignored;
* false: handling was not successful, refuse to proceed.
*/
private boolean handleSendFailedException(SendFailedException e) {
//get wrapped excpetion
MessagingException me = (MessagingException) e.getNextException();
if (me instanceof AuthenticationFailedException) {
// catch this one separately, this kind of exception has no message
// as the other below
StringBuilder infoMessage = new StringBuilder();
infoMessage.append(translate("error.msg.send.nok"));
infoMessage.append("
* i.e. ContactForm and c_contactMsg.html
* creates an InfoMessage in the WindowController on error.
* Fires:
*
*
*
* email was sent successfully by the underlying Email subsystem
* email was not sent correct by the underlying Email subsystem
* email may be partially sent correct, but some parts failed.
* user interaction, i.e. canceled message creation
";
infoMessage += translate("error.msg.content.nok");
this.getWindowControl().setError(infoMessage);
}
/**
* @param success
*/
private void handleAddressException(boolean success) {
StringBuilder errorMessage = new StringBuilder();
if (success) {
errorMessage.append(translate("error.msg.send.partially.nok"));
errorMessage.append("
");
errorMessage.append(translate("error.msg.send.invalid.rcps"));
} else {
errorMessage.append(translate("error.msg.send.nok"));
errorMessage.append("
");
errorMessage.append(translate("error.msg.send.553"));
}
this.getWindowControl().setError(errorMessage.toString());
}
/**
* handles the sendFailedException
");
infoMessage.append(translate("error.msg.smtp.authentication.failed"));
this.getWindowControl().setInfo(infoMessage.toString());
log.warn("Mail message could not be sent: ", e);
// message could not be sent, however let user proceed with his action
return true;
}
String message = me.getMessage();
if (message.startsWith("553")) {
//javax.mail.MessagingException: 553 5.5.4
");
infoMessage.append(translate("error.msg.send.invalid.rcps"));
infoMessage.append(addressesArr2HtmlOList(e.getInvalidAddresses()));
this.getWindowControl().setInfo(infoMessage.toString());
} else if (message.startsWith("503 5.0.0")) {
// message:503 5.0.0 Need RCPT (recipient) ,javax.mail.MessagingException
StringBuilder infoMessage = new StringBuilder();
infoMessage.append(translate("error.msg.send.nok"));
infoMessage.append("
");
infoMessage.append(translate("error.msg.send.no.rcps"));
this.getWindowControl().setInfo(infoMessage.toString());
} else if (message.startsWith("Unknown SMTP host")) {
StringBuilder infoMessage = new StringBuilder();
infoMessage.append(translate("error.msg.send.nok"));
infoMessage.append("
");
infoMessage.append(translate("error.msg.unknown.smtp", WebappHelper.getMailConfig("mailFrom")));
this.getWindowControl().setInfo(infoMessage.toString());
log.warn("Mail message could not be sent: ", e);
// message could not be sent, however let user proceed with his action
return true;
} else if (message.startsWith("Could not connect to SMTP host")){
//could not connect to smtp host, no connection or connection timeout
StringBuilder infoMessage = new StringBuilder();
infoMessage.append(translate("error.msg.send.nok"));
infoMessage.append("
");
infoMessage.append(translate("error.msg.notconnectto.smtp", WebappHelper.getMailConfig("mailhost")));
this.getWindowControl().setInfo(infoMessage.toString());
log.warn(null, e);
// message could not be sent, however let user proceed with his action
return true;
}
else {
throw new OLATRuntimeException(ContactFormController.class, "" + cntctForm.getEmailTo(), e.getNextException());
}
// message could not be sent, return false
return false;
}
/**
* converts an Address[] to an HTML ordered list
*
* @param invalidAdr Address[] with invalid addresses
* @return StringBuilder
*/
private StringBuilder addressesArr2HtmlOList(Address[] invalidAdr) {
StringBuilder iAddressesSB = new StringBuilder();
if (invalidAdr != null && invalidAdr.length > 0) {
iAddressesSB.append("");
for (int i = 0; i < invalidAdr.length; i++) {
iAddressesSB.append("
");
}
return iAddressesSB;
}
/**
* @see org.olat.core.gui.control.DefaultController#doDispose(boolean)
*/
protected void doDispose() {
//
}
}