Skip to content

Commit

Permalink
Merge pull request #120 from longbai/useragent
Browse files Browse the repository at this point in the history
update jdk useragent
  • Loading branch information
longbai committed Jun 10, 2014
2 parents f495e82 + cb77d54 commit 17789c2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/qiniu/api/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class Config {
public static final String CHARSET = "utf-8";
/**本地检测不通过、程序抛异常,设置 CallRet 的 statusCode 为此错误码*/
public static final int ERROR_CODE = 0;
public static String USER_AGENT="qiniu java-sdk v6.0.0";

public static final String VERSION = "6.0.5";

/**
* You can get your accesskey from <a href="https://dev.qiniutek.com"
* target="blank"> https://dev.qiniutek.com </a>
Expand All @@ -26,14 +26,14 @@ public class Config {
public static String RS_HOST = "http://rs.qbox.me";

public static String UP_HOST = "http://up.qiniu.com";

public static String RSF_HOST = "http://rsf.qbox.me";

/**
* HTTP连接超时的时间毫秒(ms)
* Determines the timeout in milliseconds until a connection is established.
* A timeout value of zero is interpreted as an infinite timeout.
*
*
* Please note this parameter can only be applied to connections that
* are bound to a particular local address.
*/
Expand Down
45 changes: 28 additions & 17 deletions src/main/java/com/qiniu/api/net/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,45 @@

/**
* The class {@code Client} is a typical wrapper of RPC. Also see
* {@code com.qiniu.api.auth.DigestAuthClient}
* {@code com.qiniu.api.auth.DigestAuthClient}
*/
public class Client {
private final static String HEADER_AGENT="User-Agent";

private final static String HEADER_AGENT="User-Agent";

/**
*
*
* @param post
* @throws AuthException
*/
public void setAuth(HttpPost post) throws AuthException {

}

public static HttpPost newPost(String url){
HttpPost postMethod = new HttpPost(url);
postMethod.setHeader(HEADER_AGENT, getUserAgent());
return postMethod;
}

private static String getUserAgent(){
String javaVersion = "Java/" + System.getProperty("java.version");
String os = System.getProperty("os.name") + " "
+ System.getProperty("os.arch") + " " + System.getProperty("os.version");
String sdk = "QiniuJava/" + Config.VERSION;
return sdk + " (" + os +") " + javaVersion;
}

/**
* Sends a http post request to the specified url.
*
*
* @param url
* the request url
* @return A general response
*/
public CallRet call(String url) {
HttpClient client = Http.getClient();
HttpPost postMethod = new HttpPost(url);
postMethod.setHeader(HEADER_AGENT,Config.USER_AGENT);
HttpPost postMethod = newPost(url);
try {
setAuth(postMethod);
HttpResponse response = client.execute(postMethod);
Expand All @@ -58,20 +71,19 @@ public CallRet call(String url) {
/**
* Sends a http post request to the specified url with a list of
* <code>NameValuePair<code>.
*
*
* @param url
* the request url
* @param nvps
* @return A general response
*/
public CallRet call(String url, List<NameValuePair> nvps) {
HttpClient client = Http.getClient();
HttpPost postMethod = new HttpPost(url);
HttpPost postMethod = newPost(url);
try {
StringEntity entity = new UrlEncodedFormEntity(nvps, "UTF-8");
entity.setContentType("application/x-www-form-urlencoded");
postMethod.setEntity(entity);
postMethod.setHeader(HEADER_AGENT,Config.USER_AGENT);
setAuth(postMethod);
HttpResponse response = client.execute(postMethod);

Expand All @@ -90,7 +102,7 @@ public CallRet call(String url, List<NameValuePair> nvps) {
*/
public CallRet callWithBinary(String url, AbstractHttpEntity entity) {
HttpClient client = Http.getClient();
HttpPost postMethod = new HttpPost(url);
HttpPost postMethod = newPost(url);
postMethod.setEntity(entity);

try {
Expand All @@ -104,7 +116,7 @@ public CallRet callWithBinary(String url, AbstractHttpEntity entity) {
}

/**
*
*
* @param url
* the request url
* @param contentType
Expand All @@ -126,17 +138,16 @@ public CallRet callWithBinary(String url, String contentType, byte[] body) {
}

/**
*
*
* @param url
* @param requestEntity
* @return A general response format
*/
public CallRet callWithMultiPart(String url, MultipartEntity requestEntity) {
HttpPost postMethod = new HttpPost(url);
HttpPost postMethod = newPost(url);
postMethod.setEntity(requestEntity);
postMethod.setHeader(HEADER_AGENT,Config.USER_AGENT);
HttpClient client = Http.getClient();

try {
HttpResponse response = client.execute(postMethod);
return handleResult(response);
Expand All @@ -148,7 +159,7 @@ public CallRet callWithMultiPart(String url, MultipartEntity requestEntity) {

/**
* Transforms a httpresponse to user expected format.
*
*
* @param response
* http response body
* @return a formated general response structure
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/qiniu/api/resumableio/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

import com.qiniu.api.config.Config;
import com.qiniu.api.net.CallRet;
import com.qiniu.api.net.Client;

public class Util {
public static HttpPost buildUpPost(String url, String token) {
HttpPost post = new HttpPost(url);
post.setHeader("User-Agent", Config.USER_AGENT);
HttpPost post = Client.newPost(url);
post.setHeader("Authorization", "UpToken " + token);
return post;
}

public static CallRet handleResult(HttpResponse response) {
try {
StatusLine status = response.getStatusLine();
Expand All @@ -31,7 +31,7 @@ public static CallRet handleResult(HttpResponse response) {
return ret;
}
}

public static long crc32(byte[] data){
CRC32 crc32 = new CRC32();
crc32.update(data);
Expand Down

0 comments on commit 17789c2

Please sign in to comment.