using System;
using System.Collections.Generic;
using System.Text;
using COSXML.Common;
using COSXML.CosException;
namespace COSXML.Model.Object
{
///
/// 实现 Object 跨域访问配置的预请求
///
///
public sealed class OptionObjectRequest : ObjectRequest
{
///
/// 模拟跨域访问的请求来源域名
///
private string origin;
///
/// 模拟跨域访问的请求 HTTP 方法
///
private string accessControlMethod;
public OptionObjectRequest(string bucket, string key, string origin, string accessControlMethod)
: base(bucket, key)
{
this.method = CosRequestMethod.OPTIONS;
this.origin = origin;
if (accessControlMethod != null)
{
this.accessControlMethod = accessControlMethod.ToUpper();
}
}
///
/// 模拟跨域访问的请求头部
///
///
public void SetAccessControlHeaders(List accessControlHeaders)
{
if (accessControlHeaders != null)
{
StringBuilder headers = new StringBuilder();
foreach (string accessControlHeader in accessControlHeaders)
{
if (accessControlHeader != null)
{
headers.Append(accessControlHeader).Append(",");
}
}
string result = headers.ToString();
if (result.EndsWith(","))
{
result = result.Substring(0, result.Length - 1);
SetRequestHeader(CosRequestHeaderKey.ACCESS_CONTROL_REQUEST_HEADERS, result);
}
}
}
public override void CheckParameters()
{
if (origin == null)
{
throw new CosClientException((int)CosClientError.InvalidArgument, "origin = null");
}
if (accessControlMethod == null)
{
throw new CosClientException((int)CosClientError.InvalidArgument, "accessControlMethod = null");
}
base.CheckParameters();
}
protected override void InteranlUpdateHeaders()
{
try
{
this.headers.Add(CosRequestHeaderKey.ORIGIN, origin);
}
catch (ArgumentException)
{
this.headers[CosRequestHeaderKey.ORIGIN] = origin;
}
try
{
this.headers.Add(CosRequestHeaderKey.ACCESS_CONTROL_REQUEST_METHOD, accessControlMethod);
}
catch (ArgumentException)
{
this.headers[CosRequestHeaderKey.ACCESS_CONTROL_REQUEST_METHOD] = accessControlMethod;
}
}
}
}