use of org.jivesoftware.smack.datatypes.UInt16 in project Smack by igniterealtime.
the class MediaElementProvider method parse.
@Override
public MediaElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws IOException, XmlPullParserException, SmackUriSyntaxParsingException {
UInt16 height = ParserUtils.getUInt16Attribute(parser, "height");
UInt16 width = ParserUtils.getUInt16Attribute(parser, "width");
MediaElement.Builder mediaElementBuilder = MediaElement.builder();
if (height != null && width != null) {
mediaElementBuilder.setHeightAndWidth(height, width);
} else if (height != null || width != null) {
LOGGER.warning("Only one of height and width set while parsing media element");
}
outerloop: while (true) {
XmlPullParser.TagEvent event = parser.nextTag();
switch(event) {
case START_ELEMENT:
QName qname = parser.getQName();
if (qname.equals(MediaElement.Uri.QNAME)) {
MediaElement.Uri uri = parseUri(parser);
mediaElementBuilder.addUri(uri);
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
}
}
return mediaElementBuilder.build();
}
use of org.jivesoftware.smack.datatypes.UInt16 in project Smack by igniterealtime.
the class InBandBytestreamSessionTest method shouldSendDataCorrectly.
/**
* Test that the data is correctly chunked.
*
* @throws Exception should not happen
*/
@Test
public void shouldSendDataCorrectly() throws Exception {
// create random data
Random rand = new Random();
final byte[] controlData = new byte[256 * blockSize];
rand.nextBytes(controlData);
// compares the data of each packet with the control data
Verification<Data, IQ> dataVerification = new Verification<Data, IQ>() {
@Override
public void verify(Data request, IQ response) {
byte[] decodedData = request.getDataPacketExtension().getDecodedData();
UInt16 seq = request.getDataPacketExtension().getSeq();
for (int i = 0; i < decodedData.length; i++) {
assertEquals(controlData[(seq.intValue() * blockSize) + i], decodedData[i]);
}
}
};
// set acknowledgments for the data packets
IQ resultIQ = IBBPacketUtils.createResultIQ(initiatorJID, targetJID);
for (int i = 0; i < controlData.length / blockSize; i++) {
protocol.addResponse(resultIQ, incrementingSequence, dataVerification);
}
InBandBytestreamSession session = new InBandBytestreamSession(connection, initBytestream, initiatorJID);
OutputStream outputStream = session.getOutputStream();
outputStream.write(controlData);
outputStream.flush();
protocol.verifyAll();
}
use of org.jivesoftware.smack.datatypes.UInt16 in project Smack by igniterealtime.
the class RemoteXmppTcpConnectionEndpoints method lookup.
/**
* Lookups remote connection endpoints on the server for XMPP connections over TCP taking A, AAAA and SRV resource
* records into account. If no host address was configured and all lookups failed, for example with NX_DOMAIN, then
* result will be populated with the empty list.
*
* @param config the connection configuration to lookup the endpoints for.
* @return a lookup result.
*/
public static Result<Rfc6120TcpRemoteConnectionEndpoint> lookup(ConnectionConfiguration config) {
List<Rfc6120TcpRemoteConnectionEndpoint> discoveredRemoteConnectionEndpoints;
List<RemoteConnectionEndpointLookupFailure> lookupFailures;
final InetAddress hostAddress = config.getHostAddress();
final DnsName host = config.getHost();
if (hostAddress != null) {
lookupFailures = Collections.emptyList();
IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>> connectionEndpoint = IpTcpRemoteConnectionEndpoint.from(hostAddress.toString(), config.getPort(), hostAddress);
discoveredRemoteConnectionEndpoints = Collections.singletonList(connectionEndpoint);
} else if (host != null) {
lookupFailures = new ArrayList<>(1);
List<InetAddress> hostAddresses = DNSUtil.getDNSResolver().lookupHostAddress(host, lookupFailures, config.getDnssecMode());
if (hostAddresses != null) {
discoveredRemoteConnectionEndpoints = new ArrayList<>(hostAddresses.size());
UInt16 port = config.getPort();
for (InetAddress inetAddress : hostAddresses) {
IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>> connectionEndpoint = IpTcpRemoteConnectionEndpoint.from(host, port, inetAddress);
discoveredRemoteConnectionEndpoints.add(connectionEndpoint);
}
} else {
discoveredRemoteConnectionEndpoints = Collections.emptyList();
}
} else {
lookupFailures = new ArrayList<>();
// N.B.: Important to use config.serviceName and not AbstractXMPPConnection.serviceName
DnsName dnsName = config.getXmppServiceDomainAsDnsNameIfPossible();
if (dnsName == null) {
// name is also a valid DNS name, or that a host is explicitly configured.
throw new IllegalStateException();
}
discoveredRemoteConnectionEndpoints = resolveXmppServiceDomain(dnsName, lookupFailures, config.getDnssecMode());
}
// Either the populated host addresses are not empty *or* there must be at least one failed address.
assert !discoveredRemoteConnectionEndpoints.isEmpty() || !lookupFailures.isEmpty();
return new Result<>(discoveredRemoteConnectionEndpoints, lookupFailures);
}
use of org.jivesoftware.smack.datatypes.UInt16 in project Smack by igniterealtime.
the class XMPPTCPConnection method connectUsingConfiguration.
private void connectUsingConfiguration() throws ConnectionException, IOException, InterruptedException {
RemoteXmppTcpConnectionEndpoints.Result<Rfc6120TcpRemoteConnectionEndpoint> result = RemoteXmppTcpConnectionEndpoints.lookup(config);
List<RemoteConnectionException<Rfc6120TcpRemoteConnectionEndpoint>> connectionExceptions = new ArrayList<>();
SocketFactory socketFactory = config.getSocketFactory();
ProxyInfo proxyInfo = config.getProxyInfo();
int timeout = config.getConnectTimeout();
if (socketFactory == null) {
socketFactory = SocketFactory.getDefault();
}
for (Rfc6120TcpRemoteConnectionEndpoint endpoint : result.discoveredRemoteConnectionEndpoints) {
Iterator<? extends InetAddress> inetAddresses;
String host = endpoint.getHost().toString();
UInt16 portUint16 = endpoint.getPort();
int port = portUint16.intValue();
if (proxyInfo == null) {
inetAddresses = endpoint.getInetAddresses().iterator();
assert inetAddresses.hasNext();
innerloop: while (inetAddresses.hasNext()) {
// Create a *new* Socket before every connection attempt, i.e. connect() call, since Sockets are not
// re-usable after a failed connection attempt. See also SMACK-724.
SmackFuture.SocketFuture socketFuture = new SmackFuture.SocketFuture(socketFactory);
final InetAddress inetAddress = inetAddresses.next();
final InetSocketAddress inetSocketAddress = new InetSocketAddress(inetAddress, port);
LOGGER.finer("Trying to establish TCP connection to " + inetSocketAddress);
socketFuture.connectAsync(inetSocketAddress, timeout);
try {
socket = socketFuture.getOrThrow();
} catch (IOException e) {
RemoteConnectionException<Rfc6120TcpRemoteConnectionEndpoint> rce = new RemoteConnectionException<>(endpoint, inetAddress, e);
connectionExceptions.add(rce);
if (inetAddresses.hasNext()) {
continue innerloop;
} else {
break innerloop;
}
}
LOGGER.finer("Established TCP connection to " + inetSocketAddress);
// We found a host to connect to, return here
this.host = host;
this.port = portUint16;
return;
}
} else {
// TODO: Move this into the inner-loop above. There appears no reason why we should not try a proxy
// connection to every inet address of each connection endpoint.
socket = socketFactory.createSocket();
StringUtils.requireNotNullNorEmpty(host, "Host of endpoint " + endpoint + " must not be null when using a Proxy");
final String hostAndPort = host + " at port " + port;
LOGGER.finer("Trying to establish TCP connection via Proxy to " + hostAndPort);
try {
proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout);
} catch (IOException e) {
CloseableUtil.maybeClose(socket, LOGGER);
RemoteConnectionException<Rfc6120TcpRemoteConnectionEndpoint> rce = new RemoteConnectionException<>(endpoint, null, e);
connectionExceptions.add(rce);
continue;
}
LOGGER.finer("Established TCP connection to " + hostAndPort);
// We found a host to connect to, return here
this.host = host;
this.port = portUint16;
return;
}
}
// HostAddresses in the exception
throw EndpointConnectionException.from(result.lookupFailures, connectionExceptions);
}
use of org.jivesoftware.smack.datatypes.UInt16 in project Smack by igniterealtime.
the class RemoteXmppTcpConnectionEndpoints method resolveDomain.
/**
* @param domain the domain.
* @param domainType the XMPP domain type, server or client.
* @param lookupFailures a list that will be populated with all failures that oocured during lookup.
* @param dnssecMode the DNSSEC mode.
* @param dnsResolver the DNS resolver to use.
* @return a list of resolved host addresses for this domain.
*/
private static List<Rfc6120TcpRemoteConnectionEndpoint> resolveDomain(DnsName domain, DomainType domainType, List<RemoteConnectionEndpointLookupFailure> lookupFailures, DnssecMode dnssecMode, DNSResolver dnsResolver) {
List<Rfc6120TcpRemoteConnectionEndpoint> endpoints = new ArrayList<>();
// Step one: Do SRV lookups
DnsName srvDomain = DnsName.from(domainType.srvPrefix, domain);
Collection<SRV> srvRecords = dnsResolver.lookupSrvRecords(srvDomain, lookupFailures, dnssecMode);
if (srvRecords != null && !srvRecords.isEmpty()) {
if (LOGGER.isLoggable(Level.FINE)) {
String logMessage = "Resolved SRV RR for " + srvDomain + ":";
for (SRV r : srvRecords) logMessage += " " + r;
LOGGER.fine(logMessage);
}
List<SRV> sortedSrvRecords = SrvUtil.sortSrvRecords(srvRecords);
for (SRV srv : sortedSrvRecords) {
List<InetAddress> targetInetAddresses = dnsResolver.lookupHostAddress(srv.target, lookupFailures, dnssecMode);
if (targetInetAddresses != null) {
SrvXmppRemoteConnectionEndpoint endpoint = new SrvXmppRemoteConnectionEndpoint(srv, targetInetAddresses);
endpoints.add(endpoint);
}
}
} else {
LOGGER.info("Could not resolve DNS SRV resource records for " + srvDomain + ". Consider adding those.");
}
UInt16 defaultPort;
switch(domainType) {
case client:
defaultPort = UInt16.from(5222);
break;
case server:
defaultPort = UInt16.from(5269);
break;
default:
throw new AssertionError();
}
// Step two: Add the hostname to the end of the list
List<InetAddress> hostAddresses = dnsResolver.lookupHostAddress(domain, lookupFailures, dnssecMode);
if (hostAddresses != null) {
for (InetAddress inetAddress : hostAddresses) {
IpTcpRemoteConnectionEndpoint<InternetAddressRR<?>> endpoint = IpTcpRemoteConnectionEndpoint.from(domain, defaultPort, inetAddress);
endpoints.add(endpoint);
}
}
return endpoints;
}
Aggregations