Author: krasserm
Date: Tue Jan 19 14:37:11 2010
New Revision: 900800

URL: http://svn.apache.org/viewvc?rev=900800&view=rev
Log:
addition of login feature to Camel GAE example and major revision of 
application logic implementation

Added:
    
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportData.java
    
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportGenerator.java
    
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/RequestProcessor.java
    
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ResponseProcessor.java
Removed:
    
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/WeatherProcessor.java
Modified:
    
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/TutorialRouteBuilder.java
    camel/trunk/examples/camel-example-gae/src/main/resources/context.xml
    
camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/appengine-web.xml
    camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/web.xml
    camel/trunk/examples/camel-example-gae/src/main/webapp/index.html

Added: 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportData.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportData.java?rev=900800&view=auto
==============================================================================
--- 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportData.java
 (added)
+++ 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportData.java
 Tue Jan 19 14:37:11 2010
@@ -0,0 +1,85 @@
+/**
+ * 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.camel.example.gae;
+
+import java.io.Serializable;
+
+import org.w3c.dom.Document;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+
+public class ReportData implements Serializable {
+
+    private static final long serialVersionUID = -468314239950430108L;
+
+    private String city;
+    private String recipient;
+    private String requestor;
+    private Document weather;
+    
+    public ReportData(String city, String recipient, String requestor) {
+        this.city = city;
+        this.recipient = recipient;
+        this.requestor = requestor;
+    }
+    
+    public String getCity() {
+        return city;
+    }
+    
+    public String getRecipient() {
+        return recipient;
+    }
+    
+    public String getRequestor() {
+        return requestor;
+    }
+ 
+    public Document getWeather() {
+        return weather;
+    }
+    
+    public void setWeather(Document weather) {
+        this.weather = weather;
+    }
+    
+    public static Expression city() {
+        return new Expression() {
+            public <T> T evaluate(Exchange exchange, Class<T> type) {
+                return 
type.cast(exchange.getIn().getBody(ReportData.class).getCity());
+            }
+        };
+    }
+
+    public static Expression recipient() {
+        return new Expression() {
+            public <T> T evaluate(Exchange exchange, Class<T> type) {
+                return 
type.cast(exchange.getIn().getBody(ReportData.class).getRecipient());
+            }
+        };
+    }
+
+    public static Expression requestor() {
+        return new Expression() {
+            public <T> T evaluate(Exchange exchange, Class<T> type) {
+                return 
type.cast(exchange.getIn().getBody(ReportData.class).getRequestor());
+            }
+        };
+    }
+
+}

Added: 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportGenerator.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportGenerator.java?rev=900800&view=auto
==============================================================================
--- 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportGenerator.java
 (added)
+++ 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ReportGenerator.java
 Tue Jan 19 14:37:11 2010
@@ -0,0 +1,50 @@
+/**
+ * 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.camel.example.gae;
+
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathFactory;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+
+public class ReportGenerator implements Processor {
+
+    public void process(Exchange exchange) throws Exception {
+        ReportData data = exchange.getIn().getBody(ReportData.class);
+        
+        XPathFactory xpfactory = XPathFactory.newInstance();
+        XPath xpath = xpfactory.newXPath();
+
+        // Extract result values via XPath
+        String city = xpath.evaluate("//forecast_information/city/@data", 
data.getWeather());
+        String cond = xpath.evaluate("//current_conditions/condition/@data", 
data.getWeather());
+        String temp = xpath.evaluate("//current_conditions/temp_c/@data", 
data.getWeather());
+        
+        if (city == null || city.length() == 0) {
+            city = data.getCity();
+            cond = "<error retrieving current condition>";
+            temp = "<error retrieving current temperature>";
+        }
+        
+        exchange.getIn().setBody(new StringBuffer()
+            .append("\n").append("Weather report for:  ").append(city)
+            .append("\n").append("Current condition:   ").append(cond)
+            .append("\n").append("Current temperature: 
").append(temp).append(" (Celsius)").toString());
+    }
+
+}

Added: 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/RequestProcessor.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/RequestProcessor.java?rev=900800&view=auto
==============================================================================
--- 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/RequestProcessor.java
 (added)
+++ 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/RequestProcessor.java
 Tue Jan 19 14:37:11 2010
@@ -0,0 +1,44 @@
+/**
+ * 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.camel.example.gae;
+
+import java.util.logging.Logger;
+
+import com.google.appengine.api.users.UserService;
+import com.google.appengine.api.users.UserServiceFactory;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+
+public class RequestProcessor implements Processor {
+
+    private static final Logger LOGGER = 
Logger.getLogger(RequestProcessor.class.getName());
+    
+    public void process(Exchange exchange) throws Exception {
+        UserService userService = UserServiceFactory.getUserService();
+        String city = (String)exchange.getIn().removeHeader("city");
+        String requestor = userService.getCurrentUser().getEmail();
+        String recipient = requestor;
+        
+        if (exchange.getIn().removeHeader("mailtocurrent") == null) {
+            recipient = (String)exchange.getIn().removeHeader("mailto");
+        }
+        exchange.getIn().setBody(new ReportData(city, recipient, requestor));
+        LOGGER.info(requestor + " requested weather data for " + city  + ". 
Report will be sent to " + recipient);
+    }
+
+}

Added: 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ResponseProcessor.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ResponseProcessor.java?rev=900800&view=auto
==============================================================================
--- 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ResponseProcessor.java
 (added)
+++ 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/ResponseProcessor.java
 Tue Jan 19 14:37:11 2010
@@ -0,0 +1,40 @@
+/**
+ * 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.camel.example.gae;
+
+import com.google.appengine.api.users.UserServiceFactory;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+
+public class ResponseProcessor implements Processor {
+
+    public void process(Exchange exchange) throws Exception {
+        ReportData request = exchange.getIn().getBody(ReportData.class);
+        
+        String logoutUrl = 
UserServiceFactory.getUserService().createLogoutURL("/");
+        String logoutLink = "<a href=\"" + logoutUrl + "\">" + "logout</a>";
+        String homeLink = "<a href=\"/\">home</a>";
+        
+        String body = "Weather report for " + request.getCity() + " will be 
sent to "
+            + request.getRecipient() + " (" + homeLink + ", " + logoutLink + 
")";
+
+        exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "text/html");
+        exchange.getOut().setBody(body);
+    }
+
+}

Modified: 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/TutorialRouteBuilder.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/TutorialRouteBuilder.java?rev=900800&r1=900799&r2=900800&view=diff
==============================================================================
--- 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/TutorialRouteBuilder.java
 (original)
+++ 
camel/trunk/examples/camel-example-gae/src/main/java/org/apache/camel/example/gae/TutorialRouteBuilder.java
 Tue Jan 19 14:37:11 2010
@@ -16,32 +16,43 @@
  */
 package org.apache.camel.example.gae;
 
+import org.w3c.dom.Document;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.gae.mail.GMailBinding;
+import org.apache.camel.processor.aggregate.AggregationStrategy;
 
 public class TutorialRouteBuilder extends RouteBuilder {
 
-    private String sender;
-    
-    public void setSender(String sender) {
-        this.sender = sender;
-    }
-    
     @Override
     public void configure() throws Exception {
         from("ghttp:///weather";)
+            .process(new RequestProcessor())
+            .marshal().serialization()
             .to("gtask://default")
-            .setHeader(Exchange.CONTENT_TYPE, constant("text/plain"))
-            .transform(constant("Weather report will be sent to 
").append(header("mailto")));
+            .unmarshal().serialization()
+            .process(new ResponseProcessor());
       
         from("gtask://default")
-            .setHeader(Exchange.HTTP_QUERY, 
constant("weather=").append(header("city")))
-            .to("ghttp://www.google.com/ig/api";)
-            .process(new WeatherProcessor())        
+            .unmarshal().serialization()
+            .setHeader(Exchange.HTTP_QUERY, 
constant("weather=").append(ReportData.city()))
+            .enrich("ghttp://www.google.com/ig/api";, reportDataAggregator())
             .setHeader(GMailBinding.GMAIL_SUBJECT, constant("Weather report"))
-            .setHeader(GMailBinding.GMAIL_TO, header("mailto"))
-            .to("gmail://" + sender);
+            .setHeader(GMailBinding.GMAIL_SENDER, ReportData.requestor())
+            .setHeader(GMailBinding.GMAIL_TO, ReportData.recipient())
+            .process(new ReportGenerator())        
+            .to("gmail://default");
     }
 
+    private static AggregationStrategy reportDataAggregator() {
+        return new AggregationStrategy() {
+            public Exchange aggregate(Exchange reportExchange, Exchange 
weatherExchange) {
+                ReportData reportData = 
reportExchange.getIn().getBody(ReportData.class);
+                
reportData.setWeather(weatherExchange.getIn().getBody(Document.class));
+                return reportExchange;
+            }
+        };
+    }
+    
 }

Modified: camel/trunk/examples/camel-example-gae/src/main/resources/context.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-gae/src/main/resources/context.xml?rev=900800&r1=900799&r2=900800&view=diff
==============================================================================
--- camel/trunk/examples/camel-example-gae/src/main/resources/context.xml 
(original)
+++ camel/trunk/examples/camel-example-gae/src/main/resources/context.xml Tue 
Jan 19 14:37:11 2010
@@ -12,9 +12,8 @@
         <camel:routeBuilder ref="tutorialRouteBuilder"/>
     </camel:camelContext>
     
-    <bean id="tutorialRouteBuilder"
+    <bean id="tutorialRouteBuilder" 
         class="org.apache.camel.example.gae.TutorialRouteBuilder">
-        <property name="sender" value="replac...@gmail.com" />
     </bean>
     
 </beans>

Modified: 
camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/appengine-web.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/appengine-web.xml?rev=900800&r1=900799&r2=900800&view=diff
==============================================================================
--- 
camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/appengine-web.xml
 (original)
+++ 
camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/appengine-web.xml
 Tue Jan 19 14:37:11 2010
@@ -3,7 +3,10 @@
        <application>replaceme</application>
        <version>1</version>
        
-       <!-- Configure java.util.logging -->
+    <static-files>
+        <exclude path="/index.html" />
+    </static-files>
+
        <system-properties>
                <property name="java.util.logging.config.file" 
value="WEB-INF/logging.properties"/>
        </system-properties>

Modified: camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/web.xml
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/web.xml?rev=900800&r1=900799&r2=900800&view=diff
==============================================================================
--- camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/web.xml 
(original)
+++ camel/trunk/examples/camel-example-gae/src/main/webapp/WEB-INF/web.xml Tue 
Jan 19 14:37:11 2010
@@ -20,14 +20,27 @@
         <servlet-name>CamelServlet</servlet-name>
         <url-pattern>/camel/*</url-pattern>
     </servlet-mapping>
-    
     <servlet-mapping>
         <servlet-name>CamelServlet</servlet-name>
         <url-pattern>/worker/*</url-pattern>
     </servlet-mapping>
     
-    <welcome-file-list>
-        <welcome-file>index.html</welcome-file>
-    </welcome-file-list>
+    <security-constraint>
+        <web-resource-collection>
+            <url-pattern>/*</url-pattern>
+        </web-resource-collection>
+        <auth-constraint>
+            <role-name>*</role-name>
+        </auth-constraint>
+    </security-constraint>
+    <security-constraint>
+        <web-resource-collection>
+            <url-pattern>/worker/*</url-pattern>
+        </web-resource-collection>
+        <auth-constraint>
+            <role-name>admin</role-name>
+        </auth-constraint>
+    </security-constraint>
+    
 </web-app>
 

Modified: camel/trunk/examples/camel-example-gae/src/main/webapp/index.html
URL: 
http://svn.apache.org/viewvc/camel/trunk/examples/camel-example-gae/src/main/webapp/index.html?rev=900800&r1=900799&r2=900800&view=diff
==============================================================================
--- camel/trunk/examples/camel-example-gae/src/main/webapp/index.html (original)
+++ camel/trunk/examples/camel-example-gae/src/main/webapp/index.html Tue Jan 
19 14:37:11 2010
@@ -1,25 +1,43 @@
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
 <head>
+<meta http-equiv="expires" content="0">
 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 <title>Weather Report</title>
+<script type="text/javascript">
+function toggleMailto() {
+  in1 = document.getElementById("input1")
+  in2 = document.getElementById("input2")
+  in1.disabled = in2.checked
+}
+</script>
 </head>
 
 <body>
 <h1>Weather Report</h1>
+Generates a simple weather report for a city and sends the report via 
email.<br>
+This application is described in the <a 
href="http://camel.apache.org/tutorial-for-camel-on-google-app-engine.html";>Camel
 tutorial for Google App Engine</a>.   
+<p/>
 <form action="camel/weather" method="post">
 <table>
     <tr>
         <td>City:</td>
-        <td><input type="text" name="city"></td>
+        <td><input type="text" name="city" /></td>
+        <td></td>
+        <td></td>
     </tr>
     <tr>
         <td>Mailto:</td>
-        <td><input type="text" name="mailto"></td>
+        <td><input id="input1" type="text" name="mailto" disabled /></td>
+        <td><input id="input2" type="checkbox" name="mailtocurrent" 
+                   checked onclick="toggleMailto()" /></td>
+        <td>Send report to me</td>
     </tr>
     <tr>
         <td></td>
         <td align="right"><input type="submit" value="Submit" /></td>
+        <td></td>
+        <td></td>
     </tr>
 </table>
 </form>


Reply via email to