/*
 * Copyright 2014 Wyona
 */
package org.wyona.yanel.impl.resources.mailman;

import org.wyona.yanel.impl.resources.BasicXMLResource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

/**
 * Get email, password and list names in order to subscribe user to various mailman mailing lists
 */
public class SubscribeToMailmanResourceV100 extends BasicXMLResource {
    
    private static Logger log = LogManager.getLogger(SubscribeToMailmanResourceV100.class);
    
    /**
     * @see org.wyona.yanel.impl.resources.BasicXMLResource#getContentXML(String)
     */
    @Override
    protected InputStream getContentXML(String viewId) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("requested viewId: " + viewId);
        }

        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\"?>");

        String email = getEnvironment().getRequest().getParameter("email");
        if (email != null) {
            String password = getEnvironment().getRequest().getParameter("password");
            String passwordConfirmed = getEnvironment().getRequest().getParameter("password-confirmed");
            if (passwordsMatch(password, passwordConfirmed)) {
                subscribeToMailman(email, password);
                sb.append("<subscribe-to-mailman>");
                sb.append("  <success/>");
                sb.append("</subscribe-to-mailman>");
            } else {
                sb.append("<subscribe-to-mailman>");
                sb.append("  <exception message=\"passwords did not match\"/>");
                sb.append("</subscribe-to-mailman>");
            }
        } else {
            sb.append("<subscribe-to-mailman/>");
        }

        return new ByteArrayInputStream(sb.toString().getBytes());
    }

    /**
     * @param email E-Mail address of user
     * @param password Password of user
     */
    private void subscribeToMailman(String email, String password) {
        log.warn("DEBUG: Subscribe user '" + email + "' to mailman ...");
        java.util.Enumeration<String> paras = getEnvironment().getRequest().getParameterNames();
        while(paras.hasMoreElements()) {
            String para = paras.nextElement().toString();
            if (para.startsWith("list-")) {
                String listname = para.substring(5);
                log.warn("DEBUG: Subscribe to list '" + listname + "' ...");
            }
        }
    }

    /**
     * Check whether passwords match
     * @param password User password
     * @param passwordConfirmed Confirmed user password
     * @return true when passwords match and false otherwise
     */
    private boolean passwordsMatch(String password, String passwordConfirmed) {
        if (password != null && passwordConfirmed != null && password.equals(passwordConfirmed)) {
            return true;
        } else {
            log.warn("Passwords did not match!");
            return false;
        }
    }
}

@@ -134,45 +134,8 @@
 e;%0A%0A
-import java.io.ByteArrayInputStream;%0A
 impo
@@ -244,16 +244,115 @@
 nager;%0A%0A
+import org.w3c.dom.Document;%0Aimport org.w3c.dom.Element;%0A%0Aimport org.wyona.commons.xml.XMLHelper;%0A%0A
 /**%0A * G
@@ -921,70 +921,165 @@
     
-StringBuilder sb = new StringBuilder(%22%3C?xml version=%5C%221.0%5C%22?%3E%22
+Document doc = XMLHelper.createDocument(%22http://www.wyona.org/yanel/mailman/1.0.0%22, %22subscribe-to-mailman%22);%0A        Element rootEl = doc.getDocumentElement(
 );%0A%0A
@@ -1179,24 +1179,73 @@
  != null) %7B%0A
+            rootEl.setAttribute(%22email%22, email);%0A
             
@@ -1536,16 +1536,24 @@
 password
+, rootEl
 );%0A     
@@ -1567,265 +1567,228 @@
     
-sb
+rootEl
 .append
-(%22%3Csubscribe-to-mailman%3E%22);%0A                sb.append
+Child(doc.createElement
 (%22
-  %3C
 success
-/%3E%22);%0A                sb.append(%22%3C/subscribe-to-mailman%3E%22);%0A            %7D else %7B%0A                sb.append(%22%3Csubscribe-to-mailman%3E%22);%0A                sb.append(%22  %3Cexception message=%5C
+%22));%0A            %7D else %7B%0A                Element exceptionEl = (Element) rootEl.appendChild(doc.createElement(%22exception%22));%0A                exceptionEl.setAttribute(%22code%22, 
 %22pas
@@ -1797,26 +1797,22 @@
 ords
- did not 
+-did-not-
 match
-%5C%22/%3E
 %22);%0A
@@ -1831,202 +1831,211 @@
     
-sb.append(%22%3C/subscribe-to-mailman%3E%22);%0A            %7D%0A        %7D else %7B%0A            sb.append(%22%3Csubscribe-to-mailman/%3E%22);%0A        %7D%0A%0A        return new ByteArrayInputStream(sb.toString().getBytes()
+exceptionEl.setAttribute(%22message%22, %22passwords did not match%22);%0A            %7D%0A        %7D else %7B%0A            // INFO: No input yet.%0A        %7D%0A%0A        return XMLHelper.getInputStream(doc, false, true, null
 );%0A 
@@ -2127,24 +2127,50 @@
 ord of user%0A
+     * @param rootEl TODO%0A
      */%0A    
@@ -2230,16 +2230,32 @@
 password
+, Element rootEl
 ) %7B%0A    
@@ -2706,27 +2706,569 @@
         
-%7D%0A        %7D
+    if (subscribeToMailingList(email, password, listname)) %7B%0A                    Element listEl = (Element) rootEl.appendChild(rootEl.getOwnerDocument().createElement(%22list%22));%0A                    listEl.setAttribute(%22name%22, listname);%0A                %7D else %7B%0A                    log.error(%22Subscription to list '%22 + listname + %22' for user '%22 + email + %22' failed!%22);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     *%0A     */%0A    private boolean subscribeToMailingList(String email, String password, String listname) %7B%0A        return true;
 %0A    %7D%0A%0A

use dom instead string builder