Author: thorsten
Date: Mon Jul 22 13:56:51 2013
New Revision: 1505683
URL: http://svn.apache.org/r1505683
Log:
COCOON3-129
Adding example to use a pipeline to transform a the body of a mail prior to send.
Added:
cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/mail/
cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/mail/pipelines/
cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/mail/pipelines/pipes/
cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/mail/pipelines/pipes/EmailPlainPipe.java
cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/sample/SendMailPipeService.java
Modified:
cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/js/mail.js
cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/welcome.html
cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/META-INF/cocoon/spring/block-application-context.xml
Added: cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/mail/pipelines/pipes/EmailPlainPipe.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/mail/pipelines/pipes/EmailPlainPipe.java?rev=1505683&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/mail/pipelines/pipes/EmailPlainPipe.java (added)
+++ cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/mail/pipelines/pipes/EmailPlainPipe.java Mon Jul 22 13:56:51 2013
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.cocoon.rest.optional.mail.pipelines.pipes;
+
+import java.io.ByteArrayInputStream;
+import java.io.OutputStream;
+import java.util.Date;
+import java.util.Map;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.cocoon.pipeline.NonCachingPipeline;
+import org.apache.cocoon.pipeline.Pipeline;
+import org.apache.cocoon.sax.SAXPipelineComponent;
+import org.apache.cocoon.sax.component.TextSerializer;
+import org.apache.cocoon.sax.component.XMLGenerator;
+import org.apache.cocoon.sax.component.XSLTTransformer;
+import org.springframework.beans.factory.config.ConfigurableBeanFactory;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
+
+@Component
+@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
+public class EmailPlainPipe extends NonCachingPipeline<SAXPipelineComponent>
+ implements Pipeline<SAXPipelineComponent> {
+
+ @Override
+ public void setup(OutputStream outputStream, Map<String, Object> parameters) {
+ byte[] bytes = (byte[]) parameters.get("input");
+ XMLGenerator generator = new XMLGenerator(bytes);
+ this.addComponent(generator);
+ byte[] xsl = (byte[]) parameters.get("xsl");
+ Source xslSource = new StreamSource(new ByteArrayInputStream(xsl));
+ XSLTTransformer transformer = new XSLTTransformer(
+ xslSource, new Date().getTime());
+ // pass all parameter to the xslTransformer
+ transformer.setParameters(parameters);
+ this.addComponent(transformer);
+ this.addComponent(TextSerializer.createPlainSerializer());
+ super.setup(outputStream, parameters);
+ }
+}
Added: cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/sample/SendMailPipeService.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/sample/SendMailPipeService.java?rev=1505683&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/sample/SendMailPipeService.java (added)
+++ cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/java/org/apache/cocoon/rest/optional/sample/SendMailPipeService.java Mon Jul 22 13:56:51 2013
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.cocoon.rest.optional.sample;
+
+import java.io.ByteArrayOutputStream;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.cocoon.pipeline.Pipeline;
+import org.apache.cocoon.rest.controller.annotation.RESTController;
+import org.apache.cocoon.rest.controller.annotation.RequestParameter;
+import org.apache.cocoon.rest.controller.method.Post;
+import org.apache.cocoon.rest.controller.response.RestResponse;
+import org.apache.cocoon.rest.controller.response.TextResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.mail.MailException;
+import org.springframework.mail.MailSender;
+import org.springframework.mail.SimpleMailMessage;
+import org.springframework.util.StringUtils;
+
+@RESTController
+public class SendMailPipeService implements Post {
+ @RequestParameter
+ private String name;
+ @Autowired
+ private MailSender mailSender;
+ @Autowired
+ private SimpleMailMessage templateMessage;
+ @Autowired
+ @Qualifier("emailPlainPipe")
+ private Pipeline<?> mailPipe;
+ private String xml="<root> Hello <name/> how do you like the pipeline example</root>";
+ private String xsl="<xsl:stylesheet xmlns=\"http://www.w3.org/1999/xhtml\" "
+ + "version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
+ + "<xsl:param name=\"name\" select=\"'cocoon'\"/>"
+ + "<xsl:template match=\"/\"><text><xsl:apply-templates/></text></xsl:template>"
+ + "<xsl:template match=\"name\"><xsl:value-of select=\"$name\"/></xsl:template>"
+ + "</xsl:stylesheet>";
+ /**
+ * Logger.
+ */
+ private static final Logger logger = LoggerFactory
+ .getLogger(SendMailPipeService.class);
+
+ @Override
+ public RestResponse doPost() throws Exception {
+ try {
+ boolean success = sendMail();
+ if (success) {
+ return new TextResponse("{\"status\":" + true + " }", "application/json");
+ }
+ } catch (Exception e) {
+ logger.error("could not send mail");
+ }
+ return new TextResponse("{\"status\":" + false + " }", "application/json");
+ }
+
+ private boolean sendMail() throws Exception {
+ String text;
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ Map<String, Object> parameters = new HashMap<String, Object>();
+ parameters.put("input", xml.getBytes());
+ parameters.put("xsl", xsl.getBytes());
+ if(!StringUtils.isEmpty(name)){
+ parameters.put("name", name);
+ }
+ // prepare the pipe
+ mailPipe.setup(baos, parameters);
+ // execute the pipe and create the txt
+ mailPipe.execute();
+ // set text response
+ text = new String(baos.toByteArray());
+ SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
+ if(!StringUtils.isEmpty(text)){
+ this.templateMessage.setText(text);
+ } else {
+ this.templateMessage.setText("Something went wrong!");
+ }
+ msg.setSentDate(new Date());
+ try {
+ this.mailSender.send(msg);
+ return true;
+ } catch (MailException ex) {
+ logger.error(String.format(
+ "Could not send Mail!\n%s", msg.toString()), ex);
+ return false;
+ }
+ }
+
+}
Modified: cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/js/mail.js
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/js/mail.js?rev=1505683&r1=1505682&r2=1505683&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/js/mail.js (original)
+++ cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/js/mail.js Mon Jul 22 13:56:51 2013
@@ -19,6 +19,11 @@ var onMailSend = function(data){
//Emabling button
$("#sendMail").removeAttr('disabled');
};
+var onMailSendPipe = function(data){
+ $("#serverResponsePipe").html("Result: " + data.status);
+ //Emabling button
+ $("#sendMailPipe").removeAttr('disabled');
+};
$(document).ready(function(){
//Button click event
@@ -42,4 +47,18 @@ $(document).ready(function(){
onMailSend(data);
}, "json");
});
+ $("#sendMailPipe").click(function(e){
+ $("#serverResponsePipe").html("Processing request...");
+ e.preventDefault();
+ //Disabling button
+ $("#sendMailPipe").attr('disabled', 'disabled');
+ // get the input data
+ var name = $("#mailname").val();
+ //Perform POST for triggering long running sendMail
+ $.post('rest/SendMailPipeService', {
+ name: name
+ }, function(data){
+ onMailSendPipe(data);
+ }, "json");
+ });
});
Modified: cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/welcome.html
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/welcome.html?rev=1505683&r1=1505682&r2=1505683&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/welcome.html (original)
+++ cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/COB-INF/welcome.html Mon Jul 22 13:56:51 2013
@@ -58,5 +58,17 @@
</div>
<div id="serverResponse">Here comes the response from server...</div>
</div>
+ <div id="mailPipe">
+ <h2>show case: sendMailPipe</h2>
+ <p>Show how to send a simple eMail with cocoon based on a internal java pipeline doing a transformation.
+ Same as above regarding mail.properties!</p>
+ <div id="form">
+ <form action="rest/SendMailPipeService" method="post">
+ <input type="text" name="name" id="mailname" placeholder="your name" />
+ <input id="sendMailPipe" type="submit" value="Submit" />
+ </form>
+ </div>
+ <div id="serverResponsePipe">Here comes the response from server...</div>
+ </div>
</body>
</html>
Modified: cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/META-INF/cocoon/spring/block-application-context.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/META-INF/cocoon/spring/block-application-context.xml?rev=1505683&r1=1505682&r2=1505683&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/META-INF/cocoon/spring/block-application-context.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-rest-optional/src/main/resources/META-INF/cocoon/spring/block-application-context.xml Mon Jul 22 13:56:51 2013
@@ -27,6 +27,7 @@
<context:include-filter type="annotation"
expression="org.apache.cocoon.rest.controller.annotation.RESTController" />
</context:component-scan>
+ <context:component-scan base-package="org.apache.cocoon.rest.optional.mail.pipelines.pipes" />
<context:annotation-config />
<!-- MAIL -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.