Java Code Examples for com.google.api.client.http.HttpHeaders#serializeHeadersForMultipartRequests()

The following examples show how to use com.google.api.client.http.HttpHeaders#serializeHeadersForMultipartRequests() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpRequestContent.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
@Override
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(" ");
  writer.write(HTTP_VERSION);
  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);
  }
}
 
Example 2
Source File: MultipartFormDataContent.java    From Broadsheet.ie-Android with MIT License 4 votes vote down vote up
@Override
public void writeTo(OutputStream out) throws IOException {

    Writer writer = new OutputStreamWriter(out, getCharset());
    String boundary = getBoundary();

    for (Part part : parts) {
        HttpHeaders headers = new HttpHeaders().setAcceptEncoding(null);
        if (part.headers != null) {
            headers.fromHttpHeaders(part.headers);
        }
        headers.setContentEncoding(null).setUserAgent(null).setContentType(null).setContentLength(null);
        // analyze the content
        HttpContent content = part.content;
        StreamingContent streamingContent = null;
        String contentDisposition = String.format("form-data; name=\"%s\"", part.name);
        if (part.filename != null) {
            headers.setContentType(content.getType());
            contentDisposition += String.format("; filename=\"%s\"", part.filename);
        }
        headers.set("Content-Disposition", contentDisposition);
        HttpEncoding encoding = part.encoding;
        if (encoding == null) {
            streamingContent = content;
        } else {
            headers.setContentEncoding(encoding.getName());
            streamingContent = new HttpEncodingStreamingContent(content, encoding);
        }
        // write separator
        writer.write(TWO_DASHES);
        writer.write(boundary);
        writer.write(NEWLINE);
        // write headers
        HttpHeaders.serializeHeadersForMultipartRequests(headers, null, null, writer);
        // write content
        if (streamingContent != null) {
            writer.write(NEWLINE);
            writer.flush();
            streamingContent.writeTo(out);
            writer.write(NEWLINE);
        }
    }
    // write end separator
    writer.write(TWO_DASHES);
    writer.write(boundary);
    writer.write(TWO_DASHES);
    writer.write(NEWLINE);
    writer.flush();
}