/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.glassfish.comms.clb.proxy.outbound;
import com.sun.grizzly.CallbackHandler;
import com.sun.grizzly.ConnectorHandler;
import com.sun.grizzly.Context;
import com.sun.grizzly.Context.OpType;
import com.sun.grizzly.Context.KeyRegistrationState;
import com.sun.grizzly.Controller;
import com.sun.grizzly.IOEvent;
import com.sun.grizzly.SelectorHandler;
import com.sun.grizzly.filter.ReadFilter;
import com.sun.grizzly.util.OutputWriter;
import com.sun.grizzly.util.SSLOutputWriter;
import com.sun.grizzly.util.WorkerThread;
import org.jvnet.glassfish.comms.clb.proxy.HttpProxy;
import org.jvnet.glassfish.comms.clb.proxy.ProxyRequestHandler;
import org.jvnet.glassfish.comms.clb.proxy.config.ProxyConfig;
import javax.net.ssl.SSLEngine;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jvnet.glassfish.comms.clb.proxy.api.Endpoint;
/**
* CallBack handler to handle the responses coming from the backend. This can
* be reused only after calling refresh.
*
* @author
*/
public class DefaultCallBackHandler implements CallbackHandler<Context> {
protected ConnectorHandler connectorHandler;
protected Logger _logger = null;
protected SelectionKey clientKey;
protected SelectionKey serverKey;
protected ReadFilter readFilter = new ReadFilter();
protected ResponseHandler responseHandler = new ResponseHandler();
protected boolean keepAlive = false;
protected static ConnectionManager connectionManager =
HttpProxy.getInstance().getConnectionManager();
protected SSLEngine sslEngine;
protected ByteBuffer outputBB;
protected boolean isSecure = false;
boolean parsed = false;
protected Endpoint endpoint = null;
boolean inpool = true;
private static int READ_ATTEMPTS = 2;
/** Creates a new instance of DefaultCallBackHandler */
public DefaultCallBackHandler(ConnectorHandler handler,
ProxyRequestHandler reqHandler) {
_logger = ProxyConfig.getInstance().getLogger();
init(handler, reqHandler);
}
public void init(ConnectorHandler handler, ProxyRequestHandler reqHandler) {
connectorHandler = handler;
clientKey = reqHandler.getSelectionKey();
keepAlive = reqHandler.getKeepAlive();
sslEngine = reqHandler.getSSLEngine();
outputBB = reqHandler.getOutputBB();
isSecure = reqHandler.isSecure();
responseHandler.recycle();
readFilter.setReadAttempts(READ_ATTEMPTS);
parsed = false;
endpoint = reqHandler.getEndpoint();
inpool = false;
}
public void refresh(ConnectorHandler handler,
ProxyRequestHandler reqHandler) {
init(handler, reqHandler);
if (serverKey.isValid() &&
(serverKey.interestOps() & SelectionKey.OP_READ) !=
SelectionKey.OP_READ) {
handler.getSelectorHandler().register(serverKey,
SelectionKey.OP_READ);
}
}
public void onWrite(IOEvent<Context> ioEvent) {
throw new IllegalStateException("Invalid state");
}
/*
* Utility method to print the message
*/
private void dumpBuffer(ByteBuffer buf) {
int pos = buf.position();
int limit = buf.limit();
StringBuffer buff = new StringBuffer(buf.limit());
for (int i = 0; i < buf.limit(); i++) {
buff.append((char) buf.get(i));
}
buf.position(pos);
buf.limit(limit);
_logger.log(Level.FINEST, buff.toString());
}
/**
* Called when the connection has been established.
*/
public void onConnect(IOEvent<Context> ioEvent) {
serverKey = ioEvent.attachment().getSelectionKey();
try {
connectorHandler.finishConnect(serverKey);
} catch (Exception e) {
/*
* Throw an exception so that the calling
* thread knows that onConnect failed. There
* is no other means to convey the failure.
*/
_logger.log(Level.SEVERE, e.getMessage());
ioEvent.attachment().getSelectorHandler().
getSelectionKeyHandler().cancel(serverKey);
throw new RuntimeException(e);
}
ioEvent.attachment().getController().registerKey(
serverKey, SelectionKey.OP_READ, Controller.Protocol.TCP);
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "clb.proxy.callback_serverkey" + serverKey);
_logger.log(Level.FINEST, "clb.proxy.callback.clientkey" +
clientKey.channel());
_logger.log(Level.FINEST, "clb.proxy.callback.server_channel" +
serverKey.channel());
}
}
/*
* This is required to ensure that the connect is invoked
* on the same calling thread,
*/
public boolean isRunInSeparateThread(OpType opType) {
return opType != OpType.OP_CONNECT;
}
/*
* A handler has to be cleaned if the connection is closed by the peer.
*/
private void cleanFeBeHandler(SelectorHandler selectorhandler,
SelectionKey key) {
connectionManager.cleanUpHandler(connectorHandler, endpoint);
selectorhandler.getSelectionKeyHandler().
cancel(key);
}
/*
* Utility method to read from channel. If read returns false, we have to
* close this channel. We are using the ReadFilter here to read from
* the channel.
*/
private boolean readFromFeBeChannel(Context ctx) {
boolean ret = true;
try {
/* Required because if we do not set this the read filter would
* return false if 0 bytes are read and attempts exhausted.
*/
ctx.setKeyRegistrationState(KeyRegistrationState.NONE);
ret = readFilter.execute(ctx);
} catch (Exception e) {
_logger.log(Level.SEVERE,"Read Exception ", e);
ret = false;
}
/*
* If filter returns false we have to close the febe channel because
* probably this is a half closed socket.
*/
if (!ret) {
cleanFeBeHandler(ctx.getSelectorHandler(),
ctx.getSelectionKey());
if (!inpool){
connectionManager.removeClientEndpoint(clientKey);
connectionManager.cancelClientKey(clientKey);
}
}
return ret;
}
/*
* Method invoked when the read event is selected for this channel.
*/
public void onRead(IOEvent<Context> ioEvent) {
Context ctx = ioEvent.attachment();
ByteBuffer byteBuffer = null;
WorkerThread workerThread = (WorkerThread) Thread.currentThread();
byteBuffer = workerThread.getByteBuffer();
SelectionKey febeSelectionKey = ctx.getSelectionKey();
if (!readFromFeBeChannel(ctx)) {
return;
}
/*
* Paranoid check to fail early.
*/
if (byteBuffer.position() == 0 || responseHandler == null
|| clientKey == null ) {
ctx.getSelectorHandler().register(febeSelectionKey,
SelectionKey.OP_READ);
return;
}
/* Parsed is an instance variable whose value has to be preserved
* until the lifecycle of the callback handler.
*/
if (!parsed) {
while (!responseHandler.parse(byteBuffer)) {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE,
"clb.proxy.callback_algorithm_parse_false");
}
if (!readFromFeBeChannel(ctx)) {
return;
}
}
parsed = true;
} else {
byteBuffer.flip();
}
SocketChannel clientChannel = null;
if (clientKey != null){
clientChannel = (SocketChannel)clientKey.channel();
}
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST, "clb.proxy.callback.algorithm_finished",
clientChannel.socket().getRemoteSocketAddress());
_logger.log(Level.FINEST, "clb.proxy.callback_clientkey",
clientKey.channel());
_logger.log(Level.FINEST, "clb.proxy.callback.server_channel",
ctx.getSelectionKey().channel());
_logger.log(Level.FINEST, "Byte Buffer " + byteBuffer);
try {
dumpBuffer(byteBuffer);
} catch (Exception e) {
//ignore
}
}
long byteswritten = 0;
int iPos = byteBuffer.position();
int iLimit = byteBuffer.limit();
int bytesToBeWritten = byteBuffer.remaining();
boolean writeSuccess = true;
/*
* If we have the bytes ready write it to the client
*/
if ((clientChannel != null) && (clientChannel.isConnected())) {
try {
if (isSecure) {
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST,
"clb.proxy.callback.ssl_writer",
clientChannel + " " + byteBuffer + " " +
outputBB + " " + sslEngine);
}
byteswritten = SSLOutputWriter.flushChannel(clientChannel,
byteBuffer, outputBB, sslEngine);
} else {
byteswritten = OutputWriter.flushChannel(clientChannel,
byteBuffer);
}
} catch (Exception ioe) {
/*
* If write fails then close client and update the response
* handler. We have to drop the response because we cannot send
* it through another channel.
*/
writeSuccess = false;
responseHandler.updateBytesWritten(bytesToBeWritten);
byteBuffer.position(iPos);
byteBuffer.limit(iLimit);
}
if (responseHandler.isTransferEncoding()) {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "Transfer encoding Byte buffer " +
byteBuffer);
}
/**
* It is quite inefficient to search for termination in the whole
* bytebuffer, Needs to be modified
*/
responseHandler.updateEnd((ByteBuffer) byteBuffer.flip());
} else {
responseHandler.updateBytesWritten(byteswritten);
}
} else {
_logger.log(Level.SEVERE,
"clb.proxy.callback_client_channel_closed");
writeSuccess = false;
}
if (byteBuffer != null) {
byteBuffer.clear();
}
boolean register = false;
if (outputBB != null) {
outputBB.clear();
}
if (responseHandler.hasRemaining()) {
/*
* If write to client failed and there is still more data
* to be read from the backend we cannot re-use the febe channel
* anymore.
*/
if (!writeSuccess) {
cleanFeBeHandler(ctx.getSelectorHandler(),
febeSelectionKey);
connectionManager.removeClientEndpoint(clientKey);
connectionManager.cancelClientKey(clientKey);
} else {
register = true;
}
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST,
"clb.proxy.callback.algorithm_more_data",
clientChannel.socket().getRemoteSocketAddress());
}
} else {
/* keep the key registered because it can be closed by the
* peer when in the cache, when this happens the read will
* will be invoked and we can close the channel. We dont have to
* keep it registered when keep alive is disabled because we
* can be sure that the channel will not be closed.
*/
register = true;
// refactor
if (!writeSuccess){
/*
* Close only the client because we can re-use febe channel
*/
connectionManager.removeClientEndpoint(clientKey);
connectionManager.cancelClientKey(clientKey);
}
if (!keepAlive) {
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST,
"clb.proxy.callback.algorithm_cancelled_key",
clientChannel.socket().getRemoteSocketAddress());
}
connectionManager.cancelClientKey(clientKey);
} else {
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST,
"clb.proxy.callback.algorithm_registered_key",
clientChannel.socket().getRemoteSocketAddress());
}
connectionManager.registerClientKey(clientKey);
}
release();
}
if (register) {
ctx.getSelectorHandler().register(febeSelectionKey,
SelectionKey.OP_READ);
}
}
private void release() {
try {
connectionManager.removeClientEndpoint(clientKey);
connectionManager.releaseConnection(this.connectorHandler, endpoint);
if (_logger.isLoggable(Level.FINEST)) {
_logger.log(Level.FINEST,
"clb.proxy.callback.release");
}
} catch (Exception exception) {
_logger.log(Level.SEVERE,
"clb.proxy.callback.release_error", exception);
} finally {
inpool = true;
}
}
}