Example usage for org.springframework.core NestedIOException getCause

List of usage examples for org.springframework.core NestedIOException getCause

Introduction

In this page you can find the example usage for org.springframework.core NestedIOException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:org.cruk.genologics.api.impl.GenologicsAPIImpl.java

@Override
public void deleteAndRemoveFile(Linkable<GenologicsFile> file) throws IOException {
    if (file == null) {
        throw new IllegalArgumentException("file cannot be null");
    }//ww  w .  jav a  2 s . co  m

    GenologicsFile realFile;
    if (file instanceof GenologicsFile) {
        realFile = (GenologicsFile) file;
        if (realFile.getContentLocation() == null) {
            // Don't know where the actual file is, so fetch to get the full info.
            realFile = retrieve(file.getUri(), GenologicsFile.class);
        }
    } else {
        realFile = retrieve(file.getUri(), GenologicsFile.class);
    }

    URL targetURL = new URL(null, realFile.getContentLocation().toString(), NullURLStreamHandler.INSTANCE);

    if ("sftp".equalsIgnoreCase(targetURL.getProtocol())) {
        logger.info("Deleting file {} from file store on {}", targetURL.getPath(), targetURL.getHost());

        checkFilestoreSet();

        Session<LsEntry> session = filestoreSessionFactory.getSession();
        try {
            session.remove(targetURL.getPath());
        } catch (NestedIOException e) {
            // Don't want things to fail if the file doesn't exist on the file store,
            // just a warning. This handling code deals with this.

            try {
                if (e.getCause() != null) {
                    throw e.getCause();
                } else {
                    // There is an error in line 71 of SftpSession, where instead of the
                    // SftpException being the cause, its own message is appended to the
                    // detail message for the outer exception with a +.
                    // Bug raised with Spring Integrations as issue INT-3954.
                    if ("Failed to remove file: 2: No such file".equals(e.getMessage())) {
                        throw new SftpException(2, e.getMessage());
                    }

                    throw e;
                }
            } catch (SftpException se) {
                // See if it's just a "file not found".
                if (se.id == 2) {
                    logger.warn("File {} does not exist on {}", targetURL.getPath(), targetURL.getHost());
                } else {
                    throw e;
                }
            } catch (Throwable t) {
                throw e;
            }
        } finally {
            session.close();
        }
    } else {
        logger.debug("File {} is not in the file store, so just removing its record.", targetURL.getPath());
    }

    delete(realFile);
}