/** * Creates a new decorator that supports all thrift protocols and defaults to * {@link ThriftSerializationFormats#BINARY TBinary} protocol when the client doesn't specify one. * * <p>Currently, the only way to specify a serialization format is by using the HTTP session * protocol and setting the Content-Type header to the appropriate {@link SerializationFormat#mediaType()}. */ public static Function<Service<RpcRequest, RpcResponse>, THttpService> newDecorator() { return newDecorator(ThriftSerializationFormats.BINARY); }
/** * Creates a new multiplexed {@link ThriftCallService} with the specified service implementations. * * @param implementations a {@link Map} whose key is service name and value is the implementation of * {@code *.Iface} or {@code *.AsyncIface} service interface generated by * the Apache Thrift compiler */ public static ThriftCallService of(Map<String, ?> implementations) { return new ThriftCallService(implementations); }
/** * Returns the information about the Thrift services being served. * * @return a {@link Map} whose key is a service name, which could be an empty string if this service * is not multiplexed */ public Map<String, ThriftServiceEntry> entries() { return thriftService.entries(); }
/** * Returns newly created {@link ObjectMapper} which is configured properly to serialize some knows classes * in a good way. */ public static ObjectMapper newObjectMapper(SimpleModule... userModules) { final ObjectMapper objectMapper = new ObjectMapper(); final SimpleModule module = new SimpleModule(); module.addSerializer(TMessage.class, new TMessageSerializer()); module.addSerializer(TBase.class, new TBaseSerializer()); module.addSerializer(TApplicationException.class, new TApplicationExceptionSerializer()); module.addSerializer(ThriftCall.class, new ThriftCallSerializer()); module.addSerializer(ThriftReply.class, new ThriftReplySerializer()); objectMapper.registerModule(module); for (SimpleModule userModule : userModules) { objectMapper.registerModule(userModule); } return objectMapper; }
/** * Creates a new multiplexed {@link THttpService} with the specified service implementations, supporting * the protocols specified in {@code allowedSerializationFormats} and defaulting to the specified * {@code defaultSerializationFormat} when the client doesn't specify one. * * <p>Currently, the only way to specify a serialization format is by using the HTTP session protocol and * setting the Content-Type header to the appropriate {@link SerializationFormat#mediaType()}. * * @param implementations a {@link Map} whose key is service name and value is the implementation of * {@code *.Iface} or {@code *.AsyncIface} service interface generated by * the Apache Thrift compiler * @param defaultSerializationFormat the default serialization format to use when not specified by the * client * @param otherAllowedSerializationFormats other serialization formats that should be supported by this * service in addition to the default */ public static THttpService ofFormats( Map<String, ?> implementations, SerializationFormat defaultSerializationFormat, Iterable<SerializationFormat> otherAllowedSerializationFormats) { return new THttpService(ThriftCallService.of(implementations), newAllowedSerializationFormats(defaultSerializationFormat, otherAllowedSerializationFormats)); }
/** * Creates a new {@link THttpService} with the specified service implementation, supporting all thrift * protocols and defaulting to {@link ThriftSerializationFormats#BINARY TBinary} protocol when the client * doesn't specify one. * * <p>Currently, the only way to specify a serialization format is by using the HTTP session * protocol and setting the Content-Type header to the appropriate {@link SerializationFormat#mediaType()}. * * @param implementation an implementation of {@code *.Iface} or {@code *.AsyncIface} service interface * generated by the Apache Thrift compiler */ public static THttpService of(Object implementation) { return of(implementation, ThriftSerializationFormats.BINARY); }
private static void handleException( ServiceRequestContext ctx, RpcResponse rpcRes, CompletableFuture<HttpResponse> httpRes, SerializationFormat serializationFormat, int seqId, ThriftFunction func, Throwable cause) { final TBase<?, ?> result = func.newResult(); final HttpData content; if (func.setException(result, cause)) { content = encodeSuccess(ctx, rpcRes, serializationFormat, func.name(), seqId, result); } else { content = encodeException(ctx, rpcRes, serializationFormat, seqId, func.name(), cause); } respond(serializationFormat, content, httpRes); }
/** * Creates a new decorator that supports the protocols specified in {@code allowedSerializationFormats} and * defaults to the specified {@code defaultSerializationFormat} when the client doesn't specify one. * Currently, the only way to specify a serialization format is by using the HTTP session protocol and * setting the Content-Type header to the appropriate {@link SerializationFormat#mediaType()}. * * @param defaultSerializationFormat the default serialization format to use when not specified by the * client * @param otherAllowedSerializationFormats other serialization formats that should be supported by this * service in addition to the default */ public static Function<Service<RpcRequest, RpcResponse>, THttpService> newDecorator( SerializationFormat defaultSerializationFormat, Iterable<SerializationFormat> otherAllowedSerializationFormats) { final SerializationFormat[] allowedSerializationFormatArray = newAllowedSerializationFormats( defaultSerializationFormat, otherAllowedSerializationFormats); return delegate -> new THttpService(delegate, allowedSerializationFormatArray); }
private static void handlePreDecodeException( ServiceRequestContext ctx, CompletableFuture<HttpResponse> httpRes, Throwable cause, SerializationFormat serializationFormat, int seqId, String methodName) { final HttpData content = encodeException( ctx, new DefaultRpcResponse(cause), serializationFormat, seqId, methodName, cause); respond(serializationFormat, content, httpRes); }
private static void handleSuccess( ServiceRequestContext ctx, RpcResponse rpcRes, CompletableFuture<HttpResponse> httpRes, SerializationFormat serializationFormat, int seqId, ThriftFunction func, Object returnValue) { final TBase<?, ?> wrappedResult = func.newResult(); func.setSuccess(wrappedResult, returnValue); respond(serializationFormat, encodeSuccess(ctx, rpcRes, serializationFormat, func.name(), seqId, wrappedResult), httpRes); }
Entry build() { return new Entry(serviceType, endpointInfos); } }
protected abstract TTransport newTransport(String uri, HttpHeaders headers) throws TTransportException;
protected final TTransport newTransport(String scheme, String path) throws TTransportException { return newTransport(scheme, path, HttpHeaders.EMPTY_HEADERS); }
@Override @SuppressWarnings("unchecked") public void hello(String name, AsyncMethodCallback resultHandler) throws TException { resultHandler.onComplete(getResponse(name)); }
/** * Creates a new multiplexed {@link THttpService} with the specified service implementations, supporting * all thrift protocols and defaulting to {@link ThriftSerializationFormats#BINARY TBinary} protocol when * the client doesn't specify one. * * <p>Currently, the only way to specify a serialization format is by using the HTTP session * protocol and setting the Content-Type header to the appropriate {@link SerializationFormat#mediaType()}. * * @param implementations a {@link Map} whose key is service name and value is the implementation of * {@code *.Iface} or {@code *.AsyncIface} service interface generated by * the Apache Thrift compiler */ public static THttpService of(Map<String, ?> implementations) { return of(implementations, ThriftSerializationFormats.BINARY); }
/** * Creates a new {@link ThriftCallService} with the specified service implementation. * * @param implementation an implementation of {@code *.Iface} or {@code *.AsyncIface} service interface * generated by the Apache Thrift compiler */ public static ThriftCallService of(Object implementation) { return new ThriftCallService(ImmutableMap.of("", implementation)); }
@Override protected void configure(ServerBuilder sb) throws Exception { sb.service("/hello", THttpService.of(helloService)); } };
@Override protected void configure(ServerBuilder sb) throws Exception { sb.service("/serverIp", THttpService.of(handler)); } }