Skip to content

Instantly share code, notes, and snippets.

@mingliangfeng
Last active December 11, 2015 06:49
Show Gist options
  • Save mingliangfeng/4562092 to your computer and use it in GitHub Desktop.
Save mingliangfeng/4562092 to your computer and use it in GitHub Desktop.
Interceptor to append JSP location to outputted html by struts2, for development or debug only.
<interceptors>
<interceptor name="jspLocationInterceptor"
class="com.fml.java.struts2.interceptor.JSPLocationInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="jspLocationInterceptor" >
<param name="includeMethods">*</param>
<param name="excludeViews">someJSP.jsp</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
package com.fml.java.struts2.interceptor;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletDispatcherResult;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class JSPLocationInterceptor implements Interceptor {
private static final long serialVersionUID = -2077919479128101204L;
protected static final Logger LOGGER = Logger.getLogger(JSPLocationInterceptor.class);
private String excludeViews;
private Set<String> excludeViewsList;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
LOGGER.info(getExcludeViews());
excludeViewsList = new HashSet<String>();
if (StringUtils.isNotEmpty(getExcludeViews())) {
excludeViewsList.addAll(Arrays.asList(getExcludeViews().split(",")));
}
}
@Override
public String intercept(ActionInvocation invocation) throws Exception {
final ActionContext context = invocation.getInvocationContext();
HttpServletResponse response = (HttpServletResponse) context.get(
ServletActionContext.HTTP_RESPONSE);
CharResponseWrapper wrapper = new CharResponseWrapper(response);
ServletActionContext.setResponse(wrapper);
// Actual Action called
String result = invocation.invoke();
String htmlReturned = wrapper.toString();
if (invocation.getResult() instanceof ServletDispatcherResult) {
ServletDispatcherResult r = (ServletDispatcherResult)invocation.getResult();
if (StringUtils.isNotEmpty(r.getLastFinalLocation())) {
int index = r.getLastFinalLocation().lastIndexOf("/");
if (index == -1 || !excludeViewsList.contains(
r.getLastFinalLocation().substring(index + 1))) {
htmlReturned = htmlReturned + "<br/><strong>JSP location: " +
r.getLastFinalLocation() + "</strong>";
}
}
}
PrintWriter out = response.getWriter();
out.write(htmlReturned);
out.flush();
return result;
}
public String getExcludeViews() {
return excludeViews;
}
public void setExcludeViews(String excludeViews) {
this.excludeViews = excludeViews;
}
}
class CharResponseWrapper extends HttpServletResponseWrapper {
private CharArrayWriter output;
public CharResponseWrapper(HttpServletResponse response) {
super(response);
this.output = new CharArrayWriter();
}
public String toString() {
return output.toString();
}
public PrintWriter getWriter() {
return new PrintWriter(output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment