protected void onClose()

in commons-vfs2-jackrabbit2/src/main/java/org/apache/commons/vfs2/provider/webdav4/Webdav4FileObject.java [111:205]


        protected void onClose() throws IOException {
            final HttpEntity entity = new ByteArrayEntity(((ByteArrayOutputStream) out).toByteArray());
            final GenericURLFileName fileName = (GenericURLFileName) getName();
            final String urlStr = toUrlString(fileName);
            if (builder.isVersioning(getFileSystem().getFileSystemOptions())) {
                DavPropertySet set = null;
                boolean fileExists = true;
                boolean isCheckedIn = true;
                try {
                    set = getPropertyNames(fileName);
                } catch (final FileNotFoundException fnfe) {
                    fileExists = false;
                }
                if (fileExists && set != null) {
                    if (set.contains(VersionControlledResource.CHECKED_OUT)) {
                        isCheckedIn = false;
                    } else if (!set.contains(VersionControlledResource.CHECKED_IN)) {
                        DavProperty<?> prop = set.get(VersionControlledResource.AUTO_VERSION);
                        if (prop != null) {
                            prop = getProperty(fileName, VersionControlledResource.AUTO_VERSION);
                            if (DeltaVConstants.XML_CHECKOUT_CHECKIN.equals(prop.getValue())) {
                                createVersion(urlStr);
                            }
                        }
                    }
                }
                if (fileExists && isCheckedIn) {
                    try {
                        final HttpCheckout request = setupRequest(new HttpCheckout(urlStr));
                        // AutoClose the underlying HTTP connection which is held by the response object
                        try (CloseableHttpResponse unused = (CloseableHttpResponse) executeRequest(request)) {
                            isCheckedIn = false;
                        }
                    } catch (final FileSystemException ex) {
                        log(ex);
                    }
                }

                try {
                    final HttpPut request = new HttpPut(urlStr);
                    request.setEntity(entity);
                    setupRequest(request);
                    // AutoClose the underlying HTTP connection which is held by the response object
                    try (CloseableHttpResponse unused = (CloseableHttpResponse) executeRequest(request)) {
                        setUserName(fileName, urlStr);
                    }
                } catch (final FileSystemException ex) {
                    if (!isCheckedIn) {
                        try {
                            final HttpCheckin request = new HttpCheckin(urlStr);
                            setupRequest(request);
                            // AutoClose the underlying HTTP connection which is held by the response object
                            try (CloseableHttpResponse unused = (CloseableHttpResponse) executeRequest(request)) {
                                isCheckedIn = true;
                            }
                        } catch (final Exception e) {
                            // Going to throw original.
                            log(e);
                        }
                        throw ex;
                    }
                }
                if (!fileExists) {
                    createVersion(urlStr);
                    try {
                        final DavPropertySet props = getPropertyNames(fileName);
                        isCheckedIn = !props.contains(VersionControlledResource.CHECKED_OUT);
                    } catch (final FileNotFoundException fnfe) {
                        log(fnfe);
                    }
                }
                if (!isCheckedIn) {
                    final HttpCheckin request = new HttpCheckin(urlStr);
                    setupRequest(request);
                    // AutoClose the underlying HTTP connection which is held by the response object
                    try (CloseableHttpResponse unused = (CloseableHttpResponse) executeRequest(request)) {
                        isCheckedIn = true;
                    }
                }
            } else {
                final HttpPut request = new HttpPut(urlStr);
                request.setEntity(entity);
                setupRequest(request);
                // AutoClose the underlying HTTP connection which is held by the response object
                try (CloseableHttpResponse unused = (CloseableHttpResponse) executeRequest(request)) {
                    try {
                        setUserName(fileName, urlStr);
                    } catch (final IOException e) {
                        // Unable to set the user name.
                        log(e);
                    }
                }
            }
            ((DefaultFileContent) file.getContent()).resetAttributes();
        }