From e75788e3e0d1794ff5e0b03e01ce5a4a28314eb5 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Thu, 11 Sep 2014 10:56:10 +0100 Subject: [PATCH] Consolidate redirection, remove fairly redundant custom error page --- .../web/LegacyJavadocRedirectServlet.java | 44 ------------ .../com/google/zxing/web/RedirectFilter.java | 71 +++++++++++++++++++ zxingorg/src/web/error.jspx | 52 -------------- zxingorg/src/web/index.jspx | 24 ------- zxingorg/src/web/w/decoderesult.jspx | 4 +- zxingorg/src/web/w/index.jspx | 24 ------- 6 files changed, 74 insertions(+), 145 deletions(-) delete mode 100644 zxingorg/src/main/java/com/google/zxing/web/LegacyJavadocRedirectServlet.java create mode 100644 zxingorg/src/main/java/com/google/zxing/web/RedirectFilter.java delete mode 100644 zxingorg/src/web/error.jspx delete mode 100644 zxingorg/src/web/index.jspx delete mode 100644 zxingorg/src/web/w/index.jspx diff --git a/zxingorg/src/main/java/com/google/zxing/web/LegacyJavadocRedirectServlet.java b/zxingorg/src/main/java/com/google/zxing/web/LegacyJavadocRedirectServlet.java deleted file mode 100644 index 693fe8d12..000000000 --- a/zxingorg/src/main/java/com/google/zxing/web/LegacyJavadocRedirectServlet.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2014 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing.web; - -import com.google.common.base.Preconditions; -import com.google.common.net.HttpHeaders; - -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -@WebServlet("/w/docs/javadoc/*") -public final class LegacyJavadocRedirectServlet extends HttpServlet { - - private static final String PREFIX = "/w/docs/javadoc"; - - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) { - String requestURI = request.getRequestURI(); - Preconditions.checkArgument(requestURI.startsWith(PREFIX)); - String requestWithoutPrefix = requestURI.substring(PREFIX.length()); - if (requestWithoutPrefix.isEmpty()) { - requestWithoutPrefix = "/"; - } - response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); - response.setHeader(HttpHeaders.LOCATION, "http://zxing.github.io/zxing/apidocs" + requestWithoutPrefix); - } - -} diff --git a/zxingorg/src/main/java/com/google/zxing/web/RedirectFilter.java b/zxingorg/src/main/java/com/google/zxing/web/RedirectFilter.java new file mode 100644 index 000000000..0d6ed0b0a --- /dev/null +++ b/zxingorg/src/main/java/com/google/zxing/web/RedirectFilter.java @@ -0,0 +1,71 @@ +/* + * Copyright 2014 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.zxing.web; + +import com.google.common.net.HttpHeaders; + +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.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebFilter("/*") +public final class RedirectFilter implements Filter { + + private static final String OLD_JAVADOC_PREFIX = "/w/docs/javadoc"; + + @Override + public void init(FilterConfig filterConfig) { + // do nothing + } + + @Override + public void doFilter(ServletRequest servletRequest, + ServletResponse servletResponse, + FilterChain filterChain) throws IOException, ServletException { + HttpServletRequest request = (HttpServletRequest) servletRequest; + String requestURI = request.getRequestURI(); + if ("/index.jspx".equals(requestURI) || + "/w/".equals(requestURI) || + "/w/index.jspx".equals(requestURI)) { + redirect(servletResponse, "/w/decode.jspx"); + } else if (requestURI.startsWith(OLD_JAVADOC_PREFIX)) { + String withoutPrefix = requestURI.substring(OLD_JAVADOC_PREFIX.length()); + redirect(servletResponse, "http://zxing.github.io/zxing/apidocs" + withoutPrefix); + } else { + filterChain.doFilter(servletRequest, servletResponse); + } + } + + private static void redirect(ServletResponse servletResponse, String location) { + HttpServletResponse response = (HttpServletResponse) servletResponse; + response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); + response.setHeader(HttpHeaders.LOCATION, location); + } + + @Override + public void destroy() { + // do nothing + } + +} diff --git a/zxingorg/src/web/error.jspx b/zxingorg/src/web/error.jspx deleted file mode 100644 index 9da9e0f44..000000000 --- a/zxingorg/src/web/error.jspx +++ /dev/null @@ -1,52 +0,0 @@ - - - - -]]> - - -Error - - - -

Error ${pageContext.errorData.statusCode} : ${pageContext.errorData.requestURI}

- -if (request.getAttribute("javax.servlet.error.message") != null) { - String responseMessage = request.getAttribute("javax.servlet.error.message").toString(); - pageContext.setAttribute("responseMessage", XmlEscapers.xmlContentEscaper().escape(responseMessage)); - -

${responseMessage}

- -} - - -Throwable t = pageContext.getErrorData().getThrowable(); -if (t != null) { - StringWriter sw = new StringWriter(); - t.printStackTrace(new PrintWriter(sw)); - pageContext.setAttribute("stackTrace", XmlEscapers.xmlContentEscaper().escape(sw.toString())); - -

${stackTrace}

- -} - - - -
diff --git a/zxingorg/src/web/index.jspx b/zxingorg/src/web/index.jspx deleted file mode 100644 index 0c9f2e167..000000000 --- a/zxingorg/src/web/index.jspx +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - -response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); -response.setHeader("Location", "w/decode.jspx"); - - diff --git a/zxingorg/src/web/w/decoderesult.jspx b/zxingorg/src/web/w/decoderesult.jspx index 6a2499002..9e9159c2e 100644 --- a/zxingorg/src/web/w/decoderesult.jspx +++ b/zxingorg/src/web/w/decoderesult.jspx @@ -41,7 +41,9 @@
- for (Result result : (Iterable<Result>) request.getAttribute("results")) { + @SuppressWarnings("unchecked") + Iterable<Result> results = (Iterable<Result>) request.getAttribute("results"); + for (Result result : results) { ParsedResult parsedResult = ResultParser.parseResult(result); String text = result.getText(); diff --git a/zxingorg/src/web/w/index.jspx b/zxingorg/src/web/w/index.jspx deleted file mode 100644 index af6ac6352..000000000 --- a/zxingorg/src/web/w/index.jspx +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - -response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); -response.setHeader("Location", "decode.jspx"); - -