use of org.apache.cxf.ws.discovery.wsdl.ProbeMatchesType in project cxf by apache.
the class WSDiscoveryClient method probe.
public ProbeMatchesType probe(ProbeType params, int timeout) {
Dispatch<Object> disp = this.getDispatchInternal(false, version.getProbeAction());
if (adHoc) {
disp.getRequestContext().put("udp.multi.response.timeout", timeout);
final ProbeMatchesType response = new ProbeMatchesType();
AsyncHandler<Object> handler = new AsyncHandler<Object>() {
public void handleResponse(Response<Object> res) {
try {
Object o = res.get();
while (o instanceof JAXBElement) {
o = ((JAXBElement) o).getValue();
}
if (o instanceof ProbeMatchesType) {
response.getProbeMatch().addAll(((ProbeMatchesType) o).getProbeMatch());
} else if (o instanceof HelloType) {
HelloType h = (HelloType) o;
QName sn = version.getServiceName();
if (h.getTypes().contains(sn) || h.getTypes().contains(new QName("", sn.getLocalPart()))) {
// A DiscoveryProxy wants us to flip to managed mode
uncache();
resetDispatch(h.getXAddrs().get(0));
}
}
} catch (InterruptedException e) {
// ?
} catch (ExecutionException e) {
// ?
}
}
};
disp.invokeAsync(new ObjectFactory().createProbe(params), handler);
return response;
}
Object o = disp.invoke(new ObjectFactory().createProbe(params));
while (o instanceof JAXBElement) {
o = ((JAXBElement) o).getValue();
}
return (ProbeMatchesType) o;
}
use of org.apache.cxf.ws.discovery.wsdl.ProbeMatchesType in project cxf by apache.
the class WSDiscoveryClient method probe.
public List<EndpointReference> probe(QName type) {
ProbeType p = new ProbeType();
if (type != null) {
p.getTypes().add(type);
}
ProbeMatchesType pmt = probe(p, defaultProbeTimeout);
List<EndpointReference> er = new ArrayList<>();
for (ProbeMatchType pm : pmt.getProbeMatch()) {
for (String add : pm.getXAddrs()) {
W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
builder.address(add);
// builder.serviceName(type);
// builder.endpointName(type);
er.add(builder.build());
}
}
return er;
}
use of org.apache.cxf.ws.discovery.wsdl.ProbeMatchesType in project cxf by apache.
the class WSDiscoveryServiceImpl method handleProbe.
public ProbeMatchesType handleProbe(ProbeType pt) {
List<HelloType> consider = new LinkedList<HelloType>(registered);
// ALL types in the probe must be in the registered type
if (pt.getTypes() != null && !pt.getTypes().isEmpty()) {
ListIterator<HelloType> cit = consider.listIterator();
while (cit.hasNext()) {
HelloType ht = cit.next();
boolean matches = true;
for (QName qn : pt.getTypes()) {
if (!ht.getTypes().contains(qn)) {
matches = false;
}
}
if (!matches) {
cit.remove();
}
}
}
// next, consider the scopes
matchScopes(pt, consider);
if (consider.isEmpty()) {
return null;
}
ProbeMatchesType pmt = new ProbeMatchesType();
for (HelloType ht : consider) {
ProbeMatchType m = new ProbeMatchType();
m.setEndpointReference(ht.getEndpointReference());
m.setScopes(ht.getScopes());
m.setMetadataVersion(ht.getMetadataVersion());
m.getTypes().addAll(ht.getTypes());
m.getXAddrs().addAll(ht.getXAddrs());
pmt.getProbeMatch().add(m);
}
return pmt;
}
use of org.apache.cxf.ws.discovery.wsdl.ProbeMatchesType in project cxf by apache.
the class WSDiscoveryClientTest method testMultiResponses.
@Test
public void testMultiResponses() throws Exception {
// Disable the test on Redhat Enterprise Linux which doesn't enable the UDP broadcast by default
if (System.getProperties().getProperty("os.name").equals("Linux") && System.getProperties().getProperty("os.version").indexOf("el") > 0) {
System.out.println("Skipping MultiResponse test for REL");
return;
}
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
int count = 0;
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();
if (!networkInterface.isUp() || networkInterface.isLoopback()) {
continue;
}
count++;
}
if (count == 0) {
// no non-loopbacks, cannot do broadcasts
System.out.println("Skipping MultiResponse test");
return;
}
new Thread(new Runnable() {
public void run() {
try {
// fake a discovery server to send back some canned messages.
InetAddress address = InetAddress.getByName("239.255.255.250");
MulticastSocket s = new MulticastSocket(Integer.parseInt(PORT));
s.setBroadcast(true);
s.setNetworkInterface(findIpv4Interface());
s.setLoopbackMode(false);
s.setReuseAddress(true);
s.joinGroup(address);
s.setReceiveBufferSize(64 * 1024);
s.setSoTimeout(5000);
byte[] bytes = new byte[64 * 1024];
DatagramPacket p = new DatagramPacket(bytes, bytes.length, address, Integer.parseInt(PORT));
s.receive(p);
SocketAddress sa = p.getSocketAddress();
String incoming = new String(p.getData(), 0, p.getLength(), StandardCharsets.UTF_8);
int idx = incoming.indexOf("MessageID");
idx = incoming.indexOf('>', idx);
incoming = incoming.substring(idx + 1);
idx = incoming.indexOf("</");
incoming = incoming.substring(0, idx);
for (int x = 1; x < 4; x++) {
InputStream ins = WSDiscoveryClientTest.class.getResourceAsStream("msg" + x + ".xml");
String msg = IOUtils.readStringFromStream(ins);
msg = msg.replace("urn:uuid:883d0d53-92aa-4066-9b6f-9eadb1832366", incoming);
byte[] out = msg.getBytes(StandardCharsets.UTF_8);
DatagramPacket outp = new DatagramPacket(out, 0, out.length, sa);
s.send(outp);
}
s.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
}).start();
Bus bus = BusFactory.newInstance().createBus();
WSDiscoveryClient c = new WSDiscoveryClient(bus);
c.setVersion10();
c.setAddress("soap.udp://239.255.255.250:" + PORT);
ProbeType pt = new ProbeType();
ScopesType scopes = new ScopesType();
pt.setScopes(scopes);
ProbeMatchesType pmts = c.probe(pt, 1000);
Assert.assertEquals(2, pmts.getProbeMatch().size());
c.close();
bus.shutdown(true);
}
use of org.apache.cxf.ws.discovery.wsdl.ProbeMatchesType in project cxf by apache.
the class WSDiscoveryClientTest method main.
// this is a standalone test
public static void main(String[] arg) throws Exception {
try {
Bus bus = BusFactory.getDefaultBus();
Endpoint ep = Endpoint.publish("http://localhost:51919/Foo/Snarf", new FooImpl());
WSDiscoveryServiceImpl service = new WSDiscoveryServiceImpl(bus);
service.startup();
// this service will just generate an error. However, the probes should still
// work to probe the above stuff.
WSDiscoveryServiceImpl s2 = new WSDiscoveryServiceImpl() {
public ProbeMatchesType handleProbe(ProbeType pt) {
throw new RuntimeException("Error!!!");
}
};
s2.startup();
HelloType h = service.register(ep.getEndpointReference());
bus = BusFactory.newInstance().createBus();
WSDiscoveryClient c = new WSDiscoveryClient(bus);
c.setVersion10();
System.out.println("1");
ProbeType pt = new ProbeType();
ScopesType scopes = new ScopesType();
pt.setScopes(scopes);
ProbeMatchesType pmts = c.probe(pt, 1000);
System.out.println("2");
if (pmts != null) {
for (ProbeMatchType pmt : pmts.getProbeMatch()) {
System.out.println("Found " + pmt.getEndpointReference());
System.out.println(pmt.getTypes());
System.out.println(pmt.getXAddrs());
}
}
if (pmts.getProbeMatch().isEmpty()) {
System.exit(0);
}
pmts = c.probe(pt);
System.out.println("Size:" + pmts.getProbeMatch().size());
System.out.println("3");
W3CEndpointReference ref = null;
if (pmts != null) {
for (ProbeMatchType pmt : pmts.getProbeMatch()) {
ref = pmt.getEndpointReference();
System.out.println("Found " + pmt.getEndpointReference());
System.out.println(pmt.getTypes());
System.out.println(pmt.getXAddrs());
}
}
ResolveMatchType rmt = c.resolve(ref);
System.out.println("Resolved " + rmt.getEndpointReference());
System.out.println(rmt.getTypes());
System.out.println(rmt.getXAddrs());
service.unregister(h);
System.out.println("4");
c.close();
System.exit(0);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
Aggregations