ratpack.stream.internal.WiretapPublisher Maven / Gradle / Ivy
/*
* Copyright 2014 the original author or authors.
*
* 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 ratpack.stream.internal;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import ratpack.api.Nullable;
import ratpack.func.Action;
import ratpack.stream.StreamEvent;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
public class WiretapPublisher implements Publisher {
private final Publisher publisher;
private final Action super StreamEvent super T>> listener;
private final AtomicInteger counter = new AtomicInteger();
public WiretapPublisher(Publisher publisher, Action super StreamEvent super T>> listener) {
this.publisher = publisher;
this.listener = listener;
}
@Override
public void subscribe(final Subscriber outSubscriber) {
final int subscriptionId = counter.getAndIncrement();
publisher.subscribe(new Subscriber() {
private Subscription subscription;
private final AtomicBoolean done = new AtomicBoolean();
@Override
public void onSubscribe(final Subscription subscription) {
this.subscription = subscription;
outSubscriber.onSubscribe(new Subscription() {
@Override
public void request(int n) {
try {
listener.execute(new RequestEvent<>(subscriptionId, n));
} catch (Throwable throwable) {
subscription.cancel();
onError(throwable);
return;
}
subscription.request(n);
}
@Override
public void cancel() {
try {
listener.execute(new CancelEvent<>(subscriptionId));
} catch (Throwable throwable) {
try {
subscription.cancel();
} catch (Throwable e) {
throwable.addSuppressed(e);
}
onError(throwable);
return;
}
subscription.cancel();
}
});
}
@Override
public void onNext(T in) {
try {
listener.execute(new DataEvent<>(subscriptionId, in));
} catch (Throwable throwable) {
subscription.cancel();
onError(throwable);
return;
}
if (!done.get()) {
outSubscriber.onNext(in);
}
}
@Override
public void onError(Throwable t) {
if (done.compareAndSet(false, true)) {
try {
listener.execute(new ErrorEvent<>(subscriptionId, t));
} catch (Throwable throwable) {
t.addSuppressed(throwable);
onError(t);
return;
}
outSubscriber.onError(t);
}
}
@Override
public void onComplete() {
if (done.compareAndSet(false, true)) {
try {
listener.execute(new CompletionEvent<>(subscriptionId));
} catch (Throwable throwable) {
outSubscriber.onError(throwable);
return;
}
outSubscriber.onComplete();
}
}
});
}
private static class DataEvent implements StreamEvent {
private final int subscriptionId;
private final T data;
private DataEvent(int subscriptionId, T data) {
this.subscriptionId = subscriptionId;
this.data = data;
}
public int getSubscriptionId() {
return subscriptionId;
}
@Override
public boolean isComplete() {
return false;
}
@Override
public boolean isError() {
return false;
}
@Override
public boolean isData() {
return true;
}
@Nullable
@Override
public Throwable getError() {
return null;
}
@Nullable
@Override
public T getItem() {
return data;
}
@Override
public boolean isCancel() {
return false;
}
@Override
public boolean isRequest() {
return false;
}
@Override
public int getRequestAmount() {
return 0;
}
}
private static class CompletionEvent implements StreamEvent {
private final int subscriptionId;
private CompletionEvent(int subscriptionId) {
this.subscriptionId = subscriptionId;
}
public int getSubscriptionId() {
return subscriptionId;
}
@Override
public boolean isComplete() {
return true;
}
@Override
public boolean isError() {
return false;
}
@Override
public boolean isData() {
return false;
}
@Nullable
@Override
public Throwable getError() {
return null;
}
@Nullable
@Override
public T getItem() {
return null;
}
@Override
public boolean isCancel() {
return false;
}
@Override
public boolean isRequest() {
return false;
}
@Override
public int getRequestAmount() {
return 0;
}
}
private static class ErrorEvent implements StreamEvent {
private final int subscriptionId;
private final Throwable error;
private ErrorEvent(int subscriptionId, Throwable error) {
this.subscriptionId = subscriptionId;
this.error = error;
}
public int getSubscriptionId() {
return subscriptionId;
}
@Override
public boolean isComplete() {
return false;
}
@Override
public boolean isError() {
return true;
}
@Override
public boolean isData() {
return false;
}
@Nullable
@Override
public Throwable getError() {
return error;
}
@Nullable
@Override
public T getItem() {
return null;
}
@Override
public boolean isCancel() {
return false;
}
@Override
public boolean isRequest() {
return false;
}
@Override
public int getRequestAmount() {
return 0;
}
}
private static class CancelEvent implements StreamEvent {
private final int subscriptionId;
private CancelEvent(int subscriptionId) {
this.subscriptionId = subscriptionId;
}
@Override
public int getSubscriptionId() {
return subscriptionId;
}
@Override
public boolean isComplete() {
return false;
}
@Override
public boolean isError() {
return false;
}
@Override
public boolean isData() {
return false;
}
@Override
public boolean isCancel() {
return true;
}
@Override
public boolean isRequest() {
return false;
}
@Override
public int getRequestAmount() {
return 0;
}
@Nullable
@Override
public Throwable getError() {
return null;
}
@Nullable
@Override
public T getItem() {
return null;
}
}
private static class RequestEvent implements StreamEvent {
private final int requestAmount;
private final int subscriptionId;
private RequestEvent(int requestAmount, int subscriptionId) {
this.requestAmount = requestAmount;
this.subscriptionId = subscriptionId;
}
@Override
public int getSubscriptionId() {
return subscriptionId;
}
@Override
public boolean isComplete() {
return false;
}
@Override
public boolean isError() {
return false;
}
@Override
public boolean isData() {
return false;
}
@Override
public boolean isCancel() {
return false;
}
@Override
public boolean isRequest() {
return true;
}
@Override
public int getRequestAmount() {
return requestAmount;
}
@Nullable
@Override
public Throwable getError() {
return null;
}
@Nullable
@Override
public T getItem() {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy