REST:Atom的开源框架Apache Abdera

     Apache Abdera的目标是高效的实现Atom Syndication Format(简称Atom)  和 Atom Publishing Protocol(简称AtomPub) 定义的规范,并且功能完整,性能高。

AtomPub Server

Abdera提供classes对应atomPub的basics,详细概念解释见上篇。

  • Services - Provider
  • Workspaces - WorkspaceInfo & WorkspaceManager
  • Collections - CollectionAdapter

引入maven最小依赖

<dependency>
  <groupId>org.apache.abdera</groupId>
  <artifactId>abdera-server</artifactId>
  <version>1.1.1</version>
</dependency>
CollectionAdapter
public class Employee {
     private int id;
     private String name;
     private Date updated;

     public int getId() {
         return id;
     }

     public void setId(int id) {
         this.id = id;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public Date getUpdated() {
         return updated;
     }

     public void setUpdated(Date updated) {
         this.updated = updated;
     }
 }


AtomPub Client
添加maven最小依赖
<dependency>
  <groupId>org.apache.abdera</groupId>
  <artifactId>abdera-client</artifactId>
  <version>1.1.1</version> 
</dependency>
检索资源(GET)
Abdera abdera = new Abdera();
AbderaClient client = new AbderaClient(abdera);
ClientResponse resp = client.get("http://localhost:8080/abdera-server-example/employee");
if (resp.getType() == ResponseType.SUCCESS) {
  Document<Feed> doc = resp.getDocument();
} else {
  // there was an error
}
如果资源不是XML 文档,ClientResponse提供了inputStream
InputStream in = resp.getInputStream();
resp.getContentType();

ClientResponse提供范围headers的方法,比如ETag ,Last-Modified
System.out.println(resp.getEntityTag());
System.out.println(resp.getLastModified());
System.out.println(resp.getContentLocation());
System.out.println(resp.getSlug())

客户端解析Atom Document并且打印entry titles

Abdera abdera = new Abdera();
Parser parser = abdera.getParser();
             
URL url = new URL("http://intertwingly.net/blog/index.atom");
Document<Feed> doc = parser.parse(url.openStream(),url.toString());
Feed feed = doc.getRoot();
System.out.println(feed.getTitle());
for (Entry entry : feed.getEntries()) {
  System.out.println("\t" + entry.getTitle());
}
System.out.println (feed.getAuthor());
创建资源(POST)

AbderaClient client = new AbderaClient(abdera);
 
Entry entry = abdera.newEntry();       
entry.setId(...);
entry.setTitle(...);
entry.setUpdated(...);
entry.setContent(...);
//...
 
ClientResponse resp = client.post("http://localhost:8080/abdera-server-example/employee",entry);
 
if (resp.getType() == ResponseType.SUCCESS) {
  // success
} else {
  // there was an error
}

更新资源(PUT)
AbderaClient client = new AbderaClient(abdera);
 
Entry entry = abdera.newEntry();
// ...
 
ClientResponse resp = client.put("http://www.example.org/collection",entry);
 
if (resp.getType() == ResponseType.SUCCESS) {
  // success
} else {
  // there was an error
}
删除资源
AbderaClient client = new AbderaClient(abdera);
 
ClientResponse resp = client.delete("http://www.example.org/collection");
 
if (resp.getType() == ResponseType.SUCCESS) {
  // success
} else {
  // there was an error
}
使用传统的HTTP方法
Abdera abdera = Abdera.getInstance();
AbderaClient client = new AbderaClient(abdera);
 
RequestEntity entity = ...
client.execute("PATCH", "http://example.org/foo", entity, null);
 
if (resp.getType() == ResponseType.SUCCESS) {
  // success
} else {
  // there was an error
}

请求选项

RequestOptions options = client.getDefaultRequestOptions();
options.setIfMatch(new EntityTag("foo"));
options.setNoCache(true);
 
ClientResponse resp = client.get("http://example.org/foo", options);
 
if (resp.getType() == ResponseType.SUCCESS) {
  // success
} else {
  // there was an error
}
使用SSL
Abdera abdera = new Abdera();
AbderaClient client = new AbderaClient(abdera);
     
// Default trust manager provider registered for port 443
AbderaClient.registerTrustManager();
     
client.get("https://localhost:9080/foo");





未完待续。。。


参考:

https://cwiki.apache.org/confluence/display/ABDERA/AtomPub+Client

http://abdera.apache.org/index.html

猜你喜欢

转载自blog.csdn.net/dongnan591172113/article/details/52368507