View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.gshell.remote.server.handler;
21  
22  import org.apache.geronimo.gshell.remote.crypto.CryptoContext;
23  import org.apache.geronimo.gshell.remote.message.ConnectMessage;
24  import org.apache.geronimo.gshell.remote.server.RshServer;
25  import org.apache.geronimo.gshell.remote.server.timeout.TimeoutManager;
26  import org.apache.mina.common.IoSession;
27  import org.codehaus.plexus.component.annotations.Component;
28  import org.codehaus.plexus.component.annotations.Requirement;
29  
30  /**
31   * ???
32   *
33   * @version $Rev: 580731 $ $Date: 2007-09-30 07:52:42 -0700 (Sun, 30 Sep 2007) $
34   */
35  @Component(role=ServerMessageHandler.class, hint="connect")
36  public class ConnectHandler
37      extends ServerMessageHandlerSupport<ConnectMessage>
38  {
39      @Requirement
40      private CryptoContext crypto;
41      
42      @Requirement
43      private TimeoutManager timeoutManager;
44      
45      public ConnectHandler() {
46          super(ConnectMessage.class);
47      }
48  
49      public void handle(final IoSession session, final ServerSessionContext context, final ConnectMessage message) throws Exception {
50          // Try to cancel the timeout task
51          if (!timeoutManager.cancelTimeout(session)) {
52              log.warn("Aborting handshake processing; timeout has triggered");
53          }
54          else {
55              // Hold on to the client's public key
56              context.pk = message.getPublicKey();
57  
58              // Reply to the client with some details about the connection
59              ConnectMessage.Result reply = new ConnectMessage.Result(crypto.getPublicKey());
60              reply.setCorrelationId(message.getId());
61              session.write(reply);
62  
63              // Schedule a task to timeout the login process
64              timeoutManager.scheduleTimeout(session, RshServer.AUTH_TIMEOUT, new Runnable() {
65                  public void run() {
66                      log.error("Timeout waiting for login from: {}", session.getRemoteAddress());
67                      session.close();
68                  }
69              });
70          }
71      }
72  }