Example usage for com.google.common.net HttpHeaders CONNECTION

List of usage examples for com.google.common.net HttpHeaders CONNECTION

Introduction

In this page you can find the example usage for com.google.common.net HttpHeaders CONNECTION.

Prototype

String CONNECTION

To view the source code for com.google.common.net HttpHeaders CONNECTION.

Click Source Link

Document

The HTTP Connection header field name.

Usage

From source file:org.jclouds.http.filters.ConnectionCloseHeader.java

@Override
public HttpRequest filter(HttpRequest request) throws HttpException {
    return request.toBuilder().addHeader(HttpHeaders.CONNECTION, "close").build();
}

From source file:io.kubernetes.client.util.WebSockets.java

/**
 * Create a new WebSocket stream/*from   ww w  . j a  v  a2 s  .co m*/
 * @param path The HTTP Path to request from the API
 * @param method The HTTP method to use for the call
 * @param client The ApiClient for communicating with the API
 * @param listener The socket listener to handle socket events
 */
public static void stream(String path, String method, ApiClient client, SocketListener listener)
        throws ApiException, IOException {
    HashMap<String, String> headers = new HashMap<String, String>();
    String allProtocols = String.format("%s,%s,%s,%s", V4_STREAM_PROTOCOL, V3_STREAM_PROTOCOL,
            V2_STREAM_PROTOCOL, V1_STREAM_PROTOCOL);
    headers.put(STREAM_PROTOCOL_HEADER, allProtocols);
    headers.put(HttpHeaders.CONNECTION, HttpHeaders.UPGRADE);
    headers.put(HttpHeaders.UPGRADE, SPDY_3_1);

    Request request = client.buildRequest(path, method, new ArrayList<Pair>(), new ArrayList<Pair>(), null,
            headers, new HashMap<String, Object>(), new String[0], null);
    WebSocketCall.create(client.getHttpClient(), request).enqueue(new Listener(listener));
}

From source file:com.google.zxing.web.DecodeServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String imageURIString = request.getParameter("u");
    if (imageURIString == null || imageURIString.isEmpty()) {
        log.info("URI was empty");
        errorResponse(request, response, "badurl");
        return;/*from  ww  w  .  j ava  2 s .  c om*/
    }

    imageURIString = imageURIString.trim();
    for (CharSequence substring : blockedURLSubstrings) {
        if (imageURIString.contains(substring)) {
            log.info("Disallowed URI " + imageURIString);
            errorResponse(request, response, "badurl");
            return;
        }
    }

    URI imageURI;
    try {
        imageURI = new URI(imageURIString);
        // Assume http: if not specified
        if (imageURI.getScheme() == null) {
            imageURI = new URI("http://" + imageURIString);
        }
    } catch (URISyntaxException urise) {
        log.info("URI " + imageURIString + " was not valid: " + urise);
        errorResponse(request, response, "badurl");
        return;
    }

    // Shortcut for data URI
    if ("data".equals(imageURI.getScheme())) {
        try {
            BufferedImage image = ImageReader.readDataURIImage(imageURI);
            processImage(image, request, response);
        } catch (IOException ioe) {
            log.info(ioe.toString());
            errorResponse(request, response, "badurl");
        }
        return;
    }

    URL imageURL;
    try {
        imageURL = imageURI.toURL();
    } catch (MalformedURLException ignored) {
        log.info("URI was not valid: " + imageURIString);
        errorResponse(request, response, "badurl");
        return;
    }

    String protocol = imageURL.getProtocol();
    if (!"http".equalsIgnoreCase(protocol) && !"https".equalsIgnoreCase(protocol)) {
        log.info("URI was not valid: " + imageURIString);
        errorResponse(request, response, "badurl");
        return;
    }

    HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) imageURL.openConnection();
    } catch (IllegalArgumentException ignored) {
        log.info("URI could not be opened: " + imageURL);
        errorResponse(request, response, "badurl");
        return;
    }

    connection.setAllowUserInteraction(false);
    connection.setReadTimeout(5000);
    connection.setConnectTimeout(5000);
    connection.setRequestProperty(HttpHeaders.USER_AGENT, "zxing.org");
    connection.setRequestProperty(HttpHeaders.CONNECTION, "close");

    try {
        connection.connect();
    } catch (IOException | IllegalArgumentException e) {
        // Encompasses lots of stuff, including
        //  java.net.SocketException, java.net.UnknownHostException,
        //  javax.net.ssl.SSLPeerUnverifiedException,
        //  org.apache.http.NoHttpResponseException,
        //  org.apache.http.client.ClientProtocolException,
        log.info(e.toString());
        errorResponse(request, response, "badurl");
        return;
    }

    try (InputStream is = connection.getInputStream()) {
        try {
            if (connection.getResponseCode() != HttpServletResponse.SC_OK) {
                log.info("Unsuccessful return code: " + connection.getResponseCode());
                errorResponse(request, response, "badurl");
                return;
            }
            if (connection.getHeaderFieldInt(HttpHeaders.CONTENT_LENGTH, 0) > MAX_IMAGE_SIZE) {
                log.info("Too large");
                errorResponse(request, response, "badimage");
                return;
            }

            log.info("Decoding " + imageURL);
            processStream(is, request, response);
        } finally {
            consumeRemainder(is);
        }
    } catch (IOException ioe) {
        log.info(ioe.toString());
        errorResponse(request, response, "badurl");
    } finally {
        connection.disconnect();
    }

}

From source file:net.myrrix.client.ClientRecommender.java

/**
 * @param replica host and port of replica to connect to
 * @param path URL to access (relative to context root)
 * @param method HTTP method to use/* w w w.j a v a 2 s .com*/
 * @param doOutput if true, will need to write data into the request body
 * @param chunkedStreaming if true, use chunked streaming to accommodate a large upload, if possible
 * @param requestProperties additional request key/value pairs or {@code null} for none
 */
private HttpURLConnection buildConnectionToReplica(HostAndPort replica, String path, String method,
        boolean doOutput, boolean chunkedStreaming, Map<String, String> requestProperties) throws IOException {
    String contextPath = config.getContextPath();
    if (contextPath != null) {
        path = '/' + contextPath + path;
    }
    String protocol = config.isSecure() ? "https" : "http";
    URL url;
    try {
        url = new URL(protocol, replica.getHostText(), replica.getPort(), path);
    } catch (MalformedURLException mue) {
        // can't happen
        throw new IllegalStateException(mue);
    }
    log.debug("{} {}", method, url);

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(method);
    connection.setDoInput(true);
    connection.setDoOutput(doOutput);
    connection.setUseCaches(false);
    connection.setAllowUserInteraction(false);
    connection.setRequestProperty(HttpHeaders.ACCEPT, DESIRED_RESPONSE_CONTENT_TYPE);
    if (closeConnection) {
        connection.setRequestProperty(HttpHeaders.CONNECTION, "close");
    }
    if (chunkedStreaming) {
        if (needAuthentication) {
            // Must buffer in memory if using authentication since it won't handle the authorization challenge
            log.debug("Authentication is enabled, so ingest data must be buffered in memory");
        } else {
            connection.setChunkedStreamingMode(0); // Use default buffer size
        }
    }
    if (requestProperties != null) {
        for (Map.Entry<String, String> entry : requestProperties.entrySet()) {
            connection.setRequestProperty(entry.getKey(), entry.getValue());
        }
    }
    return connection;
}