/****************************************************************
 * 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.james.transport.camel;



import java.io.OutputStream;

import javax.annotation.Resource;

import org.apache.camel.Body;
import org.apache.camel.Exchange;
import org.apache.james.SpoolMessageStore;
import org.apache.mailet.Mail;

/**
 * Bean which is used in the camel route to store the real message content to some external
 * storage. This is needed because JMS is not really the best solution for big content
 * 
 * This Bean should get called before the Exchange object get send the JMS Endpoint
 *
 */
public final class MailClaimCheck {
    
    private SpoolMessageStore spoolMessageStore;

    @Resource(name="spoolMessageStore")
    public void setSpooolMessageStore(SpoolMessageStore spoolMessageStore) {
        this.spoolMessageStore = spoolMessageStore;
    }
    
    
    /**
     * Save the Email Message to an external storage
     * 
     * @param exchange
     * @param mail
     * @throws Exception
     */
    public void saveMessage(Exchange exchange, @Body Mail mail) throws Exception{
        
        OutputStream out = spoolMessageStore.saveMessage(mail.getName());
        // write the message to the store
        mail.getMessage().writeTo(out);
        
        // close stream
        out.close();
        
    }
}

@@ -1290,16 +1290,56 @@
 esource;
+%0Aimport javax.mail.internet.MimeMessage;
 %0A%0Aimport
@@ -2252,32 +2252,245 @@
 ption%7B%0A        %0A
+        MimeMessage m = mail.getMessage();%0A%0A        // Check if the message is not null first.. This could be the case if the message was set to%0A        // null before or was disposed%0A        if (m != null) %7B%0A    
         OutputSt
@@ -2555,16 +2555,20 @@
         
+    
 // write
@@ -2601,33 +2601,21 @@
         
-mail.getMessage()
+    m
 .writeTo
@@ -2638,16 +2638,20 @@
         
+   
+ 
 // close
@@ -2666,16 +2666,20 @@
         
+    
 out.clos
@@ -2683,16 +2683,26 @@
 lose();%0A
+        %7D%0A
         

Check if the mail contains a MimeMessage before trying to call writeTo on it. (Prevent NPE)