Search in sources :

Example 86 with Headers

use of com.amazonaws.services.s3.Headers in project okhttp by square.

the class Http2Codec method http2HeadersList.

public static List<Header> http2HeadersList(Request request) {
    Headers headers = request.headers();
    List<Header> result = new ArrayList<>(headers.size() + 4);
    result.add(new Header(TARGET_METHOD, request.method()));
    result.add(new Header(TARGET_PATH, RequestLine.requestPath(request.url())));
    String host = request.header("Host");
    if (host != null) {
        // Optional.
        result.add(new Header(TARGET_AUTHORITY, host));
    }
    result.add(new Header(TARGET_SCHEME, request.url().scheme()));
    for (int i = 0, size = headers.size(); i < size; i++) {
        // header names must be lowercase.
        ByteString name = ByteString.encodeUtf8(headers.name(i).toLowerCase(Locale.US));
        if (!HTTP_2_SKIPPED_REQUEST_HEADERS.contains(name)) {
            result.add(new Header(name, headers.value(i)));
        }
    }
    return result;
}
Also used : Headers(okhttp3.Headers) ByteString(okio.ByteString) ArrayList(java.util.ArrayList) ByteString(okio.ByteString)

Example 87 with Headers

use of com.amazonaws.services.s3.Headers in project okhttp by square.

the class CustomTrust method run.

public void run() throws Exception {
    Request request = new Request.Builder().url("https://publicobject.com/helloworld.txt").build();
    try (Response response = client.newCall(request).execute()) {
        if (!response.isSuccessful())
            throw new IOException("Unexpected code " + response);
        Headers responseHeaders = response.headers();
        for (int i = 0; i < responseHeaders.size(); i++) {
            System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
        }
        System.out.println(response.body().string());
    }
}
Also used : Response(okhttp3.Response) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException)

Example 88 with Headers

use of com.amazonaws.services.s3.Headers in project Parse-SDK-Android by ParsePlatform.

the class ParseOkHttpClient method getRequest.

@Override
/* package */
Request getRequest(ParseHttpRequest parseRequest) throws IOException {
    Request.Builder okHttpRequestBuilder = new Request.Builder();
    ParseHttpRequest.Method method = parseRequest.getMethod();
    // Set method
    switch(method) {
        case GET:
            okHttpRequestBuilder.get();
            break;
        case DELETE:
            okHttpRequestBuilder.delete();
            break;
        case POST:
        case PUT:
            // the following.
            break;
        default:
            // ParseRequest.newRequest().
            throw new IllegalStateException("Unsupported http method " + method.toString());
    }
    // Set url
    okHttpRequestBuilder.url(parseRequest.getUrl());
    // Set Header
    Headers.Builder okHttpHeadersBuilder = new Headers.Builder();
    for (Map.Entry<String, String> entry : parseRequest.getAllHeaders().entrySet()) {
        okHttpHeadersBuilder.add(entry.getKey(), entry.getValue());
    }
    // OkHttp automatically add gzip header so we do not need to deal with it
    Headers okHttpHeaders = okHttpHeadersBuilder.build();
    okHttpRequestBuilder.headers(okHttpHeaders);
    // Set Body
    ParseHttpBody parseBody = parseRequest.getBody();
    ParseOkHttpRequestBody okHttpRequestBody = null;
    if (parseBody != null) {
        okHttpRequestBody = new ParseOkHttpRequestBody(parseBody);
    }
    switch(method) {
        case PUT:
            okHttpRequestBuilder.put(okHttpRequestBody);
            break;
        case POST:
            okHttpRequestBuilder.post(okHttpRequestBody);
            break;
    }
    return okHttpRequestBuilder.build();
}
Also used : ParseHttpRequest(com.parse.http.ParseHttpRequest) Headers(okhttp3.Headers) ParseHttpBody(com.parse.http.ParseHttpBody) Request(okhttp3.Request) ParseHttpRequest(com.parse.http.ParseHttpRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Example 89 with Headers

use of com.amazonaws.services.s3.Headers in project openstack4j by ContainX.

the class HttpResponseImpl method headers.

/**
     * @return the a Map of Header Name to Header Value
     */
public Map<String, String> headers() {
    Map<String, String> retHeaders = new HashMap<String, String>();
    Headers headers = response.headers();
    for (String name : headers.names()) {
        retHeaders.put(name, headers.get(name));
    }
    return retHeaders;
}
Also used : HashMap(java.util.HashMap) Headers(okhttp3.Headers)

Example 90 with Headers

use of com.amazonaws.services.s3.Headers in project cloudstack by apache.

the class S3TemplateDownloader method download.

@Override
public long download(boolean resume, DownloadCompleteCallback callback) {
    if (!status.equals(Status.NOT_STARTED)) {
        // Only start downloading if we haven't started yet.
        LOGGER.debug("Template download is already started, not starting again. Template: " + downloadUrl);
        return 0;
    }
    int responseCode;
    if ((responseCode = HTTPUtils.executeMethod(httpClient, getMethod)) == -1) {
        errorString = "Exception while executing HttpMethod " + getMethod.getName() + " on URL " + downloadUrl;
        LOGGER.warn(errorString);
        status = Status.UNRECOVERABLE_ERROR;
        return 0;
    }
    if (!HTTPUtils.verifyResponseCode(responseCode)) {
        errorString = "Response code for GetMethod of " + downloadUrl + " is incorrect, responseCode: " + responseCode;
        LOGGER.warn(errorString);
        status = Status.UNRECOVERABLE_ERROR;
        return 0;
    }
    // Headers
    Header contentLengthHeader = getMethod.getResponseHeader("Content-Length");
    Header contentTypeHeader = getMethod.getResponseHeader("Content-Type");
    // Check the contentLengthHeader and transferEncodingHeader.
    if (contentLengthHeader == null) {
        errorString = "The ContentLengthHeader of " + downloadUrl + " isn't supplied";
        LOGGER.warn(errorString);
        status = Status.UNRECOVERABLE_ERROR;
        return 0;
    } else {
        // The ContentLengthHeader is supplied, parse it's value.
        remoteSize = Long.parseLong(contentLengthHeader.getValue());
    }
    if (remoteSize > maxTemplateSizeInByte) {
        errorString = "Remote size is too large for template " + downloadUrl + " remote size is " + remoteSize + " max allowed is " + maxTemplateSizeInByte;
        LOGGER.warn(errorString);
        status = Status.UNRECOVERABLE_ERROR;
        return 0;
    }
    InputStream inputStream;
    try {
        inputStream = new BufferedInputStream(getMethod.getResponseBodyAsStream());
    } catch (IOException e) {
        errorString = "Exception occurred while opening InputStream for template " + downloadUrl;
        LOGGER.warn(errorString);
        status = Status.UNRECOVERABLE_ERROR;
        return 0;
    }
    LOGGER.info("Starting download from " + downloadUrl + " to S3 bucket " + s3TO.getBucketName() + " and size " + remoteSize + " bytes");
    // Time the upload starts.
    final Date start = new Date();
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(remoteSize);
    if (contentTypeHeader.getValue() != null) {
        objectMetadata.setContentType(contentTypeHeader.getValue());
    }
    // Create the PutObjectRequest.
    PutObjectRequest putObjectRequest = new PutObjectRequest(s3TO.getBucketName(), s3Key, inputStream, objectMetadata);
    // If reduced redundancy is enabled, set it.
    if (s3TO.getEnableRRS()) {
        putObjectRequest.withStorageClass(StorageClass.ReducedRedundancy);
    }
    Upload upload = S3Utils.putObject(s3TO, putObjectRequest);
    upload.addProgressListener(new ProgressListener() {

        @Override
        public void progressChanged(ProgressEvent progressEvent) {
            // Record the amount of bytes transferred.
            totalBytes += progressEvent.getBytesTransferred();
            LOGGER.trace("Template download from " + downloadUrl + " to S3 bucket " + s3TO.getBucketName() + " transferred  " + totalBytes + " in " + ((new Date().getTime() - start.getTime()) / 1000) + " seconds");
            if (progressEvent.getEventType() == ProgressEventType.TRANSFER_STARTED_EVENT) {
                status = Status.IN_PROGRESS;
            } else if (progressEvent.getEventType() == ProgressEventType.TRANSFER_COMPLETED_EVENT) {
                status = Status.DOWNLOAD_FINISHED;
            } else if (progressEvent.getEventType() == ProgressEventType.TRANSFER_CANCELED_EVENT) {
                status = Status.ABORTED;
            } else if (progressEvent.getEventType() == ProgressEventType.TRANSFER_FAILED_EVENT) {
                status = Status.UNRECOVERABLE_ERROR;
            }
        }
    });
    try {
        // Wait for the upload to complete.
        upload.waitForCompletion();
    } catch (InterruptedException e) {
        // Interruption while waiting for the upload to complete.
        LOGGER.warn("Interruption occurred while waiting for upload of " + downloadUrl + " to complete");
    }
    downloadTime = new Date().getTime() - start.getTime();
    if (status == Status.DOWNLOAD_FINISHED) {
        LOGGER.info("Template download from " + downloadUrl + " to S3 bucket " + s3TO.getBucketName() + " transferred  " + totalBytes + " in " + (downloadTime / 1000) + " seconds, completed successfully!");
    } else {
        LOGGER.warn("Template download from " + downloadUrl + " to S3 bucket " + s3TO.getBucketName() + " transferred  " + totalBytes + " in " + (downloadTime / 1000) + " seconds, completed with status " + status.toString());
    }
    // Close input stream
    getMethod.releaseConnection();
    // Call the callback!
    if (callback != null) {
        callback.downloadComplete(status);
    }
    return totalBytes;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Upload(com.amazonaws.services.s3.transfer.Upload) IOException(java.io.IOException) ProgressEvent(com.amazonaws.event.ProgressEvent) Date(java.util.Date) Header(org.apache.commons.httpclient.Header) ProgressListener(com.amazonaws.event.ProgressListener) BufferedInputStream(java.io.BufferedInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Aggregations

Headers (okhttp3.Headers)128 Request (okhttp3.Request)61 Response (okhttp3.Response)54 Test (org.junit.Test)40 IOException (java.io.IOException)32 Call (okhttp3.Call)30 RequestBody (okhttp3.RequestBody)25 CancelledException (com.hippo.ehviewer.client.exception.CancelledException)20 EhException (com.hippo.ehviewer.client.exception.EhException)20 NoHAtHClientException (com.hippo.ehviewer.client.exception.NoHAtHClientException)20 ParseException (com.hippo.ehviewer.client.exception.ParseException)20 StatusCodeException (com.hippo.network.StatusCodeException)20 ResponseBody (okhttp3.ResponseBody)18 HttpHeaders (okhttp3.internal.http.HttpHeaders)18 MediaType (okhttp3.MediaType)15 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)14 HeaderResponseBoolHeaders (fixtures.header.models.HeaderResponseBoolHeaders)14 HeaderResponseByteHeaders (fixtures.header.models.HeaderResponseByteHeaders)14 HeaderResponseDateHeaders (fixtures.header.models.HeaderResponseDateHeaders)14 HeaderResponseDatetimeHeaders (fixtures.header.models.HeaderResponseDatetimeHeaders)14