Example usage for org.apache.commons.net.ftp FTPClient retrieveFileStream

List of usage examples for org.apache.commons.net.ftp FTPClient retrieveFileStream

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClient retrieveFileStream.

Prototype

public InputStream retrieveFileStream(String remote) throws IOException 

Source Link

Document

Returns an InputStream from which a named file from the server can be read.

Usage

From source file:org.apache.flume.source.FTPSource.java

@SuppressWarnings("UnnecessaryContinue")
public void discoverElements(FTPClient ftpClient, String parentDir, String currentDir, int level)
        throws IOException {

    String dirToList = parentDir;
    if (!currentDir.equals("")) {
        dirToList += "/" + currentDir;
    }// w  w w.  j  a  v a  2s . com
    FTPFile[] subFiles = ftpClient.listFiles(dirToList);
    if (subFiles != null && subFiles.length > 0) {

        for (FTPFile aFile : subFiles) {
            String currentFileName = aFile.getName();
            if (currentFileName.equals(".") || currentFileName.equals("..")) {
                log.info("Skip parent directory and directory itself");
                continue;
            }

            if (aFile.isDirectory()) {
                log.info("[" + aFile.getName() + "]");
                ftpClient.changeWorkingDirectory(parentDir);
                discoverElements(ftpClient, dirToList, aFile.getName(), level + 1);
                continue;
            } else if (aFile.isFile()) { //aFile is a regular file
                ftpClient.changeWorkingDirectory(dirToList);
                existFileList.add(dirToList + "/" + aFile.getName()); //control of deleted files in server
                String fileName = aFile.getName();
                if (!(sizeFileList.containsKey(dirToList + "/" + aFile.getName()))) { //new file
                    ftpSourceCounter.incrementFilesCount();
                    InputStream inputStream = null;
                    try {
                        inputStream = ftpClient.retrieveFileStream(aFile.getName());
                        listener.fileStreamRetrieved();
                        readStream(inputStream, 0);
                        boolean success = inputStream != null && ftpClient.completePendingCommand(); //mandatory
                        if (success) {
                            sizeFileList.put(dirToList + "/" + aFile.getName(), aFile.getSize());
                            saveMap(sizeFileList);
                            ftpSourceCounter.incrementFilesProcCount();
                            log.info("discovered: " + fileName + " ," + sizeFileList.size());
                        } else {
                            handleProcessError(fileName);
                        }
                    } catch (FTPConnectionClosedException e) {
                        handleProcessError(fileName);
                        log.error("Ftp server closed connection ", e);
                        continue;
                    }

                    ftpClient.changeWorkingDirectory(dirToList);
                    continue;

                } else { //known file                        
                    long dif = aFile.getSize() - sizeFileList.get(dirToList + "/" + aFile.getName());
                    if (dif > 0) { //known and modified
                        long prevSize = sizeFileList.get(dirToList + "/" + aFile.getName());
                        InputStream inputStream = null;
                        try {
                            inputStream = ftpClient.retrieveFileStream(aFile.getName());
                            listener.fileStreamRetrieved();
                            readStream(inputStream, prevSize);

                            boolean success = inputStream != null && ftpClient.completePendingCommand(); //mandatory
                            if (success) {
                                sizeFileList.put(dirToList + "/" + aFile.getName(), aFile.getSize());
                                saveMap(sizeFileList);
                                ftpSourceCounter.incrementCountModProc();
                                log.info("modified: " + fileName + " ," + sizeFileList.size());
                            } else {
                                handleProcessError(fileName);
                            }
                        } catch (FTPConnectionClosedException e) {
                            log.error("Ftp server closed connection ", e);
                            handleProcessError(fileName);
                            continue;
                        }

                        ftpClient.changeWorkingDirectory(dirToList);
                        continue;
                    } else if (dif < 0) { //known and full modified
                        existFileList.remove(dirToList + "/" + aFile.getName()); //will be rediscovered as new file
                        saveMap(sizeFileList);
                        continue;
                    }
                    ftpClient.changeWorkingDirectory(parentDir);
                    continue;
                }
            } else if (aFile.isSymbolicLink()) {
                log.info(aFile.getName() + " is a link of " + aFile.getLink() + " access denied");
                ftpClient.changeWorkingDirectory(parentDir);
                continue;
            } else if (aFile.isUnknown()) {
                log.info(aFile.getName() + " unknown type of file");
                ftpClient.changeWorkingDirectory(parentDir);
                continue;
            } else {
                ftpClient.changeWorkingDirectory(parentDir);
                continue;
            }

        } //fin de bucle
    } //el listado no es vaco
}

From source file:org.apache.hadoop.fs.ftp.FTPFileSystem.java

@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
    FTPClient client = connect();
    Path workDir = new Path(client.printWorkingDirectory());
    Path absolute = makeAbsolute(workDir, file);
    FileStatus fileStat = getFileStatus(client, absolute);
    if (fileStat.isDir()) {
        disconnect(client);//from w ww . j a  v  a  2 s .c o  m
        throw new IOException("Path " + file + " is a directory.");
    }
    client.allocate(bufferSize);
    Path parent = absolute.getParent();
    // Change to parent directory on the
    // server. Only then can we read the
    // file
    // on the server by opening up an InputStream. As a side effect the working
    // directory on the server is changed to the parent directory of the file.
    // The FTP client connection is closed when close() is called on the
    // FSDataInputStream.
    client.changeWorkingDirectory(parent.toUri().getPath());
    InputStream is = client.retrieveFileStream(file.getName());
    FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is, client, statistics));
    if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
        // The ftpClient is an inconsistent state. Must close the stream
        // which in turn will logout and disconnect from FTP server
        fis.close();
        throw new IOException("Unable to open file: " + file + ", Aborting");
    }
    return fis;
}

From source file:org.apache.hive.hplsql.Ftp.java

/**
 * Run a thread to transfer files//w  ww .ja  va  2s  .c o m
 */
public void run() {
    byte[] data = null;
    Timer timer = new Timer();
    FTPClient ftp = this.ftp;
    if (currentThreadCnt.getAndIncrement() > 0) {
        ftp = openConnection(null);
    }
    while (true) {
        String file = filesQueue.poll();
        if (file == null) {
            break;
        }
        int num = currentFileCnt.getAndIncrement();
        FTPFile ftpFile = filesMap.get(file);
        long ftpSizeInBytes = ftpFile.getSize();
        String fmtSizeInBytes = Utils.formatSizeInBytes(ftpSizeInBytes);
        String targetFile = getTargetFileName(file);
        if (info) {
            info(null, "  " + file + " - started (" + num + " of " + fileCnt + ", " + fmtSizeInBytes + ")");
        }
        try {
            InputStream in = ftp.retrieveFileStream(file);
            OutputStream out = null;
            java.io.File targetLocalFile = null;
            File targetHdfsFile = null;
            if (local) {
                targetLocalFile = new java.io.File(targetFile);
                if (!targetLocalFile.exists()) {
                    targetLocalFile.getParentFile().mkdirs();
                    targetLocalFile.createNewFile();
                }
                out = new FileOutputStream(targetLocalFile, false /*append*/);
            } else {
                targetHdfsFile = new File();
                out = targetHdfsFile.create(targetFile, true /*overwrite*/);
            }
            if (data == null) {
                data = new byte[3 * 1024 * 1024];
            }
            int bytesRead = -1;
            long bytesReadAll = 0;
            long start = timer.start();
            long prev = start;
            long readTime = 0;
            long writeTime = 0;
            long cur, cur2, cur3;
            while (true) {
                cur = timer.current();
                bytesRead = in.read(data);
                cur2 = timer.current();
                readTime += (cur2 - cur);
                if (bytesRead == -1) {
                    break;
                }
                out.write(data, 0, bytesRead);
                out.flush();
                cur3 = timer.current();
                writeTime += (cur3 - cur2);
                bytesReadAll += bytesRead;
                if (info) {
                    cur = timer.current();
                    if (cur - prev > 13000) {
                        long elapsed = cur - start;
                        info(null,
                                "  " + file + " - in progress (" + Utils.formatSizeInBytes(bytesReadAll)
                                        + " of " + fmtSizeInBytes + ", "
                                        + Utils.formatPercent(bytesReadAll, ftpSizeInBytes) + ", "
                                        + Utils.formatTime(elapsed) + ", "
                                        + Utils.formatBytesPerSec(bytesReadAll, elapsed) + ", "
                                        + Utils.formatBytesPerSec(bytesReadAll, readTime) + " read, "
                                        + Utils.formatBytesPerSec(bytesReadAll, writeTime) + " write)");
                        prev = cur;
                    }
                }
            }
            if (ftp.completePendingCommand()) {
                in.close();
                cur = timer.current();
                out.close();
                readTime += (timer.current() - cur);
                bytesTransferredAll.addAndGet(bytesReadAll);
                fileCntSuccess.incrementAndGet();
                if (info) {
                    long elapsed = timer.stop();
                    info(null, "  " + file + " - complete (" + Utils.formatSizeInBytes(bytesReadAll) + ", "
                            + Utils.formatTime(elapsed) + ", " + Utils.formatBytesPerSec(bytesReadAll, elapsed)
                            + ", " + Utils.formatBytesPerSec(bytesReadAll, readTime) + " read, "
                            + Utils.formatBytesPerSec(bytesReadAll, writeTime) + " write)");
                }
            } else {
                in.close();
                out.close();
                if (info) {
                    info(null, "  " + file + " - failed");
                }
                exec.signal(Signal.Type.SQLEXCEPTION, "File transfer failed: " + file);
            }
        } catch (IOException e) {
            exec.signal(e);
        }
    }
    try {
        if (ftp.isConnected()) {
            ftp.logout();
            ftp.disconnect();
        }
    } catch (IOException e) {
    }
}

From source file:org.apache.jmeter.protocol.ftp.sampler.FTPSampler.java

@Override
public SampleResult sample(Entry e) {
    SampleResult res = new SampleResult();
    res.setSuccessful(false); // Assume failure
    String remote = getRemoteFilename();
    String local = getLocalFilename();
    boolean binaryTransfer = isBinaryMode();
    res.setSampleLabel(getName());//from   w ww. j  a v a 2 s  .  c  o m
    final String label = getLabel();
    res.setSamplerData(label);
    try {
        res.setURL(new URL(label));
    } catch (MalformedURLException e1) {
        log.warn("Cannot set URL: " + e1.getLocalizedMessage());
    }
    InputStream input = null;
    OutputStream output = null;

    res.sampleStart();
    FTPClient ftp = new FTPClient();
    try {
        savedClient = ftp;
        final int port = getPortAsInt();
        if (port > 0) {
            ftp.connect(getServer(), port);
        } else {
            ftp.connect(getServer());
        }
        res.latencyEnd();
        int reply = ftp.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            if (ftp.login(getUsername(), getPassword())) {
                if (binaryTransfer) {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                ftp.enterLocalPassiveMode();// should probably come from the setup dialog
                boolean ftpOK = false;
                if (isUpload()) {
                    String contents = getLocalFileContents();
                    if (contents.length() > 0) {
                        byte[] bytes = contents.getBytes(); // TODO - charset?
                        input = new ByteArrayInputStream(bytes);
                        res.setBytes(bytes.length);
                    } else {
                        File infile = new File(local);
                        res.setBytes((int) infile.length());
                        input = new BufferedInputStream(new FileInputStream(infile));
                    }
                    ftpOK = ftp.storeFile(remote, input);
                } else {
                    final boolean saveResponse = isSaveResponse();
                    ByteArrayOutputStream baos = null; // No need to close this
                    OutputStream target = null; // No need to close this
                    if (saveResponse) {
                        baos = new ByteArrayOutputStream();
                        target = baos;
                    }
                    if (local.length() > 0) {
                        output = new FileOutputStream(local);
                        if (target == null) {
                            target = output;
                        } else {
                            target = new TeeOutputStream(output, baos);
                        }
                    }
                    if (target == null) {
                        target = new NullOutputStream();
                    }
                    input = ftp.retrieveFileStream(remote);
                    if (input == null) {// Could not access file or other error
                        res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                        res.setResponseMessage(ftp.getReplyString());
                    } else {
                        long bytes = IOUtils.copy(input, target);
                        ftpOK = bytes > 0;
                        if (saveResponse && baos != null) {
                            res.setResponseData(baos.toByteArray());
                            if (!binaryTransfer) {
                                res.setDataType(SampleResult.TEXT);
                            }
                        } else {
                            res.setBytes((int) bytes);
                        }
                    }
                }

                if (ftpOK) {
                    res.setResponseCodeOK();
                    res.setResponseMessageOK();
                    res.setSuccessful(true);
                } else {
                    res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                    res.setResponseMessage(ftp.getReplyString());
                }
            } else {
                res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                res.setResponseMessage(ftp.getReplyString());
            }
        } else {
            res.setResponseCode("501"); // TODO
            res.setResponseMessage("Could not connect");
            //res.setResponseCode(Integer.toString(ftp.getReplyCode()));
            res.setResponseMessage(ftp.getReplyString());
        }
    } catch (IOException ex) {
        res.setResponseCode("000"); // TODO
        res.setResponseMessage(ex.toString());
    } finally {
        savedClient = null;
        if (ftp.isConnected()) {
            try {
                ftp.logout();
            } catch (IOException ignored) {
            }
            try {
                ftp.disconnect();
            } catch (IOException ignored) {
            }
        }
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);
    }

    res.sampleEnd();
    return res;
}

From source file:org.blue.star.plugins.check_ftp.java

public boolean execute_check() {
    /* Declare variables */
    FTPClient ftp = new FTPClient();
    File filename = null;//from w  w w  . ja  v  a2 s  .  com
    FileChannel channel;
    InputStream is;
    OutputStream os;
    int reply;

    if (super.verbose > 0)
        verbose = true;

    /* Configure client to meet our requirements */
    ftp.setDefaultPort(port);
    ftp.setDefaultTimeout(timeout);

    if (verbose) {
        System.out.println("Using FTP Server: " + hostname);
        System.out.println("Using FTP Port: " + port);
        System.out.println("Using Timeout of: " + timeout);
    }

    if (passive) {
        ftp.enterLocalPassiveMode();
        if (verbose)
            System.out.println("Using Passive Mode");
    }

    try {
        filename = new File(file);
        channel = new RandomAccessFile(filename, "rw").getChannel();

        if (verbose)
            System.out.println("Attempting FTP Connection to " + hostname);
        ftp.connect(hostname);
        reply = ftp.getReplyCode();

        /* Test to see if we actually managed to connect */
        if (!FTPReply.isPositiveCompletion(reply)) {
            if (verbose)
                System.out.println("FTP Connection to " + hostname + " failed");
            check_state = common_h.STATE_CRITICAL;
            check_message = ftp.getReplyString();
            filename.delete();
            ftp.disconnect();
            return true;
        }

        /* Try and login if we're using username/password */
        if (username != null && password != null) {
            if (verbose)
                System.out.println("Attempting to log in into FTP Server " + hostname);

            if (!ftp.login(username, password)) {
                if (verbose)
                    System.out.println("Unable to log in to FTP Server " + hostname);
                check_state = common_h.STATE_CRITICAL;
                check_message = ftp.getReplyString();
                ftp.disconnect();
                filename.delete();
                return true;
            }
        }

        if (verbose)
            System.out.println("Attempting to change to required directory");
        /* Try and change to the given directory */
        if (!ftp.changeWorkingDirectory(directory)) {
            if (verbose)
                System.out.println("Required directory cannot be found!");
            check_state = common_h.STATE_WARNING;
            check_message = ftp.getReplyString();
            ftp.disconnect();
            filename.delete();
            return true;
        }

        if (verbose)
            System.out.println("Attempting to retrieve specified file!");
        /* Try to get Stream on Remote File! */
        is = ftp.retrieveFileStream(file);

        if (is == null) {
            if (verbose)
                System.out.println("Unable to locate required file.");
            check_state = common_h.STATE_WARNING;
            check_message = ftp.getReplyString();
            ftp.disconnect();
            filename.delete();
            return true;
        }

        /* OutputStream */
        os = Channels.newOutputStream(channel);

        /* Create the buffer */
        byte[] buf = new byte[4096];

        if (verbose)
            System.out.println("Beginning File transfer...");
        for (int len = -1; (len = is.read(buf)) != -1;)
            os.write(buf, 0, len);

        if (verbose) {
            System.out.println("...transfer complete.");
            System.out.println("Attempting to finalise Command");
        }

        /* Finalise the transfer details */
        if (!ftp.completePendingCommand()) {
            if (verbose)
                System.out.println("Unable to finalise command");
            check_state = common_h.STATE_WARNING;
            check_message = ftp.getReplyString();
            ftp.disconnect();
            filename.delete();
            return true;
        }

        /* Clean up */
        if (verbose)
            System.out.println("Check Completed.");
        check_state = common_h.STATE_OK;
        check_message = ftp.getReplyString();

        /* Close out things */
        is.close();
        os.close();
        channel.close();
        filename.delete();

    } catch (IOException e) {
        check_state = common_h.STATE_CRITICAL;
        check_message = e.getMessage();
        if (filename != null)
            filename.delete();
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.logout();
                ftp.disconnect();

            } catch (Exception e) {
            }
        }
    }

    return true;
}

From source file:org.codice.alliance.nsili.source.NsiliSource.java

/**
 * Uses FTPClient to obtain the IOR string from the provided URL via FTP.
 *///from  w w w  .j  ava 2s  .  com
private void getIorStringFromFtpSource() {
    URI uri = null;
    try {
        uri = new URI(iorUrl);
    } catch (URISyntaxException e) {
        LOGGER.error("{} : Invalid URL specified for IOR string: {} {}", id, iorUrl, e.getMessage());
        LOGGER.debug("{} : Invalid URL specified for IOR string: {}", id, iorUrl, e);
    }

    FTPClient ftpClient = new FTPClient();
    try {
        if (uri.getPort() > 0) {
            ftpClient.connect(uri.getHost(), uri.getPort());
        } else {
            ftpClient.connect(uri.getHost());
        }

        if (!ftpClient.login(serverUsername, serverPassword)) {
            LOGGER.error("{} : FTP server log in unsuccessful.", id);
        } else {
            int timeoutMsec = clientTimeout * 1000;
            ftpClient.setConnectTimeout(timeoutMsec);
            ftpClient.setControlKeepAliveReplyTimeout(timeoutMsec);
            ftpClient.setDataTimeout(timeoutMsec);

            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            InputStream inputStream = ftpClient.retrieveFileStream(uri.getPath());

            iorString = IOUtils.toString(inputStream, StandardCharsets.ISO_8859_1.name());
            //Remove leading/trailing whitespace as the CORBA init can't handle that.
            iorString = iorString.trim();
        }
    } catch (Exception e) {
        LOGGER.warn("{} : Error retrieving IOR file for {}. {}", id, iorUrl, e.getMessage());
        LOGGER.debug("{} : Error retrieving IOR file for {}.", id, iorUrl, e);
    }
}

From source file:org.ecoinformatics.datamanager.download.DownloadHandler.java

/**
 * Gets content from given source and writes it to DataStorageInterface 
 * to store them. This method will be called by run()
 * // w  w w.  ja  va 2  s  .  c o m
 * @param resourceName  the URL to the source data to be retrieved
 */
protected boolean getContentFromSource(String resourceName) {
    boolean successFlag = false;
    QualityCheck onlineURLsQualityCheck = null;
    boolean onlineURLsException = false; // used to determine status of onlineURLs quality check

    if (resourceName != null) {
        resourceName = resourceName.trim();
    }

    if (resourceName != null && (resourceName.startsWith("http://") || resourceName.startsWith("https://")
            || resourceName.startsWith("file://") || resourceName.startsWith("ftp://"))) {
        // get the data from a URL
        int responseCode = 0;
        String responseMessage = null;

        try {
            URL url = new URL(resourceName);
            boolean isFTP = false;

            if (entity != null) {
                String contentType = null;

                // Find the right MIME type and set it as content type
                if (resourceName.startsWith("http")) {
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("HEAD");
                    httpURLConnection.connect();
                    contentType = httpURLConnection.getContentType();
                    responseCode = httpURLConnection.getResponseCode();
                    responseMessage = httpURLConnection.getResponseMessage();
                } else if (resourceName.startsWith("file")) {
                    URLConnection urlConnection = url.openConnection();
                    urlConnection.connect();
                    contentType = urlConnection.getContentType();
                } else { // FTP
                    isFTP = true;
                    contentType = "application/octet-stream";
                }

                entity.setUrlContentType(contentType);
            }

            if (!isFTP) { // HTTP(S) or FILE
                InputStream filestream = url.openStream();

                try {
                    successFlag = this.writeRemoteInputStreamIntoDataStorage(filestream);
                } catch (IOException e) {
                    exception = e;
                    String errorMessage = e.getMessage();
                    if (errorMessage.startsWith(ONLINE_URLS_EXCEPTION_MESSAGE)) {
                        onlineURLsException = true;
                    }
                } finally {
                    filestream.close();
                }
            } else { // FTP
                String[] urlParts = resourceName.split("/");
                String address = urlParts[2];
                String dir = "/";
                for (int i = 3; i < urlParts.length - 1; i++) {
                    dir += urlParts[i] + "/";
                }
                String fileName = urlParts[urlParts.length - 1];
                FTPClient ftpClient = new FTPClient();
                ftpClient.connect(address);
                ftpClient.login(ANONYMOUS, anonymousFtpPasswd);
                ftpClient.changeWorkingDirectory(dir);
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode(); // necessary to avoid firewall blocking
                InputStream filestream = ftpClient.retrieveFileStream(fileName);
                try {
                    successFlag = this.writeRemoteInputStreamIntoDataStorage(filestream);
                } catch (IOException e) {
                    exception = e;
                    String errorMessage = e.getMessage();
                    if (errorMessage.startsWith(ONLINE_URLS_EXCEPTION_MESSAGE)) {
                        onlineURLsException = true;
                    }
                } finally {
                    try {
                        filestream.close();
                    } catch (IOException e) {
                        exception = new DataSourceNotFoundException(String
                                .format("Error closing local file '%s': %s", resourceName, e.getMessage()));
                        onlineURLsException = true;
                    }
                }

                // logout and disconnect if FTP session
                if (resourceName.startsWith("ftp") && ftpClient != null) {
                    try {
                        ftpClient.enterLocalActiveMode();
                        ftpClient.logout();
                        ftpClient.disconnect();
                    } catch (IOException e) {
                        exception = new DataSourceNotFoundException(
                                String.format("Error disconnecting from FTP with resource '%s': %s",
                                        resourceName, e.getMessage()));
                        onlineURLsException = true;
                    }
                }
            }
        } catch (MalformedURLException e) {
            String eClassName = e.getClass().getName();
            String eMessage = String.format("%s: %s", eClassName, e.getMessage());
            exception = new DataSourceNotFoundException(
                    String.format("The URL '%s' is a malformed URL: %s", resourceName, eMessage));
        } catch (IOException e) {
            String eClassName = e.getClass().getName();
            String eMessage = String.format("%s: %s", eClassName, e.getMessage());
            if (responseCode > 0) {
                eMessage = String.format("Response Code: %d %s; %s", responseCode, responseMessage, eMessage);
            }
            exception = new DataSourceNotFoundException(
                    String.format("The URL '%s' is not reachable: %s", resourceName, eMessage));
        }

        // Initialize the "Online URLs are live" quality check
        String qualityCheckIdentifier = "onlineURLs";
        QualityCheck qualityCheckTemplate = QualityReport.getQualityCheckTemplate(qualityCheckIdentifier);
        onlineURLsQualityCheck = new QualityCheck(qualityCheckIdentifier, qualityCheckTemplate);

        if (QualityCheck.shouldRunQualityCheck(entity, onlineURLsQualityCheck)) {
            String resourceNameEscaped = embedInCDATA(resourceName);

            if (!onlineURLsException) {
                onlineURLsQualityCheck.setStatus(Status.valid);
                onlineURLsQualityCheck.setFound("true");
                onlineURLsQualityCheck.setExplanation("Succeeded in accessing URL: " + resourceNameEscaped);
            } else {
                onlineURLsQualityCheck.setFailedStatus();
                onlineURLsQualityCheck.setFound("false");
                String explanation = "Failed to access URL: " + resourceNameEscaped;
                explanation = explanation + "; " + embedInCDATA(exception.getMessage());
                onlineURLsQualityCheck.setExplanation(explanation);
            }

            entity.addQualityCheck(onlineURLsQualityCheck);
        }

        return successFlag;
    } else if (resourceName != null && resourceName.startsWith("ecogrid://")) {
        // get the docid from url
        int start = resourceName.indexOf("/", 11) + 1;
        //log.debug("start: " + start);
        int end = resourceName.indexOf("/", start);

        if (end == -1) {
            end = resourceName.length();
        }

        //log.debug("end: " + end);
        String ecogridIdentifier = resourceName.substring(start, end);
        // pass this docid and get data item
        //System.out.println("the endpoint is "+ECOGRIDENDPOINT);
        //System.out.println("The identifier is "+ecogridIdentifier);
        //return false;
        return getContentFromEcoGridSource(ecogridEndPoint, ecogridIdentifier);
    } else if (resourceName != null && resourceName.startsWith("srb://")) {
        // get srb docid from the url
        String srbIdentifier = transformSRBurlToDocid(resourceName);
        // reset endpoint for srb (This is hack we need to figure ou
        // elegent way to do this
        //mEndPoint = Config.getValue("//ecogridService/srb/endPoint");
        // pass this docid and get data item
        //log.debug("before get srb data");
        return getContentFromEcoGridSource(SRBENDPOINT, srbIdentifier);
    } else {
        successFlag = false;
        return successFlag;
    }
}

From source file:org.gitools.datasources.obo.OBOStream.java

public OBOStream(URL baseUrl) throws IOException {
    this.baseUrl = baseUrl;

    // Workaround for FTP problem connecting ftp.geneontology.org with URL.openStream()
    if (baseUrl.getProtocol().equalsIgnoreCase("ftp")) {
        FTPClient ftp = new FTPClient();
        if (baseUrl.getPort() != -1) {
            ftp.connect(baseUrl.getHost(), baseUrl.getPort());
        } else {//from w w  w. ja v  a2 s  . co m
            ftp.connect(baseUrl.getHost());
        }
        ftp.login("anonymous", "");
        ftp.enterLocalPassiveMode();
        ftp.setControlKeepAliveTimeout(60);
        InputStream is = ftp.retrieveFileStream(baseUrl.getPath());
        this.reader = new BufferedReader(new InputStreamReader(is));
    } else {
        this.reader = new BufferedReader(new InputStreamReader(baseUrl.openStream()));
    }

    this.linePos = 0;
}

From source file:org.glassfish.common.util.admin.MapInjectionResolver.java

private void FormatUriToFile() throws IOException {
    List<String> value = parameters.get("DEFAULT");
    String uri = value.get(0);/*w ww  . j av  a2s  .  c o  m*/
    URL url = new URL(uri);
    File file = null;

    if (uri.startsWith("file:/")) {
        file = new File(url.getFile());
    } else if (uri.startsWith("http://")) {
        InputStream inStream = url.openStream();
        BufferedInputStream bufIn = new BufferedInputStream(inStream);

        file = new File(System.getenv().get("TEMP") + uri.substring(uri.lastIndexOf("/")));
        if (file.exists()) {
            file.delete();
        }
        OutputStream out = new FileOutputStream(file);
        BufferedOutputStream bufOut = new BufferedOutputStream(out);
        byte buffer[] = new byte[204800];
        while (true) {
            int nRead = bufIn.read(buffer, 0, buffer.length);
            if (nRead <= 0)
                break;
            bufOut.write(buffer, 0, nRead);
        }
        bufOut.flush();
        out.close();
        inStream.close();
    } else if (uri.startsWith("ftp://")) {
        String pattern = "^ftp://(.+?)(:.+?)?@(\\S+):(\\d+)(\\S+)$";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(uri);
        if (m.matches()) {
            String username = m.group(1);
            String password = "";
            if (m.group(2) != null) {
                password = m.group(2).replace(":", "");
            }
            String ipAddress = m.group(3);
            String port = m.group(4);
            String path = m.group(5);
            FTPClient ftp = new FTPClient();
            ftp.connect(ipAddress);
            ftp.setDefaultPort(Integer.parseInt(port));
            boolean isLogin = ftp.login(username, password);
            if (isLogin) {
                ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
                byte[] buf = new byte[204800];
                int bufsize = 0;

                file = new File(System.getenv().get("TEMP") + uri.substring(uri.lastIndexOf("/")));
                if (file.exists()) {
                    file.delete();
                }
                OutputStream ftpOut = new FileOutputStream(file);
                InputStream ftpIn = ftp.retrieveFileStream(path);
                System.out.println(ftpIn);
                while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) {
                    ftpOut.write(buf, 0, bufsize);
                }
                ftpOut.flush();
                ftpOut.close();
                ftpIn.close();
            } else {
                ftp.logout();
                ftp.disconnect();
            }
        } else {
            localStrings.getLocalString("IncorrectFtpAddress",
                    "The ftp address is not correct, please change another one.");
        }
    }
    if (file != null)
        parameters.set("DEFAULT", file.getAbsolutePath());
}

From source file:org.gogpsproject.parser.rinex.RinexNavigation.java

private RinexNavigationParser getFromFTP(String url) throws IOException {
    RinexNavigationParser rnp = null;//  w w w . j av a  2 s.  c o  m

    String origurl = url;
    if (negativeChache.containsKey(url)) {
        if (System.currentTimeMillis() - negativeChache.get(url).getTime() < 60 * 60 * 1000) {
            throw new FileNotFoundException("cached answer");
        } else {
            negativeChache.remove(url);
        }
    }

    String filename = url.replaceAll("[ ,/:]", "_");
    if (filename.endsWith(".Z"))
        filename = filename.substring(0, filename.length() - 2);
    File rnf = new File(RNP_CACHE, filename);

    if (!rnf.exists()) {
        System.out.println(url + " from the net.");
        FTPClient ftp = new FTPClient();

        try {
            int reply;
            System.out.println("URL: " + url);
            url = url.substring("ftp://".length());
            String server = url.substring(0, url.indexOf('/'));
            String remoteFile = url.substring(url.indexOf('/'));
            String remotePath = remoteFile.substring(0, remoteFile.lastIndexOf('/'));
            remoteFile = remoteFile.substring(remoteFile.lastIndexOf('/') + 1);

            ftp.connect(server);
            ftp.login("anonymous", "info@eriadne.org");

            System.out.print(ftp.getReplyString());

            // After connection attempt, you should check the reply code to
            // verify
            // success.
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                System.err.println("FTP server refused connection.");
                return null;
            }

            System.out.println("cwd to " + remotePath + " " + ftp.changeWorkingDirectory(remotePath));
            System.out.println(ftp.getReplyString());
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            System.out.println(ftp.getReplyString());

            System.out.println("open " + remoteFile);
            InputStream is = ftp.retrieveFileStream(remoteFile);
            InputStream uis = is;
            System.out.println(ftp.getReplyString());
            if (ftp.getReplyString().startsWith("550")) {
                negativeChache.put(origurl, new Date());
                throw new FileNotFoundException();
            }

            if (remoteFile.endsWith(".Z")) {
                uis = new UncompressInputStream(is);
            }

            rnp = new RinexNavigationParser(uis, rnf);
            rnp.init();
            is.close();

            ftp.completePendingCommand();

            ftp.logout();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                    // do nothing
                }
            }
        }
    } else {
        System.out.println(url + " from cache file " + rnf);
        rnp = new RinexNavigationParser(rnf);
        rnp.init();
    }
    return rnp;
}