关闭

REST:Atom的开源框架Apache Abdera

330人阅读 评论(0) 收藏 举报
分类:

     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

0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:252223次
    • 积分:2089
    • 等级:
    • 排名:第18219名
    • 原创:44篇
    • 转载:72篇
    • 译文:5篇
    • 评论:9条
    文章分类
    最新评论