com.google.api.client.http.HttpContent

Here are the examples of the java api class com.google.api.client.http.HttpContent taken from open source projects.

1. MediaHttpUploader#executeUploadInitiation()

View license
/**
   * This method sends a POST request with empty content to get the unique upload URL.
   *
   * @param initiationRequestUrl The request URL where the initiation request will be sent
   */
private HttpResponse executeUploadInitiation(GenericUrl initiationRequestUrl) throws IOException {
    updateStateAndNotifyListener(UploadState.INITIATION_STARTED);
    initiationRequestUrl.put("uploadType", "resumable");
    HttpContent content = metadata == null ? new EmptyContent() : metadata;
    HttpRequest request = requestFactory.buildRequest(initiationRequestMethod, initiationRequestUrl, content);
    initiationHeaders.set(CONTENT_TYPE_HEADER, mediaContent.getType());
    if (isMediaLengthKnown()) {
        initiationHeaders.set(CONTENT_LENGTH_HEADER, getMediaContentLength());
    }
    request.getHeaders().putAll(initiationHeaders);
    HttpResponse response = executeCurrentRequest(request);
    boolean notificationCompleted = false;
    try {
        updateStateAndNotifyListener(UploadState.INITIATION_COMPLETE);
        notificationCompleted = true;
    } finally {
        if (!notificationCompleted) {
            response.disconnect();
        }
    }
    return response;
}

2. Authenticate#appId()

Project: datacollector
Source File: Authenticate.java
View license
public Secret appId(String appId, String userId) throws VaultException {
    HttpContent content = new JsonHttpContent(getJsonFactory(), ImmutableMap.of("app_id", appId, "user_id", userId));
    return getSecret("/v1/auth/app-id/login", "POST", content);
}

3. Logical#write()

Project: datacollector
Source File: Logical.java
View license
public Secret write(String path, Map<String, Object> params) throws VaultException {
    HttpContent content = new JsonHttpContent(getJsonFactory(), params);
    return getSecret("/v1/" + path, "POST", content);
}

4. Auth#enable()

Project: datacollector
Source File: Auth.java
View license
public boolean enable(String path, String type, String description) throws VaultException {
    Map<String, Object> data = new HashMap<>();
    data.put("type", type);
    if (description != null) {
        data.put("description", description);
    }
    HttpContent content = new JsonHttpContent(getJsonFactory(), data);
    try {
        HttpRequest request = getRequestFactory().buildRequest("POST", new GenericUrl(getConf().getAddress() + "/v1/sys/auth/" + path), content);
        HttpResponse response = request.execute();
        if (!response.isSuccessStatusCode()) {
            LOG.error("Request failed status: {} message: {}", response.getStatusCode(), response.getStatusMessage());
        }
        return response.isSuccessStatusCode();
    } catch (IOException e) {
        LOG.error(e.toString(), e);
        throw new VaultException("Failed to authenticate: " + e.toString(), e);
    }
}

5. HttpRequestContent#writeTo()

View license
public void writeTo(OutputStream out) throws IOException {
    Writer writer = new OutputStreamWriter(out, getCharset());
    // write method and URL
    writer.write(request.getRequestMethod());
    writer.write(" ");
    writer.write(request.getUrl().build());
    writer.write(NEWLINE);
    // write headers
    HttpHeaders headers = new HttpHeaders();
    headers.fromHttpHeaders(request.getHeaders());
    headers.setAcceptEncoding(null).setUserAgent(null).setContentEncoding(null).setContentType(null).setContentLength(null);
    // analyze the content
    HttpContent content = request.getContent();
    if (content != null) {
        headers.setContentType(content.getType());
        // NOTE: batch does not support gzip encoding
        long contentLength = content.getLength();
        if (contentLength != -1) {
            headers.setContentLength(contentLength);
        }
    }
    HttpHeaders.serializeHeadersForMultipartRequests(headers, null, null, writer);
    // HTTP headers are always terminated with an empty line; RFC 7230 3
    writer.write(NEWLINE);
    writer.flush();
    // write content
    if (content != null) {
        content.writeTo(out);
    }
}

6. MediaHttpUploader#directUpload()

View license
/**
   * Direct Uploads the media.
   *
   * @param initiationRequestUrl The request URL where the initiation request will be sent
   * @return HTTP response
   */
private HttpResponse directUpload(GenericUrl initiationRequestUrl) throws IOException {
    updateStateAndNotifyListener(UploadState.MEDIA_IN_PROGRESS);
    HttpContent content = mediaContent;
    if (metadata != null) {
        content = new MultipartContent().setContentParts(Arrays.asList(metadata, mediaContent));
        initiationRequestUrl.put("uploadType", "multipart");
    } else {
        initiationRequestUrl.put("uploadType", "media");
    }
    HttpRequest request = requestFactory.buildRequest(initiationRequestMethod, initiationRequestUrl, content);
    request.getHeaders().putAll(initiationHeaders);
    // We do not have to do anything special here if media content length is unspecified because
    // direct media upload works even when the media content length == -1.
    HttpResponse response = executeCurrentRequest(request);
    boolean responseProcessed = false;
    try {
        if (isMediaLengthKnown()) {
            totalBytesServerReceived = getMediaContentLength();
        }
        updateStateAndNotifyListener(UploadState.MEDIA_COMPLETE);
        responseProcessed = true;
    } finally {
        if (!responseProcessed) {
            response.disconnect();
        }
    }
    return response;
}