forked from rsocket/rsocket-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestHandler.java
More file actions
140 lines (112 loc) · 5.03 KB
/
RequestHandler.java
File metadata and controls
140 lines (112 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright 2015 Netflix, Inc.
*
* 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.reactivesocket;
import io.reactivesocket.internal.PublisherUtils;
import org.reactivestreams.Publisher;
import java.util.function.Function;
public abstract class RequestHandler {
public static final Function<Payload, Publisher<Payload>> NO_REQUEST_RESPONSE_HANDLER =
payload -> PublisherUtils.errorPayload(new RuntimeException("No 'requestResponse' handler"));
public static final Function<Payload, Publisher<Payload>> NO_REQUEST_STREAM_HANDLER =
payload -> PublisherUtils.errorPayload(new RuntimeException("No 'requestStream' handler"));
public static final Function<Payload, Publisher<Payload>> NO_REQUEST_SUBSCRIPTION_HANDLER =
payload -> PublisherUtils.errorPayload(new RuntimeException("No 'requestSubscription' handler"));
public static final Function<Payload, Publisher<Void>> NO_FIRE_AND_FORGET_HANDLER =
payload -> PublisherUtils.errorVoid(new RuntimeException("No 'fireAndForget' handler"));
public static final Function<Publisher<Payload>, Publisher<Payload>> NO_REQUEST_CHANNEL_HANDLER =
payloads -> PublisherUtils.errorPayload(new RuntimeException("No 'requestChannel' handler"));
public static final Function<Payload, Publisher<Void>> NO_METADATA_PUSH_HANDLER =
payload -> PublisherUtils.errorVoid(new RuntimeException("No 'metadataPush' handler"));
public abstract Publisher<Payload> handleRequestResponse(final Payload payload);
public abstract Publisher<Payload> handleRequestStream(final Payload payload);
public abstract Publisher<Payload> handleSubscription(final Payload payload);
public abstract Publisher<Void> handleFireAndForget(final Payload payload);
/**
* @note The initialPayload will also be part of the inputs publisher.
* It is there to simplify routing logic.
*/
public abstract Publisher<Payload> handleChannel(Payload initialPayload, final Publisher<Payload> inputs);
public abstract Publisher<Void> handleMetadataPush(final Payload payload);
public static class Builder
{
private Function<Payload, Publisher<Payload>> handleRequestResponse = NO_REQUEST_RESPONSE_HANDLER;
private Function<Payload, Publisher<Payload>> handleRequestStream = NO_REQUEST_STREAM_HANDLER;
private Function<Payload, Publisher<Payload>> handleRequestSubscription = NO_REQUEST_SUBSCRIPTION_HANDLER;
private Function<Payload, Publisher<Void>> handleFireAndForget = NO_FIRE_AND_FORGET_HANDLER;
private Function<Publisher<Payload>, Publisher<Payload>> handleRequestChannel = NO_REQUEST_CHANNEL_HANDLER;
private Function<Payload, Publisher<Void>> handleMetadataPush = NO_METADATA_PUSH_HANDLER;
public Builder withRequestResponse(final Function<Payload, Publisher<Payload>> handleRequestResponse)
{
this.handleRequestResponse = handleRequestResponse;
return this;
}
public Builder withRequestStream(final Function<Payload, Publisher<Payload>> handleRequestStream)
{
this.handleRequestStream = handleRequestStream;
return this;
}
public Builder withRequestSubscription(final Function<Payload, Publisher<Payload>> handleRequestSubscription)
{
this.handleRequestSubscription = handleRequestSubscription;
return this;
}
public Builder withFireAndForget(final Function<Payload, Publisher<Void>> handleFireAndForget)
{
this.handleFireAndForget = handleFireAndForget;
return this;
}
public Builder withRequestChannel(final Function<Publisher<Payload> , Publisher<Payload>> handleRequestChannel)
{
this.handleRequestChannel = handleRequestChannel;
return this;
}
public Builder withMetadataPush(final Function<Payload, Publisher<Void>> handleMetadataPush)
{
this.handleMetadataPush = handleMetadataPush;
return this;
}
public RequestHandler build()
{
return new RequestHandler()
{
public Publisher<Payload> handleRequestResponse(Payload payload)
{
return handleRequestResponse.apply(payload);
}
public Publisher<Payload> handleRequestStream(Payload payload)
{
return handleRequestStream.apply(payload);
}
public Publisher<Payload> handleSubscription(Payload payload)
{
return handleRequestSubscription.apply(payload);
}
public Publisher<Void> handleFireAndForget(Payload payload)
{
return handleFireAndForget.apply(payload);
}
public Publisher<Payload> handleChannel(Payload initialPayload, Publisher<Payload> inputs)
{
return handleRequestChannel.apply(inputs);
}
public Publisher<Void> handleMetadataPush(Payload payload)
{
return handleMetadataPush.apply(payload);
}
};
}
}
}