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.server.rrpc;
018
019import java.lang.reflect.Method;
020import java.util.function.*;
021
022import org.apache.juneau.http.remote.*;
023import org.apache.juneau.http.response.*;
024import org.apache.juneau.rest.server.*;
025
026import jakarta.servlet.*;
027
028/**
029 * A specialized {@link RestOpContext} for handling <js>"RRPC"</js> HTTP methods.
030 *
031 * <h5 class='section'>Notes:</h5><ul>
032 *    <li class='note'>This class is thread safe and reusable.
033 * </ul>
034 *
035 * <h5 class='section'>See Also:</h5><ul>
036 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestRpc">REST/RPC</a>
037 * </ul>
038 */
039@SuppressWarnings({
040   "java:S2160" // equals() inherited from parent context; RrpcRestOpContext adds only runtime-state fields not relevant to identity
041})
042public class RrpcRestOpContext extends RestOpContext {
043
044   private final RrpcInterfaceMeta meta;
045
046   /**
047    * Constructor.
048    *
049    * @param method The Java method this context represents. Must not be <jk>null</jk>.
050    * @param context The owning {@link RestContext}. Must not be <jk>null</jk>.
051    * @throws ServletException If context could not be created.
052    */
053   public RrpcRestOpContext(Method method, RestContext context) throws ServletException {
054      super(method, context);
055      var interfaceClass = getMarshallingContext().getClassMeta(getJavaMethod().getGenericReturnType());
056      meta = new RrpcInterfaceMeta(interfaceClass.inner(), null);
057      if (meta.getMethodsByPath().isEmpty())
058         throw new InternalServerError("Method %s returns an interface %s that doesn't define any remote methods.", getJavaMethod().getName(), interfaceClass.getNameFull());
059   }
060
061   /**
062    * Constructor.
063    *
064    * @param method The Java method this context represents. Must not be <jk>null</jk>.
065    * @param context The owning {@link RestContext}. Must not be <jk>null</jk>.
066    * @param resourceSupplier Supplier that returns the invocation target for this operation.
067    * @throws ServletException If context could not be created.
068    */
069   public RrpcRestOpContext(Method method, RestContext context, Supplier<Object> resourceSupplier) throws ServletException {
070      super(method, context, resourceSupplier);
071      var interfaceClass = getMarshallingContext().getClassMeta(getJavaMethod().getGenericReturnType());
072      meta = new RrpcInterfaceMeta(interfaceClass.inner(), null);
073      if (meta.getMethodsByPath().isEmpty())
074         throw new InternalServerError("Method %s returns an interface %s that doesn't define any remote methods.", getJavaMethod().getName(), interfaceClass.getNameFull());
075   }
076
077   @Override
078   public RrpcRestOpSession.Builder createSession(RestSession session) {
079      return RrpcRestOpSession.create(this, session);
080   }
081
082   /**
083    * Returns the metadata about the RRPC Java method.
084    *
085    * @return The metadata about the RRPC Java method.
086    */
087   protected RrpcInterfaceMeta getMeta() { return meta; }
088}