All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.api.client.http.MultipartRelatedContent Maven / Gradle / Ivy

Go to download

Google API Client Library for Java. Supports Java 5 (or higher) desktop (SE) and web (EE), Android, and Google App Engine.

There is a newer version: 1.4.1-beta
Show newest version
package com.google.api.client.http;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Serializes MIME Multipart/Related content as specified by RFC 2387: The MIME
 * Multipart/Related Content-type.
 * 

* Limitations: *

    *
  • No support of parameters other than {@code "boundary"}
  • *
  • No support for specifying headers for each content part
  • *
*

*

* Sample usage: * *


  static void setMediaWithMetadataContent(HttpRequest request,
      AtomContent atomContent, InputStreamContent imageContent) {
    MultipartRelatedContent content =
        MultipartRelatedContent.forRequest(request);
    content.parts.add(atomContent);
    content.parts.add(imageContent);
  }
 * 
* * @since 1.1 * @author Yaniv Inbar */ public final class MultipartRelatedContent implements HttpContent { /** Boundary string to use. By default, it is {@code "END_OF_PART"}. */ public String boundary = "END_OF_PART"; /** Collection of HTTP content parts. By default, it is an empty list. */ public Collection parts = new ArrayList(); private static final byte[] CR_LF = "\r\n".getBytes(); private static final byte[] CONTENT_TYPE = "Content-Type: ".getBytes(); private static final byte[] CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding: binary".getBytes(); private static final byte[] TWO_DASHES = "--".getBytes(); /** * Returns a new multi-part content serializer as the content for the given * HTTP request. * *

* It also sets the {@link HttpHeaders#mimeVersion} of * {@link HttpRequest#headers headers} to {@code "1.0"}. *

* * @param request HTTP request * @return new multi-part content serializer */ public static MultipartRelatedContent forRequest(HttpRequest request) { MultipartRelatedContent result = new MultipartRelatedContent(); request.headers.mimeVersion = "1.0"; request.content = result; return result; } public void writeTo(OutputStream out) throws IOException { byte[] END_OF_PART = boundary.getBytes(); out.write(TWO_DASHES); out.write(END_OF_PART); for (HttpContent part : parts) { String contentType = part.getType(); byte[] typeBytes = contentType.getBytes(); out.write(CR_LF); out.write(CONTENT_TYPE); out.write(typeBytes); out.write(CR_LF); if (!LogContent.isTextBasedContentType(contentType)) { out.write(CONTENT_TRANSFER_ENCODING); out.write(CR_LF); } out.write(CR_LF); part.writeTo(out); out.write(CR_LF); out.write(TWO_DASHES); out.write(END_OF_PART); } out.write(TWO_DASHES); out.flush(); } public String getEncoding() { return null; } public long getLength() { // TODO: compute this? return -1; } public String getType() { return "multipart/related; boundary=\"END_OF_PART\""; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy