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

com.signalfx.shaded.jetty.client.ContinueProtocolHandler Maven / Gradle / Ivy

The newest version!
//
//  ========================================================================
//  Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//  ------------------------------------------------------------------------
//  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 com.signalfx.shaded.jetty.client;

import java.util.List;

import com.signalfx.shaded.jetty.client.api.Request;
import com.signalfx.shaded.jetty.client.api.Response;
import com.signalfx.shaded.jetty.client.api.Result;
import com.signalfx.shaded.jetty.client.util.BufferingResponseListener;
import com.signalfx.shaded.jetty.http.HttpHeader;
import com.signalfx.shaded.jetty.http.HttpHeaderValue;
import com.signalfx.shaded.jetty.http.HttpStatus;

/**
 * 

A protocol handler that handles the 100 response code.

*/ public class ContinueProtocolHandler implements ProtocolHandler { public static final String NAME = "continue"; private static final String ATTRIBUTE = ContinueProtocolHandler.class.getName() + ".100continue"; private final ResponseNotifier notifier; public ContinueProtocolHandler() { this.notifier = new ResponseNotifier(); } @Override public String getName() { return NAME; } @Override public boolean accept(Request request, Response response) { boolean is100 = response.getStatus() == HttpStatus.CONTINUE_100; boolean expect100 = request.getHeaders().contains(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()); boolean handled100 = request.getAttributes().containsKey(ATTRIBUTE); return (is100 || expect100) && !handled100; } @Override public Response.Listener getResponseListener() { // Return new instances every time to keep track of the response content return new ContinueListener(); } protected void onContinue(Request request) { } protected class ContinueListener extends BufferingResponseListener { @Override public void onSuccess(Response response) { // Handling of success must be done here and not from onComplete(), // since the onComplete() is not invoked because the request is not completed yet. Request request = response.getRequest(); HttpConversation conversation = ((HttpRequest)request).getConversation(); // Mark the 100 Continue response as handled request.attribute(ATTRIBUTE, Boolean.TRUE); // Reset the conversation listeners, since we are going to receive another response code conversation.updateResponseListeners(null); HttpExchange exchange = conversation.getExchanges().peekLast(); if (response.getStatus() == HttpStatus.CONTINUE_100) { // All good, continue. exchange.resetResponse(); exchange.proceed(null); onContinue(request); } else { // Server either does not support 100 Continue, // or it does and wants to refuse the request content, // or we got some other HTTP status code like a redirect. List listeners = exchange.getResponseListeners(); HttpContentResponse contentResponse = new HttpContentResponse(response, getContent(), getMediaType(), getEncoding()); notifier.forwardSuccess(listeners, contentResponse); exchange.proceed(new HttpRequestException("Expectation failed", request)); } } @Override public void onFailure(Response response, Throwable failure) { HttpConversation conversation = ((HttpRequest)response.getRequest()).getConversation(); // Mark the 100 Continue response as handled conversation.setAttribute(ATTRIBUTE, Boolean.TRUE); // Reset the conversation listeners to allow the conversation to be completed conversation.updateResponseListeners(null); HttpExchange exchange = conversation.getExchanges().peekLast(); assert exchange.getResponse() == response; List listeners = exchange.getResponseListeners(); HttpContentResponse contentResponse = new HttpContentResponse(response, getContent(), getMediaType(), getEncoding()); notifier.forwardFailureComplete(listeners, exchange.getRequest(), exchange.getRequestFailure(), contentResponse, failure); } @Override public void onComplete(Result result) { } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy