1
2
3
4
5 package org.rcfaces.core.internal.version;
6
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import java.util.Map;
10
11 import javax.servlet.RequestDispatcher;
12 import javax.servlet.ServletConfig;
13 import javax.servlet.ServletException;
14 import javax.servlet.ServletOutputStream;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import javax.servlet.http.HttpServletResponseWrapper;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.rcfaces.core.internal.util.ServletTools;
22 import org.rcfaces.core.internal.webapp.ConfiguredHttpServlet;
23 import org.rcfaces.core.internal.webapp.ExpirationDate;
24
25
26
27
28
29
30 public class ApplicationVersionServlet extends ConfiguredHttpServlet {
31 private static final String REVISION = "$Revision: 1.3.6.1 $";
32
33 private static final long serialVersionUID = -4209462021160100620L;
34
35 private static final Log LOG = LogFactory
36 .getLog(ApplicationVersionServlet.class);
37
38 private static final String DEFAULT_APPLICATION_VERSION_URL = "/ap-v";
39
40 private static final String APPLICATION_VERSION_URL_PROPERTY = "org.rcfaces.core.internal.rewriting.APPLICATION_VERSION_URL_PROPERTY";
41
42 public void init(ServletConfig config) throws ServletException {
43
44 super.init(config);
45
46 String applicationVersionURL = ServletTools.computeResourceURI(
47 getServletContext(), DEFAULT_APPLICATION_VERSION_URL,
48 getClass());
49 if (applicationVersionURL == null) {
50 return;
51 }
52 LOG.info("Base of application version url is '" + applicationVersionURL
53 + "'.");
54
55 getServletContext().setAttribute(APPLICATION_VERSION_URL_PROPERTY,
56 applicationVersionURL);
57 }
58
59 static String getApplicationVersionURI(Map applicationMap) {
60 return (String) applicationMap.get(APPLICATION_VERSION_URL_PROPERTY);
61 }
62
63 protected void service(HttpServletRequest request,
64 HttpServletResponse response) throws ServletException, IOException {
65
66 String url = request.getRequestURI();
67
68 String contextPath = request.getContextPath();
69 if (contextPath != null) {
70 url = url.substring(contextPath.length());
71 }
72
73 String servletPath = request.getServletPath();
74 if (servletPath != null) {
75 url = url.substring(servletPath.length());
76 }
77
78
79 int idx = url.indexOf('/');
80 if (idx < 0) {
81 throw new ServletException("Can not understand URI '"
82 + request.getRequestURI() + "'.");
83 }
84
85 url = url.substring(idx + 1);
86
87 idx = url.indexOf('/');
88 if (idx < 0) {
89 throw new ServletException("Can not understand URI '"
90 + request.getRequestURI() + "'.");
91 }
92 String version = url.substring(0, idx);
93 url = url.substring(idx);
94
95 if (LOG.isDebugEnabled()) {
96 LOG.debug("Forward url='" + url + "' version='" + version
97 + "' requested url='" + request.getRequestURI() + "'.");
98 }
99
100 RequestDispatcher requestDispatcher = getServletContext()
101 .getRequestDispatcher(url);
102 if (requestDispatcher == null) {
103 LOG.error("Can not get request dispatcher for url '" + url + "'.");
104
105 throw new ServletException(
106 "Can not get request dispatcher for url '" + url + "'.");
107 }
108
109 HttpServletResponse wrappedResponse = new HttpServletResponseWrapper(
110 response) {
111
112 protected void updateExpiration() {
113 ExpirationDate expirationDate = getDefaultExpirationDate(true);
114 if (expirationDate != null) {
115 expirationDate.sendExpires(this);
116 }
117 }
118
119 public ServletOutputStream getOutputStream() throws IOException {
120 updateExpiration();
121
122 return super.getOutputStream();
123 }
124
125 public PrintWriter getWriter() throws IOException {
126 updateExpiration();
127
128 return super.getWriter();
129 }
130
131 };
132
133 requestDispatcher.forward(request, wrappedResponse);
134 }
135 }