ConnectionOpenMethodHandler.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.server.handler;
022 
023 import org.apache.qpid.AMQException;
024 import org.apache.qpid.framing.*;
025 import org.apache.qpid.protocol.AMQConstant;
026 import org.apache.qpid.server.protocol.AMQProtocolSession;
027 import org.apache.qpid.server.security.access.Permission;
028 import org.apache.qpid.server.state.AMQState;
029 import org.apache.qpid.server.state.AMQStateManager;
030 import org.apache.qpid.server.state.StateAwareMethodListener;
031 import org.apache.qpid.server.virtualhost.VirtualHost;
032 import org.apache.log4j.Logger;
033 
034 public class ConnectionOpenMethodHandler implements StateAwareMethodListener<ConnectionOpenBody>
035 {
036     private static final Logger _logger = Logger.getLogger(ConnectionOpenMethodHandler.class);
037 
038     private static ConnectionOpenMethodHandler _instance = new ConnectionOpenMethodHandler();
039 
040     public static ConnectionOpenMethodHandler getInstance()
041     {
042         return _instance;
043     }
044 
045     private ConnectionOpenMethodHandler()
046     {
047     }
048 
049     private static AMQShortString generateClientID()
050     {
051         return new AMQShortString(Long.toString(System.currentTimeMillis()));
052     }
053 
054     public void methodReceived(AMQStateManager stateManager, ConnectionOpenBody body, int channelIdthrows AMQException
055     {
056         AMQProtocolSession session = stateManager.getProtocolSession();
057 
058 
059         //ignore leading '/'
060         String virtualHostName;
061         if ((body.getVirtualHost() != null&& body.getVirtualHost().charAt(0== '/')
062         {
063             virtualHostName = new StringBuilder(body.getVirtualHost().subSequence(1, body.getVirtualHost().length())).toString();
064         }
065         else
066         {
067             virtualHostName = body.getVirtualHost() == null null : String.valueOf(body.getVirtualHost());
068         }
069 
070         VirtualHost virtualHost = stateManager.getVirtualHostRegistry().getVirtualHost(virtualHostName);
071 
072         if (virtualHost == null)
073         {
074             throw body.getConnectionException(AMQConstant.NOT_FOUND, "Unknown virtual host: '" + virtualHostName + "'");
075         }
076         else
077         {
078             session.setVirtualHost(virtualHost);
079 
080             //Perform ACL
081             if (!virtualHost.getAccessManager().authoriseConnect(session, virtualHost))
082             {
083                 throw body.getConnectionException(AMQConstant.ACCESS_REFUSED, "Permission denied");
084             }
085 
086             // See Spec (0.8.2). Section  3.1.2 Virtual Hosts
087             if (session.getContextKey() == null)
088             {
089                 session.setContextKey(generateClientID());
090             }
091 
092             MethodRegistry methodRegistry = session.getMethodRegistry();
093             AMQMethodBody responseBody = methodRegistry.createConnectionOpenOkBody(body.getVirtualHost());
094 
095             stateManager.changeState(AMQState.CONNECTION_OPEN);
096 
097             session.writeFrame(responseBody.generateFrame(channelId));
098 
099             
100         }
101     }
102 }