summaryrefslogtreecommitdiff
path: root/java/common/src/main/java/org/apache/qpid/transport/ConnectionDelegate.java
blob: 2aa1db7b288f9578e5bb735607bc0950fa3cc83a (plain)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.qpid.transport;

import org.apache.qpid.transport.util.Logger;

import org.apache.qpid.SecurityHelper;
import org.apache.qpid.QpidException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

import javax.security.sasl.Sasl;
import javax.security.sasl.SaslClient;
import javax.security.sasl.SaslException;
import javax.security.sasl.SaslServer;


/**
 * ConnectionDelegate
 *
 * @author Rafael H. Schloming
 */

/**
 * Currently only implemented client specific methods
 * the server specific methods are dummy impls for testing
 *
 * the connectionClose is kind of different for both sides
 */
public abstract class ConnectionDelegate extends MethodDelegate<Channel>
{

    private static final Logger log = Logger.get(ConnectionDelegate.class);

    private String _username = "guest";
    private String _password = "guest";;
    private String _mechanism;
    private String _virtualHost;
    private SaslClient saslClient;
    private SaslServer saslServer;
    private String _locale = "utf8";
    private int maxFrame = 64*1024;
    private Condition _negotiationComplete;
    private Lock _negotiationCompleteLock;

    public abstract SessionDelegate getSessionDelegate();

    public abstract void exception(Throwable t);

    public abstract void closed();

    public void setCondition(Lock negotiationCompleteLock,Condition negotiationComplete)
    {
        _negotiationComplete = negotiationComplete;
        _negotiationCompleteLock = negotiationCompleteLock;
    }

    public void init(Channel ch, ProtocolHeader hdr)
    {
        ch.getConnection().send(new ProtocolHeader (1, hdr.getMajor(), hdr.getMinor()));
        List<Object> plain = new ArrayList<Object>();
        plain.add("PLAIN");
        List<Object> utf8 = new ArrayList<Object>();
        utf8.add("utf8");
        ch.connectionStart(null, plain, utf8);
    }

    // ----------------------------------------------
    //           Client side
    //-----------------------------------------------
    @Override public void connectionStart(Channel context, ConnectionStart struct)
    {
        String mechanism = null;
        byte[] response = null;
        try
        {
            mechanism = SecurityHelper.chooseMechanism(struct.getMechanisms());
            saslClient = Sasl.createSaslClient(new String[]{ mechanism },null, "AMQP", "localhost", null,
                                                  SecurityHelper.createCallbackHandler(mechanism,_username,_password ));
            response = saslClient.evaluateChallenge(new byte[0]);
        }
        catch (UnsupportedEncodingException e)
        {
           // need error handling
        }
        catch (SaslException e)
        {
          // need error handling
        }
        catch (QpidException e)
        {
          //  need error handling
        }

        Map<String,Object> props = new HashMap<String,Object>();
        context.connectionStartOk(props, mechanism, response, _locale);
    }

    @Override public void connectionSecure(Channel context, ConnectionSecure struct)
    {
        try
        {
            byte[] response = saslClient.evaluateChallenge(struct.getChallenge());
            context.connectionSecureOk(response);
        }
        catch (SaslException e)
        {
          // need error handling
        }
    }

    @Override public void connectionTune(Channel context, ConnectionTune struct)
    {
        context.getConnection().setChannelMax(struct.getChannelMax());
        context.connectionTuneOk(struct.getChannelMax(), struct.getMaxFrameSize(), struct.getHeartbeatMax());
        context.connectionOpen(_virtualHost, null, Option.INSIST);
    }


    @Override public void connectionOpenOk(Channel context, ConnectionOpenOk struct)
    {
        List<Object> knownHosts = struct.getKnownHosts();
        if(_negotiationCompleteLock != null)
        {
            _negotiationCompleteLock.lock();
            try
            {
                _negotiationComplete.signalAll();
            }
            finally
            {
                _negotiationCompleteLock.unlock();
            }
        }
    }

    public void connectionRedirect(Channel context, ConnectionRedirect struct)
    {
        // not going to bother at the moment
    }

    //  ----------------------------------------------
    //           Server side
    //-----------------------------------------------
    @Override public void connectionStartOk(Channel context, ConnectionStartOk struct)
    {
        //set the client side locale on the server side
        _locale = struct.getLocale();
        _mechanism = struct.getMechanism();

        //try
        //{
            //saslServer = Sasl.createSaslServer(_mechanism, "AMQP", "ABC",null,SecurityHelper.createCallbackHandler(_mechanism,_username,_password));
            //byte[] challenge = saslServer.evaluateResponse(struct.getResponse().getBytes());
            byte[] challenge = null;
            if ( challenge == null)
            {
                context.connectionTune(Integer.MAX_VALUE, maxFrame, 0, Integer.MAX_VALUE);
            }
            else
            {
                try
                {
                    context.connectionSecure(challenge);
                }
                catch(Exception e)
                {

                }
            }


        /*}
        catch (SaslException e)
        {
          // need error handling
        }
        catch (QpidException e)
        {
          //  need error handling
        }*/
    }

    @Override public void connectionSecureOk(Channel context, ConnectionSecureOk struct)
    {
        try
        {
            saslServer = Sasl.createSaslServer(_mechanism, "AMQP", "ABC",new HashMap(),SecurityHelper.createCallbackHandler(_mechanism,_username,_password));
            byte[] challenge = saslServer.evaluateResponse(struct.getResponse());
            if ( challenge == null)
            {
                context.connectionTune(Integer.MAX_VALUE, maxFrame, 0, Integer.MAX_VALUE);
            }
            else
            {
                try
                {
                    context.connectionSecure(challenge);
                }
                catch(Exception e)
                {

                }
            }


        }
        catch (SaslException e)
        {
          // need error handling
        }
        catch (QpidException e)
        {
          //  need error handling
        }
    }


    @Override public void connectionOpen(Channel context, ConnectionOpen struct)
    {
        List<Object> hosts = new ArrayList<Object>();
        hosts.add("amqp:1223243232325");
        context.connectionOpenOk(hosts);
    }

    @Override public void connectionClose(Channel ch, ConnectionClose close)
    {
        ch.getConnection().closeCode(close);
        ch.connectionCloseOk();
    }

    public String getPassword()
    {
        return _password;
    }

    public void setPassword(String password)
    {
        _password = password;
    }

    public String getUsername()
    {
        return _username;
    }

    public void setUsername(String username)
    {
        _username = username;
    }

    public String getVirtualHost()
    {
        return _virtualHost;
    }

    public void setVirtualHost(String host)
    {
        _virtualHost = host;
    }

}