NamedNodeMap attrsSOAPFunction = nSOAPFunction.getAttributes(); String funcNamespace =  null; for(int i = 0; i < attrsSOAPFunction.getLength(); i++) { Node attr = attrsSOAPFunction.item(i); if(attr.getNodeName().equals("xmlns:")) { funcNamespace = attr.getNodeValue(); break; } }
String funcNamespace =  nSOAPFunction.getNamespaceURI();
	public void doPost(DBBroker broker, HttpServletRequest request, HttpServletResponse response, String path) throws BadRequestException, PermissionDeniedException, IOException    {			/*		 * Example incoming SOAP Request		 * 			<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">    			<SOAP-ENV:Header/>    			<SOAP-ENV:Body>        			<echo xmlns="http://localhost:8080/exist/servlet/db/echo.xqws">            			<arg1>adam</arg1>        			</echo>    			</SOAP-ENV:Body>			</SOAP-ENV:Envelope>		 */				// 1) Read the incoming SOAP request		InputStream is = request.getInputStream();		byte[] buf = new byte[request.getContentLength()];		int bytes = 0;		int offset = 0;		int max = 4096;	    while((bytes = is.read(buf, offset, max)) != -1)	    {			offset += bytes;	    }				// 2) Create an XML Document from the SOAP Request	    Document soapRequest = null;		try		{			soapRequest = BuildXMLDocument(buf);		}		catch(Exception e)		{			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);			writeResponse(response, formatXPathException(null, path, new XPathException(null, "Unable to construct an XML document from the SOAP Request, probably an invalid request: " + e.getMessage(), e)), "text/html", ENCODING);			return;		}				// 3) Validate the SOAP Request 		//TODO: validate the SOAP Request				// 4) Extract the function call from the SOAP Request		//NodeList nlBody = soapRequest.getElementsByTagNameNS("http://schemas.xmlsoap.org/soap/envelope/", "Body");		NodeList nlBody = soapRequest.getElementsByTagName("SOAP-ENV:Body");		Node nSOAPBody = nlBody.item(0);		NodeList nlBodyChildren = nSOAPBody.getChildNodes();		Node nSOAPFunction = null;		for(int i = 0; i < nlBodyChildren.getLength(); i++)		{			Node bodyChild = nlBodyChildren.item(i);			if(bodyChild.getNodeType() == Node.ELEMENT_NODE)			{				nSOAPFunction = bodyChild;				break;			}		}				// Check the namespace for the function in the SOAP document is the same as the request path?		NamedNodeMap attrsSOAPFunction = nSOAPFunction.getAttributes();		String funcNamespace =  null;		for(int i = 0; i < attrsSOAPFunction.getLength(); i++)		{			Node attr = attrsSOAPFunction.item(i);			if(attr.getNodeName().equals("xmlns:"))			{				funcNamespace = attr.getNodeValue();				break;			}		}				if(funcNamespace != null)		{			if(!funcNamespace.equals(request.getRequestURL().toString()))			{				//function in SOAP request has an invalid namespace				response.setStatus(HttpServletResponse.SC_BAD_REQUEST);				writeResponse(response, "SOAP Function call has invalid namespace, got: " + funcNamespace + " but expected: " + request.getRequestURL().toString(), "text/html", ENCODING);				return;			}		}		else		{			//function in SOAP request has no namespace			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);			writeResponse(response, "SOAP Function call has no namespace, expected: " + request.getRequestURL().toString(), "text/html", ENCODING);			return;		}				// 5) Execute the XQWS function indicated by the SOAP request  		try		{			//Get the internal description for the function requested by SOAP (should be in the cache)			XQWSDescription description = getXQWSDescription(broker, path, request);						//Create an XQuery to call the XQWS function			CompiledXQuery xqCallXQWS = XQueryExecuteXQWSFunction(broker, nSOAPFunction, description, request, response);						//xqCallXQWS			XQuery xqueryService = broker.getXQueryService();			Sequence xqwsResult = xqueryService.execute(xqCallXQWS, null);						// 6) Create a SOAP Response describing the Result        	byte[] result = description.getSOAPResponse(nSOAPFunction.getNodeName(), xqwsResult, request);        	// 7) Send the SOAP Response to the http servlet response        	response.setContentType("text/xml");			ServletOutputStream os = response.getOutputStream();			BufferedOutputStream bos = new BufferedOutputStream(os);			bos.write(result);			bos.close();			os.close();		}		catch(XPathException xpe)		{			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);			writeResponse(response, formatXPathException(null, path, xpe), "text/html", ENCODING);		}		catch(SAXException saxe)		{			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);			writeResponse(response, formatXPathException(null, path, new XPathException(null, "SAX exception while transforming node: " + saxe.getMessage(), saxe)), "text/html", ENCODING);		}		catch(TransformerConfigurationException tce)		{			response.setStatus(HttpServletResponse.SC_BAD_REQUEST);			writeResponse(response, formatXPathException(null, path, new XPathException(null, "SAX exception while transforming node: " + tce.getMessage(), tce)), "text/html", ENCODING);		}    }