src/java/de/uapcore/lightpit/AbstractLightPITServlet.java

changeset 18
a94b172c3a93
parent 17
d1036b776eee
child 20
bd1a76c91d5b
equal deleted inserted replaced
17:d1036b776eee 18:a94b172c3a93
71 ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException, ServletException; 71 ResponseType apply(HttpServletRequest t, HttpServletResponse u) throws IOException, ServletException;
72 } 72 }
73 73
74 /** 74 /**
75 * Invocation mapping gathered from the {@link RequestMapping} annotations. 75 * Invocation mapping gathered from the {@link RequestMapping} annotations.
76 *
77 * Paths in this map must always start with a leading slash, although
78 * the specification in the annotation must not start with a leading slash.
79 *
80 * The reason for this is the different handling of empty paths in
81 * {@link HttpServletRequest#getPathInfo()}.
76 */ 82 */
77 private final Map<HttpMethod, Map<String, HandlerMethod>> mappings = new HashMap<>(); 83 private final Map<HttpMethod, Map<String, HandlerMethod>> mappings = new HashMap<>();
78 84
79 /** 85 /**
80 * Gives implementing modules access to the {@link ModuleManager}. 86 * Gives implementing modules access to the {@link ModuleManager}.
143 149
144 Class<?>[] params = method.getParameterTypes(); 150 Class<?>[] params = method.getParameterTypes();
145 if (params.length == 2 151 if (params.length == 2
146 && HttpServletRequest.class.isAssignableFrom(params[0]) 152 && HttpServletRequest.class.isAssignableFrom(params[0])
147 && HttpServletResponse.class.isAssignableFrom(params[1])) { 153 && HttpServletResponse.class.isAssignableFrom(params[1])) {
154
155 final String requestPath = "/"+mapping.get().requestPath();
148 156
149 if (mappings.computeIfAbsent(mapping.get().method(), k -> new HashMap<>()). 157 if (mappings.computeIfAbsent(mapping.get().method(), k -> new HashMap<>()).
150 putIfAbsent(mapping.get().requestPath(), 158 putIfAbsent(requestPath,
151 (req, resp) -> invokeMapping(method, req, resp)) != null) { 159 (req, resp) -> invokeMapping(method, req, resp)) != null) {
152 LOG.warn("{} {} has multiple mappings", 160 LOG.warn("{} {} has multiple mappings",
153 mapping.get().method(), 161 mapping.get().method(),
154 mapping.get().requestPath() 162 mapping.get().requestPath()
155 ); 163 );
156 } 164 }
157 165
158 LOG.info("{} {} maps to {}", 166 LOG.info("{} {} maps to {}",
159 mapping.get().method(), 167 mapping.get().method(),
160 mapping.get().requestPath(), 168 requestPath,
161 method.getName() 169 method.getName()
162 ); 170 );
163 } else { 171 } else {
164 LOG.warn("{} is annotated with {} but has the wrong parameters - (HttpServletRequest,HttpServletResponse) required", 172 LOG.warn("{} is annotated with {} but has the wrong parameters - (HttpServletRequest,HttpServletResponse) required",
165 method.getName(), RequestMapping.class.getSimpleName() 173 method.getName(), RequestMapping.class.getSimpleName()
217 req.getRequestDispatcher(HTML_FULL_DISPATCHER).forward(req, resp); 225 req.getRequestDispatcher(HTML_FULL_DISPATCHER).forward(req, resp);
218 } 226 }
219 227
220 private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) { 228 private Optional<HandlerMethod> findMapping(HttpMethod method, HttpServletRequest req) {
221 return Optional.ofNullable(mappings.get(method)).map( 229 return Optional.ofNullable(mappings.get(method)).map(
222 (rm) -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse("")) 230 (rm) -> rm.get(Optional.ofNullable(req.getPathInfo()).orElse("/"))
223 ); 231 );
224 } 232 }
225 233
226 private void forwardAsSepcified(ResponseType type, HttpServletRequest req, HttpServletResponse resp) 234 private void forwardAsSepcified(ResponseType type, HttpServletRequest req, HttpServletResponse resp)
227 throws ServletException, IOException { 235 throws ServletException, IOException {

mercurial