Client.java 4.21 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
package com.lyms.cm.qiniu;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.util.EntityUtils;

/**
* The class {@code Client} is a typical wrapper of RPC. Also see
* {@code com.qiniu.api.auth.DigestAuthClient}
*/
public class Client {

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

}

/**
* 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);
try {
setAuth(postMethod);
HttpResponse response = client.execute(postMethod);
return handleResult(response);
} catch (Exception e) {
e.printStackTrace();
return new CallRet(400, e);
}
}

/**
* 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);
try {
StringEntity entity = new UrlEncodedFormEntity(nvps, "UTF-8");
entity.setContentType("application/x-www-form-urlencoded");
postMethod.setEntity(entity);

setAuth(postMethod);
HttpResponse response = client.execute(postMethod);

return handleResult(response);
} catch (Exception e) {
e.printStackTrace();
return new CallRet(400, e);
}
}

/**
* Sends a http request to the specified url with the specified entity.
* @param url
* @param entity
* @return A general response
*/
public CallRet callWithBinary(String url, AbstractHttpEntity entity) {
HttpClient client = Http.getClient();
HttpPost postMethod = new HttpPost(url);
postMethod.setEntity(entity);

try {
setAuth(postMethod);
HttpResponse response = client.execute(postMethod);
return handleResult(response);
} catch (Exception e) {
e.printStackTrace();
return new CallRet(400, e);
}
}

/**
*
* @param url
* the request url
* @param contentType
* the request content type
* @param body
* the request body
* @param bodyLength
* the length of the request body
* @return A general response
*/
public CallRet callWithBinary(String url, String contentType, byte[] body) {
ByteArrayEntity entity = new ByteArrayEntity(body);

if (contentType == null || contentType.isEmpty()) {
contentType = "application/octet-stream";
}
entity.setContentType(contentType);
return callWithBinary(url, entity);
}

/**
*
* @param url
* @param requestEntity
* @return A general response format
*/
public CallRet callWithMultiPart(String url, MultipartEntity requestEntity) {
HttpPost postMethod = new HttpPost(url);
postMethod.setEntity(requestEntity);
HttpClient client = Http.getClient();
try {
HttpResponse response = client.execute(postMethod);
return handleResult(response);
} catch (Exception e) {
e.printStackTrace();
return new CallRet(400, e);
}
}

/**
* Transforms a httpresponse to user expected format.
*
* @param response
* http response body
* @return a formated general response structure
*/
private CallRet handleResult(HttpResponse response) {
if (response == null || response.getStatusLine() == null) {
return new CallRet(400, "No response");
}

String responseBody;
try {
responseBody = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
e.printStackTrace();
return new CallRet(400, e);
}

StatusLine status = response.getStatusLine();
int statusCode = (status == null) ? 400 : status.getStatusCode();
return new CallRet(statusCode, responseBody);
}

}