<?xml version="1.0"?>
<%@page contentType="text/xml"%>
<%@page import="org.w3c.dom.*,javax.xml.parsers.*"%>
<%@page import="org.apache.xpath.*"%>
<env:Envelope xmlns:xsd="http://www.w3.org/1999/XMLSchema"
     xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
     xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
<env:Body>
<%
DocumentBuilderFactory factory =
    DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(request.getInputStream());
Element root = document.getDocumentElement(),
        namespaces = document.createElement("namespaces");
namespaces.setAttribute("xmlns:env",
                    "http://schemas.xmlsoap.org/soap/envelope/");
namespaces.setAttribute("xmlns:rv",
                        "http://www.psol.com/2001/reverse");
boolean versionMismatch =
    root.getLocalName().equals("Envelope") &&
    !root.getNamespaceURI().equals(
                    "http://schemas.xmlsoap.org/soap/envelope/");
XPathAPI xpath = new XPathAPI();
Node node = xpath.selectSingleNode(document,
         "/env:Envelope/env:Header/*[@env:mustUnderstand = '1']",
         namespaces);
Text text = (Text)xpath.selectSingleNode(document,
               "/env:Envelope/env:Body/rv:reverse/string/text()",
               namespaces);
if(!versionMismatch && node == null && text != null) {
    StringBuffer st = new StringBuffer(text.getData());
    st = st.reverse();
%>
<rv:reverseResponse xmlns:rv="http://www.psol.com/2001/reverse"
   env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <return xsi:type="xsd:string"><%= st.toString() %></return>
</rv:reverseResponse>
<%
}
else {
%>
<env:Fault>
<faultcode><%= versionMismatch ? "env:VersionMismatch" :
                   node != null ? "env:MustUnderstand" :
                                  "env:Client" %></faultcode>
<faultstring>Niestety, żądanie nie może zostać obsłużone.</</faultstring>
</env:Fault>
<%
}
%>
</env:Body>
</env:Envelope>
