`
sb33060418
  • 浏览: 150203 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • REST
阅读更多
很久没更新这篇博客了,今天继续。

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

参考:http://ajaxcn.iteye.com/blog/416611

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

1.Application
在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绑定到""路径上。

2.Component
在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发布到新的路径上。

3.Main
修改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来发布资源。

4.运行
运行com.sunny.restlet.order包中OrderMain类。
通过浏览器访问http://localhost:8182/customers/1可以看到提示信息
  • get customer id :1

访问http://localhost:8182/orders/1/2可以看到提示信息
  • the order id is : 1 and the sub order id is : 2

证明使用Component发布Application和两个Resource成功。

5.web.xml
将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来发布资源。

6.运行
重新部署后(Eclipse中修改web.xml会自动重新部署,稍后即可),通过浏览器访问http://localhost:8080/firstSteps/customers/1可以看到提示信息
  • get customer id :1

访问http://localhost:8080/firstSteps/orders/1/2可以看到提示信息
  • the order id is : 1 and the sub order id is : 2

说明在Servlet容器中使用Component类发布Application和两个Resource成功。

7.web.xml
将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。

8.restlet.xml
在WEB-INF/目录下创建restlet.xml文件,代码如下:
<?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>

文件中将两个Application直接绑定到原有路径上,并不需要创建Component类。

9.运行
重新部署后,通过浏览器访问http://localhost:8080/firstSteps/customers/1可以看到提示信息
  • get customer id :1

访问http://localhost:8080/firstSteps/orders/1/2可以看到提示信息
  • the order id is : 1 and the sub order id is : 2

说明在Servlet容器中使用restlet.xml发布Application和两个Resource成功。

10.进一步思考
如果我们想要访问某个客户的某次订单中的单件商品,例如http://localhost:8080/firstSteps/customers/1/orders/1/2这种资源时,又该如何配置呢?
我们可以将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再次绑定到"("/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属性。

重新部署后,通过浏览器访问http://localhost:8080/firstSteps/customers/1/orders/1/2可以看到提示信息
  • get customer id :1 and  the order id is : 1 and the sub order id is : 2

说明路径匹配成功。


分享到:
评论
1 楼 gaofeng_867 2013-02-19  
感谢

相关推荐

Global site tag (gtag.js) - Google Analytics