/*
 * Copyright 2013 Google Inc. All Rights Reserved.
 * 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 com.google.appengine.tck.mail.support;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.tck.base.TestBase;

/**
 * Handle incoming mail.
 *
 * @author terryok@google.com
 */
public class MailHandlerServlet extends HttpServlet {

    Logger log = Logger.getLogger(MailHandlerServlet.class.getName());

    /**
     * Stores subject and headers into memcache for test to confirm mail delivery.
     */
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

        Session session = Session.getDefaultInstance(new Properties(), null);
        MimeMessage message;
        MimeProperties mp;
        try {
            message = new MimeMessage(session, req.getInputStream());
            mp = new MimeProperties(message);

            List<String> headers = new ArrayList<>();
            StringBuilder sb = new StringBuilder();
            Enumeration e = message.getAllHeaderLines();
            while (e.hasMoreElements()) {
                String headerLine = (String) e.nextElement();
                headers.add(headerLine);
                sb.append("\n").append(headerLine);
            }
            log.info("HEADERS: " + sb.toString());

            mp.headers = headers.toString();
            TestBase.putTempData(mp);

        } catch (MessagingException me) {
            log.severe("Error while processing email: " + me.toString());
        }
    }
}

@@ -1230,21 +1230,41 @@
 rvlet %7B%0A
-%0A
     
+private static final 
 Logger l
@@ -1508,24 +1508,16 @@
 se resp)
-%0A       
  throws 
@@ -1530,17 +1530,16 @@
 ption %7B%0A
-%0A
         
@@ -1612,64 +1612,8 @@
 l);%0A
-        MimeMessage message;%0A        MimeProperties mp;%0A
     
@@ -1634,16 +1634,28 @@
         
+MimeMessage 
 message 
@@ -1716,16 +1716,31 @@
         
+MimeProperties 
 mp = new
@@ -2275,17 +2275,16 @@
 ta(mp);%0A
-%0A
         
@@ -2333,18 +2333,29 @@
     
-log.severe
+throw new IOException
 (%22Er
@@ -2384,27 +2384,14 @@
 mail
-: %22 + me.toString()
+.%22, me
 );%0A 

Throw msg exception.