001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest; 018 019import java.lang.reflect.*; 020 021import org.apache.juneau.http.response.*; 022import org.apache.juneau.reflect.*; 023import org.apache.juneau.rest.arg.*; 024import org.apache.juneau.rest.stats.*; 025 026/** 027 * A specialized invoker for methods that are called during a servlet request. 028 * 029 * <h5 class='section'>See Also:</h5><ul> 030 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestContext">RestContext</a> 031 * </ul> 032 */ 033public class RestOpInvoker extends MethodInvoker { 034 035 private final RestOpArg[] opArgs; 036 037 /** 038 * Constructor. 039 * 040 * @param m The method being wrapped. 041 * @param opArgs The parameter resolvers. 042 * @param stats The instrumentor. 043 */ 044 public RestOpInvoker(Method m, RestOpArg[] opArgs, MethodExecStats stats) { 045 super(m, stats); 046 this.opArgs = opArgs; 047 } 048 049 /** 050 * Invokes this method from the specified {@link RestSession}. 051 * 052 * @param opSession The REST call. 053 * @throws Throwable If an error occurred during either parameter resolution or method invocation. 054 */ 055 public void invoke(RestOpSession opSession) throws Throwable { 056 Object[] args = new Object[opArgs.length]; 057 for (int i = 0; i < opArgs.length; i++) { 058 ParamInfo pi = inner().getParam(i); 059 try { 060 args[i] = opArgs[i].resolve(opSession); 061 } catch (BasicHttpException e) { 062 throw e; 063 } catch (Exception e) { 064 throw new BadRequest(e, "Could not resolve parameter {0} of type ''{1}'' on method ''{2}''.", i, pi.getParameterType(), getFullName()); 065 } 066 } 067 try { 068 RestSession session = opSession.getRestSession(); 069 RestRequest req = opSession.getRequest(); 070 RestResponse res = opSession.getResponse(); 071 072 Object output = super.invoke(session.getResource(), args); 073 074 // Handle manual call to req.setDebug(). 075 Boolean debug = req.getAttribute("Debug").as(Boolean.class).orElse(null); 076 if (debug == Boolean.TRUE) { 077 session.debug(true); 078 } else if (debug == Boolean.FALSE) { 079 session.debug(false); 080 } 081 082 if (! inner().hasReturnType(Void.TYPE)) 083 if (output != null || ! res.getOutputStreamCalled()) 084 res.setContent(output); 085 086 } catch (IllegalAccessException|IllegalArgumentException e) { 087 throw new InternalServerError(e, "Error occurred invoking method ''{0}''.", inner().getFullName()); 088 } catch (InvocationTargetException e) { 089 RestResponse res = opSession.getResponse(); 090 Throwable e2 = e.getTargetException(); 091 res.setStatus(500); // May be overridden later. 092 res.setContent(opSession.getRestContext().convertThrowable(e2)); 093 } 094 } 095}