
org.apache.tinkerpop.gremlin.driver.handler.GremlinResponseHandler Maven / Gradle / Ivy
The newest version!
/*
* 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.tinkerpop.gremlin.driver.handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.util.AttributeKey;
import javax.net.ssl.SSLException;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.tinkerpop.gremlin.driver.Result;
import org.apache.tinkerpop.gremlin.driver.ResultQueue;
import org.apache.tinkerpop.gremlin.driver.exception.ResponseException;
import org.apache.tinkerpop.gremlin.process.remote.traversal.DefaultRemoteTraverser;
import org.apache.tinkerpop.gremlin.util.ExceptionHelper;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.apache.tinkerpop.gremlin.util.message.ResponseMessage;
import org.apache.tinkerpop.gremlin.util.ser.SerializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import static org.apache.tinkerpop.gremlin.driver.Channelizer.HttpChannelizer.LAST_CONTENT_READ_RESPONSE;
/**
* Takes a map of requests pending responses and writes responses to the {@link ResultQueue} of a request
* as the {@link ResponseMessage} objects are deserialized.
*/
public class GremlinResponseHandler extends SimpleChannelInboundHandler {
public static final AttributeKey INBOUND_SSL_EXCEPTION = AttributeKey.valueOf("inboundSslException");
private static final Logger logger = LoggerFactory.getLogger(GremlinResponseHandler.class);
private static final AttributeKey CAUGHT_EXCEPTION = AttributeKey.valueOf("caughtException");
private final AtomicReference pending;
public GremlinResponseHandler(final AtomicReference pending) {
this.pending = pending;
}
@Override
public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
// occurs when the server shuts down in a disorderly fashion, otherwise in an orderly shutdown the server
// should fire off a close message which will properly release the driver.
super.channelInactive(ctx);
final ResultQueue current = pending.getAndSet(null);
if (current != null) {
current.markError(new IllegalStateException("Connection to server is no longer active"));
}
}
@Override
protected void channelRead0(final ChannelHandlerContext channelHandlerContext, final ResponseMessage response) {
final HttpResponseStatus statusCode = response.getStatus() == null ? null : response.getStatus().getCode();
final ResultQueue queue = pending.get();
if ((null == statusCode) || (statusCode == HttpResponseStatus.OK)) {
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy