Search in sources :

Example 21 with W3CEndpointReference

use of javax.xml.ws.wsaddressing.W3CEndpointReference in project cxf by apache.

the class MultiplexHttpAddressClientServerTest method testWithGetPortExtensionHttp.

@Test
public void testWithGetPortExtensionHttp() throws Exception {
    NumberFactoryService service = new NumberFactoryService();
    NumberFactory factory = service.getNumberFactoryPort();
    updateAddressPort(factory, PORT);
    NumberService numService = new NumberService();
    ServiceImpl serviceImpl = ServiceDelegateAccessor.get(numService);
    W3CEndpointReference numberTwoRef = factory.create("20");
    assertNotNull("reference", numberTwoRef);
    Number num = serviceImpl.getPort(numberTwoRef, Number.class);
    assertTrue("20 is even", num.isEven().isEven());
    W3CEndpointReference numberTwentyThreeRef = factory.create("23");
    num = serviceImpl.getPort(numberTwentyThreeRef, Number.class);
    assertTrue("23 is not even", !num.isEven().isEven());
}
Also used : NumberService(org.apache.cxf.factory_pattern.NumberService) NumberFactory(org.apache.cxf.factory_pattern.NumberFactory) Number(org.apache.cxf.factory_pattern.Number) ServiceImpl(org.apache.cxf.jaxws.ServiceImpl) W3CEndpointReference(javax.xml.ws.wsaddressing.W3CEndpointReference) NumberFactoryService(org.apache.cxf.factory_pattern.NumberFactoryService) Test(org.junit.Test)

Example 22 with W3CEndpointReference

use of javax.xml.ws.wsaddressing.W3CEndpointReference in project cxf by apache.

the class CallbackClientServerTest method testCallback.

@Test
public void testCallback() throws Exception {
    Object implementor = new CallbackImpl();
    String address = "http://localhost:" + CB_PORT + "/CallbackContext/NestedCallbackPort";
    Endpoint.publish(address, implementor);
    URL wsdlURL = getClass().getResource("/wsdl/nested_callback.wsdl");
    SOAPService ss = new SOAPService(wsdlURL, SERVICE_NAME);
    ServerPortType port = ss.getPort(PORT_NAME, ServerPortType.class);
    updateAddressPort(port, PORT);
    EndpointReferenceType ref = null;
    try {
        ref = EndpointReferenceUtils.getEndpointReference(wsdlURL, SERVICE_NAME_CALLBACK, PORT_NAME_CALLBACK.getLocalPart());
        EndpointReferenceUtils.setInterfaceName(ref, PORT_TYPE_CALLBACK);
        EndpointReferenceUtils.setAddress(ref, address);
    } catch (Exception e) {
        e.printStackTrace();
    }
    NestedCallback callbackObject = new NestedCallback();
    Source source = EndpointReferenceUtils.convertToXML(ref);
    W3CEndpointReference w3cEpr = new W3CEndpointReference(source);
    callbackObject.setCallback(w3cEpr);
    String resp = port.registerCallback(callbackObject);
    assertEquals("registerCallback called", resp);
}
Also used : SOAPService(org.apache.nested_callback.SOAPService) NestedCallback(org.apache.nested_callback.NestedCallback) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) W3CEndpointReference(javax.xml.ws.wsaddressing.W3CEndpointReference) ServerPortType(org.apache.nested_callback.ServerPortType) URL(java.net.URL) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 23 with W3CEndpointReference

use of javax.xml.ws.wsaddressing.W3CEndpointReference in project cxf by apache.

the class ServerImpl method registerCallback.

public String registerCallback(NestedCallback callbackObject) {
    try {
        W3CEndpointReference w3cEpr = callbackObject.getCallback();
        EndpointReferenceType callback = ProviderImpl.convertToInternal(w3cEpr);
        WSDLManager manager = new WSDLManagerImpl();
        QName interfaceName = EndpointReferenceUtils.getInterfaceName(callback, bus);
        String wsdlLocation = EndpointReferenceUtils.getWSDLLocation(callback);
        QName serviceName = EndpointReferenceUtils.getServiceName(callback, bus);
        String address = EndpointReferenceUtils.getAddress(callback);
        String portString = EndpointReferenceUtils.getPortName(callback);
        QName portName = new QName(serviceName.getNamespaceURI(), portString);
        StringBuilder seiName = new StringBuilder();
        seiName.append(JAXBUtils.namespaceURIToPackage(interfaceName.getNamespaceURI()));
        seiName.append(".");
        seiName.append(JAXBUtils.nameToIdentifier(interfaceName.getLocalPart(), JAXBUtils.IdentifierType.INTERFACE));
        Class<?> sei = null;
        try {
            sei = Class.forName(seiName.toString(), true, manager.getClass().getClassLoader());
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        URL wsdlURL = new URL(wsdlLocation);
        Service service = Service.create(wsdlURL, serviceName);
        CallbackPortType port = (CallbackPortType) service.getPort(portName, sei);
        ((BindingProvider) port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, address);
        port.serverSayHi("Sean");
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
    return "registerCallback called";
}
Also used : EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) QName(javax.xml.namespace.QName) WSDLManagerImpl(org.apache.cxf.wsdl11.WSDLManagerImpl) Service(javax.xml.ws.Service) URL(java.net.URL) W3CEndpointReference(javax.xml.ws.wsaddressing.W3CEndpointReference) CallbackPortType(org.apache.nested_callback.CallbackPortType) WSDLManager(org.apache.cxf.wsdl.WSDLManager)

Example 24 with W3CEndpointReference

use of javax.xml.ws.wsaddressing.W3CEndpointReference in project cxf by apache.

the class ProviderImpl method convertToInternal.

/**
 * Convert from EndpointReference to CXF internal 2005/08 EndpointReferenceType
 *
 * @param external the javax.xml.ws.EndpointReference
 * @return CXF internal 2005/08 EndpointReferenceType
 */
public static EndpointReferenceType convertToInternal(EndpointReference external) {
    if (external instanceof W3CEndpointReference) {
        Unmarshaller um = null;
        try {
            DocumentFragment frag = DOMUtils.getEmptyDocument().createDocumentFragment();
            DOMResult result = new DOMResult(frag);
            external.writeTo(result);
            W3CDOMStreamReader reader = new W3CDOMStreamReader(frag);
            // CXF internal 2005/08 EndpointReferenceType should be
            // compatible with W3CEndpointReference
            // jaxContext = ContextUtils.getJAXBContext();
            JAXBContext context = JAXBContext.newInstance(new Class[] { org.apache.cxf.ws.addressing.ObjectFactory.class });
            um = context.createUnmarshaller();
            return um.unmarshal(reader, EndpointReferenceType.class).getValue();
        } catch (JAXBException e) {
            throw new IllegalArgumentException("Could not unmarshal EndpointReference", e);
        } finally {
            JAXBUtils.closeUnmarshaller(um);
        }
    }
    return null;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) EndpointReferenceType(org.apache.cxf.ws.addressing.EndpointReferenceType) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) W3CEndpointReference(javax.xml.ws.wsaddressing.W3CEndpointReference) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 25 with W3CEndpointReference

use of javax.xml.ws.wsaddressing.W3CEndpointReference in project cxf by apache.

the class ProviderImpl method createW3CEndpointReference.

// CHECKSTYLE:OFF - spec requires a bunch of params
public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters, List<Element> elements, Map<QName, String> attributes) {
    // CHECKSTYLE:ON
    if (serviceName != null && portName != null && wsdlDocumentLocation != null && interfaceName == null) {
        Bus bus = BusFactory.getThreadDefaultBus();
        WSDLManager wsdlManager = bus.getExtension(WSDLManager.class);
        try {
            Definition def = wsdlManager.getDefinition(wsdlDocumentLocation);
            interfaceName = def.getService(serviceName).getPort(portName.getLocalPart()).getBinding().getPortType().getQName();
        } catch (Exception e) {
        // do nothing
        }
    }
    if (serviceName == null && portName == null && address == null) {
        throw new IllegalStateException("Address in an EPR cannot be null, " + " when serviceName or portName is null");
    }
    try {
        final W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
        writer.setPrefix(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.NS_WSA);
        writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_ERF_NAME, JAXWSAConstants.NS_WSA);
        writer.writeNamespace(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.NS_WSA);
        writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_ADDRESS_NAME, JAXWSAConstants.NS_WSA);
        address = address == null ? "" : address;
        writer.writeCharacters(address);
        writer.writeEndElement();
        if (referenceParameters != null) {
            writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_REFERENCEPARAMETERS_NAME, JAXWSAConstants.NS_WSA);
            for (Element ele : referenceParameters) {
                StaxUtils.writeElement(ele, writer, true);
            }
            writer.writeEndElement();
        }
        if (wsdlDocumentLocation != null || interfaceName != null || serviceName != null || (metadata != null && !metadata.isEmpty())) {
            writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_METADATA_NAME, JAXWSAConstants.NS_WSA);
            writer.writeNamespace(JAXWSAConstants.WSAW_PREFIX, JAXWSAConstants.NS_WSAW);
            writer.writeNamespace(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.NS_WSAM);
            if (wsdlDocumentLocation != null) {
                boolean includeLocationOnly = false;
                org.apache.cxf.message.Message message = PhaseInterceptorChain.getCurrentMessage();
                if (message != null) {
                    includeLocationOnly = MessageUtils.getContextualBoolean(message, "org.apache.cxf.wsa.metadata.wsdlLocationOnly", false);
                }
                String attrubuteValue = serviceName != null && !includeLocationOnly ? serviceName.getNamespaceURI() + " " + wsdlDocumentLocation : wsdlDocumentLocation;
                writer.writeNamespace(JAXWSAConstants.WSDLI_PFX, JAXWSAConstants.NS_WSDLI);
                writer.writeAttribute(JAXWSAConstants.WSDLI_PFX, JAXWSAConstants.NS_WSDLI, JAXWSAConstants.WSDLI_WSDLLOCATION, attrubuteValue);
            }
            if (interfaceName != null) {
                writer.writeStartElement(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.WSAM_INTERFACE_NAME, JAXWSAConstants.NS_WSAM);
                String portTypePrefix = interfaceName.getPrefix();
                if (portTypePrefix == null || portTypePrefix.equals("")) {
                    portTypePrefix = "ns1";
                }
                writer.writeNamespace(portTypePrefix, interfaceName.getNamespaceURI());
                writer.writeCharacters(portTypePrefix + ":" + interfaceName.getLocalPart());
                writer.writeEndElement();
            }
            String serviceNamePrefix = null;
            if (serviceName != null) {
                serviceNamePrefix = (serviceName.getPrefix() == null || serviceName.getPrefix().length() == 0) ? "ns2" : serviceName.getPrefix();
                writer.writeStartElement(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.WSAM_SERVICENAME_NAME, JAXWSAConstants.NS_WSAM);
                if (portName != null) {
                    writer.writeAttribute(JAXWSAConstants.WSAM_ENDPOINT_NAME, portName.getLocalPart());
                }
                writer.writeNamespace(serviceNamePrefix, serviceName.getNamespaceURI());
                writer.writeCharacters(serviceNamePrefix + ":" + serviceName.getLocalPart());
                writer.writeEndElement();
            }
            if (wsdlDocumentLocation != null) {
                writer.writeStartElement(WSDLConstants.WSDL_PREFIX, WSDLConstants.QNAME_DEFINITIONS.getLocalPart(), WSDLConstants.NS_WSDL11);
                writer.writeNamespace(WSDLConstants.WSDL_PREFIX, WSDLConstants.NS_WSDL11);
                writer.writeStartElement(WSDLConstants.WSDL_PREFIX, WSDLConstants.QNAME_IMPORT.getLocalPart(), WSDLConstants.QNAME_IMPORT.getNamespaceURI());
                if (serviceName != null) {
                    writer.writeAttribute(WSDLConstants.ATTR_NAMESPACE, serviceName.getNamespaceURI());
                }
                writer.writeAttribute(WSDLConstants.ATTR_LOCATION, wsdlDocumentLocation);
                writer.writeEndElement();
                writer.writeEndElement();
            }
            if (metadata != null) {
                for (Element e : metadata) {
                    StaxUtils.writeElement(e, writer, true);
                }
            }
            writer.writeEndElement();
        }
        if (elements != null) {
            for (Element e : elements) {
                StaxUtils.writeElement(e, writer, true);
            }
        }
        writer.writeEndElement();
        writer.flush();
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<W3CEndpointReference>() {

                public W3CEndpointReference run() throws Exception {
                    Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
                    try {
                        return (W3CEndpointReference) unmarshaller.unmarshal(writer.getDocument());
                    } finally {
                        JAXBUtils.closeUnmarshaller(unmarshaller);
                    }
                }
            });
        } catch (PrivilegedActionException pae) {
            Exception e = pae.getException();
            if (e instanceof JAXBException) {
                throw (JAXBException) e;
            }
            throw new SecurityException(e);
        }
    } catch (Exception e) {
        throw new WebServiceException(new Message("ERROR_UNMARSHAL_ENDPOINTREFERENCE", LOG).toString(), e);
    }
}
Also used : Bus(org.apache.cxf.Bus) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) WebServiceException(javax.xml.ws.WebServiceException) Message(org.apache.cxf.common.i18n.Message) PrivilegedActionException(java.security.PrivilegedActionException) Element(org.w3c.dom.Element) JAXBException(javax.xml.bind.JAXBException) Definition(javax.wsdl.Definition) XMLStreamException(javax.xml.stream.XMLStreamException) PrivilegedActionException(java.security.PrivilegedActionException) JAXBException(javax.xml.bind.JAXBException) WebServiceException(javax.xml.ws.WebServiceException) W3CEndpointReference(javax.xml.ws.wsaddressing.W3CEndpointReference) WSDLManager(org.apache.cxf.wsdl.WSDLManager) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

W3CEndpointReference (javax.xml.ws.wsaddressing.W3CEndpointReference)37 Test (org.junit.Test)17 QName (javax.xml.namespace.QName)9 EndpointReferenceType (org.apache.cxf.ws.addressing.EndpointReferenceType)9 URL (java.net.URL)8 InputStream (java.io.InputStream)6 StreamResult (javax.xml.transform.stream.StreamResult)6 EndpointReference (javax.xml.ws.EndpointReference)6 Element (org.w3c.dom.Element)6 Number (org.apache.cxf.factory_pattern.Number)5 NumberFactory (org.apache.cxf.factory_pattern.NumberFactory)5 NumberFactoryService (org.apache.cxf.factory_pattern.NumberFactoryService)5 NumberService (org.apache.cxf.factory_pattern.NumberService)5 Document (org.w3c.dom.Document)5 Source (javax.xml.transform.Source)4 Endpoint (javax.xml.ws.Endpoint)4 ServiceImpl (org.apache.cxf.jaxws.ServiceImpl)4 ArrayList (java.util.ArrayList)2 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBException (javax.xml.bind.JAXBException)2