blob: 73c143eb29de43046fbc2f22a3516488821e7214 (
plain) (
blame)
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.jdisc;
import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.handler.AbstractRequestHandler;
import com.yahoo.jdisc.handler.CompletionHandler;
import com.yahoo.jdisc.handler.ContentChannel;
import com.yahoo.jdisc.handler.ResponseHandler;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.MessageHandler;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.ReplyHandler;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a>
*/
public abstract class MbusRequestHandler extends AbstractRequestHandler implements MessageHandler {
@Override
public ContentChannel handleRequest(final Request request, final ResponseHandler handler) {
if (!(request instanceof MbusRequest)) {
throw new UnsupportedOperationException("Expected MbusRequest, got " + request.getClass().getName() + ".");
}
final Message msg = ((MbusRequest)request).getMessage();
msg.pushHandler(new RespondingReplyHandler(handler));
handleMessage(msg);
return null;
}
private static class RespondingReplyHandler implements ReplyHandler {
private final ResponseHandler handler;
RespondingReplyHandler(final ResponseHandler handler) {
this.handler = handler;
}
@Override
public void handleReply(final Reply reply) {
final MbusResponse response = new MbusResponse(StatusCodes.fromMbusReply(reply), reply);
handler.handleResponse(response).close(IgnoringCompletionHandler.INSTANCE);
}
}
private static class IgnoringCompletionHandler implements CompletionHandler {
public static final IgnoringCompletionHandler INSTANCE = new IgnoringCompletionHandler();
@Override
public void completed() {
}
@Override
public void failed(final Throwable t) {
}
}
}
|