先上两张class hierarchy图:
[img]http://www.restlet.org/documentation/1.1/images/restlets[/img]
[img]http://www.restlet.org/documentation/1.1/images/representations[/img]
把Restlet及子类理解成处理Http Request的东东,类似Servlet。把Representation及子类理解成Http Response的的展示对象。
(1). 作为Http Client使用。(类似Apache HttpClient,但这不是Restlet主要想提供的东东)
Client client = new Client(Protocol.HTTP);
//将http response打印到控制台
client.get("http://10.68.92.64").getEntity().write(System.out);
//另外的用法,构造request再发请求
Request request = new Request(Method.GET, "http://10.68.92.64");
Response response = client.handle(request);
Representation output = response.getEntity();
output.write(System.out);
(2). 作为Http Server使用。
1. 简单的http server.
//restlet可以理解成Servlet。(Restlet的名称估计来源于Servlet)
//任意请求都返回同样内容.
Restlet restlet = new Restlet() {
public void handle(Request request, Response response) {
response.setEntity("Hello World!", MediaType.TEXT_PLAIN);
}
};
//启动server,可以访问http://localhost:8182
new Server(Protocol.HTTP, 8182, restlet).start();
2. Component, Host, Application与Resource的关系与使用
[img]http://www.restlet.org/documentation/1.1/images/tutorial05[/img]
Component component = new Component();
// 添加host
component.getServers().add(Protocol.HTTP, 8182);
//attach application到host。在application可以attach多个resource。第一个参数对应url中的路径,如果没有表示是根路径"/"
component.getDefaultHost().attach("app", new MyApplication());
//可以直接attach restlet instance
//component.getDefaultHost().attach(restlet);
// Start the component.
component.start();
//MyApplication类
public class MyApplication extends Application {
@Override
public Restlet createRoot() {
Router router = new Router(getContext());
//第一个参数表示资源名,表示对该资源进行操作,url为/app/hello
router.attach("/hello",MyResource.class);
return router;
}
}
(3) 实现Resource
1. 简单的Resouce,只有http get的实现
public class MyResource extends Resource {
public MyResource(Context context, Request request,
Response response) {
super(context, request, response);
getVariants().add(new Variant(MediaType.TEXT_PLAIN));
}
//get "http://host:port/app/hello"将调用该方法
public Representation represent(Variant variant) {
Representation representation = new StringRepresentation(
"hello, world", MediaType.TEXT_PLAIN);
return representation;
}
}
2. 其他http method的实现
//delete
public void removeRepresentations() throws ResourceException {...)
//put
public void storeRepresentation(Representation entity) {...}
//post
public void acceptRepresentation(Representation entity) {...}
(4) Router
MyApplication中已经使用过Router了,它的作用是映射url到restlet或resource
[img]http://www.restlet.org/documentation/1.1/images/tutorial11[/img]
例如:
router.attach("/hello1", restlet1);
router.attach("/hello2", restlet2);
router.attach("/hello3", restlet3);
[img]http://www.restlet.org/documentation/1.1/images/tutorial12[/img]
router.attach("/hello1", MyResource1.class);
router.attach("/hello2", MyResource2.class);
router.attach("/hello3", MyResource3.class);
(5) 将文件系统以web的方式发布
public static final String ROOT_URI = "file:///c:/restlet/docs/api/";
Application application = new Application() {
@Override
public Restlet createRoot() {
return new Directory(getContext(), ROOT_URI);
}
};
component.getDefaultHost().attach("", application);
(6) 给Http Server加上认证的功能
[img]http://www.restlet.org/documentation/1.1/images/tutorial09[/img]
Guard也是Restlet的子类,相当于一个filter,处理request之前处理认证
@Override
public Restlet createRoot() {
Guard guard = new Guard(getContext(),
ChallengeScheme.HTTP_BASIC, "title");
guard.getSecrets().put("user", "password".toCharArray());
Directory directory = new Directory(getContext(), ROOT_URI);
guard.setNext(directory);
return guard;
}
//request增加认证信息
ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;
ChallengeResponse authentication = new ChallengeResponse(scheme,
"user", "password");
request.setChallengeResponse(authentication);
(7) 重定向功能
Router router = new Router(getContext());
String target = "http://www.google.com/search?q={keywords}";
Redirector redirector = new Redirector(getContext(), target,
Redirector.MODE_CLIENT_TEMPORARY);
Route route = router.attach("/search", redirector);
// http://localhost:8182/search?kwd=myKeyword1+myKeyword2
// 重定向到
// http://www.google.com/search?q=myKeyword1%20myKeyword2
route.extractQuery("keywords", "kwd", true);
参考:
http://www.restlet.org/documentation/
[img]http://www.restlet.org/documentation/1.1/images/restlets[/img]
[img]http://www.restlet.org/documentation/1.1/images/representations[/img]
把Restlet及子类理解成处理Http Request的东东,类似Servlet。把Representation及子类理解成Http Response的的展示对象。
(1). 作为Http Client使用。(类似Apache HttpClient,但这不是Restlet主要想提供的东东)
Client client = new Client(Protocol.HTTP);
//将http response打印到控制台
client.get("http://10.68.92.64").getEntity().write(System.out);
//另外的用法,构造request再发请求
Request request = new Request(Method.GET, "http://10.68.92.64");
Response response = client.handle(request);
Representation output = response.getEntity();
output.write(System.out);
(2). 作为Http Server使用。
1. 简单的http server.
//restlet可以理解成Servlet。(Restlet的名称估计来源于Servlet)
//任意请求都返回同样内容.
Restlet restlet = new Restlet() {
public void handle(Request request, Response response) {
response.setEntity("Hello World!", MediaType.TEXT_PLAIN);
}
};
//启动server,可以访问http://localhost:8182
new Server(Protocol.HTTP, 8182, restlet).start();
2. Component, Host, Application与Resource的关系与使用
[img]http://www.restlet.org/documentation/1.1/images/tutorial05[/img]
Component component = new Component();
// 添加host
component.getServers().add(Protocol.HTTP, 8182);
//attach application到host。在application可以attach多个resource。第一个参数对应url中的路径,如果没有表示是根路径"/"
component.getDefaultHost().attach("app", new MyApplication());
//可以直接attach restlet instance
//component.getDefaultHost().attach(restlet);
// Start the component.
component.start();
//MyApplication类
public class MyApplication extends Application {
@Override
public Restlet createRoot() {
Router router = new Router(getContext());
//第一个参数表示资源名,表示对该资源进行操作,url为/app/hello
router.attach("/hello",MyResource.class);
return router;
}
}
(3) 实现Resource
1. 简单的Resouce,只有http get的实现
public class MyResource extends Resource {
public MyResource(Context context, Request request,
Response response) {
super(context, request, response);
getVariants().add(new Variant(MediaType.TEXT_PLAIN));
}
//get "http://host:port/app/hello"将调用该方法
public Representation represent(Variant variant) {
Representation representation = new StringRepresentation(
"hello, world", MediaType.TEXT_PLAIN);
return representation;
}
}
2. 其他http method的实现
//delete
public void removeRepresentations() throws ResourceException {...)
//put
public void storeRepresentation(Representation entity) {...}
//post
public void acceptRepresentation(Representation entity) {...}
(4) Router
MyApplication中已经使用过Router了,它的作用是映射url到restlet或resource
[img]http://www.restlet.org/documentation/1.1/images/tutorial11[/img]
例如:
router.attach("/hello1", restlet1);
router.attach("/hello2", restlet2);
router.attach("/hello3", restlet3);
[img]http://www.restlet.org/documentation/1.1/images/tutorial12[/img]
router.attach("/hello1", MyResource1.class);
router.attach("/hello2", MyResource2.class);
router.attach("/hello3", MyResource3.class);
(5) 将文件系统以web的方式发布
public static final String ROOT_URI = "file:///c:/restlet/docs/api/";
Application application = new Application() {
@Override
public Restlet createRoot() {
return new Directory(getContext(), ROOT_URI);
}
};
component.getDefaultHost().attach("", application);
(6) 给Http Server加上认证的功能
[img]http://www.restlet.org/documentation/1.1/images/tutorial09[/img]
Guard也是Restlet的子类,相当于一个filter,处理request之前处理认证
@Override
public Restlet createRoot() {
Guard guard = new Guard(getContext(),
ChallengeScheme.HTTP_BASIC, "title");
guard.getSecrets().put("user", "password".toCharArray());
Directory directory = new Directory(getContext(), ROOT_URI);
guard.setNext(directory);
return guard;
}
//request增加认证信息
ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC;
ChallengeResponse authentication = new ChallengeResponse(scheme,
"user", "password");
request.setChallengeResponse(authentication);
(7) 重定向功能
Router router = new Router(getContext());
String target = "http://www.google.com/search?q={keywords}";
Redirector redirector = new Redirector(getContext(), target,
Redirector.MODE_CLIENT_TEMPORARY);
Route route = router.attach("/search", redirector);
// http://localhost:8182/search?kwd=myKeyword1+myKeyword2
// 重定向到
// http://www.google.com/search?q=myKeyword1%20myKeyword2
route.extractQuery("keywords", "kwd", true);
参考:
http://www.restlet.org/documentation/