
io.fluxcapacitor.javaclient.web.DefaultSocketSession Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-client Show documentation
Show all versions of java-client Show documentation
Default Java client library for interfacing with Flux Capacitor.
/*
* Copyright (c) Flux Capacitor IP B.V. or its affiliates. All Rights Reserved.
*
* 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.
*/
package io.fluxcapacitor.javaclient.web;
import io.fluxcapacitor.common.Guarantee;
import io.fluxcapacitor.common.ObjectUtils;
import io.fluxcapacitor.common.api.Data;
import io.fluxcapacitor.common.handling.Handler;
import io.fluxcapacitor.common.handling.HandlerInvoker;
import io.fluxcapacitor.common.serialization.JsonUtils;
import io.fluxcapacitor.common.tracking.TaskScheduler;
import io.fluxcapacitor.javaclient.common.Message;
import io.fluxcapacitor.javaclient.common.serialization.DeserializingMessage;
import io.fluxcapacitor.javaclient.publishing.ResultGateway;
import io.fluxcapacitor.javaclient.tracking.handling.Request;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Value;
import lombok.experimental.Accessors;
import lombok.experimental.Delegate;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import static java.util.Optional.ofNullable;
@AllArgsConstructor
@Slf4j
public class DefaultSocketSession implements SocketSession {
@Getter
@Accessors(fluent = true)
private final String sessionId;
private final String target;
@Getter
private final String url;
@Getter
private final Map> headers;
private final ResultGateway webResponseGateway;
private final TaskScheduler taskScheduler;
private final BiConsumer abortCallback;
private final AtomicBoolean closed = new AtomicBoolean();
private final Map> pendingRequests = new ConcurrentHashMap<>();
@Override
public CompletableFuture sendMessage(Object value, Guarantee guarantee) {
return sendMessage(Message.asMessage(value).addMetadata("function", "message"), guarantee);
}
CompletableFuture sendMessage(Message message, Guarantee guarantee) {
return webResponseGateway.respond(message.getPayload(), message.getMetadata().with("sessionId", sessionId),
target, null, guarantee);
}
@Override
public CompletionStage sendRequest(Request request, Duration timeout) {
SocketRequest socketRequest = SocketRequest.valueOf(request);
CompletableFuture response = taskScheduler.orTimeout(new CompletableFuture()
.whenComplete((r, e) -> pendingRequests.remove(socketRequest.getRequestId())), timeout);
pendingRequests.put(socketRequest.getRequestId(), new PendingRequest<>(request, response));
if (isOpen()) {
try {
sendMessage(socketRequest);
} catch (Throwable e) {
log.error("Failed to send request {}", request, e);
response.completeExceptionally(e);
}
} else {
response.completeExceptionally(
new IllegalStateException("Websocket session %s is no longer open".formatted(sessionId)));
}
return response;
}
public Optional tryHandleRequest(DeserializingMessage message,
Handler handler) {
SocketRequest request;
try {
request = JsonUtils.fromJson(message.getSerializedObject().getData().getValue(), SocketRequest.class);
} catch (Throwable ignored) {
return Optional.empty();
}
if (!request.isValid()) {
return Optional.empty();
}
return message.withData(new Data<>(JsonUtils.asBytes(request.getRequest()), null, 0))
.apply(m -> handler.getInvoker(m)
.map(i -> new HandlerInvoker.DelegatingHandlerInvoker(i) {
@Override
public Object invoke(BiFunction
© 2015 - 2025 Weber Informatics LLC | Privacy Policy