001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest; 014 015import static javax.servlet.http.HttpServletResponse.*; 016import static org.apache.juneau.internal.IOUtils.*; 017import static org.apache.juneau.internal.StringUtils.*; 018import static org.apache.juneau.rest.Enablement.*; 019 020import java.io.*; 021import java.util.*; 022 023import javax.servlet.*; 024import javax.servlet.http.*; 025 026import org.apache.juneau.http.StreamResource; 027import org.apache.juneau.http.annotation.*; 028import org.apache.juneau.rest.RestContext.*; 029import org.apache.juneau.http.exception.*; 030import org.apache.juneau.reflect.*; 031import org.apache.juneau.rest.util.*; 032 033/** 034 * Default implementation of {@link RestCallHandler}. 035 * 036 * <p> 037 * Subclasses can override these methods to tailor how HTTP REST calls are handled. 038 * <br>Subclasses MUST implement a public constructor that takes in a {@link RestContext} object. 039 * 040 * <ul class='seealso'> 041 * <li class='jf'>{@link RestContext#REST_callHandler} 042 * </ul> 043 */ 044public class BasicRestCallHandler implements RestCallHandler { 045 046 private final RestContext context; 047 private final Map<String,RestCallRouter> restCallRouters; 048 049 /** 050 * Constructor. 051 * 052 * @param context The resource context. 053 */ 054 public BasicRestCallHandler(RestContext context) { 055 this.context = context; 056 this.restCallRouters = context.getCallRouters(); 057 } 058 059 @Override /* RestCallHandler */ 060 public RestCall createCall(HttpServletRequest req, HttpServletResponse res) { 061 return new RestCall(req, res).logger(context.getCallLogger()).loggerConfig(context.getCallLoggerConfig()); 062 } 063 064 @Override /* RestCallHandler */ 065 public RestRequest createRequest(RestCall call) throws ServletException { 066 return new RestRequest(context, call.getRequest()); 067 } 068 069 @Override /* RestCallHandler */ 070 public RestResponse createResponse(RestCall call) throws ServletException { 071 return new RestResponse(context, call.getRestRequest(), call.getResponse()); 072 } 073 074 /** 075 * The main service method. 076 * 077 * <p> 078 * Subclasses can optionally override this method if they want to tailor the behavior of requests. 079 * 080 * @param r1 The incoming HTTP servlet request object. 081 * @param r2 The incoming HTTP servlet response object. 082 * @throws ServletException General servlet exception. 083 * @throws IOException Thrown by underlying stream. 084 */ 085 @Override /* RestCallHandler */ 086 public void service(HttpServletRequest r1, HttpServletResponse r2) throws ServletException, IOException { 087 088 RestCall call = createCall(r1, r2); 089 090 try { 091 context.checkForInitException(); 092 093 // If the resource path contains variables (e.g. @Rest(path="/f/{a}/{b}"), then we want to resolve 094 // those variables and push the servletPath to include the resolved variables. The new pathInfo will be 095 // the remainder after the new servletPath. 096 // Only do this for the top-level resource because the logic for child resources are processed next. 097 if (context.pathPattern.hasVars() && context.getParentContext() == null) { 098 String sp = call.getServletPath(); 099 String pi = call.getPathInfoUndecoded(); 100 UrlPathInfo upi2 = new UrlPathInfo(pi == null ? sp : sp + pi); 101 UrlPathPatternMatch uppm = context.pathPattern.match(upi2); 102 if (uppm != null && ! uppm.hasEmptyVars()) { 103 RequestPath.addPathVars(call.getRequest(), uppm.getVars()); 104 call.request( 105 new OverrideableHttpServletRequest(call.getRequest()) 106 .pathInfo(nullIfEmpty(urlDecode(uppm.getSuffix()))) 107 .servletPath(uppm.getPrefix()) 108 ); 109 } else { 110 call.debug(isDebug(call)).status(SC_NOT_FOUND).finish(); 111 return; 112 } 113 } 114 115 // If this resource has child resources, try to recursively call them. 116 String pi = call.getPathInfoUndecoded(); 117 if (context.hasChildResources() && pi != null && ! pi.equals("/")) { 118 for (RestContext rc : context.getChildResources().values()) { 119 UrlPathPattern upp = rc.pathPattern; 120 UrlPathPatternMatch uppm = upp.match(call.getUrlPathInfo()); 121 if (uppm != null) { 122 if (! uppm.hasEmptyVars()) { 123 RequestPath.addPathVars(call.getRequest(), uppm.getVars()); 124 HttpServletRequest childRequest = new OverrideableHttpServletRequest(call.getRequest()) 125 .pathInfo(nullIfEmpty(urlDecode(uppm.getSuffix()))) 126 .servletPath(call.getServletPath() + uppm.getPrefix()); 127 rc.getCallHandler().service(childRequest, call.getResponse()); 128 } else { 129 call.debug(isDebug(call)).status(SC_NOT_FOUND).finish(); 130 } 131 return; 132 } 133 } 134 } 135 136 call.debug(isDebug(call)); 137 138 context.startCall(call); 139 140 call.restRequest(createRequest(call)); 141 call.restResponse(createResponse(call)); 142 143 context.setRequest(call.getRestRequest()); 144 context.setResponse(call.getRestResponse()); 145 146 StreamResource r = null; 147 if (call.getPathInfoUndecoded() != null) { 148 String p = call.getPathInfoUndecoded().substring(1); 149 if (context.isStaticFile(p)) { 150 StaticFile sf = context.resolveStaticFile(p); 151 r = sf.resource; 152 call.responseMeta(sf.meta); 153 } else if (p.equals("favicon.ico")) { 154 call.output(null); 155 } 156 } 157 158 if (r != null) { 159 call.status(SC_OK); 160 call.output(r); 161 } else { 162 163 // If the specified method has been defined in a subclass, invoke it. 164 int rc = 0; 165 String m = call.getMethod(); 166 if (restCallRouters.containsKey(m)) { 167 rc = restCallRouters.get(m).invoke(call); 168 } else if (restCallRouters.containsKey("*")) { 169 rc = restCallRouters.get("*").invoke(call); 170 } 171 172 // Should be 405 if the URL pattern matched but HTTP method did not. 173 if (rc == 0) 174 for (RestCallRouter rcc : restCallRouters.values()) 175 if (rcc.matches(call)) 176 rc = SC_METHOD_NOT_ALLOWED; 177 178 // Should be 404 if URL pattern didn't match. 179 if (rc == 0) 180 rc = SC_NOT_FOUND; 181 182 // If not invoked above, see if it's an OPTIONs request 183 if (rc != SC_OK) 184 handleNotFound(call.status(rc)); 185 186 if (call.getStatus() == 0) 187 call.status(rc); 188 } 189 190 if (call.hasOutput()) { 191 // Now serialize the output if there was any. 192 // Some subclasses may write to the OutputStream or Writer directly. 193 handleResponse(call); 194 } 195 196 197 } catch (Throwable e) { 198 handleError(call, convertThrowable(e)); 199 } finally { 200 context.clearState(); 201 } 202 203 call.finish(); 204 context.finishCall(call); 205 } 206 207 private boolean isDebug(RestCall call) { 208 Enablement e = context.getDebug(); 209 if (e == TRUE) 210 return true; 211 if (e == FALSE) 212 return false; 213 return "true".equalsIgnoreCase(call.getRequest().getHeader("X-Debug")); 214 } 215 216 /** 217 * The main method for serializing POJOs passed in through the {@link RestResponse#setOutput(Object)} method or 218 * returned by the Java method. 219 * 220 * <p> 221 * Subclasses may override this method if they wish to modify the way the output is rendered or support other output 222 * formats. 223 * 224 * <p> 225 * The default implementation simply iterates through the response handlers on this resource 226 * looking for the first one whose {@link ResponseHandler#handle(RestRequest,RestResponse)} method returns 227 * <jk>true</jk>. 228 * 229 * @param call The HTTP call. 230 * @throws IOException Thrown by underlying stream. 231 * @throws HttpException Non-200 response. 232 */ 233 @Override /* RestCallHandler */ 234 public void handleResponse(RestCall call) throws IOException, HttpException, NotImplemented { 235 236 RestRequest req = call.getRestRequest(); 237 RestResponse res = call.getRestResponse(); 238 239 // Loop until we find the correct handler for the POJO. 240 for (ResponseHandler h : context.getResponseHandlers()) 241 if (h.handle(req, res)) 242 return; 243 244 Object output = res.getOutput(); 245 throw new NotImplemented("No response handlers found to process output of type '"+(output == null ? null : output.getClass().getName())+"'"); 246 } 247 248 /** 249 * Method that can be subclassed to allow uncaught throwables to be treated as other types of throwables. 250 * 251 * <p> 252 * The default implementation looks at the throwable class name to determine whether it can be converted to another type: 253 * 254 * <ul> 255 * <li><js>"*AccessDenied*"</js> - Converted to {@link Unauthorized}. 256 * <li><js>"*Empty*"</js>,<js>"*NotFound*"</js> - Converted to {@link NotFound}. 257 * </ul> 258 * 259 * @param t The thrown object. 260 * @return The converted thrown object. 261 */ 262 @SuppressWarnings("deprecation") 263 @Override 264 public Throwable convertThrowable(Throwable t) { 265 ClassInfo ci = ClassInfo.ofc(t); 266 if (ci.is(HttpRuntimeException.class)) 267 t = ((HttpRuntimeException)t).getInner(); 268 if (ci.isChildOf(RestException.class) || ci.hasAnnotation(Response.class)) 269 return t; 270 String n = t.getClass().getName(); 271 if (n.contains("AccessDenied")) 272 return new Unauthorized(t); 273 if (n.contains("Empty") || n.contains("NotFound")) 274 return new NotFound(t); 275 return t; 276 } 277 278 /** 279 * Handle the case where a matching method was not found. 280 * 281 * <p> 282 * Subclasses can override this method to provide a 2nd-chance for specifying a response. 283 * The default implementation will simply throw an exception with an appropriate message. 284 * 285 * @param call The HTTP call. 286 */ 287 @Override /* RestCallHandler */ 288 public void handleNotFound(RestCall call) throws Exception { 289 String pathInfo = call.getPathInfo(); 290 String methodUC = call.getMethod(); 291 int rc = call.getStatus(); 292 String onPath = pathInfo == null ? " on no pathInfo" : String.format(" on path '%s'", pathInfo); 293 if (rc == SC_NOT_FOUND) 294 throw new NotFound("Method ''{0}'' not found on resource with matching pattern{1}.", methodUC, onPath); 295 else if (rc == SC_PRECONDITION_FAILED) 296 throw new PreconditionFailed("Method ''{0}'' not found on resource{1} with matching matcher.", methodUC, onPath); 297 else if (rc == SC_METHOD_NOT_ALLOWED) 298 throw new MethodNotAllowed("Method ''{0}'' not found on resource.", methodUC); 299 else 300 throw new ServletException("Invalid method response: " + rc); 301 } 302 303 /** 304 * Method for handling response errors. 305 * 306 * <p> 307 * Subclasses can override this method to provide their own custom error response handling. 308 * 309 * @param call The rest call. 310 * @param e The exception that occurred. 311 * @throws IOException Can be thrown if a problem occurred trying to write to the output stream. 312 */ 313 @Override /* RestCallHandler */ 314 @SuppressWarnings("deprecation") 315 public synchronized void handleError(RestCall call, Throwable e) throws IOException { 316 317 call.exception(e); 318 319 int occurrence = context == null ? 0 : context.getStackTraceOccurrence(e); 320 321 int code = 500; 322 323 ClassInfo ci = ClassInfo.ofc(e); 324 Response r = ci.getAnnotation(Response.class); 325 if (r != null) 326 if (r.code().length > 0) 327 code = r.code()[0]; 328 329 RestException e2 = (e instanceof RestException ? (RestException)e : new RestException(e, code)).setOccurrence(occurrence); 330 331 HttpServletRequest req = call.getRequest(); 332 HttpServletResponse res = call.getResponse(); 333 334 Throwable t = null; 335 if (e instanceof HttpRuntimeException) 336 t = ((HttpRuntimeException)e).getInner(); 337 if (t == null) 338 t = e2.getRootCause(); 339 if (t != null) { 340 res.setHeader("Exception-Name", stripInvalidHttpHeaderChars(t.getClass().getName())); 341 res.setHeader("Exception-Message", stripInvalidHttpHeaderChars(t.getMessage())); 342 } 343 344 try { 345 res.setContentType("text/plain"); 346 res.setHeader("Content-Encoding", "identity"); 347 res.setStatus(e2.getStatus()); 348 349 PrintWriter w = null; 350 try { 351 w = res.getWriter(); 352 } catch (IllegalStateException x) { 353 w = new PrintWriter(new OutputStreamWriter(res.getOutputStream(), UTF8)); 354 } 355 356 try (PrintWriter w2 = w) { 357 String httpMessage = RestUtils.getHttpResponseText(e2.getStatus()); 358 if (httpMessage != null) 359 w2.append("HTTP ").append(String.valueOf(e2.getStatus())).append(": ").append(httpMessage).append("\n\n"); 360 if (context != null && context.isRenderResponseStackTraces()) 361 e.printStackTrace(w2); 362 else 363 w2.append(e2.getFullStackMessage(true)); 364 } 365 366 } catch (Exception e1) { 367 req.setAttribute("Exception", e1); 368 } 369 } 370 371 /** 372 * Returns the session objects for the specified request. 373 * 374 * <p> 375 * The default implementation simply returns a single map containing <c>{'req':req}</c>. 376 * 377 * @param req The REST request. 378 * @return The session objects for that request. 379 */ 380 @Override /* RestCallHandler */ 381 public Map<String,Object> getSessionObjects(RestRequest req, RestResponse res) { 382 Map<String,Object> m = new HashMap<>(); 383 m.put("req", req); 384 m.put("res", res); 385 return m; 386 } 387}