当前位置:首页 > 开发 > 编程语言 > Java > 正文

baidu云存储java sdk源码阅读之请求发送

发表于: 2013-06-17   作者:asialee   来源:转载   浏览:
摘要:       这个系列的文章中断了很长时间,今天继续写。我们首先来看BaiduBCS的入口类 public class BaiduBCS { private static final Log log = LogFactory.getLog(BaiduBCS.class); private BCSHttpClient bcsHttpClient = nul

      这个系列的文章中断了很长时间,今天继续写。我们首先来看BaiduBCS的入口类

public class BaiduBCS
{
  private static final Log log = LogFactory.getLog(BaiduBCS.class);
  private BCSHttpClient bcsHttpClient = null;
  private BCSCredentials credentials = null;
  private String endpoint = null;

  public BaiduBCS(BCSCredentials paramBCSCredentials, String paramString)
  {
    this.credentials = paramBCSCredentials;
    setEndpoint(paramString);
    this.bcsHttpClient = new BCSHttpClient(new ClientConfiguration());
  }

  public BaiduBCS(BCSCredentials paramBCSCredentials, String paramString, ClientConfiguration paramClientConfiguration)
  {
    this.credentials = paramBCSCredentials;
    setEndpoint(paramString);
    this.bcsHttpClient = new BCSHttpClient(paramClientConfiguration);
  }

   从构造函数来看bcsCredential是必须得,后一个就是endpoint,其实就是host,最后是客户端配置,默认是ClientConfiguration,从这个里面可以看到可以传递一个ClientConfirguration对象。

    我们接下来再看一下一个具体的动作,以copy文件为例:

  public BaiduBCSResponse<Empty> copyObject(CopyObjectRequest paramCopyObjectRequest)
    throws BCSClientException, BCSServiceException
  {
    assertParameterNotNull(paramCopyObjectRequest, "The request parameter can be null.");
    assertParameterNotNull(paramCopyObjectRequest.getHttpMethod(), "The http method parameter in Request must be specified.");
    assertParameterNotNull(paramCopyObjectRequest.getSource().getBucket(), "The bucket parameter of source must be specified when copy an object.");

    assertParameterNotNull(paramCopyObjectRequest.getSource().getObject(), "The object parameter of source must be specified when copy an object.");

    assertParameterNotNull(paramCopyObjectRequest.getDest().getBucket(), "The bucket parameter of dest must be specified when copy an object.");
    assertParameterNotNull(paramCopyObjectRequest.getDest().getObject(), "The object parameter of dest must be specified when copy an object.");
    log.debug("copy object, src[Bucket:" + paramCopyObjectRequest.getSource().getBucket() + "][Object:" + paramCopyObjectRequest.getSource().getObject() + "] to dest[Bucket" + paramCopyObjectRequest.getDest().getBucket() + "][Object" + paramCopyObjectRequest.getDest().getObject() + "]");

    BCSHttpRequest localBCSHttpRequest = createHttpRequest(paramCopyObjectRequest);

    localBCSHttpRequest.addHeader("x-bs-copy-source", "bs://" + paramCopyObjectRequest.getSource().getBucket() + paramCopyObjectRequest.getSource().getObject());

    if (null != paramCopyObjectRequest.getSourceEtag()) {
      localBCSHttpRequest.addHeader("x-bs-copy-source-tag", paramCopyObjectRequest.getSourceEtag());
    }

    if (null != paramCopyObjectRequest.getSourceDirective()) {
      localBCSHttpRequest.addHeader("x-bs-copy-source-directive", paramCopyObjectRequest.getSourceDirective());
    }

    populateRequestMetadata(localBCSHttpRequest, paramCopyObjectRequest.getDestMetadata());

    return this.bcsHttpClient.execute(localBCSHttpRequest, new VoidResponseHandler());
  }

     从代码的整体结构来看,是通过一个request对象,创建一个httpRequest对象,然后通过bcsHttpClient的execute方法执行,一个参数是HttpRequest,一个是ResponseHandler,从这个地方来看,只需要构造响应的request和响应的responseHandler就好。

baidu云存储java sdk源码阅读之请求发送

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号