Created
December 11, 2013 15:37
-
-
Save nhojpatrick/7912559 to your computer and use it in GitHub Desktop.
XUACompatibleInterceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Pattern; | |
import javax.servlet.http.HttpServletResponse; | |
import com.rbsg.bizmon.core.constants.GUIConstants; | |
import org.apache.struts2.StrutsStatics; | |
import com.opensymphony.xwork2.ActionContext; | |
import com.opensymphony.xwork2.ActionInvocation; | |
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; | |
/** | |
* IE Compatibility Mode - IE 7 and 8 render as IE7. IE9 renders as IE9 | |
*/ | |
public class XUACompatibleInterceptor extends AbstractInterceptor { | |
/** | |
* serialVersionUID. | |
*/ | |
private static final long serialVersionUID = -2149133042114261687L; | |
private static final String CONTENT_TYPE___TEXT_HTML__REGEX = "^(?i)text/html.*"; | |
private static final String X_UA_COMPATIBLE = "X-UA-Compatible"; | |
@Override | |
public String intercept(final ActionInvocation invocation) throws Exception { | |
final String result = invocation.invoke(); | |
final ActionContext context = (ActionContext) invocation.getInvocationContext(); | |
final HttpServletResponse response = (HttpServletResponse) context.get(StrutsStatics.HTTP_RESPONSE); | |
final String contextType = response.getContentType(); | |
if (checkContentType(contextType)) { | |
response.setHeader(X_UA_COMPATIBLE, GUIConstants.HTTP_RESPONSE___X_UA_COMPATIBLE); | |
} | |
return result; | |
} | |
public boolean checkContentType(final String contextType) { | |
return Pattern.matches(XUACompatibleInterceptor.CONTENT_TYPE___TEXT_HTML__REGEX, | |
(contextType == null ? "" : contextType)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.testng.Assert.assertFalse; | |
import static org.testng.Assert.assertTrue; | |
import org.testng.annotations.Test; | |
@Test(singleThreaded = true) | |
public class XUACompatibleInterceptorTest { | |
@Test | |
public void testContentTypeCheck_empty() { | |
assertFalse(new XUACompatibleInterceptor().checkContentType("")); | |
} | |
@Test | |
public void testContentTypeCheck_null() { | |
assertFalse(new XUACompatibleInterceptor().checkContentType(null)); | |
} | |
@Test | |
public void testContentTypeCheck_qwerty() { | |
assertFalse(new XUACompatibleInterceptor().checkContentType("qwerty")); | |
} | |
@Test | |
public void testContentTypeCheck_qwerty_text() { | |
assertFalse(new XUACompatibleInterceptor().checkContentType("qwerty text")); | |
} | |
@Test | |
public void testContentTypeCheck_qwerty_text__html() { | |
assertFalse(new XUACompatibleInterceptor().checkContentType("qwerty text html")); | |
} | |
@Test | |
public void testContentTypeCheck_qwerty_text_html() { | |
assertFalse(new XUACompatibleInterceptor().checkContentType("qwerty text/html")); | |
} | |
@Test | |
public void testContentTypeCheck_qwerty_text_html_() { | |
assertFalse(new XUACompatibleInterceptor().checkContentType("qwerty text/html;")); | |
} | |
@Test | |
public void testContentTypeCheck_text_html() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("text/html")); | |
} | |
@Test | |
public void testContentTypeCheck_text_html_() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("text/html;")); | |
} | |
@Test | |
public void testContentTypeCheck_text_HTML_() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("text/HTML;")); | |
} | |
@Test | |
public void testContentTypeCheck_TEXT_html_() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("TEXT/html;")); | |
} | |
@Test | |
public void testContentTypeCheck_TEXT_HTML_() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("TEXT/HTML;")); | |
} | |
@Test | |
public void testContentTypeCheck_text_html_charset_UTF_8() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("text/html; charset=UTF-8")); | |
} | |
@Test | |
public void testContentTypeCheck_text_HTML_charset_UTF_8() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("text/HTML; charset=UTF-8")); | |
} | |
@Test | |
public void testContentTypeCheck_TEXT_html_charset_UTF_8() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("TEXT/html; charset=UTF-8")); | |
} | |
@Test | |
public void testContentTypeCheck_TEXT_HTML_charset_UTF_8() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("TEXT/HTML; charset=UTF-8")); | |
} | |
@Test | |
public void testContentTypeCheck_text_html_charset_UTF_8_() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("text/html; charset=UTF-8;")); | |
} | |
@Test | |
public void testContentTypeCheck_text_HTML_charset_UTF_8_() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("text/HTML; charset=UTF-8;")); | |
} | |
@Test | |
public void testContentTypeCheck_TEXT_html_charset_UTF_8_() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("TEXT/html; charset=UTF-8;")); | |
} | |
@Test | |
public void testContentTypeCheck_TEXT_HTML_charset_UTF_8_() { | |
assertTrue(new XUACompatibleInterceptor().checkContentType("TEXT/HTML; charset=UTF-8;")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment