Example usage for org.apache.commons.net MalformedServerReplyException MalformedServerReplyException

List of usage examples for org.apache.commons.net MalformedServerReplyException MalformedServerReplyException

Introduction

In this page you can find the example usage for org.apache.commons.net MalformedServerReplyException MalformedServerReplyException.

Prototype

public MalformedServerReplyException(String message) 

Source Link

Document

Constructs a MalformedServerReplyException with a specified message.

Usage

From source file:com.myJava.file.driver.remote.ftp.FTPClient.java

public int pasv() throws IOException {
    int passiveReturnCode = super.pasv();

    if (passiveReturnCode == FTPReply.ENTERING_PASSIVE_MODE) {

        // Parse and display reply host and port in passive mode
        // It's quite ugly but there is no entry point in apache's ftp library to perform this check.
        String reply = getReplyStrings()[0];
        int i, index, lastIndex;
        String octet1, octet2;//from  ww w.ja v a 2  s  .co m
        StringBuffer host;

        reply = reply.substring(reply.indexOf('(') + 1, reply.indexOf(')')).trim();

        host = new StringBuffer(24);
        lastIndex = 0;
        index = reply.indexOf(',');
        host.append(reply.substring(lastIndex, index));

        for (i = 0; i < 3; i++) {
            host.append('.');
            lastIndex = index + 1;
            index = reply.indexOf(',', lastIndex);
            host.append(reply.substring(lastIndex, index));
        }

        lastIndex = index + 1;
        index = reply.indexOf(',', lastIndex);

        octet1 = reply.substring(lastIndex, index);
        octet2 = reply.substring(index + 1);

        // index and lastIndex now used as temporaries
        try {
            index = Integer.parseInt(octet1);
            lastIndex = Integer.parseInt(octet2);
        } catch (NumberFormatException e) {
            throw new MalformedServerReplyException(
                    "Could not parse passive host information.\nServer Reply: " + reply);
        }

        index <<= 8;
        index |= lastIndex;

        String passvHost = host.toString();

        //int passvPort = index;
        //Logger.defaultLogger().info("Passive host received from server : " + passvHost + ":" + passvPort);

        InetAddress refAddress = InetAddress.getByName(passvHost);
        if (!refAddress.equals(this._socket_.getInetAddress())) {
            String msg = "Passive address (" + refAddress + ") differs from host address ("
                    + this._socket_.getInetAddress()
                    + "). This is probably because your server is behind a router. Please check your FTP server's configuration. (for instance, use a masquerade address)";
            Logger.defaultLogger().warn(msg);
            if (!ignorePasvErrors) {
                throw new IOException(msg);
            }
        }
    }

    return passiveReturnCode;
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

/**
 * Parse the reply and store the id and number in the pointer.
 *
 * @param reply the reply to parse "22n nnn <aaa>"
 * @param pointer the pointer to update/* w w  w.  j a va 2 s . c  o  m*/
 *
 * @throws MalformedServerReplyException
 */
private void __parseArticlePointer(String reply, ArticleInfo pointer) throws MalformedServerReplyException {
    String tokens[] = reply.split(" ");
    if (tokens.length >= 3) { // OK, we can parset the line
        int i = 1; // skip reply code
        try {
            // Get article number
            pointer.articleNumber = Long.parseLong(tokens[i++]);
            // Get article id
            pointer.articleId = tokens[i++];
            return; // done
        } catch (NumberFormatException e) {
            // drop through and raise exception
        }
    }
    throw new MalformedServerReplyException("Could not parse article pointer.\nServer reply: " + reply);
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

private static void __parseGroupReply(String reply, NewsgroupInfo info) throws MalformedServerReplyException {
    String tokens[] = reply.split(" ");
    if (tokens.length >= 5) {
        int i = 1; // Skip numeric response value
        try {/*from  www.  ja  va2s  .  co m*/
            // Get estimated article count
            info._setArticleCount(Long.parseLong(tokens[i++]));
            // Get first article number
            info._setFirstArticle(Long.parseLong(tokens[i++]));
            // Get last article number
            info._setLastArticle(Long.parseLong(tokens[i++]));
            // Get newsgroup name
            info._setNewsgroup(tokens[i++]);

            info._setPostingPermission(NewsgroupInfo.UNKNOWN_POSTING_PERMISSION);
            return;
        } catch (NumberFormatException e) {
            // drop through to report error
        }
    }

    throw new MalformedServerReplyException("Could not parse newsgroup info.\nServer reply: " + reply);
}

From source file:net.longfalcon.newsj.nntp.client.CustomNNTPClient.java

private NewsgroupInfo[] __readNewsgroupListing() throws IOException {

    BufferedReader reader = new DotTerminatedMessageReader(_reader_);
    // Start of with a big vector because we may be reading a very large
    // amount of groups.
    Vector<NewsgroupInfo> list = new Vector<NewsgroupInfo>(2048);

    String line;/* w ww .j  a v a 2  s  .c om*/
    try {
        while ((line = reader.readLine()) != null) {
            NewsgroupInfo tmp = __parseNewsgroupListEntry(line);
            if (tmp != null) {
                list.addElement(tmp);
            } else {
                throw new MalformedServerReplyException(line);
            }
        }
    } finally {
        reader.close();
    }
    int size;
    if ((size = list.size()) < 1) {
        return new NewsgroupInfo[0];
    }

    NewsgroupInfo[] info = new NewsgroupInfo[size];
    list.copyInto(info);

    return info;
}

From source file:com.atomicleopard.thundr.ftp.commons.FTP.java

private void __getReply(boolean reportReply) throws IOException {
    int length;//from w w w  . j  a v  a2  s  . co m

    _newReplyString = true;
    _replyLines.clear();

    String line = _controlInput_.readLine();

    if (line == null) {
        throw new FTPConnectionClosedException("Connection closed without indication.");
    }

    // In case we run into an anomaly we don't want fatal index exceptions
    // to be thrown.
    length = line.length();
    if (length < REPLY_CODE_LEN) {
        throw new MalformedServerReplyException("Truncated server reply: " + line);
    }

    String code = null;
    try {
        code = line.substring(0, REPLY_CODE_LEN);
        _replyCode = Integer.parseInt(code);
    } catch (NumberFormatException e) {
        throw new MalformedServerReplyException("Could not parse response code.\nServer Reply: " + line);
    }

    _replyLines.add(line);

    // Get extra lines if message continues.
    if (length > REPLY_CODE_LEN && line.charAt(REPLY_CODE_LEN) == '-') {
        do {
            line = _controlInput_.readLine();

            if (line == null) {
                throw new FTPConnectionClosedException("Connection closed without indication.");
            }

            _replyLines.add(line);

            // The length() check handles problems that could arise from readLine()
            // returning too soon after encountering a naked CR or some other
            // anomaly.
        } while (isStrictMultilineParsing() ? __strictCheck(line, code) : __lenientCheck(line));
    }

    fireReplyReceived(_replyCode, getReplyString());

    if (_replyCode == FTPReply.SERVICE_NOT_AVAILABLE) {
        throw new FTPConnectionClosedException("FTP response 421 received.  Server closed connection.");
    }
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

/**
 * @since 3.1//from  w  w  w . ja  v  a  2s. c o  m
 */
protected void _parsePassiveModeReply(String reply) throws MalformedServerReplyException {
    java.util.regex.Matcher m = __PARMS_PAT.matcher(reply);
    if (!m.find()) {
        throw new MalformedServerReplyException(
                "Could not parse passive host information.\nServer Reply: " + reply);
    }

    __passiveHost = m.group(1).replace(',', '.'); // Fix up to look like IP address

    try {
        int oct1 = Integer.parseInt(m.group(2));
        int oct2 = Integer.parseInt(m.group(3));
        __passivePort = (oct1 << 8) | oct2;
    } catch (NumberFormatException e) {
        throw new MalformedServerReplyException(
                "Could not parse passive port information.\nServer Reply: " + reply);
    }

    if (__passiveNatWorkaround) {
        try {
            InetAddress host = InetAddress.getByName(__passiveHost);
            // reply is a local address, but target is not - assume NAT box changed the PASV reply
            if (host.isSiteLocalAddress()) {
                InetAddress remote = getRemoteAddress();
                if (!remote.isSiteLocalAddress()) {
                    String hostAddress = remote.getHostAddress();
                    fireReplyReceived(0,
                            "[Replacing site local address " + __passiveHost + " with " + hostAddress + "]\n");
                    __passiveHost = hostAddress;
                }
            }
        } catch (UnknownHostException e) { // Should not happen as we are passing in an IP address
            throw new MalformedServerReplyException(
                    "Could not parse passive host information.\nServer Reply: " + reply);
        }
    }
}

From source file:com.atomicleopard.thundr.ftp.commons.FTPClient.java

protected void _parseExtendedPassiveModeReply(String reply) throws MalformedServerReplyException {
    reply = reply.substring(reply.indexOf('(') + 1, reply.indexOf(')')).trim();

    char delim1, delim2, delim3, delim4;
    delim1 = reply.charAt(0);/* w  w w. j  a va2 s  . co m*/
    delim2 = reply.charAt(1);
    delim3 = reply.charAt(2);
    delim4 = reply.charAt(reply.length() - 1);

    if (!(delim1 == delim2) || !(delim2 == delim3) || !(delim3 == delim4)) {
        throw new MalformedServerReplyException(
                "Could not parse extended passive host information.\nServer Reply: " + reply);
    }

    int port;
    try {
        port = Integer.parseInt(reply.substring(3, reply.length() - 1));
    } catch (NumberFormatException e) {
        throw new MalformedServerReplyException(
                "Could not parse extended passive host information.\nServer Reply: " + reply);
    }

    // in EPSV mode, the passive host address is implicit
    __passiveHost = getRemoteAddress().getHostAddress();
    __passivePort = port;
}

From source file:nl.nn.adapterframework.ftp.FTPsClient.java

private Object _readReply(InputStream in, boolean concatenateLines) throws IOException {
    // obtain the result
    BufferedReader reader = new BufferedReader(new InputStreamReader(in, "ISO-8859-1"));

    int replyCode = 0;
    StringBuffer reply = new StringBuffer();
    List replyList = new ArrayList();
    String line = reader.readLine();

    if (line == null)
        throw new FTPConnectionClosedException("Connection closed without indication.");
    reply.append(line).append("\n");
    replyList.add(line);/*  w  ww. j av  a  2 s. c o m*/

    // In case we run into an anomaly we don't want fatal index exceptions
    // to be thrown.
    int length = line.length();
    if (length < 3)
        throw new MalformedServerReplyException("Truncated server reply: " + line);

    try {
        String code = line.substring(0, 3);
        replyCode = Integer.parseInt(code);
    } catch (NumberFormatException e) {
        MalformedServerReplyException mfre = new MalformedServerReplyException(
                "Could not parse response code.\nServer Reply [" + line + "]");
        mfre.initCause(e);
        throw mfre;
    }

    // Get extra lines if message continues.
    if (length > 3 && line.charAt(3) == '-') {
        do {
            line = reader.readLine();
            if (line == null)
                throw new FTPConnectionClosedException(
                        "Connection closed without indication after having read [" + reply.toString() + "]");

            reply.append(line).append("\n");
            replyList.add(line);
        } while (!(line.length() >= 4 && line.charAt(3) != '-' && Character.isDigit(line.charAt(0))));
    }

    if (replyCode == FTPReply.SERVICE_NOT_AVAILABLE)
        throw new FTPConnectionClosedException("FTP response 421 received. Server closed connection.");

    if (!FTPReply.isPositiveCompletion(replyCode))
        throw new IOException("Exception while sending command \n" + reply.toString());

    log.debug("_readReply [" + reply.toString() + "]");

    if (concatenateLines) {
        return reply.toString();
    }
    return (String[]) replyList.toArray(new String[0]);
}

From source file:org.apache.nutch.protocol.ftp.Client.java

private void __parsePassiveModeReply(String reply) throws MalformedServerReplyException {
    int i, index, lastIndex;
    String octet1, octet2;//from ww  w .  ja v a 2  s  .co  m
    StringBuffer host;

    reply = reply.substring(reply.indexOf('(') + 1, reply.indexOf(')')).trim();

    host = new StringBuffer(24);
    lastIndex = 0;
    index = reply.indexOf(',');
    host.append(reply.substring(lastIndex, index));

    for (i = 0; i < 3; i++) {
        host.append('.');
        lastIndex = index + 1;
        index = reply.indexOf(',', lastIndex);
        host.append(reply.substring(lastIndex, index));
    }

    lastIndex = index + 1;
    index = reply.indexOf(',', lastIndex);

    octet1 = reply.substring(lastIndex, index);
    octet2 = reply.substring(index + 1);

    // index and lastIndex now used as temporaries
    try {
        index = Integer.parseInt(octet1);
        lastIndex = Integer.parseInt(octet2);
    } catch (NumberFormatException e) {
        throw new MalformedServerReplyException(
                "Could not parse passive host information.\nServer Reply: " + reply);
    }

    index <<= 8;
    index |= lastIndex;

    __passiveHost = host.toString();
    __passivePort = index;
}

From source file:org.random_access.newsreader.nntp.CustomNNTP.java

private void __getReply() throws IOException {
    this._replyString = this._reader_.readLine();
    if (this._replyString == null) {
        throw new NNTPConnectionClosedException("Connection closed without indication.");
    } else if (this._replyString.length() < 3) {
        throw new MalformedServerReplyException("Truncated server reply: " + this._replyString);
    } else {/* www  . j a va 2  s  .  c om*/
        try {
            this._replyCode = Integer.parseInt(this._replyString.substring(0, 3));
        } catch (NumberFormatException var2) {
            throw new MalformedServerReplyException(
                    "Could not parse response code.\nServer Reply: " + this._replyString);
        }

        this.fireReplyReceived(this._replyCode, this._replyString + "\r\n");
        if (this._replyCode == 400) {
            throw new NNTPConnectionClosedException("NNTP response 400 received.  Server closed connection.");
        }
    }
}