package de.factoryfx.soap;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.*;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Objects;
import java.util.StringTokenizer;
import java.util.function.Predicate;

public class SoapHandler extends GenericServlet {

    private final WebServiceRequestDispatcher dispatcher;
    private final SOAPMessageUtil soapMessageUtil;

    public SoapHandler(WebServiceRequestDispatcher dispatcher, SOAPMessageUtil soapXmlParser) {
        this.dispatcher = dispatcher;
        this.soapMessageUtil = soapXmlParser;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {

        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)res;

        try {
            MessageFactory messageFactory;
            boolean soap12 = false;
            if (Objects.equals(request.getHeader("Content-Type"),"text/xml")){
                messageFactory  = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
            } else {
                //"application/soap+xml"
                messageFactory  = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
                soap12 = true;
            }

            StreamSource messageSource = new StreamSource(request.getInputStream());
            SOAPMessage message = messageFactory.createMessage();
            SOAPPart soapPart = message.getSOAPPart();
            soapPart.setContent(messageSource);
            message.saveChanges();

            WebServiceCallResult callResult = dispatcher.execute(soapMessageUtil.parseRequest(message));
            SOAPMessage responseMessage =
                    callResult.result != null?soapMessageUtil.wrapResponse(callResult.result, messageFactory)
                    :soapMessageUtil.wrapFault(callResult.createFaultDetail(),callResult.fault.getMessage(), messageFactory, soap12);

            if (responseMessage.saveRequired()) {
                responseMessage.saveChanges();
            }
            response.setStatus(HttpServletResponse.SC_OK);
            putHeaders(responseMessage.getMimeHeaders(), response);

            OutputStream os = response.getOutputStream();
            responseMessage.writeTo(os);
            os.flush();

        } catch (SOAPException e) {
            throw new RuntimeException(e);
        }

    }

    private MimeHeaders getHeaders(HttpServletRequest req) {

        Enumeration headerNames = req.getHeaderNames();
        MimeHeaders headers = new MimeHeaders();

        while (headerNames.hasMoreElements()) {
            String headerName = (String)headerNames.nextElement();
            String headerValue = req.getHeader(headerName);

            StringTokenizer values = new StringTokenizer(headerValue, ",");
            while (values.hasMoreTokens()) {
                headers.addHeader(headerName, values.nextToken().trim());
            }
        }
        return headers;
    }
    private void putHeaders(MimeHeaders headers, HttpServletResponse res) {

        Iterator it = headers.getAllHeaders();
        while (it.hasNext()) {
            MimeHeader header = (MimeHeader)it.next();

            String[] values = headers.getHeader(header.getName());
            if (values.length == 1)
                res.setHeader(header.getName(), header.getValue());
            else {
                StringBuffer concat = new StringBuffer();
                int i = 0;
                while (i < values.length) {
                    if (i != 0) {
                        concat.append(',');
                    }
                    concat.append(values[i++]);
                }
                res.setHeader(header.getName(), concat.toString());
            }
        }
    }



}

@@ -46,136 +46,9 @@
 let.
-GenericServlet;%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.ServletRequest;%0Aimport javax.servlet.ServletResponse
+*
 ;%0Aim
@@ -272,38 +272,8 @@
 am;%0A
-import java.util.Enumeration;%0A
 impo
@@ -324,79 +324,8 @@
 cts;
-%0Aimport java.util.StringTokenizer;%0Aimport java.util.function.Predicate;
 %0A%0Apu
@@ -351,23 +351,19 @@
 ler 
-extend
+implement
 s 
-Generic
 Serv
@@ -755,26 +755,8 @@
 rows
- ServletException,
  IOE
@@ -2515,216 +2515,51 @@
     
-private MimeHeaders getHeaders(HttpServletRequest req) %7B%0A%0A        Enumeration headerNames = req.getHeaderNames();%0A        MimeHeaders headers = new MimeHeaders();%0A%0A        while (headerNames.hasMoreElements()
+@Override%0A    public String getServletInfo(
 ) %7B%0A
@@ -2570,377 +2570,87 @@
     
-    String headerName = (String)headerNames.nextElement();%0A            String headerValue = req.getHeader(headerName);%0A%0A   
+return %22SOAP Handler%22;%0A
     
- 
+%7D%0A%0A
     
-StringTokenizer values = new StringTokenizer(headerValue, %22,%22);%0A            while (values.hasMoreTokens()) %7B%0A                headers.addHeader(headerName, values.nextToken().trim());%0A            %7D%0A        %7D%0A        return headers;
+@Override%0A    public void destroy() %7B
 %0A    %7D%0A
+%0A
     
@@ -3440,12 +3440,223 @@
 %0A    %7D%0A%0A
+    ServletConfig config;%0A    @Override%0A    public void init(ServletConfig config) %7B%0A        this.config = config;%0A    %7D%0A%0A    @Override%0A    public ServletConfig getServletConfig() %7B%0A        return config;%0A    %7D%0A
 %0A%0A%7D%0A

Make FB happy