aboutsummaryrefslogtreecommitdiffstats
path: root/container-messagebus/src/main/java/com/yahoo/messagebus/jdisc/MbusRequestHandler.java
blob: 86e84beb01b75c951fd063490531528a0c53c84d (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 Yahoo. 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 Simon Thoresen Hult
 */
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) {

        }
    }
}