001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.transport.ws.jetty9;
018
019import java.io.IOException;
020import java.util.concurrent.TimeUnit;
021
022import org.apache.activemq.transport.stomp.Stomp;
023import org.apache.activemq.transport.stomp.StompFrame;
024import org.apache.activemq.transport.ws.AbstractStompSocket;
025import org.eclipse.jetty.websocket.api.Session;
026import org.eclipse.jetty.websocket.api.WebSocketListener;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030/**
031 * Implements web socket and mediates between servlet and the broker
032 */
033public class StompSocket extends AbstractStompSocket implements WebSocketListener {
034
035    private static final Logger LOG = LoggerFactory.getLogger(StompSocket.class);
036
037    private final int ORDERLY_CLOSE_TIMEOUT = 10;
038
039    private Session session;
040
041    public StompSocket(String remoteAddress) {
042        super(remoteAddress);
043    }
044
045    @Override
046    public void sendToStomp(StompFrame command) throws IOException {
047        session.getRemote().sendString(command.format());
048    }
049
050    @Override
051    public void handleStopped() throws IOException {
052        if (session != null && session.isOpen()) {
053            session.close();
054        }
055    }
056
057    //----- WebSocketListener event callbacks --------------------------------//
058
059    @Override
060    public void onWebSocketBinary(byte[] arg0, int arg1, int arg2) {
061    }
062
063    @Override
064    public void onWebSocketClose(int arg0, String arg1) {
065        try {
066            if (protocolLock.tryLock() || protocolLock.tryLock(ORDERLY_CLOSE_TIMEOUT, TimeUnit.SECONDS)) {
067                LOG.info("Stomp WebSocket closed: code[{}] message[{}]", arg0, arg1);
068                protocolConverter.onStompCommand(new StompFrame(Stomp.Commands.DISCONNECT));
069            }
070        } catch (Exception e) {
071            LOG.warn("Failed to close WebSocket", e);
072        } finally {
073            if (protocolLock.isHeldByCurrentThread()) {
074                protocolLock.unlock();
075            }
076        }
077    }
078
079    @Override
080    public void onWebSocketConnect(Session session) {
081        this.session = session;
082    }
083
084    @Override
085    public void onWebSocketError(Throwable arg0) {
086    }
087
088    @Override
089    public void onWebSocketText(String data) {
090        processStompFrame(data);
091    }
092}