Example usage for org.apache.commons.net.tftp TFTPClient close

List of usage examples for org.apache.commons.net.tftp TFTPClient close

Introduction

In this page you can find the example usage for org.apache.commons.net.tftp TFTPClient close.

Prototype

public void close() 

Source Link

Document

Closes the DatagramSocket used for the connection.

Usage

From source file:org.apache.commons.net.examples.ftp.TFTPExample.java

private static boolean receive(int transferMode, String hostname, String localFilename, String remoteFilename,
        TFTPClient tftp) {
    boolean closed;
    FileOutputStream output = null;
    File file;/*from  w w w.  ja  v a 2 s.  co m*/

    file = new File(localFilename);

    // If file exists, don't overwrite it.
    if (file.exists()) {
        System.err.println("Error: " + localFilename + " already exists.");
        return false;
    }

    // Try to open local file for writing
    try {
        output = new FileOutputStream(file);
    } catch (IOException e) {
        tftp.close();
        throw new RuntimeException("Error: could not open local file for writing.", e);
    }

    open(tftp);

    // Try to receive remote file via TFTP
    try {
        String[] parts = hostname.split(":");
        if (parts.length == 2) {
            tftp.receiveFile(remoteFilename, transferMode, output, parts[0], Integer.parseInt(parts[1]));
        } else {
            tftp.receiveFile(remoteFilename, transferMode, output, hostname);
        }
    } catch (UnknownHostException e) {
        System.err.println("Error: could not resolve hostname.");
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Error: I/O exception occurred while receiving file.");
        System.err.println(e.getMessage());
        System.exit(1);
    } finally {
        // Close local socket and output file
        closed = close(tftp, output);
    }

    return closed;
}

From source file:org.apache.commons.net.examples.ftp.TFTPExample.java

private static boolean close(TFTPClient tftp, Closeable output) {
    boolean closed;
    tftp.close();
    try {/* w w w  .j a v a  2  s . co m*/
        if (output != null) {
            output.close();
        }
        closed = true;
    } catch (IOException e) {
        closed = false;
        System.err.println("Error: error closing file.");
        System.err.println(e.getMessage());
    }
    return closed;
}

From source file:org.jnode.protocol.tftp.TFTPURLConnection.java

/**
 * @see java.net.URLConnection#getInputStream()
 *//*from  w w  w. j  av a  2 s.c  om*/
public InputStream getInputStream() throws IOException {
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    final TFTPClient tftp = new TFTPClient();
    final InetAddress hostAddr = InetAddress.getByName(host);
    tftp.open(TFTP.DEFAULT_PORT);
    try {
        tftp.receiveFile(path, TFTP.BINARY_MODE, os, hostAddr);
    } finally {
        tftp.close();
    }
    return new ByteArrayInputStream(os.toByteArray());
}

From source file:org.sipfoundry.preflight.TFTP.java

public ResultCode validate(int timeout, NetworkResources networkResources, JournalService journalService,
        InetAddress bindAddress) {
    ResultCode results = NONE;/*from  w  ww  .  ja v a  2 s .c  o m*/
    InetAddress tftpServerAddress = null;
    String testFile = new String("00D01EFFFFFE");
    String[] verificationStrings = { "LIP-68XX configuration information", "[VOIP]", "outbound_proxy_server",
            "[PROVISION]", "decrypt_key" };

    if (networkResources.configServer == null) {
        journalService.println("No TFTP server provided, skipping test.\n");
        return CONFIG_SERVER_MISSING;
    }

    journalService.println("Starting TFTP server test.");

    TFTPClient tftp = new TFTPClient();

    tftp.setDefaultTimeout(timeout * 1000);

    if (IPAddressUtil.isLiteralIPAddress(networkResources.configServer)) {
        try {
            tftpServerAddress = InetAddress.getByName(networkResources.configServer);
        } catch (UnknownHostException e) {
            // Should never get here.
            e.printStackTrace();
        }
        journalService.println("Using TFTP server literal address: " + networkResources.configServer);
    } else {
        // Try to retrieve A RECORD for TFTP server, checking each DNS server.
        SimpleResolver resolver = null;
        try {
            resolver = new SimpleResolver();
            resolver.setTimeout(timeout);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

        for (InetAddress dnsServer : networkResources.domainNameServers) {
            journalService.println(
                    "Looking up TFTP server address via DNS server: " + dnsServer.getCanonicalHostName());
            String targetMessage = new String(
                    "  TFTP server address \"" + networkResources.configServer + "\"");
            resolver.setAddress(dnsServer);
            Lookup aLookup = null;
            try {
                aLookup = new Lookup(networkResources.configServer, Type.A);
            } catch (TextParseException e) {
                journalService.println("  is malformed.\n");
                journalService.println(targetMessage);
                return TFTP_ADDRESS_MALFORMED;
            }
            aLookup.setResolver(resolver);
            Record[] aRecords = aLookup.run();
            switch (aLookup.getResult()) {
            case Lookup.SUCCESSFUL:
                if (aRecords != null) {
                    InetAddress targetAddress = ((ARecord) aRecords[0]).getAddress();
                    targetMessage += " resolves to: " + targetAddress.getHostAddress();
                    journalService.println(targetMessage);
                    if (tftpServerAddress == null) {
                        tftpServerAddress = targetAddress;
                    } else {
                        // Check that multiple lookups result in same address.
                        if (!tftpServerAddress.equals(targetAddress)) {
                            journalService.println("  TFTP server address does not match prior lookup.");
                            results = MULTIPLE_CONFIG_TARGETS;
                        }
                    }
                } else {
                    targetMessage += " could not be resolved.";
                    journalService.println(targetMessage);
                    results = TFTP_TARGET_UNRESOLVED;
                }
                break;
            case Lookup.UNRECOVERABLE:
                targetMessage += " [Unrecoverable error]";
                journalService.println(targetMessage);
                results = TFTP_TARGET_UNRESOLVED;
                break;
            case Lookup.TRY_AGAIN:
                targetMessage += " [Lookup timeout]";
                journalService.println(targetMessage);
                results = TFTP_TARGET_UNRESOLVED;
                break;
            case Lookup.HOST_NOT_FOUND:
                targetMessage += " could not be resolved.";
                journalService.println(targetMessage);
                results = TFTP_TARGET_UNRESOLVED;
                break;
            case Lookup.TYPE_NOT_FOUND:
                targetMessage += " could not be resolved.";
                journalService.println(targetMessage);
                results = TFTP_TARGET_UNRESOLVED;
                break;
            }
        }
    }

    if ((tftpServerAddress == null) || (results == MULTIPLE_CONFIG_TARGETS)) {
        journalService.println("Cannot recover from previous errors, aborting TFTP test.\n");
        return results;
    }

    journalService.println("Beginning TFTP get request of test file: " + testFile);
    // Open local socket
    try {
        tftp.open();
    } catch (SocketException e) {
        journalService.println("TFTP client failure. " + e.getMessage() + "\n");
        return TFTP_CLIENT_FAILURE;
    }

    // Try to receive remote file via TFTP
    try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        tftp.open(bindPort, bindAddress);
        tftp.receiveFile(testFile, org.apache.commons.net.tftp.TFTP.ASCII_MODE, output, tftpServerAddress);
        String testFileContents = output.toString();
        boolean verified = true;
        for (String verificationString : verificationStrings) {
            if (!testFileContents.contains(verificationString)) {
                verified = false;
            }
        }
        if (verified) {
            journalService.println("File received successfully.");
        } else {
            journalService.println("File received but contents do not verify.");
            results = TFTP_CONTENTS_FAILED;
        }
    } catch (UnknownHostException e) {
        journalService.println("TFTP client failure. " + e.getMessage());
        results = TFTP_CLIENT_FAILURE;
    } catch (IOException e) {
        journalService.println("TFTP get failed. " + e.getMessage());
        results = TFTP_GET_FAILED;
    } finally {
        // Close local socket and output file
        tftp.close();
    }

    journalService.println("");
    return results;
}

From source file:org.xerela.net.sim.tftp.TftpInterface.java

public void sendFile(IpAddress localIp, IpAddress remoteIp, String filename, InputStream file)
        throws UnknownHostException, IOException {
    TFTPClient client = null;
    try {/* ww  w .j  av  a 2s  .c o m*/
        client = new TFTPClient();
        client.setDatagramSocketFactory(new SocketFactory(localIp.getRealAddress()));
        client.open();
        client.sendFile(filename, TFTPClient.BINARY_MODE, file, remoteIp.getIp());
    } finally {
        try {
            client.close();
        } catch (NullPointerException e) {
        }
    }
}

From source file:person.daizhongde.virtue.util.test.TFTPExample.java

private static boolean send(int transferMode, String hostname, String localFilename, String remoteFilename,
        TFTPClient tftp) {
    boolean closed;
    FileInputStream input = null;

    // Try to open local file for reading
    try {/* ww w.  j  ava2  s .  co  m*/
        input = new FileInputStream(localFilename);
    } catch (IOException e) {
        tftp.close();
        System.err.println("Error: could not open local file for reading.");
        System.err.println(e.getMessage());
        System.exit(1);
    }

    open(tftp);

    // Try to send local file via TFTP
    try {
        String[] parts = hostname.split(":");
        if (parts.length == 2) {
            tftp.sendFile(remoteFilename, transferMode, input, parts[0], Integer.parseInt(parts[1]));
        } else {
            tftp.sendFile(remoteFilename, transferMode, input, hostname);
        }
    } catch (UnknownHostException e) {
        System.err.println("Error: could not resolve hostname.");
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Error: I/O exception occurred while sending file.");
        System.err.println(e.getMessage());
        System.exit(1);
    } finally {
        // Close local socket and input file
        closed = close(tftp, input);
    }

    return closed;
}

From source file:person.daizhongde.virtue.util.test.TFTPExample.java

private static boolean receive(int transferMode, String hostname, String localFilename, String remoteFilename,
        TFTPClient tftp) {
    boolean closed;
    FileOutputStream output = null;
    File file;/* www.  j av  a2 s.com*/

    file = new File(localFilename);

    // If file exists, don't overwrite it.
    if (file.exists()) {
        System.err.println("Error: " + localFilename + " already exists.");
        System.exit(1);
    }

    // Try to open local file for writing
    try {
        output = new FileOutputStream(file);
    } catch (IOException e) {
        tftp.close();
        System.err.println("Error: could not open local file for writing.");
        System.err.println(e.getMessage());
        System.exit(1);
    }

    open(tftp);

    // Try to receive remote file via TFTP
    try {
        String[] parts = hostname.split(":");
        if (parts.length == 2) {
            tftp.receiveFile(remoteFilename, transferMode, output, parts[0], Integer.parseInt(parts[1]));
        } else {
            tftp.receiveFile(remoteFilename, transferMode, output, hostname);
        }
    } catch (UnknownHostException e) {
        System.err.println("Error: could not resolve hostname.");
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Error: I/O exception occurred while receiving file.");
        System.err.println(e.getMessage());
        System.exit(1);
    } finally {
        // Close local socket and output file
        closed = close(tftp, output);
    }

    return closed;
}