FileDocCategorySizeDatePackage
MEXEndpoint.javaAPI DocExample8645Wed Jul 11 22:23:52 BST 2007com.sun.xml.ws.mex.server

MEXEndpoint

public class MEXEndpoint extends Object implements Provider

Fields Summary
protected WebServiceContext
wsContext
private static final Logger
logger
Constructors Summary
Methods Summary
public com.sun.xml.ws.api.message.Messageinvoke(com.sun.xml.ws.api.message.Message requestMsg)


        
        if (requestMsg == null || !requestMsg.hasHeaders()) {
            // TODO: Better error message
            throw new WebServiceException("Malformed MEX Request");
        }

        WSEndpoint wsEndpoint = (WSEndpoint) wsContext.getMessageContext().get(JAXWSProperties.WSENDPOINT);
        SOAPVersion soapVersion = wsEndpoint.getBinding().getSOAPVersion();

        // try w3c version of ws-a first, then member submission version
        final HeaderList headers = requestMsg.getHeaders();

        String action = headers.getAction(AddressingVersion.W3C, soapVersion);
        AddressingVersion wsaVersion = AddressingVersion.W3C;
        if (action == null) {
            action = headers.getAction(AddressingVersion.MEMBER, soapVersion);
            wsaVersion = AddressingVersion.MEMBER;
        }

        if (action == null) {
            // TODO: Better error message
            throw new WebServiceException("No wsa:Action specified");
        }
        else if (action.equals(GET_REQUEST)) {
            final String toAddress = headers.getTo(wsaVersion, soapVersion);
            return processGetRequest(requestMsg, toAddress, wsaVersion, soapVersion);
        }
        else if (action.equals(GET_MDATA_REQUEST)) {
            final Message faultMessage = Messages.create(GET_MDATA_REQUEST,
                wsaVersion, soapVersion);
            wsContext.getMessageContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, wsaVersion.getDefaultFaultAction());
            return faultMessage;
        }
        // If here, either action is unsupported 
        // TODO: Better error message
        throw new UnsupportedOperationException(action);
    
private com.sun.xml.ws.api.message.MessageprocessGetRequest(com.sun.xml.ws.api.message.Message request, java.lang.String address, com.sun.xml.ws.api.addressing.AddressingVersion wsaVersion, com.sun.xml.ws.api.SOAPVersion soapVersion)

        
        try {
            final MutableXMLStreamBuffer buffer = new MutableXMLStreamBuffer();
            final XMLStreamWriter writer = buffer.createFromXMLStreamWriter();

            WSEndpoint wsEndpoint = (WSEndpoint) wsContext.getMessageContext().get(JAXWSProperties.WSENDPOINT);
            HttpServletRequest servletRequest = (HttpServletRequest)wsContext.getMessageContext().get(MessageContext.SERVLET_REQUEST);
            if (servletRequest == null) {
                // TODO: better error message
                throw new WebServiceException("MEX: no ServletRequest can be found");
            }
            
            // Derive the address of the owner endpoint.
            // e.g. http://localhost/foo/mex --> http://localhost/foo
            WSEndpoint ownerEndpoint = null;
            ServletModule module = (ServletModule) wsEndpoint.getContainer().getSPI(ServletModule.class);
            String baseAddress = module.getContextPath(servletRequest);
            String ownerEndpointAddress = null;
            List<BoundEndpoint> boundEndpoints = module.getBoundEndpoints();
            for (BoundEndpoint endpoint : boundEndpoints) {
                if (wsEndpoint == endpoint.getEndpoint()) {
                    ownerEndpointAddress = endpoint.getAddress(baseAddress).toString();                
                    break;
                }
            }
            ownerEndpointAddress = ownerEndpointAddress.substring(0,ownerEndpointAddress.length() - "/mex".length());

            boundEndpoints = module.getBoundEndpoints();
            for (BoundEndpoint endpoint : boundEndpoints) {
                //compare ownerEndpointAddress with this endpoints address
                //   if matches, set ownerEndpoint to the corresponding WSEndpoint
                String endpointAddress = endpoint.getAddress(baseAddress).toString();
                if (endpointAddress.equals(ownerEndpointAddress)) {
                    ownerEndpoint = endpoint.getEndpoint();
                    break;
                }
            }
            

            // If the owner endpoint has been found, then
            // get its metadata and write it to the response message
            if (ownerEndpoint != null) {
                address = address.substring(0 , address.length() - 4);
                writeStartEnvelope(writer, wsaVersion, soapVersion);
                WSDLRetriever wsdlRetriever = new WSDLRetriever(ownerEndpoint);
                wsdlRetriever.addDocuments(writer, null, address);
                writer.writeEndDocument();
                writer.flush();
                final Message responseMessage = Messages.create(buffer);
                wsContext.getMessageContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, GET_RESPONSE);
                return responseMessage;
            }

            // If we get here there was no metadata for the owner endpoint
            // TODO: This should probably be something other than unsupported action for clarity
            final Message faultMessage = Messages.create(GET_REQUEST,
                wsaVersion, soapVersion);
            wsContext.getMessageContext().put(BindingProvider.SOAPACTION_URI_PROPERTY, wsaVersion.getDefaultFaultAction());
            return faultMessage;
        } catch (XMLStreamException streamE) {
            final String exceptionMessage = 
               MessagesMessages.MEX_0001_RESPONSE_WRITING_FAILURE(address);
            logger.log(Level.SEVERE, exceptionMessage, streamE);
            throw new WebServiceException(exceptionMessage, streamE);
        }
    
private voidwriteStartEnvelope(javax.xml.stream.XMLStreamWriter writer, com.sun.xml.ws.api.addressing.AddressingVersion wsaVersion, com.sun.xml.ws.api.SOAPVersion soapVersion)


        final String soapPrefix = "soapenv";

        writer.writeStartDocument();
        writer.writeStartElement(soapPrefix, "Envelope", soapVersion.nsUri);

        // todo: this line should go away after bug fix - 6418039
        writer.writeNamespace(soapPrefix, soapVersion.nsUri);

        writer.writeNamespace(MetadataConstants.WSA_PREFIX, wsaVersion.nsUri);
        writer.writeNamespace(MetadataConstants.MEX_PREFIX, MetadataConstants.MEX_NAMESPACE);

        writer.writeStartElement(soapPrefix, "Body", soapVersion.nsUri);
        writer.writeStartElement(MetadataConstants.MEX_PREFIX, "Metadata", MetadataConstants.MEX_NAMESPACE);