001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.example.reportincident;
018    
019    import org.apache.camel.CamelContext;
020    import org.apache.camel.Exchange;
021    import org.apache.camel.builder.RouteBuilder;
022    import org.apache.camel.impl.DefaultCamelContext;
023    import org.apache.camel.language.bean.BeanLanguage;
024    
025    /**
026     * Our routes that we can build using Camel DSL as we extend the RouteBuilder class.
027     * <p/>
028     * In the configure method we have all kind of DSL methods we use for expressing our routes.
029     */
030    public class ReportIncidentRoutes extends RouteBuilder {
031        private boolean usingServletTransport = true;
032        
033        public void setUsingServletTransport(boolean flag) {
034            usingServletTransport = flag;
035        }
036    
037        public void configure() throws Exception {
038            // webservice response for OK
039            OutputReportIncident ok = new OutputReportIncident();
040            ok.setCode("0");
041    
042            // endpoint to our CXF webservice  
043            // We should use the related path to publish the service, when using the ServletTransport
044            // so we need to configure set the bus which is configured to use the ServletTranspot
045            String cxfEndpointAddress = "cxf:/incident?bus=#cxf&";
046            // Using the full http address for stand alone running
047            if (!usingServletTransport) {
048                cxfEndpointAddress = "cxf://http://localhost:9080/camel-example-reportincident/webservices/incident?";
049            }
050            String cxfEndpoint = cxfEndpointAddress
051                    + "serviceClass=org.apache.camel.example.reportincident.ReportIncidentEndpoint"
052                    + "&wsdlURL=etc/report_incident.wsdl";
053    
054            // first part from the webservice -> file backup
055            from(cxfEndpoint)
056                // we need to convert the CXF payload to InputReportIncident that FilenameGenerator and velocity expects
057                .convertBodyTo(InputReportIncident.class)
058                // then set the file name using the FilenameGenerator bean
059                .setHeader(Exchange.FILE_NAME, BeanLanguage.bean(FilenameGenerator.class, "generateFilename"))
060                // and create the mail body using velocity template
061                .to("velocity:etc/MailBody.vm")
062                // and store the file
063                .to("file://target/subfolder")
064                // return OK as response
065                .log("Wrote ${file:name} and returning OK response")
066                .transform(constant(ok));
067    
068            // second part from the file backup -> send email
069            from("file://target/subfolder")
070                // set the subject of the email
071                .setHeader("subject", constant("new incident reported"))
072                // send the email
073                .log("Sending email to incident@mycompany.com:\n${body}")
074                .to("smtp://someone@localhost?password=secret&to=incident@mycompany.com");
075        }
076        
077        public static void main(String args[]) throws Exception {
078            CamelContext camel = new DefaultCamelContext();
079            ReportIncidentRoutes routes = new ReportIncidentRoutes();
080            routes.setUsingServletTransport(false);
081            camel.addRoutes(routes);
082            camel.start();
083        }
084    
085    }