Search in sources :

Example 36 with SmackException

use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.

the class IoTRemoveProvider method parse.

@Override
public IoTRemove parse(XmlPullParser parser, int initialDepth) throws Exception {
    Jid jid = ParserUtils.getJidAttribute(parser);
    if (jid.hasResource()) {
        throw new SmackException("JID must be without resourcepart");
    }
    BareJid bareJid = jid.asBareJid();
    NodeInfo nodeInfo = NodeInfoParser.parse(parser);
    return new IoTRemove(bareJid, nodeInfo);
}
Also used : IoTRemove(org.jivesoftware.smackx.iot.discovery.element.IoTRemove) Jid(org.jxmpp.jid.Jid) BareJid(org.jxmpp.jid.BareJid) BareJid(org.jxmpp.jid.BareJid) NodeInfo(org.jivesoftware.smackx.iot.element.NodeInfo) SmackException(org.jivesoftware.smack.SmackException)

Example 37 with SmackException

use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.

the class AbstractDelayInformationProvider method parse.

@Override
public final DelayInformation parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException {
    String stampString = (parser.getAttributeValue("", "stamp"));
    String from = parser.getAttributeValue("", "from");
    String reason = null;
    if (!parser.isEmptyElementTag()) {
        int event = parser.next();
        switch(event) {
            case XmlPullParser.TEXT:
                reason = parser.getText();
                parser.next();
                break;
            case XmlPullParser.END_TAG:
                reason = "";
                break;
            default:
                throw new IllegalStateException("Unexpected event: " + event);
        }
    } else {
        parser.next();
    }
    Date stamp;
    try {
        stamp = parseDate(stampString);
    } catch (ParseException e) {
        throw new SmackException(e);
    }
    return new DelayInformation(stamp, from, reason);
}
Also used : DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) SmackException(org.jivesoftware.smack.SmackException) ParseException(java.text.ParseException) Date(java.util.Date)

Example 38 with SmackException

use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.

the class OutgoingFileTransfer method sendFile.

/**
     * This method handles the stream negotiation process and transmits the file
     * to the remote user. It returns immediately and the progress of the file
     * transfer can be monitored through several methods:
     *
     * <UL>
     * <LI>{@link FileTransfer#getStatus()}
     * <LI>{@link FileTransfer#getProgress()}
     * <LI>{@link FileTransfer#isDone()}
     * </UL>
     *
     * @param file the file to transfer to the remote entity.
     * @param description a description for the file to transfer.
     * @throws SmackException
     *             If there is an error during the negotiation process or the
     *             sending of the file.
     */
public synchronized void sendFile(final File file, final String description) throws SmackException {
    checkTransferThread();
    if (file == null || !file.exists() || !file.canRead()) {
        throw new IllegalArgumentException("Could not read file");
    } else {
        setFileInfo(file.getAbsolutePath(), file.getName(), file.length());
    }
    transferThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                outputStream = negotiateStream(file.getName(), file.length(), description);
            } catch (XMPPErrorException e) {
                handleXMPPException(e);
                return;
            } catch (Exception e) {
                setException(e);
            }
            if (outputStream == null) {
                return;
            }
            if (!updateStatus(Status.negotiated, Status.in_progress)) {
                return;
            }
            InputStream inputStream = null;
            try {
                inputStream = new FileInputStream(file);
                writeToStream(inputStream, outputStream);
            } catch (FileNotFoundException e) {
                setStatus(FileTransfer.Status.error);
                setError(Error.bad_file);
                setException(e);
            } catch (IOException e) {
                setStatus(FileTransfer.Status.error);
                setException(e);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        LOGGER.log(Level.WARNING, "Closing input stream", e);
                    }
                }
                try {
                    outputStream.close();
                } catch (IOException e) {
                    LOGGER.log(Level.WARNING, "Closing output stream", e);
                }
            }
            updateStatus(Status.in_progress, FileTransfer.Status.complete);
        }
    }, "File Transfer " + streamID);
    transferThread.start();
}
Also used : XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) FileNotFoundException(java.io.FileNotFoundException) IllegalStateChangeException(org.jivesoftware.smack.SmackException.IllegalStateChangeException) XMPPException(org.jivesoftware.smack.XMPPException) FileInputStream(java.io.FileInputStream)

Example 39 with SmackException

use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.

the class Socks5TransferNegotiator method negotiateIncomingStream.

@Override
InputStream negotiateIncomingStream(Stanza streamInitiation) throws InterruptedException, SmackException, XMPPErrorException {
    // build SOCKS5 Bytestream request
    Socks5BytestreamRequest request = new ByteStreamRequest(this.manager, (Bytestream) streamInitiation);
    // always accept the request
    Socks5BytestreamSession session = request.accept();
    // test input stream
    try {
        PushbackInputStream stream = new PushbackInputStream(session.getInputStream());
        int firstByte = stream.read();
        stream.unread(firstByte);
        return stream;
    } catch (IOException e) {
        throw new SmackException("Error establishing input stream", e);
    }
}
Also used : Socks5BytestreamRequest(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest) PushbackInputStream(java.io.PushbackInputStream) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) Socks5BytestreamSession(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)

Example 40 with SmackException

use of org.jivesoftware.smack.SmackException in project Smack by igniterealtime.

the class Socks5BytestreamManager method establishSession.

/**
     * Establishes a SOCKS5 Bytestream with the given user using the given session ID and returns
     * the Socket to send/receive data to/from the user.
     * 
     * @param targetJID the JID of the user a SOCKS5 Bytestream should be established
     * @param sessionID the session ID for the SOCKS5 Bytestream request
     * @return the Socket to send/receive data to/from the user
     * @throws IOException if the bytestream could not be established
     * @throws InterruptedException if the current thread was interrupted while waiting
     * @throws NoResponseException 
     * @throws SmackException if the target does not support SOCKS5.
     * @throws XMPPException 
     */
@Override
public Socks5BytestreamSession establishSession(Jid targetJID, String sessionID) throws IOException, InterruptedException, NoResponseException, SmackException, XMPPException {
    XMPPConnection connection = connection();
    XMPPErrorException discoveryException = null;
    // check if target supports SOCKS5 Bytestream
    if (!supportsSocks5(targetJID)) {
        throw new FeatureNotSupportedException("SOCKS5 Bytestream", targetJID);
    }
    List<Jid> proxies = new ArrayList<>();
    // determine SOCKS5 proxies from XMPP-server
    try {
        proxies.addAll(determineProxies());
    } catch (XMPPErrorException e) {
        // don't abort here, just remember the exception thrown by determineProxies()
        // determineStreamHostInfos() will at least add the local Socks5 proxy (if enabled)
        discoveryException = e;
    }
    // determine address and port of each proxy
    List<StreamHost> streamHosts = determineStreamHostInfos(proxies);
    if (streamHosts.isEmpty()) {
        if (discoveryException != null) {
            throw discoveryException;
        } else {
            throw new SmackException("no SOCKS5 proxies available");
        }
    }
    // compute digest
    String digest = Socks5Utils.createDigest(sessionID, connection.getUser(), targetJID);
    // prioritize last working SOCKS5 proxy if exists
    if (this.proxyPrioritizationEnabled && this.lastWorkingProxy != null) {
        StreamHost selectedStreamHost = null;
        for (StreamHost streamHost : streamHosts) {
            if (streamHost.getJID().equals(this.lastWorkingProxy)) {
                selectedStreamHost = streamHost;
                break;
            }
        }
        if (selectedStreamHost != null) {
            streamHosts.remove(selectedStreamHost);
            streamHosts.add(0, selectedStreamHost);
        }
    }
    Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
    try {
        // add transfer digest to local proxy to make transfer valid
        socks5Proxy.addTransfer(digest);
        // create initiation packet
        Bytestream initiation = createBytestreamInitiation(sessionID, targetJID, streamHosts);
        // send initiation packet
        Stanza response = connection.createStanzaCollectorAndSend(initiation).nextResultOrThrow(getTargetResponseTimeout());
        // extract used stream host from response
        StreamHostUsed streamHostUsed = ((Bytestream) response).getUsedHost();
        StreamHost usedStreamHost = initiation.getStreamHost(streamHostUsed.getJID());
        if (usedStreamHost == null) {
            throw new SmackException("Remote user responded with unknown host");
        }
        // build SOCKS5 client
        Socks5Client socks5Client = new Socks5ClientForInitiator(usedStreamHost, digest, connection, sessionID, targetJID);
        // establish connection to proxy
        Socket socket = socks5Client.getSocket(getProxyConnectionTimeout());
        // remember last working SOCKS5 proxy to prioritize it for next request
        this.lastWorkingProxy = usedStreamHost.getJID();
        // negotiation successful, return the output stream
        return new Socks5BytestreamSession(socket, usedStreamHost.getJID().equals(connection.getUser()));
    } catch (TimeoutException e) {
        throw new IOException("Timeout while connecting to SOCKS5 proxy");
    } finally {
        // remove transfer digest if output stream is returned or an exception
        // occurred
        socks5Proxy.removeTransfer(digest);
    }
}
Also used : StreamHostUsed(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHostUsed) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Jid(org.jxmpp.jid.Jid) SmackException(org.jivesoftware.smack.SmackException) Stanza(org.jivesoftware.smack.packet.Stanza) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) ArrayList(java.util.ArrayList) XMPPConnection(org.jivesoftware.smack.XMPPConnection) IOException(java.io.IOException) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

SmackException (org.jivesoftware.smack.SmackException)50 IOException (java.io.IOException)21 XMPPException (org.jivesoftware.smack.XMPPException)17 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)10 Test (org.junit.Test)9 Socket (java.net.Socket)8 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)6 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)5 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)5 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)5 XmppStringprepException (org.jxmpp.stringprep.XmppStringprepException)5 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 ConnectException (java.net.ConnectException)4 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)4 InputStream (java.io.InputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ServerSocket (java.net.ServerSocket)3 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3