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