1 17 package org.apache.servicemix.wsn.client; 18 19 import javax.jbi.JBIException; 20 import javax.jbi.component.ComponentContext; 21 import javax.xml.bind.JAXBContext; 22 import javax.xml.bind.JAXBException; 23 import javax.xml.namespace.QName ; 24 25 import org.apache.servicemix.client.DefaultServiceMixClient; 26 import org.apache.servicemix.client.ServiceMixClient; 27 import org.apache.servicemix.client.ServiceMixClientFacade; 28 import org.apache.servicemix.jbi.container.JBIContainer; 29 import org.apache.servicemix.jbi.resolver.EndpointResolver; 30 import org.apache.servicemix.jbi.resolver.ServiceAndEndpointNameResolver; 31 import org.oasis_open.docs.wsn.b_2.Subscribe; 32 import org.oasis_open.docs.wsn.br_2.RegisterPublisher; 33 import org.w3._2005._08.addressing.AttributedURIType; 34 import org.w3._2005._08.addressing.EndpointReferenceType; 35 36 public abstract class AbstractWSAClient { 37 38 private EndpointReferenceType endpoint; 39 private EndpointResolver resolver; 40 private ServiceMixClient client; 41 42 public AbstractWSAClient() { 43 } 44 45 public AbstractWSAClient(EndpointReferenceType endpoint, ServiceMixClient client) { 46 this.endpoint = endpoint; 47 this.resolver = resolveWSA(endpoint); 48 this.client = client; 49 } 50 51 public static EndpointReferenceType createWSA(String address) { 52 EndpointReferenceType epr = new EndpointReferenceType(); 53 AttributedURIType attUri = new AttributedURIType(); 54 attUri.setValue(address); 55 epr.setAddress(attUri); 56 return epr; 57 } 58 59 public static ServiceMixClient createJaxbClient(JBIContainer container) throws JBIException, JAXBException { 60 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 61 client.setMarshaler(new JAXBMarshaler(JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class))); 62 return client; 63 } 64 65 public static ServiceMixClient createJaxbClient(ComponentContext context) throws JAXBException { 66 ServiceMixClientFacade client = new ServiceMixClientFacade(context); 67 client.setMarshaler(new JAXBMarshaler(JAXBContext.newInstance(Subscribe.class, RegisterPublisher.class))); 68 return client; 69 } 70 71 public static EndpointResolver resolveWSA(EndpointReferenceType ref) { 72 String [] parts = splitUri(ref.getAddress().getValue()); 73 return new ServiceAndEndpointNameResolver(new QName (parts[0], parts[1]), parts[2]); 74 } 75 76 public static String [] splitUri(String uri) { 77 char sep; 78 if (uri.indexOf('/') > 0) { 79 sep = '/'; 80 } else { 81 sep = ':'; 82 } 83 int idx1 = uri.lastIndexOf(sep); 84 int idx2 = uri.lastIndexOf(sep, idx1 - 1); 85 String epName = uri.substring(idx1 + 1); 86 String svcName = uri.substring(idx2 + 1, idx1); 87 String nsUri = uri.substring(0, idx2); 88 return new String [] { nsUri, svcName, epName }; 89 } 90 91 public EndpointReferenceType getEndpoint() { 92 return endpoint; 93 } 94 95 public void setEndpoint(EndpointReferenceType endpoint) { 96 this.endpoint = endpoint; 97 } 98 99 public EndpointResolver getResolver() { 100 return resolver; 101 } 102 103 public void setResolver(EndpointResolver resolver) { 104 this.resolver = resolver; 105 } 106 107 public ServiceMixClient getClient() { 108 return client; 109 } 110 111 public void setClient(ServiceMixClient client) { 112 this.client = client; 113 } 114 115 protected Object request(Object request) throws JBIException { 116 return client.request(resolver, null, null, request); 117 } 118 119 protected void send(Object request) throws JBIException { 120 client.sendSync(resolver, null, null, request); 121 } 122 123 } 124 | Popular Tags |