Package com.google.appengine.tools.remoteapi

Source Code of com.google.appengine.tools.remoteapi.OAuthClient

package com.google.appengine.tools.remoteapi;

import com.google.api.client.http.AbstractHttpContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.common.io.ByteStreams;

import java.io.IOException;
import java.io.OutputStream;

/**
* A client that uses OAuth for authentication. Both on- and off-App Engine
* use cases are supported via the HttpTransport object provided via
* {@link RemoteApiOptions}.
*
* <p>This class is thread-safe.
*/
class OAuthClient extends BaseRemoteApiClient {

  private final HttpRequestFactory requestFactory;

  OAuthClient(RemoteApiOptions options, String appId) {
    super(options, appId);
    if (options.getOAuthCredential() == null) {
      throw new IllegalArgumentException("options does not contain an OAuth credential");
    }
    if (options.getHttpTransport() == null) {
      throw new IllegalStateException("options does not contain an HttpTransport");
    }
    requestFactory =
        options.getHttpTransport().createRequestFactory(options.getOAuthCredential());
  }

  @Override
  public String serializeCredentials() {
    throw new UnsupportedOperationException();
  }

  @Override
  public AppEngineClient.Response post(String path, String mimeType, byte[] body)
      throws IOException {
    HttpContent content = new ByteArrayHttpContent(mimeType, body);
    HttpRequest request = requestFactory.buildPostRequest(resolveUrl(path), content);
    addBaseHeaders(request);
    request.setFollowRedirects(false);
    return toResponse(request.execute());
  }

  @Override
  public AppEngineClient.Response get(String path) throws IOException {
    HttpRequest request = requestFactory.buildGetRequest(resolveUrl(path));
    addBaseHeaders(request);
    request.setFollowRedirects(false);
    return toResponse(request.execute());
  }

  private void addBaseHeaders(HttpRequest request) {
    HttpHeaders headers = request.getHeaders();
    for (String[] nameAndValue : getHeadersBase()) {
      headers.put(nameAndValue[0], nameAndValue[1]);
    }
    getHeadersBase();
  }

  private GenericUrl resolveUrl(String path) {
    return new GenericUrl(makeUrl(path));
  }

  private AppEngineClient.Response toResponse(HttpResponse httpResponse) throws IOException {
    byte[] body = (httpResponse.getContent() == null)
        ? null
        : ByteStreams.toByteArray(httpResponse.getContent());
    return new AppEngineClient.Response(httpResponse.getStatusCode(), body,
        httpResponse.getContentCharset().toString());
  }

  /**
   * Simple adapter between an byte array and an {@link HttpContent}.
   */
  private static class ByteArrayHttpContent extends AbstractHttpContent {

    private final byte[] body;

    private ByteArrayHttpContent(String mediaType, byte[] body) {
      super(mediaType);
      this.body = body;
    }

    @Override
    public void writeTo(OutputStream out) throws IOException {
      out.write(body);
    }
  }
}
TOP

Related Classes of com.google.appengine.tools.remoteapi.OAuthClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.