org.aevans.goat.net.ConnectionUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.aevans.goat.net.ConnectionUtils.java

Source

package org.aevans.goat.net;

import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.client.HttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.*;

/**
 * Connection Utilities for FX
 *
 * @author Andrew Evans
 * Copyright 2016
 * License : Free BSD
 */
public class ConnectionUtils {

    public static ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy(final int keepAlive) {
        return new ConnectionKeepAliveStrategy() {

            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                // Honor 'keep-alive' header
                HeaderElementIterator it = new BasicHeaderElementIterator(
                        response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                while (it.hasNext()) {
                    HeaderElement he = it.nextElement();
                    String param = he.getName();
                    String value = he.getValue();
                    if (value != null && param.equalsIgnoreCase("timeout")) {
                        try {
                            return Long.parseLong(value) * 1000;
                        } catch (NumberFormatException ignore) {
                        }
                    }
                }

                return keepAlive * 1000;
            }

        };
    }
}