/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.muse.core.platform.mini;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.apache.muse.core.Environment;
import org.apache.muse.core.platform.AbstractIsolationLayer;
import org.apache.muse.core.routing.ResourceRouter;
import org.apache.muse.util.LoggingUtils;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.addressing.MessageHeaders;
import org.apache.muse.ws.addressing.soap.SoapConstants;
import org.apache.muse.ws.addressing.soap.SoapUtils;
/**
*
* @author Dan Jemiolo (danjemiolo)
*
*/
public class MiniIsolationLayer extends AbstractIsolationLayer
{
private ServletContext _initialContext = null;
private HttpServletRequest _initialRequest = null;
public MiniIsolationLayer(HttpServletRequest initialRequest, ServletContext initialContext)
{
_initialRequest = initialRequest;
_initialContext = initialContext;
}
protected Environment createEnvironment()
{
return new MiniEnvironment(getInitialRequest(), getInitialContext());
}
protected ServletContext getInitialContext()
{
return _initialContext;
}
protected HttpServletRequest getInitialRequest()
{
return _initialRequest;
}
public Document handleRequest(Document request)
{
Element soap = XmlUtils.getFirstElement(request);
Element header = XmlUtils.getElement(soap, SoapConstants.HEADER_QNAME);
Element body = XmlUtils.getElement(soap, SoapConstants.BODY_QNAME);
if (header == null)
throw new RuntimeException("Invalid SOAP envelope: no header element.");
if (body == null)
throw new RuntimeException("Invalid SOAP envelope: no body element.");
Element requestData = XmlUtils.getFirstElement(body);
ResourceRouter router = getRouter();
Environment env = router.getEnvironment();
MessageHeaders addressing = null;
try
{
//
// WS-A info provides resource context for this request
//
addressing = new MessageHeaders(header);
env.addAddressingContext(addressing);
}
catch (Throwable error)
{
LoggingUtils.logError(router.getLog(), error);
}
if (!hasFailedToInitialize())
LoggingUtils.logMessage(router.getLog(), request, true);
//
// actually perform the operation with the given parameters
//
Element result = router.invoke(requestData);
//
// the standard response wsa:Action name is Action + "Response"
//
MessageHeaders replyAddressing = null;
//
// send back fault headers on exception
//
if (SoapUtils.isFault(result))
replyAddressing = addressing.createFaultHeaders();
else
replyAddressing = addressing.createReplyHeaders();
//
// DONE - must be sure to remove the request context, or
// we'll have a memory leak
//
env.removeAddressingContext();
//
// import all of the headers into the response envelope...
//
Element replyXML = replyAddressing.toXML();
Element[] children = XmlUtils.getAllElements(replyXML);
Document response = XmlUtils.createDocument();
soap = XmlUtils.createElement(response, SoapConstants.ENVELOPE_QNAME);
response.appendChild(soap);
header = XmlUtils.createElement(response, SoapConstants.HEADER_QNAME);
soap.appendChild(header);
for (int n = 0; n < children.length; ++n)
{
Node next = response.importNode(children[n], true);
header.appendChild(next);
}
//
// add the result (valid or fault) to the SOAP body...
//
body = XmlUtils.createElement(response, SoapConstants.BODY_QNAME);
soap.appendChild(body);
result = (Element)response.importNode(result, true);
body.appendChild(result);
if (!hasFailedToInitialize())
LoggingUtils.logMessage(router.getLog(), response, false);
return response;
}
}