{{! Copyright (c) Meta Platforms, Inc. and affiliates. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. }} {{> AutoGenerated}} package {{service:javaPackage}}; import java.util.*; import org.apache.thrift.protocol.*; import com.facebook.thrift.util.Readers; public class {{service:javaCapitalName}}RpcServerHandler {{#service:extends}} extends {{service:javaPackage}}.{{service:javaCapitalName}}RpcServerHandler{{/service:extends}} implements com.facebook.thrift.server.RpcServerHandler { private final java.util.Map _methodMap; private final {{service:javaCapitalName}}.Reactive _delegate; {{#service:singleRequestFunctions}} private final java.util.List _{{function:javaName}}Readers; {{/service:singleRequestFunctions}} {{#service:streamingFunctions}} private final java.util.List _{{function:javaName}}Readers; private final com.facebook.thrift.server.generated.SingleRequestStreamResponseDelegate<{{#function:return_type}}{{> StreamReturnType}}{{/function:return_type}}> _delegate_{{function:javaName}}; {{^function:stream_has_first_response?}} private final com.facebook.thrift.server.generated.StreamResponseHandler<{{#function:return_type}}{{#function:stream_elem_type}}{{> BoxedType}}{{/function:stream_elem_type}}{{/function:return_type}}> _handler_{{function:javaName}}; {{/function:stream_has_first_response?}} {{#function:stream_has_first_response?}} private final com.facebook.thrift.server.generated.StreamWithFirstResponseHandler<{{#function:return_type}}{{#function:stream_first_response_type}}{{> BoxedType}}{{/function:stream_first_response_type}},{{#function:stream_elem_type}}{{> BoxedType}}{{/function:stream_elem_type}}{{/function:return_type}}> _handler_{{function:javaName}}; {{/function:stream_has_first_response?}} {{/service:streamingFunctions}} private final java.util.List _eventHandlers; public {{service:javaCapitalName}}RpcServerHandler({{service:javaCapitalName}} _delegate, java.util.List _eventHandlers) { this(new {{service:javaCapitalName}}BlockingReactiveWrapper(_delegate), _eventHandlers); } public {{service:javaCapitalName}}RpcServerHandler({{service:javaCapitalName}}.Async _delegate, java.util.List _eventHandlers) { this(new {{service:javaCapitalName}}AsyncReactiveWrapper(_delegate), _eventHandlers); } public {{service:javaCapitalName}}RpcServerHandler({{service:javaCapitalName}}.Reactive _delegate, java.util.List _eventHandlers) { {{#service:extends}}super(_delegate, _eventHandlers);{{/service:extends}} this._methodMap = new java.util.HashMap<>(); this._delegate = _delegate; this._eventHandlers = _eventHandlers; {{#service:singleRequestFunctions}} _methodMap.put("{{function:javaName}}", this); _{{function:javaName}}Readers = _create_{{function:javaName}}_request_readers(); {{/service:singleRequestFunctions}} {{#service:streamingFunctions}} _methodMap.put("{{function:javaName}}", this); _{{function:javaName}}Readers = _create_{{function:javaName}}_request_readers(); _delegate_{{function:javaName}} = (java.util.List _list) -> { java.util.Iterator _iterator = _list.iterator(); {{#function:args}} {{#field:type}}{{> Type}}{{/field:type}} {{field:javaName}} = ({{#field:type}}{{> Type}}{{/field:type}}) _iterator.next(); {{/function:args}} return _delegate.{{function:javaName}}({{#function:args}}{{field:javaName}}{{^last?}}, {{/last?}}{{/function:args}}); }; _handler_{{function:javaName}} = {{^function:stream_has_first_response?}} new com.facebook.thrift.server.generated.StreamResponseHandler<>( _delegate_{{function:javaName}}, _ResponseWriterFactory_{{function:javaName}}, _{{function:javaName}}Readers, "{{function:javaName}}", new Class[]{ {{#function:exceptions?}}{{#function:exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}.class{{^last?}},{{/last?}}{{/function:exceptions}}{{/function:exceptions?}} }, new Integer[]{ {{#function:exceptions?}}{{#function:exceptions}}{{field:key}}{{^last?}},{{/last?}}{{/function:exceptions}}{{/function:exceptions?}} }, new Class[]{ {{#function:stream_exceptions?}}{{#function:stream_exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}.class{{^last?}},{{/last?}}{{/function:stream_exceptions}}{{/function:stream_exceptions?}} }, new Integer[]{ {{#function:stream_exceptions?}}{{#function:stream_exceptions}}{{field:key}}{{^last?}},{{/last?}}{{/function:stream_exceptions}}{{/function:stream_exceptions?}} } ); {{/function:stream_has_first_response?}} {{#function:stream_has_first_response?}} new com.facebook.thrift.server.generated.StreamWithFirstResponseHandler<>( _delegate_{{function:javaName}}, _first_ResponseWriterFactory_{{function:javaName}}, _ResponseWriterFactory_{{function:javaName}}, _{{function:javaName}}Readers, "{{function:javaName}}", new Class[]{ {{#function:exceptions?}}{{#function:exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}.class{{^last?}},{{/last?}}{{/function:exceptions}}{{/function:exceptions?}} }, new Integer[]{ {{#function:exceptions?}}{{#function:exceptions}}{{field:key}}{{^last?}},{{/last?}}{{/function:exceptions}}{{/function:exceptions?}} }, new Class[]{ {{#function:stream_exceptions?}}{{#function:stream_exceptions}}{{#field:type}}{{> BoxedType}}{{/field:type}}.class{{^last?}},{{/last?}}{{/function:stream_exceptions}}{{/function:stream_exceptions?}} }, new Integer[]{ {{#function:stream_exceptions?}}{{#function:stream_exceptions}}{{field:key}}{{^last?}},{{/last?}}{{/function:stream_exceptions}}{{/function:stream_exceptions?}} } ); {{/function:stream_has_first_response?}} {{/service:streamingFunctions}} } {{#service:streamingFunctions}} private final static java.util.List _create_{{function:javaName}}_request_readers() { java.util.List _readerList = new java.util.ArrayList<>(); {{#function:args}} {{function:unsetIsNested}} _readerList.add({{#field:type}}{{> TypeReader }}{{/field:type}}); {{/function:args}} return _readerList; } private final static com.facebook.thrift.server.generated.ResponseWriterFactory _ResponseWriterFactory_{{function:javaName}} = (java.lang.Object _o, com.facebook.swift.service.ContextChain _chain, com.facebook.thrift.payload.ServerRequestPayload _requestPayload) -> { return oprot -> { try { oprot.writeStructBegin(com.facebook.thrift.util.RpcPayloadUtil.TSTRUCT); {{function:unsetIsNested}} {{^function:voidType}} {{#function:return_type}} {{#function:stream_elem_type}} {{> Type}} _iter0 = ({{> Type}})_o; {{function:nestedDepth++}}{{> WriteResponseType}}{{function:nestedDepth--}} {{/function:stream_elem_type}} oprot.writeFieldEnd(); {{/function:return_type}} {{/function:voidType}} oprot.writeFieldStop(); oprot.writeStructEnd(); _chain.postWrite(_o); } catch (Throwable _e) { com.facebook.thrift.util.NettyUtil.releaseIfByteBufTProtocol(oprot); throw reactor.core.Exceptions.propagate(_e); } }; }; {{#function:stream_has_first_response?}} private final static com.facebook.thrift.server.generated.ResponseWriterFactory _first_ResponseWriterFactory_{{function:javaName}} = (java.lang.Object _o, com.facebook.swift.service.ContextChain _chain, com.facebook.thrift.payload.ServerRequestPayload _requestPayload) -> { return oprot -> { try { oprot.writeStructBegin(com.facebook.thrift.util.RpcPayloadUtil.TSTRUCT); {{function:unsetIsNested}} {{^function:voidType}} {{#function:return_type}} {{#function:stream_first_response_type}} {{> Type}} _iter0 = ({{> Type}})_o; {{function:nestedDepth++}}{{> WriteResponseType}}{{function:nestedDepth--}} {{/function:stream_first_response_type}} oprot.writeFieldEnd(); {{/function:return_type}} {{/function:voidType}} oprot.writeFieldStop(); oprot.writeStructEnd(); _chain.postWrite(_o); } catch (Throwable _e) { com.facebook.thrift.util.NettyUtil.releaseIfByteBufTProtocol(oprot); throw reactor.core.Exceptions.propagate(_e); } }; }; {{/function:stream_has_first_response?}} {{/service:streamingFunctions}} {{#service:requestResponseFunctions}} private static java.util.List _create_{{function:javaName}}_request_readers() { java.util.List _readerList = new java.util.ArrayList<>(); {{#function:args}} {{function:unsetIsNested}} _readerList.add({{#field:type}}{{> TypeReader }}{{/field:type}}); {{/function:args}} return _readerList; } private static com.facebook.thrift.payload.Writer _create_{{function:javaName}}_response_writer( final java.lang.Object _r, final com.facebook.swift.service.ContextChain _chain, final int _seqId) { return oprot -> { try { oprot.writeStructBegin(com.facebook.thrift.util.RpcPayloadUtil.TSTRUCT); {{function:unsetIsNested}} {{^function:voidType}} {{#function:return_type}} {{> Type}} _iter0 = ({{> Type}})_r; {{function:nestedDepth++}}{{> WriteResponseType}}{{function:nestedDepth--}} oprot.writeFieldEnd(); {{/function:return_type}} {{/function:voidType}} oprot.writeFieldStop(); oprot.writeStructEnd(); _chain.postWrite(_r); } catch (Throwable _e) { com.facebook.thrift.util.NettyUtil.releaseIfByteBufTProtocol(oprot); throw reactor.core.Exceptions.propagate(_e); } }; } {{#function:exceptions?}} private static com.facebook.thrift.payload.Writer _create_{{function:javaName}}_exception_writer( final Throwable _t, final com.facebook.swift.service.ContextChain _chain, final int _seqId, final short _fieldId) { return oprot -> { try { _chain.declaredUserException(_t); oprot.writeStructBegin(com.facebook.thrift.util.RpcPayloadUtil.TSTRUCT); oprot.writeFieldBegin( new TField("responseField", TType.STRUCT, _fieldId)); com.facebook.thrift.payload.ThriftSerializable _iter0 = (com.facebook.thrift.payload.ThriftSerializable)_t; _iter0.write0(oprot); oprot.writeFieldEnd(); oprot.writeFieldStop(); oprot.writeStructEnd(); _chain.postWriteException(_t); } catch (Throwable _e) { com.facebook.thrift.util.NettyUtil.releaseIfByteBufTProtocol(oprot); throw reactor.core.Exceptions.propagate(_e); } }; } {{/function:exceptions?}} private static reactor.core.publisher.Mono _do{{function:javaName}}( {{service:javaCapitalName}}.Reactive _delegate, com.facebook.thrift.payload.ServerRequestPayload _payload, java.util.List _readers, com.facebook.swift.service.ContextChain _chain) { _chain.preRead(); java.util.List_data = _payload.getData(_readers); java.util.Iterator _iterator = _data.iterator(); {{#function:args}} {{#field:type}}{{> Type}}{{/field:type}} {{field:javaName}} = ({{#field:type}}{{> Type}}{{/field:type}}) _iterator.next(); {{/function:args}} _chain.postRead(_data); reactor.core.publisher.Mono<{{#function:return_type}}{{> BoxedType}}{{/function:return_type}}> _delegateResponse = _delegate .{{function:javaName}}({{#function:args}}{{field:javaName}}{{^last?}}, {{/last?}}{{/function:args}}) .doFirst(() -> com.facebook.nifty.core.RequestContexts.setCurrentContext(_payload.getRequestContext())); reactor.core.publisher.Mono _internalResponse = _delegateResponse.map(_response -> { _chain.preWrite(_response); com.facebook.thrift.payload.ServerResponsePayload _serverResponsePayload = com.facebook.thrift.util.RpcPayloadUtil.createServerResponsePayload( _payload, _create_{{function:javaName}}_response_writer(_response, _chain, _payload.getMessageSeqId())); return _serverResponsePayload; }) .switchIfEmpty( reactor.core.publisher.Mono.fromSupplier( () -> { {{^function:voidType}} org.apache.thrift.TApplicationException _tApplicationException = new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, "method {{function:javaName}} returned null"); return com.facebook.thrift.util.RpcPayloadUtil.fromTApplicationException(_tApplicationException, _payload.getRequestRpcMetadata(), _chain); {{/function:voidType}} {{#function:voidType}} _chain.preWrite(null); return com.facebook.thrift.util.RpcPayloadUtil.createServerResponsePayload( _payload, _create_{{function:javaName}}_response_writer(null, _chain, _payload.getMessageSeqId())); {{/function:voidType}} } ) ) .onErrorResume(_t -> { _chain.preWriteException(_t); {{#function:exceptions}} if (_t instanceof {{#field:type}}{{> Type}}{{/field:type}}) { com.facebook.thrift.payload.Writer _exceptionWriter = _create_{{function:javaName}}_exception_writer(_t, _chain, _payload.getMessageSeqId(), (short) {{field:key}}); com.facebook.thrift.payload.ServerResponsePayload _serverResponsePayload = com.facebook.thrift.util.RpcPayloadUtil.createServerResponsePayload( _payload, _exceptionWriter, _t.getMessage()); return reactor.core.publisher.Mono.just(_serverResponsePayload); } {{/function:exceptions}} {{#function:exceptions?}} else { {{/function:exceptions?}} // exception is not of user declared type String _errorMessage = String.format("Internal error processing {{function:javaName}}: %s", _t.getMessage() == null ? "" : _t.getMessage()); org.apache.thrift.TApplicationException _tApplicationException = new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.INTERNAL_ERROR, _errorMessage); _tApplicationException.initCause(_t); com.facebook.thrift.payload.ServerResponsePayload _serverResponsePayload = com.facebook.thrift.util.RpcPayloadUtil.fromTApplicationException(_tApplicationException, _payload.getRequestRpcMetadata(), _chain); return reactor.core.publisher.Mono.just(_serverResponsePayload); {{#function:exceptions?}} } {{/function:exceptions?}} }) .doFinally(__ -> { _chain.done(); }); return _internalResponse; } {{/service:requestResponseFunctions}} {{#service:onewayFunctions}} private static java.util.List _create_{{function:javaName}}_request_readers() { java.util.List _readerList = new java.util.ArrayList<>(); {{#function:args}} {{function:unsetIsNested}} _readerList.add({{#field:type}}{{> TypeReader }}{{/field:type}}); {{/function:args}} return _readerList; } private static reactor.core.publisher.Mono _do{{function:javaName}}( {{service:javaCapitalName}}.Reactive _delegate, com.facebook.thrift.payload.ServerRequestPayload _payload, java.util.List _readers, com.facebook.swift.service.ContextChain _chain) { _chain.preRead(); java.util.List_data = _payload.getData(_readers); java.util.Iterator _iterator = _data.iterator(); {{#function:args}} {{#field:type}}{{> Type}}{{/field:type}} {{field:javaName}} = ({{#field:type}}{{> Type}}{{/field:type}}) _iterator.next(); {{/function:args}} _chain.postRead(_data); reactor.core.publisher.Mono _internalResponse = _delegate.{{function:javaName}}({{#function:args}}{{field:javaName}}{{^last?}}, {{/last?}}{{/function:args}}); return _internalResponse; } {{/service:onewayFunctions}} @java.lang.Override public reactor.core.publisher.Flux singleRequestStreamingResponse(com.facebook.thrift.payload.ServerRequestPayload _payload) { final String _name = _payload.getRequestRpcMetadata().getName(); reactor.core.publisher.Flux _retVal = reactor.core.publisher.Flux.defer(() -> { com.facebook.swift.service.ContextChain _chain; try { _chain = new com.facebook.swift.service.ContextChain(_eventHandlers, _name, _payload.getRequestContext()); } catch (Throwable _t) { org.apache.thrift.TApplicationException _tApplicationException = new org.apache.thrift.TApplicationException(_t.getMessage()); com.facebook.thrift.payload.ServerResponsePayload _serverResponsePayload = com.facebook.thrift.util.RpcPayloadUtil.fromTApplicationException(_tApplicationException, _payload.getRequestRpcMetadata(), null); return reactor.core.publisher.Flux.just(_serverResponsePayload); } reactor.core.publisher.Flux _result; try { switch(_name) { {{#service:streamingFunctions}} case "{{function:javaName}}": _result = _handler_{{function:javaName}}.handleStream(_payload, _chain); break; {{/service:streamingFunctions}} default: { {{#service:extends}} _result = super.singleRequestStreamingResponse(_payload); {{/service:extends}} {{^service:extends}} _chain.preRead(); org.apache.thrift.TApplicationException _tApplicationException = new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.UNKNOWN_METHOD, "no method found with name " + _name); com.facebook.thrift.payload.ServerResponsePayload _serverResponsePayload = com.facebook.thrift.util.RpcPayloadUtil.fromTApplicationException(_tApplicationException, _payload.getRequestRpcMetadata(), _chain); return reactor.core.publisher.Flux.just(_serverResponsePayload); {{/service:extends}} } } } catch (org.apache.thrift.TApplicationException _tApplicationException) { com.facebook.thrift.payload.ServerResponsePayload _serverResponsePayload = com.facebook.thrift.util.RpcPayloadUtil.fromTApplicationException(_tApplicationException, _payload.getRequestRpcMetadata(), _chain); return reactor.core.publisher.Flux.just(_serverResponsePayload); } catch (Throwable _t) { _result = reactor.core.publisher.Flux.error(_t); } return _result; }); if (com.facebook.thrift.util.resources.RpcResources.isForceExecutionOffEventLoop()) { _retVal = _retVal.subscribeOn(com.facebook.thrift.util.resources.RpcResources.getOffLoopScheduler()); } return _retVal; } @java.lang.Override public reactor.core.publisher.Mono singleRequestSingleResponse(com.facebook.thrift.payload.ServerRequestPayload _payload) { final String _name = _payload.getRequestRpcMetadata().getName(); reactor.core.publisher.Mono _retVal = reactor.core.publisher.Mono.defer(() -> { com.facebook.swift.service.ContextChain _chain; try { _chain = new com.facebook.swift.service.ContextChain(_eventHandlers, _name, _payload.getRequestContext()); } catch (Throwable _t) { org.apache.thrift.TApplicationException _tApplicationException = new org.apache.thrift.TApplicationException(_t.getMessage()); com.facebook.thrift.payload.ServerResponsePayload _serverResponsePayload = com.facebook.thrift.util.RpcPayloadUtil.fromTApplicationException(_tApplicationException, _payload.getRequestRpcMetadata(), null); return reactor.core.publisher.Mono.just(_serverResponsePayload); } reactor.core.publisher.Mono _result; try { switch (_name) { {{#service:requestResponseFunctions}} case "{{function:javaName}}": _result = _do{{function:javaName}}(_delegate, _payload, _{{function:javaName}}Readers, _chain); break; {{/service:requestResponseFunctions}} default: { {{#service:extends}} _result = super.singleRequestSingleResponse(_payload); {{/service:extends}} {{^service:extends}} _chain.preRead(); org.apache.thrift.TApplicationException _tApplicationException = new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.UNKNOWN_METHOD, "no method found with name " + _name); com.facebook.thrift.payload.ServerResponsePayload _serverResponsePayload = com.facebook.thrift.util.RpcPayloadUtil.fromTApplicationException(_tApplicationException, _payload.getRequestRpcMetadata(), _chain); return reactor.core.publisher.Mono.just(_serverResponsePayload); {{/service:extends}} } } } catch (org.apache.thrift.TApplicationException _tApplicationException) { com.facebook.thrift.payload.ServerResponsePayload _serverResponsePayload = com.facebook.thrift.util.RpcPayloadUtil.fromTApplicationException(_tApplicationException, _payload.getRequestRpcMetadata(), _chain); return reactor.core.publisher.Mono.just(_serverResponsePayload); } catch (Throwable _t) { _result = reactor.core.publisher.Mono.error(_t); } return _result; }); if (com.facebook.thrift.util.resources.RpcResources.isForceExecutionOffEventLoop()) { _retVal = _retVal.subscribeOn(com.facebook.thrift.util.resources.RpcResources.getOffLoopScheduler()); } return _retVal; } @java.lang.Override public reactor.core.publisher.Mono singleRequestNoResponse(com.facebook.thrift.payload.ServerRequestPayload _payload) { final String _name = _payload.getRequestRpcMetadata().getName(); reactor.core.publisher.Mono _retVal = reactor.core.publisher.Mono.defer(() -> { com.facebook.swift.service.ContextChain _chain; try { _chain = new com.facebook.swift.service.ContextChain(_eventHandlers, _name, _payload.getRequestContext()); } catch (Throwable _t) { return reactor.core.publisher.Mono.error(_t); } reactor.core.publisher.Mono _result; try { switch (_name) { {{#service:onewayFunctions}} case "{{function:javaName}}": _result = _do{{function:javaName}}(_delegate, _payload, _{{function:javaName}}Readers, _chain); break; {{/service:onewayFunctions}} default: { {{#service:extends}} _result = super.singleRequestNoResponse(_payload); {{/service:extends}} {{^service:extends}} _result = reactor.core.publisher.Mono.error(new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.UNKNOWN_METHOD, "no method found with name " + _name)); {{/service:extends}} } } } catch (Throwable _t) { _result = reactor.core.publisher.Mono.error(_t); } return _result; }); if (com.facebook.thrift.util.resources.RpcResources.isForceExecutionOffEventLoop()) { _retVal = _retVal.subscribeOn(com.facebook.thrift.util.resources.RpcResources.getOffLoopScheduler()); } return _retVal; } public java.util.Map getMethodMap() { {{#service:extends}} java.util.Map _combined = new java.util.HashMap(); _combined.putAll(_methodMap); _combined.putAll(super.getMethodMap()); return _combined; {{/service:extends}} {{^service:extends}} return _methodMap; {{/service:extends}} } }