From: <fg...@us...> - 2007-02-05 21:47:05
|
Revision: 214 http://svn.sourceforge.net/openutils/?rev=214&view=rev Author: fgiust Date: 2007-02-05 13:47:01 -0800 (Mon, 05 Feb 2007) Log Message: ----------- set a fixed length for date parsing and cleanup dead code Modified Paths: -------------- trunk/openutils-spring/pom.xml trunk/openutils-spring/src/main/java/it/openutils/web/spring/BaseFormController.java Modified: trunk/openutils-spring/pom.xml =================================================================== --- trunk/openutils-spring/pom.xml 2007-02-05 21:44:02 UTC (rev 213) +++ trunk/openutils-spring/pom.xml 2007-02-05 21:47:01 UTC (rev 214) @@ -57,7 +57,7 @@ <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> - <version>1.0.1</version> + <version>1.2</version> </dependency> <dependency> <groupId>org.hibernate</groupId> Modified: trunk/openutils-spring/src/main/java/it/openutils/web/spring/BaseFormController.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/web/spring/BaseFormController.java 2007-02-05 21:44:02 UTC (rev 213) +++ trunk/openutils-spring/src/main/java/it/openutils/web/spring/BaseFormController.java 2007-02-05 21:47:01 UTC (rev 214) @@ -38,7 +38,7 @@ // convert java.util.Date SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); dateFormat.setLenient(false); - binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true)); + binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true, 10)); // convert Integers and Longs binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, true)); @@ -51,23 +51,6 @@ } - // /** - // * Convenience method for getting a i18n key's value. Calling getMessageSourceAccessor() is used because the - // * RequestContext variable is not set in unit tests b/c there's no DispatchServlet Request. - // * @param msgKey - // * @return String - // */ - // public String getText(String msgKey) - // { - // String currentLocale = ThreadLocaleHolder.getLocale(); - // if (currentLocale != null) - // { - // return getMessageSourceAccessor().getMessage(msgKey, new Locale(currentLocale)); - // } - // - // return getMessageSourceAccessor().getMessage(msgKey); - // } - /** * Convenient method for getting a i18n key's value with a single string argument. * @param msgKey @@ -76,26 +59,9 @@ */ public String getText(String msgKey, Object arg) { - return getText(msgKey, new Object[]{arg}); + return getText(msgKey, new Object[]{arg }); } - // /** - // * Convenience method for getting a i18n key's value with arguments. - // * @param msgKey - // * @param args - // * @return String - // */ - // public String getText(String msgKey, Object[] args) - // { - // String currentLocale = ThreadLocaleHolder.getLocale(); - // if (currentLocale != null) - // { - // return getMessageSourceAccessor().getMessage(msgKey, args, new Locale(currentLocale)); - // } - // - // return getMessageSourceAccessor().getMessage(msgKey, args); - // } - public void saveMessage(HttpServletRequest request, String message) { MessageUtils.saveMessage(request, message); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <mol...@us...> - 2007-06-15 15:50:13
|
Revision: 340 http://svn.sourceforge.net/openutils/?rev=340&view=rev Author: molaschi Date: 2007-06-15 08:50:14 -0700 (Fri, 15 Jun 2007) Log Message: ----------- spring exporter for enabling bean to be called by http request and to return results in JSON protocol Modified Paths: -------------- trunk/openutils-spring/pom.xml Added Paths: ----------- trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/ trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/ trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java Modified: trunk/openutils-spring/pom.xml =================================================================== --- trunk/openutils-spring/pom.xml 2007-06-14 09:02:24 UTC (rev 339) +++ trunk/openutils-spring/pom.xml 2007-06-15 15:50:14 UTC (rev 340) @@ -1,4 +1,5 @@ -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>net.sourceforge.openutils</groupId> @@ -44,6 +45,17 @@ </exclusions> </dependency> <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-remoting</artifactId> + <version>2.0.1</version> + </dependency> + <dependency> + <groupId>net.sf.json-lib</groupId> + <artifactId>json-lib</artifactId> + <version>1.1</version> + <classifier>jdk15</classifier> + </dependency> + <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.3</version> Added: trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java (rev 0) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java 2007-06-15 15:50:14 UTC (rev 340) @@ -0,0 +1,112 @@ +package it.openutils.spring.remoting.exporters; + +import java.lang.reflect.Method; +import java.lang.reflect.TypeVariable; +import java.util.Enumeration; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import net.sf.json.JSONObject; + +import org.apache.commons.beanutils.ConvertUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.math.NumberUtils; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.remoting.support.RemoteInvocation; +import org.springframework.remoting.support.RemoteInvocationBasedExporter; +import org.springframework.remoting.support.RemoteInvocationResult; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.Controller; + + +/** + * @author mmolaschi + * @version $Id: $ + */ +public class JSONServiceExporter extends RemoteInvocationBasedExporter implements Controller +{ + private static final String METHOD = "__method__"; + + private static final String PARAM_PREFIX = "param"; + + private static final String CONTENT_TYPE = "text/x-json"; + + /** + * {@inheritDoc} + */ + public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception + { + if (request.getParameter(METHOD) != null) + { + String methodRequest = request.getParameter(METHOD); + Method[] methods = this.getServiceInterface().getMethods(); + for (Method method : methods) + { + if (method.getName().equals(methodRequest)) + { + Class[] types = method.getParameterTypes(); + Object[] values = new Object[types.length]; + if (values.length > 0) + { + int i = 0; + Enumeration<String> names = request.getParameterNames(); + while (names.hasMoreElements()) + { + String name = names.nextElement(); + if (name != null && name.startsWith(PARAM_PREFIX)) + { + String posStr = StringUtils.substringAfter(name, PARAM_PREFIX); + int pos = NumberUtils.toInt(posStr); + + String[] reqValues = request.getParameterValues(name); + if (reqValues != null && reqValues.length > 0) + { + if (types[pos].isArray()) + { + values[pos] = ConvertUtils.convert(reqValues, types[pos]); + } + else + { + values[pos] = ConvertUtils.convert(reqValues[0], types[pos]); + } + } + else + { + values[pos] = null; + } + + } + } + } + + RemoteInvocation invocation = new RemoteInvocation(); + invocation.setArguments(values); + invocation.setParameterTypes(types); + invocation.setMethodName(methodRequest); + + RemoteInvocationResult result = invokeAndCreateResult(invocation, this.getProxyForService()); + + JSONObject obj = null; + if (result.hasException()) + { + obj = JSONObject.fromBean(result.getException()); + obj.put("exception", true); + } + else + { + obj = JSONObject.fromBean(result.getValue()); + obj.put("exception", false); + } + + response.setContentType(CONTENT_TYPE); + obj.write(response.getWriter()); + + break; + } + } + + } + return null; + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2008-01-14 12:01:07
|
Revision: 527 http://openutils.svn.sourceforge.net/openutils/?rev=527&view=rev Author: fgiust Date: 2008-01-14 04:01:09 -0800 (Mon, 14 Jan 2008) Log Message: ----------- cleanup and email service draft Modified Paths: -------------- trunk/openutils-spring/pom.xml trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextHolder.java trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextListener.java trunk/openutils-spring/src/main/java/it/openutils/spring/editors/GenericSetPropertyEditor.java trunk/openutils-spring/src/main/java/it/openutils/spring/editors/GenericSinglePropertyEditor.java trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java trunk/openutils-spring/src/main/java/it/openutils/spring/validation/hibernate/AnnotationValidator.java trunk/openutils-spring/src/main/java/it/openutils/web/spring/BaseFormController.java trunk/openutils-spring/src/main/java/it/openutils/web/spring/EasyDoubleCustomEditor.java trunk/openutils-spring/src/main/java/it/openutils/web/spring/MultiController.java trunk/openutils-spring/src/main/java/it/openutils/web/spring/ParamNameUrlHandlerMapping.java trunk/openutils-spring/src/main/java/it/openutils/web/spring/SimpleController.java trunk/openutils-spring/src/main/java/it/openutils/web/util/MessageUtils.java Added Paths: ----------- trunk/openutils-spring/src/main/java/it/openutils/spring/email/ trunk/openutils-spring/src/main/java/it/openutils/spring/email/FileMessageStore.java trunk/openutils-spring/src/main/java/it/openutils/spring/email/MessageStore.java trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSender.java trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSenderImpl.java Modified: trunk/openutils-spring/pom.xml =================================================================== --- trunk/openutils-spring/pom.xml 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/pom.xml 2008-01-14 12:01:09 UTC (rev 527) @@ -9,7 +9,7 @@ </parent> <artifactId>openutils-spring</artifactId> <name>openutils spring tools</name> - <version>1.0.5-SNAPSHOT</version> + <version>2.0-SNAPSHOT</version> <description /> <dependencies> <dependency> @@ -53,6 +53,7 @@ <artifactId>json-lib</artifactId> <version>1.1</version> <classifier>jdk15</classifier> + <optional>true</optional> <exclusions> <exclusion> <groupId>commons-logging</groupId> @@ -108,6 +109,23 @@ <version>2.1_3</version> </dependency> <dependency> + <groupId>commons-io</groupId> + <artifactId>commons-io</artifactId> + <version>1.3.1</version> + </dependency> + <dependency> + <groupId>javax.mail</groupId> + <artifactId>mail</artifactId> + <version>1.4</version> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.freemarker</groupId> + <artifactId>freemarker</artifactId> + <version>2.3.11</version> + <optional>true</optional> + </dependency> + <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> Modified: trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextHolder.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextHolder.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextHolder.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.spring.context; import org.springframework.web.context.WebApplicationContext; Modified: trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextListener.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextListener.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/context/WebApplicationContextListener.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.spring.context; import javax.servlet.ServletRequestEvent; Modified: trunk/openutils-spring/src/main/java/it/openutils/spring/editors/GenericSetPropertyEditor.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/editors/GenericSetPropertyEditor.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/editors/GenericSetPropertyEditor.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.spring.editors; import java.beans.PropertyEditorSupport; @@ -128,7 +143,8 @@ parameters[0] = new Long(id); } - T value = (T) MethodUtils.invokeMethod(dao, "load", parameters, new Class[]{this.propertyType}); + T value = (T) MethodUtils + .invokeMethod(dao, "load", parameters, new Class[]{this.propertyType }); set.add(value); } catch (Throwable e) Modified: trunk/openutils-spring/src/main/java/it/openutils/spring/editors/GenericSinglePropertyEditor.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/editors/GenericSinglePropertyEditor.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/editors/GenericSinglePropertyEditor.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.spring.editors; import java.beans.PropertyEditorSupport; Added: trunk/openutils-spring/src/main/java/it/openutils/spring/email/FileMessageStore.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/email/FileMessageStore.java (rev 0) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/email/FileMessageStore.java 2008-01-14 12:01:09 UTC (rev 527) @@ -0,0 +1,100 @@ +package it.openutils.spring.email; + +import java.io.File; +import java.io.IOException; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMessage.RecipientType; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.ObjectUtils; +import org.apache.commons.lang.StringEscapeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * @author fgiust + * @version $Id: $ + */ +public class FileMessageStore implements MessageStore +{ + + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(FileMessageStore.class); + + private String outputDirectory; + + /** + * Sets the outputDirectory. + * @param outputDirectory the outputDirectory to set + */ + public void setOutputDirectory(String outputDirectory) + { + this.outputDirectory = outputDirectory; + } + + /** + * {@inheritDoc} + */ + public void saveMessage(MimeMessage message, String template) + { + File dir = new File(outputDirectory); + dir.mkdirs(); + File file = new File(dir, template + "." + System.currentTimeMillis() + ".html"); + + StringBuffer buffer = new StringBuffer(); + + try + { + buffer.append("From: "); + buffer.append(StringEscapeUtils.escapeXml(ArrayUtils.toString(message.getFrom()))); + buffer.append("\n"); + + if (message.getReplyTo() != null) + { + buffer.append("Reply-to: "); + buffer.append(StringEscapeUtils.escapeXml(ArrayUtils.toString(message.getReplyTo()))); + buffer.append("\n"); + } + + buffer.append("To: "); + buffer.append(StringEscapeUtils.escapeXml(ArrayUtils.toString(message.getRecipients(RecipientType.TO)))); + buffer.append("\n"); + + if (message.getRecipients(RecipientType.CC) != null) + { + buffer.append("CC: "); + buffer + .append(StringEscapeUtils.escapeXml(ArrayUtils.toString(message.getRecipients(RecipientType.CC)))); + buffer.append("\n"); + } + + if (message.getRecipients(RecipientType.BCC) != null) + { + buffer.append("BCC: "); + buffer.append(StringEscapeUtils + .escapeXml(ArrayUtils.toString(message.getRecipients(RecipientType.BCC)))); + buffer.append("\n"); + } + buffer.append("\n"); + + buffer.append(ObjectUtils.toString(message.getContent())); + + FileUtils.writeStringToFile(file, buffer.toString()); + } + catch (MessagingException e) + { + log.error(e.getMessage(), e); + } + catch (IOException e) + { + log.error(e.getMessage(), e); + } + } + +} Property changes on: trunk/openutils-spring/src/main/java/it/openutils/spring/email/FileMessageStore.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-spring/src/main/java/it/openutils/spring/email/MessageStore.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/email/MessageStore.java (rev 0) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/email/MessageStore.java 2008-01-14 12:01:09 UTC (rev 527) @@ -0,0 +1,14 @@ +package it.openutils.spring.email; + +import javax.mail.internet.MimeMessage; + + +/** + * @author fgiust + * @version $Id: $ + */ +public interface MessageStore +{ + + void saveMessage(MimeMessage message, String template); +} Property changes on: trunk/openutils-spring/src/main/java/it/openutils/spring/email/MessageStore.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSender.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSender.java (rev 0) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSender.java 2008-01-14 12:01:09 UTC (rev 527) @@ -0,0 +1,45 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.spring.email; + +import java.io.File; +import java.util.List; +import java.util.Map; + + +/** + * @author fgiust + * @version $Id: $ + */ +public interface TemplatedMailSender +{ + + /** + * Compiles the freemarker template with the model data and send it to the given addresses, adding a cc to the given + * addresses. + * @param templateFile Freemarker template file name + * @param model Map merged into template + * @param from Mail that will result as sender of the compiled mail. + * @param to mail to list + * @param cc mail cc list + * @param replyto TODO + * @param async send mail asynchrounisly (if allowed in configuration) + * @param attachments list of attachments + */ + void prepareAndSendMail(final String templateFile, final Map<String, Object> model, final String from, + final String[] to, final String[] cc, final String[] bcc, String replyto, boolean async, List<File> attachments); + +} Property changes on: trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSender.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Added: trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSenderImpl.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSenderImpl.java (rev 0) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSenderImpl.java 2008-01-14 12:01:09 UTC (rev 527) @@ -0,0 +1,216 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.spring.email; + +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; + +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import javax.mail.internet.MimeMessage; + +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.exception.NestableRuntimeException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.InputStreamSource; +import org.springframework.mail.MailException; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.mail.javamail.MimeMessagePreparator; +import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; + + +/** + * @author fgiust + * @version $Id: $ + */ +public class TemplatedMailSenderImpl implements TemplatedMailSender +{ + + /** + * Logger. + */ + private Logger log = LoggerFactory.getLogger(TemplatedMailSenderImpl.class); + + /** + * Spring mail sender. + */ + private JavaMailSender mailSender; + + private Configuration freemarkerConfiguration; + + private MessageStore store; + + /** + * Allow async delivery. + */ + private boolean allowAsync; + + /** + * Sets the mailSender. + * @param mailSender the mailSender to set + */ + public void setMailSender(JavaMailSender mailSender) + { + this.mailSender = mailSender; + } + + /** + * Sets the freemarkerConfiguration. + * @param freemarkerConfiguration the freemarkerConfiguration to set + */ + public void setFreemarkerConfiguration(Configuration freemarkerConfiguration) + { + this.freemarkerConfiguration = freemarkerConfiguration; + } + + /** + * Sets the allowAsync. + * @param allowAsync the allowAsync to set + */ + public void setAllowAsync(boolean allowAsync) + { + this.allowAsync = allowAsync; + } + + /** + * Sets the store. + * @param store the store to set + */ + public void setStore(MessageStore store) + { + this.store = store; + } + + /** + * {@inheritDoc} + */ + public void prepareAndSendMail(final String template, final Map<String, Object> model, final String from, + final String[] to, final String[] cc, final String[] bcc, final String replyto, boolean async, + final List<File> attachments) + { + + String errorMessageTmp = "Error sending mail to " + ArrayUtils.toString(to); + String infoMsg = "Mail is sent to " + ArrayUtils.toString(to); + String infoPrepareMsg = "Preparing to send mail to " + ArrayUtils.toString(to); + + StringBuffer infoBuf = new StringBuffer(); + StringBuffer prepareBuf = new StringBuffer(); + + final String infoMessage = infoMsg + infoBuf.toString(); + final String infoPrepareMessage = infoPrepareMsg + prepareBuf.toString(); + final String errorMessage = errorMessageTmp; + + if (StringUtils.isEmpty(from)) + { + log.error(errorMessage + "sender not set", new Exception("Stacktrace added for debug only")); + return; + } + + log.info(infoPrepareMessage); + + Thread sendmailThread = new Thread() + { + + @Override + public void run() + { + MimeMessagePreparator preparator = new MimeMessagePreparator() + { + + public void prepare(MimeMessage mimeMessage) throws Exception + { + MimeMessageHelper message = new MimeMessageHelper(mimeMessage, attachments != null + && !attachments.isEmpty()); + message.setFrom(from); + message.setTo(to); + if (StringUtils.isNotEmpty(replyto)) + { + message.setReplyTo(replyto); + } + if (cc != null && cc.length > 0) + { + message.setCc(cc); + } + if (bcc != null && bcc.length > 0) + { + message.setBcc(bcc); + } + String text; + try + { + Template freemarkerTemplate = freemarkerConfiguration.getTemplate(template, "UTF-8"); + text = FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerTemplate, model); + } + catch (IOException e) + { + throw new NestableRuntimeException(e); + } + catch (TemplateException e) + { + throw new NestableRuntimeException(e); + } + String subject = StringUtils.trim(StringUtils.substringBefore(text, "\n")); + + message.setSubject(subject); + message.setText(StringUtils.substringAfter(text, "\n"), true); + if (attachments != null && !attachments.isEmpty()) + { + for (File file : attachments) + { + InputStreamSource attachment = new FileSystemResource(file); + message.addAttachment(file.getName(), attachment); + } + } + + if (store != null) + { + store.saveMessage(mimeMessage, template); + } + } + }; + try + { + mailSender.send(preparator); + log.info(infoMessage); + } + catch (MailException me) + { + log.error(errorMessage, me); + throw me; + } + } + }; + + if (async && allowAsync) + { + sendmailThread.start(); + } + else + { + sendmailThread.run(); + } + + } + +} Property changes on: trunk/openutils-spring/src/main/java/it/openutils/spring/email/TemplatedMailSenderImpl.java ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Modified: trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/remoting/exporters/JSONServiceExporter.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.spring.remoting.exporters; import java.beans.BeanInfo; Modified: trunk/openutils-spring/src/main/java/it/openutils/spring/validation/hibernate/AnnotationValidator.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/spring/validation/hibernate/AnnotationValidator.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/spring/validation/hibernate/AnnotationValidator.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.spring.validation.hibernate; import java.util.Enumeration; Modified: trunk/openutils-spring/src/main/java/it/openutils/web/spring/BaseFormController.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/web/spring/BaseFormController.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/web/spring/BaseFormController.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.web.spring; import it.openutils.web.util.MessageUtils; Modified: trunk/openutils-spring/src/main/java/it/openutils/web/spring/EasyDoubleCustomEditor.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/web/spring/EasyDoubleCustomEditor.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/web/spring/EasyDoubleCustomEditor.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.web.spring; import java.text.NumberFormat; Modified: trunk/openutils-spring/src/main/java/it/openutils/web/spring/MultiController.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/web/spring/MultiController.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/web/spring/MultiController.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.web.spring; import it.openutils.web.util.MessageUtils; Modified: trunk/openutils-spring/src/main/java/it/openutils/web/spring/ParamNameUrlHandlerMapping.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/web/spring/ParamNameUrlHandlerMapping.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/web/spring/ParamNameUrlHandlerMapping.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.web.spring; import java.util.HashMap; Modified: trunk/openutils-spring/src/main/java/it/openutils/web/spring/SimpleController.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/web/spring/SimpleController.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/web/spring/SimpleController.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.web.spring; import it.openutils.web.util.MessageUtils; @@ -71,7 +86,7 @@ */ public String getText(String msgKey, Object arg) { - return getText(msgKey, new Object[]{arg}); + return getText(msgKey, new Object[]{arg }); } public void saveMessage(HttpServletRequest request, String message) Modified: trunk/openutils-spring/src/main/java/it/openutils/web/util/MessageUtils.java =================================================================== --- trunk/openutils-spring/src/main/java/it/openutils/web/util/MessageUtils.java 2008-01-14 12:00:28 UTC (rev 526) +++ trunk/openutils-spring/src/main/java/it/openutils/web/util/MessageUtils.java 2008-01-14 12:01:09 UTC (rev 527) @@ -1,3 +1,18 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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.openutils.web.util; import java.util.ArrayList; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |