package com.capinfo.servlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.capinfo.util.PageConstraint;
import com.capinfo.util.PigBlogUtil;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
*
* @author Administrator
*
*/
public class PigBlogServlet extends HttpServlet {
protected Log _logger = LogFactory.getLog(PigBlogServlet.class);
/**
* Initialize
*
* @param servletConfig {@link ServletConfig}
* @throws ServletException If there is an error initializing
*/
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
}
/**
* Handle requests made to
*
* @param httpServletRequest {@link HttpServletRequest} request
* @param httpServletResponse {@link HttpServletResponse} response
* @throws ServletException If there is an error serving the request
* @throws IOException If there is an error serving the request
*/
protected void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
// Make sure that we have a request URI ending with a / otherwise we need to
// redirect so that the browser can handle relative link generation
// if (!httpServletRequest.getRequestURI().endsWith("/")) {
// StringBuffer redirectURL = new StringBuffer();
// redirectURL.append(httpServletRequest.getRequestURI());
// redirectURL.append("/");
// if (httpServletRequest.getParameterMap().size() > 0) {
// redirectURL.append("?");
// redirectURL.append(PigBlogUtil.convertRequestParams(httpServletRequest));
// }
//
// if (_logger.isDebugEnabled()) {
// _logger.debug("Redirecting the user to: " + redirectURL.toString());
// }
//
// httpServletResponse.sendRedirect(redirectURL.toString());
//
// return;
// }
// Check for an overriding id
String blogId = httpServletRequest.getParameter(PageConstraint.GO);
if (PigBlogUtil.checkNullOrBlank(blogId)) {
String blogIdFromPath = PigBlogUtil.getBlogFromPath(httpServletRequest.getPathInfo());
if (blogIdFromPath == null) {
blogId = PageConstraint.GO1;
} else {
blogId = blogIdFromPath;
}
}
if (PigBlogUtil.checkNullOrBlank(blogId)) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND, "Blog ID not specified");
return;
}
StringBuffer redirectURL = new StringBuffer();
//redirectURL.append(httpServletRequest.getContextPath());
System.out.println(httpServletRequest.getRequestURI());
if(httpServletRequest.getRequestURI().indexOf("/blog/") > -1 && httpServletRequest.getRequestURI().indexOf(".jsp") == -1 ){
if(!httpServletRequest.getRequestURI().endsWith("/") && httpServletRequest.getRequestURI().indexOf(".do") > -1){
redirectURL.append(httpServletRequest.getRequestURI().substring(httpServletRequest.getRequestURI().lastIndexOf("/"), httpServletRequest.getRequestURI().length()));
}else if(httpServletRequest.getRequestURI().indexOf("/blog/") == -1){
}else{
redirectURL.append("/blogView.do");
}
redirectURL.append("?go=");
redirectURL.append(blogId);
httpServletRequest.getRequestDispatcher(redirectURL.toString())
.forward((ServletRequest)httpServletRequest, (ServletResponse)httpServletResponse);
//httpServletResponse.sendRedirect(redirectURL.toString());
}else{
httpServletRequest.getRequestDispatcher(httpServletRequest.getRequestURI())
.forward((ServletRequest)httpServletRequest, (ServletResponse)httpServletResponse);
//httpServletResponse.sendRedirect(httpServletRequest.getRequestURI());
}
System.out.println(redirectURL.toString());
return;
}
/**
* Take out of service
*/
public void destroy() {
super.destroy();
if (_logger.isDebugEnabled()) {
_logger.debug(" destroyed");
}
}
}