spagobi 引擎调用,跨web应用访问,filter的使用
一、前言
spagobi的引擎都是模块方式加载的,引擎就是一个web
app,比如SpagoBITalendEngine。spagobi门户传递一个document id给talend
engine应用,在filter层初始化session,在业务servlet,即对外开放的服务访问点,从request
session获取相关信息,并execute talend job。
本文根据以上应用,来作一个简化的模型:
1、两个web app,其中一个访问另一个的servelt
2、servlet在执行前,在filter类里初始化session,仅是设置一个attribute。(处理结果写入log日志)
3、在servlet里取到attribute,并返回到页面。(处理结果写入log日志)
附:未模拟单点登录。
二、环境准备
jvm: j2sdk 1.6
web: tomcat 6.0
三、开发
(1)创建两个“dynamic web project”
名称分别为:biweb(主调web app)和biwebengine(被调web app),当然你要先配置好它的web server为tomcat
(2)开发biwebengine
-创建类filters.AccessFilter
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import
javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
public class AccessFilter implements Filter {
public final String
DOCUMENT_ID_PARAM_NAME = "document";
private static transient Logger
logger = Logger
.getLogger(AccessFilter.class);
@Override
public void destroy() {
// TODO
Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
String documentId;
documentId =
(String) request.getParameter(DOCUMENT_ID_PARAM_NAME);
logger.info("Filter documentId from request: " + documentId);
if (request instanceof HttpServletRequest) {
((HttpServletRequest) request).getSession().setAttribute(DOCUMENT_ID_PARAM_NAME,
documentId);
}
chain.doFilter(request, response); //for
other filters
}
@Override
public void init(FilterConfig arg0) throws
ServletException {
// TODO Auto-generated method stub
}
}
-servlet类de.daniel.RunService
package de.daniel;
import java.io.IOException;
import javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class RunService extends HttpServlet{
private static final long
serialVersionUID = 1L;
private static transient Logger logger =
Logger.getLogger(RunService.class);
public final String
DOCUMENT_ID_PARAM_NAME = "document";
@Override
protected void
service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String documentId
=
String.valueOf(request.getSession().getAttribute(DOCUMENT_ID_PARAM_NAME));
logger.info("RunService has gotten attribute from session: " +
documentId);
// setup response header
if(response instanceof HttpServletResponse) {
((HttpServletResponse)response).setHeader("Content-Disposition",
"inline;filename=\"service-response\";");
}
String content = "invoked sucessfully! " + DOCUMENT_ID_PARAM_NAME + " is: " +
documentId;
response.setContentType("text/plain");
response.setContentLength(
content.length() );
if(response instanceof
HttpServletResponse) {
((HttpServletResponse)response).setStatus(200);
}
response.getWriter().print(content);
response.getWriter().flush();
}
@Override
public void init() throws ServletException {
super.init();
}
}
-log4j日志配置log4j.properties
日志将被保存到tomcat/logs下的biwebengine.log文件
log4j.rootLogger=INFO, biwebengine
# SpagoBI
Appender
log4j.appender.biwebengine=org.apache.log4j.RollingFileAppender
log4j.appender.biwebengine.File=${catalina.base}/logs/biwebengine.log
log4j.appender.biwebengine.MaxFileSize=10000KB
log4j.appender.biwebengine.MaxBackupIndex=0
log4j.appender.biwebengine.layout=org.apache.log4j.PatternLayout
log4j.appender.biwebengine.layout.ConversionPattern=%d{DATE}
%5p %c.%M:%L - %m%n
-web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>biwebengine</display-name>
<filter>
<filter-name>Run Service
Filter</filter-name>
<filter-class>filters.AccessFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Run Service
Filter</filter-name>
<url-pattern>/RunService/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>RunService</servlet-name>
<servlet-class>de.daniel.RunService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RunService</servlet-name>
<url-pattern>/RunService</url-pattern>
</servlet-mapping>
</web-app>
(3)开发biweb
只要写一个首页index.jsp就可以了,其它也不用配置:
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd
">
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title
here</title>
</head>
<body>
<form name="form1"
method="post" action="../biwebengine/RunService?document=11">
<input type="submit" value="access biwebengine"/>
</form>
</body>
</html>
四、验证
右击工程,run as -> run on server,访问:http://localhost:8080/biweb/ ,如下图:
点击按钮,跳转到另一个web:
可以看看日志文件里的内容。