public class TestFilter extends HttpServlet implements Filter {
private static final long serialVersionUID = 1L;
FilterConfig config;
/**
* Default constructor.
*/
public TestFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
if (((HttpServletRequest) request).getHeader("cookie") != null) {
if (((HttpServletRequest) request).getHeader("cookie").indexOf("e_user") != -1) {
if (request.getParameter("communityUUID") != null) {
String communityUUID = request.getParameter("communityUUID");
((HttpServletRequest) request).getSession().setAttribute("communityUUID", communityUUID);
}
File file = new File(System.getProperty("user.dir") + "//xiaoifHeader.txt");
FileWriter fw = new FileWriter(file, true);
fw.write(new Date().toLocaleString() + "!!!!!!!!!!!!");
fw.write("/r/n");
fw.write("getRequestURL = " + ((HttpServletRequest) request).getRequestURL());
fw.write("/r/n");
Enumeration en = ((HttpServletRequest) request).getSession().getAttributeNames();
while (en.hasMoreElements()) {
String st = (String) en.nextElement();
fw.write("SessionAttributeNames: " + st + " = " + ((HttpServletRequest) request).getSession().getAttribute(st));
fw.write("/r/n");
}
fw.write("ContextPath = " + ((HttpServletRequest) request).getSession().getServletContext().getContextPath());
fw.write("/r/n-----------------I'm a happy separate line!------------------/r/n");
fw.flush();
fw.close();
if (((HttpServletRequest) request).getSession().getAttribute("communityUUID") != null) {
String communityUUID = ((HttpServletRequest) request).getSession().getAttribute("communityUUID").toString();
int cookieFlag = 0;
if (((HttpServletRequest) request).getSession().getAttribute("token") == null) {
String[] cookie = ((HttpServletRequest) request).getHeader("cookie").split("[|]");
String uid = "";
for (int i = 0; i < cookie.length; i++) {
if (cookie[i].indexOf("e_user") != -1) {
cookieFlag = 1;
String[] e_user = cookie[i].split(":");
uid = e_user[1].replace("*20", " ").toLowerCase();
((HttpServletRequest) request).getSession().setAttribute("uid", uid);
if (getCommunityRights(uid, communityUUID) == 1) {
((HttpServletRequest) request).getSession().setAttribute("token", communityUUID);
chain.doFilter(request, response);
} else {
((HttpServletRequest) request).getSession().setAttribute("token", "ban");
redirectToErrorPage((HttpServletRequest) request, (HttpServletResponse) response);
}
}
}
if (cookieFlag == 0) {
chain.doFilter(request, response);
}
} else {
if (!((HttpServletRequest) request).getSession().getAttribute("token").toString().equals(communityUUID)) {
redirectToErrorPage((HttpServletRequest) request, (HttpServletResponse) response);
} else {
chain.doFilter(request, response);
}
}
} else {
chain.doFilter(request, response);
}
} else {
chain.doFilter(request, response);
}
} else {
chain.doFilter(request, response);
}
}
/**
* @param request
* @param response
* @throws ServletException
*/
private void redirectToErrorPage(HttpServletRequest request, HttpServletResponse response) throws ServletException {
MessageContext mc = new MessageContextImpl();
setupForErrorPage(request, response, mc, true);
GenerateErrorPage errorPage = new GenerateErrorPage("Finally...error...");
try {
errorPage.invokeImpl(mc);
} catch (PogoException e) {
throw new ServletException(e);
}
}
/**
* @see com.cognos.pogo.transport.PogoServlet
* @param request
* @param response
* @param mc
* @param doGet
*/
private void setupForErrorPage(HttpServletRequest request, HttpServletResponse response, MessageContext mc, boolean doGet) {
BIBusEnvelope requestEnvelope = (BIBusEnvelope) mc.getProperty("request.envelope");
if (requestEnvelope == null) {
requestEnvelope = new BIBusEnvelope();
mc.setProperty("request.envelope", requestEnvelope);
}
PogoResponder responder = (PogoResponder) mc.getProperty("pogo.responder");
if (responder == null) {
String contentType = request.getContentType();
if (doGet || contentType == null) {
requestEnvelope.setDispatcherTransport("html", "true");
responder = new PogoSOAPAttachmentResponder();
} else if (contentType.startsWith("text/xml") || contentType.startsWith("multipart")) {
requestEnvelope.setDispatcherTransport("html", "false");
responder = new PogoSOAPMessageResponder();
} else {
requestEnvelope.setDispatcherTransport("html", "true");
responder = new PogoSOAPAttachmentResponder();
}
mc.setProperty("pogo.responder", responder);
}
if (mc.getProperty("servlet.response") == null)
mc.setProperty("servlet.response", response);
if (mc.getProperty("locale") == null)
mc.setProperty("locale", getUserLocale(request));
if (mc.getProperty("WEB_CONTENT_ROOT") == null) {
String scriptName = request.getHeader("Original-Script-Name");
if (scriptName != null && scriptName.length() > 0) {
String webContentRoot = RequestProcessor.deduceWebContentRoot(scriptName);
mc.setProperty("WEB_CONTENT_ROOT", webContentRoot);
} else {
mc.setProperty("WEB_CONTENT_ROOT", "..");
}
}
}
/**
* @see com.cognos.pogo.transport.PogoServlet
* @param request
* @return
*/
private static Locale getUserLocale(HttpServletRequest request) {
String acceptLang = request.getHeader("accept-language");
if (acceptLang == null || acceptLang.length() == 0)
return Locale.getDefault();
String normalizedLocaleString = ToolkitResources.normalizeLocaleIdentifier(acceptLang);
if (normalizedLocaleString == null || normalizedLocaleString.length() == 0)
return Locale.getDefault();
else
return StringUtils.getLocale(normalizedLocaleString);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
this.config = fConfig;
}
}