Restlet 2.0 边学边写(三)使用Component发布多个Application

很久没更新这篇博客了,今天继续。

上一次实践是一个Application绑定多个Resource。但是如果Resource多了以后使用一个Application来发布并不合适,而且绑定地址写在代码中也不便于修改。那么应该怎么办呢?
可以使用Component来发布多个Application,每个Application只负责发布自己的Resource。使用Component有两种方法:可以自定义Component类并在web.xml中配置;也可以使用restlet.xml来配置。

参考:[url]http://ajaxcn.iteye.com/blog/416611[/url]

本次实践将创建一个Application:CustomerApplication;一个Component:OrderComponent,一个配置文件:restlet.xml。

[b]1.Application[/b]
在com.sunny.restlet.order包中创建CustomerApplication类,代码如下:

package com.sunny.restlet.order;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class CustomerApplication extends Application {

@Override
public Restlet createRoot() {
// TODO Auto-generated method stub
Router router = new Router(getContext());
router.attach("", CustomerResource.class);
return router;
}

}

类中将CustomerResource绑定到""路径上。

修改com.sunny.restlet.order包中OrderApplication类,代码如下:

package com.sunny.restlet.order;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;


public class OrderApplication extends Application {

@Override
public synchronized Restlet createRoot() {
Router router = new Router(getContext());
router.attach("", OrderResource.class); return router;

}
}

类中删除CustomerResource的发布路径,并将OrderResource绑定到""路径上。

[b]2.Component[/b]
在com.sunny.restlet.order包中创建OrderComponent类,代码如下:

package com.sunny.restlet.order;

import org.restlet.Component;

public class OrderComponent extends Component {

public OrderComponent() {
super();
// TODO Auto-generated constructor stub
getDefaultHost()
.attach("/orders/{orderId}/{subOrderId}", new OrderApplication());
getDefaultHost().attach("/customers/{custId}",
new CustomerApplication());
}

}

OrderComponent将OrderApplication和CustomerApplication发布到新的路径上。

[b]3.Main[/b]
修改com.sunny.restlet.order包中OrderMain类,代码如下:

package com.sunny.restlet.order;

import org.restlet.Component;
import org.restlet.data.Protocol;

public class OrderMain {
public static void main(String[] args) throws Exception {
Component component = new OrderComponent();
component.getServers().add(Protocol.HTTP, 8182);
component.start();
}
}

类中将使用OrderComponent来发布资源。

[b]4.运行[/b]
运行com.sunny.restlet.order包中OrderMain类。
通过浏览器访问[url]http://localhost:8182/customers/1[/url]可以看到提示信息
[list]
[*]get customer id :1
[/list]
访问[url]http://localhost:8182/orders/1/2[/url]可以看到提示信息
[list]
[*]the order id is : 1 and the sub order id is : 2
[/list]
证明使用Component发布Application和两个Resource成功。

[b]5.web.xml[/b]
将web.xml中的

<!-- OrderApplication -->
<init-param>
<param-name>org.restlet.component</param-name>
<param-value>com.sunny.restlet.order.OrderApplication</param-value>
</init-param>

替换为

<!-- OrderComponent -->
<init-param>
<param-name>org.restlet.component</param-name>
<param-value>com.sunny.restlet.order.OrderComponent</param-value>
</init-param>

配置文件中将使用OrderComponent来发布资源。

[b]6.运行[/b]
重新部署后(Eclipse中修改web.xml会自动重新部署,稍后即可),通过浏览器访问[url]http://localhost:8080/firstSteps/customers/1[/url]可以看到提示信息
[list]
[*]get customer id :1
[/list]
访问[url]http://localhost:8080/firstSteps/orders/1/2[/url]可以看到提示信息
[list]
[*]the order id is : 1 and the sub order id is : 2
[/list]
说明在Servlet容器中使用Component类发布Application和两个Resource成功。

[b]7.web.xml[/b]
将web.xml中以下代码注释掉

<!-- OrderComponent -->
<init-param>
<param-name>org.restlet.component</param-name>
<param-value>com.sunny.restlet.order.OrderComponent</param-value>
</init-param>

重新部署后访问页面,报404错误,说明找不到页面,后台创建ServerServlet时出错。
参考org.restlet.ext.servlet.jar库中org.restlet.ext.servlet.ServerServlet类的代码

protected Component createComponent()
{
Component component = null;
Client warClient = createWarClient(new Context(), getServletConfig());
Response response = warClient.handle(new Request(Method.GET, "war:///WEB-INF/restlet.xml"));
if(response.getStatus().isSuccess() && response.isEntityAvailable())
component = new Component(response.getEntity());
if(component == null)
{
String componentClassName = getInitParameter("org.restlet.component", null);
if(componentClassName != null)

restlet创建Component时先读取WEB-INF/restlet.xml文件中配置,读取失败才从web.xml中读取org.restlet.component这个initParameter。

[b]8.restlet.xml[/b]
在WEB-INF/目录下创建restlet.xml文件,代码如下:
[code]
<?xml version="1.0"?>
<component xmlns="http://www.restlet.org/schemas/1.1/Component"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.restlet.org/schemas/1.1/Component">

<defaultHost>
<attach uriPattern="/customers/{custId}"
targetClass="com.sunny.restlet.order.CustomerApplication" />
<attach uriPattern="/orders/{orderId}/{subOrderId}"
targetClass="com.sunny.restlet.order.OrderApplication" />
</defaultHost>
</component>
[/code]
文件中将两个Application直接绑定到原有路径上,并不需要创建Component类。

[b]9.运行[/b]
重新部署后,通过浏览器访问[url]http://localhost:8080/firstSteps/customers/1[/url]可以看到提示信息
[list]
[*]get customer id :1
[/list]
访问[url]http://localhost:8080/firstSteps/orders/1/2[/url]可以看到提示信息
[list]
[*]the order id is : 1 and the sub order id is : 2
[/list]
说明在Servlet容器中使用restlet.xml发布Application和两个Resource成功。

[b]10.进一步思考[/b]
如果我们想要访问某个客户的某次订单中的单件商品,例如[url]http://localhost:8080/firstSteps/customers/1/orders/1/2[/url]这种资源时,又该如何配置呢?
我们可以将CustomerApplication发布到"/customers/{custId}",将CustomerResource发布到"("/orders/{orderId}/{subOrderId}"路径上,通过路径匹配来实现此项功能。

修改com.sunny.restlet.order包中的CustomerApplication类,代码如下:

package com.sunny.restlet.order;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class CustomerApplication extends Application {

@Override
public Restlet createRoot() {
// TODO Auto-generated method stub
Router router = new Router(getContext());
router.attach("", CustomerResource.class);
router.attach("/orders/{orderId}/{subOrderId}", CustomerResource.class);
return router;
}

}

类中将CustomerResource[b]再次[/b]绑定到"("/orders/{orderId}/{subOrderId}"路径上。

修改com.sunny.restlet.order包中的CustomerResource类,代码如下:

package com.sunny.restlet.order;

import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

public class CustomerResource extends ServerResource {
String customerId = "";
String orderId = "";
String subOrderId = "";

@Override
protected void doInit() throws ResourceException {
this.customerId = (String) getRequest().getAttributes().get("custId");
this.orderId = (String) getRequest().getAttributes().get("orderId");
this.subOrderId = (String) getRequest().getAttributes().get(
"subOrderId");
}

@Get
public String represent() {
return "get customer id :" + customerId + " and the order id is : " + orderId + " and the sub order id is : "
+ subOrderId;
}

}

类中将读取orderId和subOrderId属性。

重新部署后,通过浏览器访问[url]http://localhost:8080/firstSteps/customers/1/orders/1/2[/url]可以看到提示信息
[list]
[*]get customer id :1 and the order id is : 1 and the sub order id is : 2
[/list]
说明路径匹配成功。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值