Jackrabbit-webdav接口调用Example

这几天在用Jackrabbit-webdav调用nextcloud提供的服务,发现apache网站也没有给出明确的调用example,而网上大部分的example是基于1.6.5的,结合github上源码的tests内容加上自己的实践,简单的总结了一个jackrabbit-webdav-2.18.3的调用例子,供有需要的同学参考。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.jackrabbit.webdav.DavConstants;
import org.apache.jackrabbit.webdav.DavException;
import org.apache.jackrabbit.webdav.MultiStatus;
import org.apache.jackrabbit.webdav.MultiStatusResponse;
import org.apache.jackrabbit.webdav.client.methods.HttpMkcol;
import org.apache.jackrabbit.webdav.client.methods.HttpPropfind;
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
import org.apache.jackrabbit.webdav.version.DeltaVConstants;
import org.apache.log4j.Logger;

public class WebDavJackRabbitUtil {
	private static Logger logger = Logger.getLogger(WebDavJackRabbitUtil.class);

	private String username, password;
	private URI uri;
	private String root;
	private HttpClient client;
	private HttpClientContext context;

	public WebDavJackRabbitUtil(String baseUri, String userName, String passWord) {
		this.uri = URI.create(baseUri);
		this.root = this.uri.toASCIIString();
		if (!this.root.endsWith("/")) {
			this.root += "/";
		}
		this.username = userName;
		this.password = passWord;

		PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
		HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort());

		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		UsernamePasswordCredentials upc = new UsernamePasswordCredentials(this.username, this.password);
		credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), upc);

		AuthCache authCache = new BasicAuthCache();
		// Generate BASIC scheme object and add it to the local auth cache
		BasicScheme basicAuth = new BasicScheme();
		authCache.put(targetHost, basicAuth);

		// Add AuthCache to the execution context
		this.context = HttpClientContext.create();
		this.context.setCredentialsProvider(credsProvider);
		this.context.setAuthCache(authCache);
		this.client = HttpClients.custom().setConnectionManager(cm).build();

	}

	public void delete(String uri) throws IOException {
		HttpDelete delete = new HttpDelete(uri);
		int status = this.client.execute(delete, this.context).getStatusLine().getStatusCode();
		logger.info("Delete " + uri + " status is :" + status);
	}

	public void upload(String uri, FileInputStream fis) throws IOException {
		HttpPut put = new HttpPut(uri);
		InputStreamEntity requestEntity = new InputStreamEntity(fis);
		put.setEntity(requestEntity);
		int status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
		logger.info("Upload " + uri + " status is :" + status);
	}

	public void mkdir(String uri) throws IOException {
		HttpMkcol mkcol = new HttpMkcol(uri);
		int status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
		logger.info("Create folder " + uri + " status is :" + status);
	}

	public void download(String uri) throws IOException {
		HttpGet get = new HttpGet(uri);
		HttpResponse execRel = this.client.execute(get, this.context);
		StatusLine status = execRel.getStatusLine();
		HttpEntity resp = execRel.getEntity();
		transStream2File(resp.getContent(), "./2.png");
		logger.info("Download " + uri + " status is :" + status);
	}

	public MultiStatusResponse[] propfind(String testuri) throws IOException {
		MultiStatusResponse[] responses = null;
		try {
			DavPropertyNameSet names = new DavPropertyNameSet();
			names.add(DeltaVConstants.COMMENT);
			// DavConstants.DEPTH_1
			HttpPropfind propfind = new HttpPropfind(testuri, DavConstants.PROPFIND_ALL_PROP_INCLUDE, names,
					DavConstants.DEPTH_1);
			HttpResponse resp = this.client.execute(propfind, this.context);
			int status = resp.getStatusLine().getStatusCode();
			logger.info("List file " + uri + " status is :" + status);
			// assertEquals(207, status);
			MultiStatus multistatus;
			multistatus = propfind.getResponseBodyAsMultiStatus(resp);
			responses = multistatus.getResponses();
		} catch (DavException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return responses;
	}

	public void transStream2File(InputStream is, String fileName) throws IOException {
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		in = new BufferedInputStream(is);
		out = new BufferedOutputStream(new FileOutputStream(fileName));
		int len = -1;
		byte[] b = new byte[1024];
		while ((len = in.read(b)) != -1) {
			out.write(b, 0, len);
		}
		in.close();
		out.close();
	}
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提示错误[ERROR] [ERROR] Some problems were encountered while processing the POMs: [ERROR] Unresolveable build extension: Plugin org.apache.maven.wagon:wagon-webdav-jackrabbit:1.0-beta-6 or one of its dependencies could not be resolved: The following artifacts could not be resolved: commons-httpclient:commons-httpclient:jar:3.1 (absent): Could not transfer artifact commons-httpclient:commons-httpclient:jar:3.1 from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443 [repo.maven.apache.org/146.75.112.215] failed: connect timed out @ @ [ERROR] The build could not read 1 project -> [Help 1] [ERROR] [ERROR] The project org.drools:droolsjbpm-integration:7.74.0-SNAPSHOT (D:\droolsjbpm-integration-main\droolsjbpm-integration-main\pom.xml) has 1 error [ERROR] Unresolveable build extension: Plugin org.apache.maven.wagon:wagon-webdav-jackrabbit:1.0-beta-6 or one of its dependencies could not be resolved: The following artifacts could not be resolved: commons-httpclient:commons-httpclient:jar:3.1 (absent): Could not transfer artifact commons-httpclient:commons-httpclient:jar:3.1 from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443 [repo.maven.apache.org/146.75.112.215] failed: connect timed out -> [Help 2] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException [ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/PluginManagerException
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值