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.mcp;
018
019
020import java.util.function.*;
021
022import org.apache.juneau.bean.jsonrpc.*;
023import static org.apache.juneau.commons.utils.Shorts.*;
024
025/**
026 * The inbound JSON-RPC envelope plus request-header access, with no servlet or HTTP types attached.
027 *
028 * <p>
029 * This is the sole argument a revision receives about the transport. It exists so a revision can
030 * read protocol headers (later MCP revisions route on them) without the core or the revision
031 * depending on a servlet API.
032 */
033public final class McpExchange {
034
035   private final JsonRpcRequest request;
036   private final UnaryOperator<String> headers;
037
038   /**
039    * Constructor.
040    *
041    * @param request The bound JSON-RPC request envelope. Can be <jk>null</jk> (an unparseable or
042    *    absent body), which a revision must report as an invalid request.
043    * @param headers Header lookup by name, returning <jk>null</jk> for an absent header. Must not
044    *    be <jk>null</jk>.
045    */
046   public McpExchange(JsonRpcRequest request, UnaryOperator<String> headers) {
047      reqnn("headers", headers);
048      this.request = request;
049      this.headers = headers;
050   }
051
052   /**
053    * The inbound JSON-RPC request envelope.
054    *
055    * @return The envelope, or <jk>null</jk> if the body was absent or unparseable.
056    */
057   public JsonRpcRequest request() {
058      return request;
059   }
060
061   /**
062    * Looks up a request header.
063    *
064    * @param name The header name. Can be <jk>null</jk>.
065    * @return The header value, or <jk>null</jk> if absent.
066    */
067   public String header(String name) {
068      return headers.apply(name);
069   }
070}