1 package com.foxinmy.weixin4j.http; 2 3 import java.util.Arrays; 4 import java.util.Set; 5 6 import com.foxinmy.weixin4j.http.entity.FormUrlEntity; 7 import com.foxinmy.weixin4j.http.entity.HttpEntity; 8 9 public abstract class AbstractHttpClient implements HttpClient { 10 11 @Override 12 public HttpResponse get(String url) throws HttpClientException { 13 return execute(HttpMethod.GET, url); 14 } 15 16 @Override 17 public HttpResponse get(String url, URLParameter... parameters) 18 throws HttpClientException { 19 return execute(HttpMethod.GET, url, parameters); 20 } 21 22 @Override 23 public HttpHeaders head(String url) throws HttpClientException { 24 return head(url, (URLParameter[]) null); 25 } 26 27 @Override 28 public HttpHeaders head(String url, URLParameter... parameters) 29 throws HttpClientException { 30 return execute(HttpMethod.HEAD, url, parameters).getHeaders(); 31 } 32 33 @Override 34 public HttpResponse post(String url) throws HttpClientException { 35 return execute(HttpMethod.POST, url); 36 } 37 38 @Override 39 public HttpResponse post(String url, URLParameter... parameters) 40 throws HttpClientException { 41 HttpEntity entity = null; 42 if (parameters != null && parameters.length > 0) { 43 entity = new FormUrlEntity(Arrays.asList(parameters)); 44 } 45 return post(url, entity); 46 } 47 48 @Override 49 public HttpResponse post(String url, HttpEntity entity) 50 throws HttpClientException { 51 HttpRequest request = new HttpRequest(HttpMethod.POST, url); 52 request.setEntity(entity); 53 return execute(request); 54 } 55 56 @Override 57 public void put(String url) throws HttpClientException { 58 execute(HttpMethod.PUT, url); 59 } 60 61 @Override 62 public void put(String url, URLParameter... parameters) 63 throws HttpClientException { 64 execute(HttpMethod.PUT, url, parameters); 65 } 66 67 @Override 68 public void delete(String url) throws HttpClientException { 69 execute(HttpMethod.DELETE, url); 70 } 71 72 @Override 73 public void delete(String url, URLParameter... parameters) 74 throws HttpClientException { 75 execute(HttpMethod.DELETE, url, parameters); 76 } 77 78 @Override 79 public Set<HttpMethod> options(String url) throws HttpClientException { 80 return options(url, (URLParameter[]) null); 81 } 82 83 @Override 84 public Set<HttpMethod> options(String url, URLParameter... parameters) 85 throws HttpClientException { 86 HttpHeaders headers = execute(HttpMethod.OPTIONS, url, parameters) 87 .getHeaders(); 88 return headers.getAllow(); 89 } 90 91 protected HttpResponse execute(HttpMethod method, String url) 92 throws HttpClientException { 93 return execute(new HttpRequest(method, url)); 94 } 95 96 protected HttpResponse execute(HttpMethod method, String url, 97 URLParameter... parameters) throws HttpClientException { 98 StringBuilder buf = new StringBuilder(url); 99 if (parameters != null && parameters.length > 0) { 100 if (url.indexOf("?") < 0) { 101 buf.append("?"); 102 } else { 103 buf.append("&"); 104 } 105 buf.append(FormUrlEntity.formatParameters(Arrays.asList(parameters))); 106 } 107 return execute(new HttpRequest(method, buf.toString())); 108 } 109 110 protected boolean hasError(HttpStatus status) { 111 return (status.series() == HttpStatus.Series.CLIENT_ERROR || status 112 .series() == HttpStatus.Series.SERVER_ERROR); 113 } 114 115 protected void handleResponse(HttpResponse response) 116 throws HttpClientException { 117 HttpStatus status = response.getStatus(); 118 HttpHeaders headers = response.getHeaders(); 119 MimeType resultType = MimeType.valueOf(headers 120 .getFirst(HttpHeaders.CONTENT_TYPE)); 121 if (!MimeType.APPLICATION_JSON.includes(resultType) && hasError(status)) { 122 switch (status.series()) { 123 case CLIENT_ERROR: 124 case SERVER_ERROR: 125 throw new HttpClientException(String.format("%d %s", 126 status.getStatusCode(), status.getStatusText())); 127 default: 128 throw new HttpClientException("Unknown status code [" + status 129 + "]"); 130 } 131 } 132 } 133 }