开发带jsp、PluginServlet的插件

有些插件是单纯的继承Plugin或Handler什么的,但有些是需要jsp页面和Servlet的。下面我们就来开发带jsp和servlet的插件。

在之前的目录下添加文件,目录结构如下:

1、 首先建立一个SampleServlet的文件,内容如下

  1. package com.hoo.server.plugin;  
  2.    
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9.    
  10. /**  
  11.  * <b>function:</b> sample servlet  
  12.  * @author hoojo  
  13.  * @createDate 2013-3-4 下午04:15:20  
  14.  * @file SampleServlet.java  
  15.  * @package com.hoo.server.plugin  
  16.  * @project OpenfirePlugin  
  17.  * @blog http://blog.csdn.net/IBM_hoojo  
  18.  * @email hoojo_@126.com  
  19.  * @version 1.0  
  20.  */ 
  21. public class SampleServlet extends HttpServlet {  
  22.       
  23.     private static final long serialVersionUID = -5404916983906926869L;  
  24.    
  25.     @Override 
  26.     public void init() throws ServletException {  
  27.         super.init();  
  28.     }  
  29.       
  30.     @Override 
  31.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  32.         super.doGet(request, response);  
  33.           
  34.         response.setContentType("text/plain");  
  35.         PrintWriter out = response.getWriter();  
  36.         System.out.println("请求SampleServlet GET Method");  
  37.         out.print("请求SampleServlet GET Method");  
  38.         out.flush();  
  39.     }  
  40.    
  41.     @Override 
  42.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  43.         super.doPost(request, response);  
  44.           
  45.         response.setContentType("text/plain");  
  46.         PrintWriter out = response.getWriter();  
  47.         System.out.println("请求SampleServlet GET Method");  
  48.         out.print("请求SampleServlet POST Method");  
  49.         out.flush();  
  50.     }  
  51.    
  52.     @Override 
  53.     public void destroy() {  
  54.         super.destroy();  
  55.     }  
  56. }  

2、 在当前插件根目录添加web目录,在目录下建立WEB-INF目录,添加web-custom.xml文件(文件名应该是固定的)。在里面配置我们的servlet。

  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  
  3. <web-app>  
  4.       
  5.     <servlet>  
  6.         <servlet-class>com.hoo.server.plugin.SampleServlet</servlet-class>  
  7.         <servlet-name>SampleServlet</servlet-name>  
  8.     </servlet>  
  9.       
  10.     <servlet-mapping>  
  11.         <servlet-name>SampleServlet</servlet-name>  
  12.         <url-pattern>/servlet</url-pattern>  
  13.     </servlet-mapping>  
  14. </web-app>  

当插件发布后你可以通过用:http://127.0.0.1:9090/plugins/sample/servlet 就可以访问到这个servlet了。但我发现我们只需用http://127.0.0.1:9090/plugins/sample也是可以访问到的。好像openfire会自动找到我们插件目录下的servlet配置。

注意:这里的http://127.0.0.1:9090/plugins/是固定的,至少plugins是固定的。所有的插件都在plugins目录下访问的。如果你想知道为什么,你可以看看openfire源码下的web.xml,具体目录路径在/openfire/src/web/WEB-INF/web.xml。里面有一个PluginServlet是过来plugin的配置的。

3、 在web目录下添加jsp文件,文件名是插件名称-自定义名称.jsp(建议规范命名)

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  3. <html
  4.   <head
  5.     <title>hello world: 你好openfire</title
  6.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
  7.     <meta name="pageID" content="sample-service"/> 
  8.   </head
  9.     
  10.   <body
  11.     <h3>hello world jsp!! <href="/plugins/sample/servlet">SampleServlet</a></h3
  12.     <div class="jive-contentBoxHeader">jive-contentBoxHeader</div
  13.     <div class="jive-contentBox">jive-contentBox</div
  14.       
  15.     <div class="jive-table"
  16.         <table cellpadding="0" cellspacing="0" border="0" width="100%"
  17.             <thead
  18.                 <tr
  19.                     <th>&nbsp;sss</th
  20.                     <th nowrap>a</th
  21.                     <th nowrap>b</th
  22.                 </tr
  23.             </thead
  24.             <tbody
  25.                 <tr
  26.                     <td align="center">asdf</td
  27.                     <td align="center">asdf</td
  28.                     <td align="center">asdf</td
  29.                 </tr
  30.                 <tr class="jive-even"
  31.                        <td align="center">asdf</td
  32.                     <td align="center">asdf</td
  33.                     <td align="center">asdf</td
  34.                 </tr
  35.                 <tr class="jive-odd"
  36.                        <td align="center">asdf</td
  37.                     <td align="center">asdf</td
  38.                     <td align="center">asdf</td
  39.                 </tr
  40.              </tbody
  41.         </table
  42.     </div
  43.   </body
  44. </html

其中最重要的一点就是:<meta name="pageID" content="sample-service"/>这个pageID。这里的是固定的,后面的content对应我们plugin.xml的内容(等下看看plguin.xml的配置)。然后可以适当的看下里面table的 属性和样式,因为很多时候会在jsp中显示内容,且用table布局的。

4、 改下之前的plugin.xml的配置,配置组件在openfire 管理员控制台的哪个地方显示,以及显示的页面。

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <plugin
  3.     <!-- Main plugin class  这里是最重要滴,就是你的插件的全路径--> 
  4.     <class>com.hoo.server.plugin.SamplePlugin</class
  5.    
  6.     <!-- Plugin meta-data --> 
  7.     <name>SimplePlugin</name
  8.     <description>This is the my sample plugin.</description
  9.     <author>hoojo</author
  10.    
  11.     <version>1.0</version
  12.     <date>28/02/2013</date
  13.     <url>http://localhost:9090/openfire/plugins.jsp</url
  14.     <minServerVersion>3.4.1</minServerVersion
  15.     <licenseType>gpl</licenseType
  16.    
  17.     <adminconsole>      
  18.         <tab id="tab-server"
  19.             <sidebar id="sidebar-server-settings"
  20.                 <item id="sample-service" name="Sample Service" url="sample-service.jsp" 
  21.                      description="Click is trigger sample plugin" /> 
  22.             </sidebar
  23.         </tab
  24.     </adminconsole
  25. </plugin

这里主要就是adminconsole这里面的配置。首先tab-server应该是在管理员控制台页面的服务器菜单中显示;sidebar中的的id配置固定这样写即可;item中的id(sample-service)对应的就是上面的sample-service.jsp的<meta name="pageID" content="sample-service"/>的content内容;item的url对应的是我们写的jsp页面;name是插件的菜单名称。也就是说在管理员控制台页面中的服务器菜单下增加一个sample service的菜单,打开的页面是sample-service.jsp页面。

5、 运行ant脚本,打包发布插件。在上一章节有完整ant脚本的,运行build.xml中的这个openfire-plugins命令即可打包。然后将打好包的sample.jar发布到openfire的plugins目录下即可。

打包后的jar插件目录结构如下:

 

启动openfire后,在openfire管理员控制台页面的服务器->服务器设置中就可以看到Sample Service插件了。

 

点击Sample Servlet就可以看到openfire控制台打印请求的文字信息。