1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.archive.wayback.util.webapp;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26
27 import javax.servlet.http.HttpServletRequest;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class PortMapper {
41 private static final Logger LOGGER = Logger.getLogger(
42 PortMapper.class.getName());
43 private int port = -1;
44 private HashMap<String, RequestHandler> pathMap = null;
45
46
47
48 public PortMapper(int port) {
49 this.port = port;
50 pathMap = new HashMap<String, RequestHandler>();
51 }
52 private String hostPathToKey(String host, String firstPath) {
53 StringBuilder sb = null;
54 if((host == null) && (firstPath == null)) {
55 return null;
56 }
57 sb = new StringBuilder();
58 if(host != null) {
59 sb.append(host);
60 }
61 sb.append("/");
62 if(firstPath != null) {
63 sb.append(firstPath);
64 }
65 return sb.toString();
66 }
67
68
69
70
71
72
73
74
75
76
77
78 public void addRequestHandler(String host, String firstPath,
79 RequestHandler requestHandler) {
80 String key = hostPathToKey(host, firstPath);
81 if (pathMap.containsKey(key)) {
82 LOGGER.warning("Duplicate port:path map for " + port +
83 ":" + key);
84 } else {
85 pathMap.put(key, requestHandler);
86 if (LOGGER.isLoggable(Level.FINE)) {
87 LOGGER.fine("Registered requestHandler(port/host/path) (" +
88 port + "/" + host + "/" + firstPath + "): " + key);
89 }
90 }
91 }
92
93 private String requestToFirstPath(HttpServletRequest request) {
94 String firstPath = null;
95 String requestPath = request.getRequestURI();
96 String contextPath = request.getContextPath();
97 if((contextPath.length() > 0) && requestPath.startsWith(contextPath)) {
98 requestPath = requestPath.substring(contextPath.length());
99 }
100 while(requestPath.startsWith("/")) {
101 requestPath = requestPath.substring(1);
102 }
103
104 int slashIdx = requestPath.indexOf("/",1);
105 if(slashIdx == -1) {
106 firstPath = requestPath;
107 } else {
108 firstPath = requestPath.substring(0,slashIdx);
109 }
110 return firstPath;
111 }
112
113 private String requestToHost(HttpServletRequest request) {
114 return request.getServerName();
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public RequestHandlerContext getRequestHandlerContext(
133 HttpServletRequest request) {
134 String host = requestToHost(request);
135 String contextPath = request.getContextPath();
136 StringBuilder pathPrefix = new StringBuilder(contextPath);
137
138 pathPrefix.append("/");
139
140 String firstPath = requestToFirstPath(request);
141 String key = hostPathToKey(host,firstPath);
142 RequestHandler handler = pathMap.get(key);
143 if(handler != null) {
144 LOGGER.fine("Mapped to RequestHandler with " + key);
145 return new RequestHandlerContext(handler,
146 pathPrefix.append(firstPath).toString());
147 } else {
148 LOGGER.finer("No mapping for " + key);
149 }
150 key = hostPathToKey(host,null);
151 handler = pathMap.get(key);
152 if(handler != null) {
153 LOGGER.fine("Mapped to RequestHandler with " + key);
154 return new RequestHandlerContext(handler,contextPath);
155 } else {
156 LOGGER.finer("No mapping for " + key);
157 }
158 key = hostPathToKey(null,firstPath);
159 handler = pathMap.get(key);
160 if(handler != null) {
161 LOGGER.fine("Mapped to RequestHandler with " + key);
162
163 return new RequestHandlerContext(handler,
164 pathPrefix.append(firstPath).toString());
165 } else {
166 LOGGER.finer("No mapping for " + key);
167 }
168 handler = pathMap.get(null);
169 if(handler != null) {
170 LOGGER.fine("Mapped to RequestHandler with null");
171 return new RequestHandlerContext(handler,contextPath);
172 }
173
174
175 ArrayList<String> paths = new ArrayList<String>();
176 for(String tmp : pathMap.keySet()) {
177
178 int idx = tmp.lastIndexOf('/');
179 if(idx != -1) {
180 String path = tmp.substring(idx+1);
181 paths.add(path);
182 }
183 }
184 if(paths.size() > 0) {
185 request.setAttribute("AccessPointNames", paths);
186 }
187 return null;
188 }
189 }