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 static org.apache.juneau.commons.utils.Utils.*; 020 021import java.lang.reflect.*; 022 023import org.apache.juneau.commons.reflect.*; 024import org.apache.juneau.http.response.*; 025import org.apache.juneau.rest.arg.*; 026import org.apache.juneau.rest.stats.*; 027 028/** 029 * A specialized invoker for methods that are called during a servlet request. 030 * 031 * <h5 class='section'>See Also:</h5><ul> 032 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestContext">RestContext</a> 033 * </ul> 034 */ 035public class RestOpInvoker extends MethodInvoker { 036 037 private final RestOpArg[] opArgs; 038 039 /** 040 * Constructor. 041 * 042 * @param m The method being wrapped. 043 * @param opArgs The parameter resolvers. 044 * @param stats The instrumentor. 045 */ 046 public RestOpInvoker(Method m, RestOpArg[] opArgs, MethodExecStats stats) { 047 super(m, stats); 048 this.opArgs = opArgs; 049 } 050 051 /** 052 * Invokes this method from the specified {@link RestSession}. 053 * 054 * @param opSession The REST call. 055 * @throws Throwable If an error occurred during either parameter resolution or method invocation. 056 */ 057 public void invoke(RestOpSession opSession) throws Throwable { 058 var args = new Object[opArgs.length]; 059 for (var i = 0; i < opArgs.length; i++) { 060 ParameterInfo pi = inner().getParameter(i); 061 try { 062 args[i] = opArgs[i].resolve(opSession); 063 } catch (BasicHttpException e) { 064 throw e; 065 } catch (Exception e) { 066 throw new BadRequest(e, "Could not resolve parameter {0} of type ''{1}'' on method ''{2}''.", i, pi.getParameterType(), getFullName()); 067 } 068 } 069 try { 070 RestSession session = opSession.getRestSession(); 071 RestRequest req = opSession.getRequest(); 072 RestResponse res = opSession.getResponse(); 073 074 Object output = super.invoke(session.getResource(), args); 075 076 // Handle manual call to req.setDebug(). 077 Boolean debug = req.getAttribute("Debug").as(Boolean.class).orElse(null); 078 if (debug == Boolean.TRUE) { 079 session.debug(true); 080 } else if (debug == Boolean.FALSE) { 081 session.debug(false); 082 } 083 084 if (! inner().hasReturnType(Void.TYPE)) 085 if (nn(output) || ! res.getOutputStreamCalled()) 086 res.setContent(output); 087 088 } catch (IllegalAccessException | IllegalArgumentException e) { 089 throw new InternalServerError(e, "Error occurred invoking method ''{0}''.", inner().getFullName()); 090 } catch (InvocationTargetException e) { 091 RestResponse res = opSession.getResponse(); 092 Throwable e2 = e.getTargetException(); 093 res.setStatus(500); // May be overridden later. 094 res.setContent(opSession.getRestContext().convertThrowable(e2)); 095 } 096 } 097}