com.github.sardine.impl.SardineException Java Examples

The following examples show how to use com.github.sardine.impl.SardineException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebdavClientImpl.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Check whether remote directory exists.
 * If HEAD used by {@link #exists(String)} fails, GET is used instead.
 * 
 * See <a href="http://code.google.com/p/sardine/issues/detail?id=48">http://code.google.com/p/sardine/issues/detail?id=48</a>
 * and <a href="https://issues.alfresco.com/jira/browse/ALF-7883">https://issues.alfresco.com/jira/browse/ALF-7883</a> 
 * for more information and motivation.
 * 
 * This method should work both for WebDAV and plain HTTP,
 * hence PROPFIND can't be used.
 * 
 * @param url
 *            Path to the directory.
 * @return True if the directory exists.
 * @throws IOException
 */
// CL-2709: The bug in Alfresco has already been fixed.
// As for Jackrabbit, http://koule:22401/repository/ returns 404 both for GET and HEAD
@Override
public boolean dirExists(String url) throws IOException {
	try {
		return exists(url); // first try with HTTP HEAD
	} catch (SardineException se) {
		// https://issues.alfresco.com/jira/browse/ALF-7883
		switch (se.getStatusCode()) {
			case HttpStatus.SC_BAD_REQUEST: // HEAD failed
			case HttpStatus.SC_METHOD_NOT_ALLOWED: // HEAD not allowed
				// try HTTP GET as a fallback
				InputStream is = getIfExists(url, Collections.<String, String>emptyMap());
				if (is == null) {
					return false;
				} else {
					is.close();
					return true;
				}
		}

		throw se;
	}
}
 
Example #2
Source File: DAVExceptionMappingService.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Override
public BackgroundException map(final SardineException failure) {
    final StringBuilder buffer = new StringBuilder();
    switch(failure.getStatusCode()) {
        case HttpStatus.SC_OK:
        case HttpStatus.SC_MULTI_STATUS:
            // HTTP method status
            this.append(buffer, failure.getMessage());
            // Failure unmarshalling XML response
            return new InteroperabilityException(buffer.toString(), failure);
    }
    this.append(buffer, String.format("%s (%d %s)", failure.getReasonPhrase(), failure.getStatusCode(), failure.getResponsePhrase()));
    return super.map(failure, buffer, failure.getStatusCode());
}
 
Example #3
Source File: DAVExceptionMappingServiceTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testMap() {
    Assert.assertEquals(LoginFailureException.class,
        new DAVExceptionMappingService().map(new SardineException("m", 401, "r")).getClass());
    assertEquals(AccessDeniedException.class,
        new DAVExceptionMappingService().map(new SardineException("m", 403, "r")).getClass());
    assertEquals(NotfoundException.class,
        new DAVExceptionMappingService().map(new SardineException("m", 404, "r")).getClass());
}
 
Example #4
Source File: WebdavClientImpl.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Execute PROPFIND with depth 0.
 * 
 * @param url
 * @return
 * @throws IOException
 */
// not used now, but may be useful for file operations
@Override
public DavResource info(String url) throws IOException {
	HttpPropFind entity = new HttpPropFind(url);
	entity.setDepth("0");
	Propfind body = new Propfind();
	body.setAllprop(new Allprop());
	entity.setEntity(new StringEntity(SardineUtil.toXml(body), UTF_8));
	
	Multistatus multistatus = execute(entity, new MultiStatusResponseHandler() {

		@Override
		public Multistatus handleResponse(HttpResponse response) throws SardineException, IOException {
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == HttpStatus.SC_NOT_FOUND)
			{
				return null; // expected response, do not throw an exception
			}
			return super.handleResponse(response);
		}
		
	});
	
	if (multistatus == null) {
		return null;
	}
	
	List<Response> responses = multistatus.getResponse();
	List<DavResource> resources = new ArrayList<DavResource>(responses.size());
	for (Response resp : responses) {
		try {
			resources.add(new DavResource(resp));
		} catch (URISyntaxException e) {
			// Ignore resource with invalid URI
		}
	}
	return !resources.isEmpty() ? resources.get(0) : null;
}
 
Example #5
Source File: WebdavFileSystem.java    From xenon with Apache License 2.0 5 votes vote down vote up
@Override
public void rename(Path source, Path target) throws XenonException {

    LOGGER.debug("move source = {} to target = {}", source, target);

    Path absSource = toAbsolutePath(source);
    Path absTarget = toAbsolutePath(target);

    assertPathExists(absSource);

    if (areSamePaths(absSource, absTarget)) {
        return;
    }

    assertParentDirectoryExists(absTarget);
    assertPathNotExists(absTarget);

    PathAttributes a = getAttributes(absSource);

    try {
        if (a.isDirectory()) {
            client.move(getDirectoryPath(absSource), getDirectoryPath(absTarget), false);
        } else {
            client.move(getFilePath(absSource), getFilePath(absTarget), false);
        }
    } catch (SardineException e) {
        if (e.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY) {
            return;
        }
        throw new XenonException(ADAPTOR_NAME, "Failed to move from " + absSource + " to " + absTarget, e);
    } catch (Exception e1) {
        throw new XenonException(ADAPTOR_NAME, "Failed to move from " + absSource + " to " + absTarget, e1);
    }
}