All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.jetty.client.ResponseNotifier Maven / Gradle / Ivy

The newest version!
//
//  ========================================================================
//  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.client;

import java.nio.ByteBuffer;
import java.util.List;

import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.api.Result;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

public class ResponseNotifier
{
    private static final Logger LOG = Log.getLogger(ResponseNotifier.class);
    private final HttpClient client;

    public ResponseNotifier(HttpClient client)
    {
        this.client = client;
    }

    public void notifyBegin(List listeners, Response response)
    {
        for (Response.ResponseListener listener : listeners)
            if (listener instanceof Response.BeginListener)
                notifyBegin((Response.BeginListener)listener, response);
    }

    private void notifyBegin(Response.BeginListener listener, Response response)
    {
        try
        {
            listener.onBegin(response);
        }
        catch (Exception x)
        {
            LOG.info("Exception while notifying listener " + listener, x);
        }
    }

    public void notifyHeaders(List listeners, Response response)
    {
        for (Response.ResponseListener listener : listeners)
            if (listener instanceof Response.HeadersListener)
                notifyHeaders((Response.HeadersListener)listener, response);
    }

    private void notifyHeaders(Response.HeadersListener listener, Response response)
    {
        try
        {
            listener.onHeaders(response);
        }
        catch (Exception x)
        {
            LOG.info("Exception while notifying listener " + listener, x);
        }
    }

    public void notifyContent(List listeners, Response response, ByteBuffer buffer)
    {
        for (Response.ResponseListener listener : listeners)
            if (listener instanceof Response.ContentListener)
                notifyContent((Response.ContentListener)listener, response, buffer);

    }

    private void notifyContent(Response.ContentListener listener, Response response, ByteBuffer buffer)
    {
        try
        {
            listener.onContent(response, buffer);
        }
        catch (Exception x)
        {
            LOG.info("Exception while notifying listener " + listener, x);
        }
    }

    public void notifySuccess(List listeners, Response response)
    {
        for (Response.ResponseListener listener : listeners)
            if (listener instanceof Response.SuccessListener)
                notifySuccess((Response.SuccessListener)listener, response);
    }

    private void notifySuccess(Response.SuccessListener listener, Response response)
    {
        try
        {
            listener.onSuccess(response);
        }
        catch (Exception x)
        {
            LOG.info("Exception while notifying listener " + listener, x);
        }
    }

    public void notifyFailure(List listeners, Response response, Throwable failure)
    {
        for (Response.ResponseListener listener : listeners)
            if (listener instanceof Response.FailureListener)
                notifyFailure((Response.FailureListener)listener, response, failure);
    }

    private void notifyFailure(Response.FailureListener listener, Response response, Throwable failure)
    {
        try
        {
            listener.onFailure(response, failure);
        }
        catch (Exception x)
        {
            LOG.info("Exception while notifying listener " + listener, x);
        }
    }

    public void notifyComplete(List listeners, Result result)
    {
        for (Response.ResponseListener listener : listeners)
            if (listener instanceof Response.CompleteListener)
                notifyComplete((Response.CompleteListener)listener, result);
    }

    private void notifyComplete(Response.CompleteListener listener, Result result)
    {
        try
        {
            listener.onComplete(result);
        }
        catch (Exception x)
        {
            LOG.info("Exception while notifying listener " + listener, x);
        }
    }

    public void forwardSuccess(List listeners, Response response)
    {
        notifyBegin(listeners, response);
        notifyHeaders(listeners, response);
        if (response instanceof ContentResponse)
            notifyContent(listeners, response, ByteBuffer.wrap(((ContentResponse)response).getContent()));
        notifySuccess(listeners, response);
    }

    public void forwardSuccessComplete(List listeners, Request request, Response response)
    {
        HttpConversation conversation = client.getConversation(request.getConversationID(), false);
        forwardSuccess(listeners, response);
        conversation.complete();
        notifyComplete(listeners, new Result(request, response));
    }

    public void forwardFailure(List listeners, Response response, Throwable failure)
    {
        notifyBegin(listeners, response);
        notifyHeaders(listeners, response);
        if (response instanceof ContentResponse)
            notifyContent(listeners, response, ByteBuffer.wrap(((ContentResponse)response).getContent()));
        notifyFailure(listeners, response, failure);
    }

    public void forwardFailureComplete(List listeners, Request request, Throwable requestFailure, Response response, Throwable responseFailure)
    {
        HttpConversation conversation = client.getConversation(request.getConversationID(), false);
        forwardFailure(listeners, response, responseFailure);
        conversation.complete();
        notifyComplete(listeners, new Result(request, requestFailure, response, responseFailure));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy