View Javadoc
1   package com.foxinmy.weixin4j.http.support.apache4;
2   
3   import java.io.ByteArrayOutputStream;
4   import java.io.IOException;
5   import java.net.URI;
6   import java.security.cert.X509Certificate;
7   import java.util.List;
8   import java.util.Map.Entry;
9   
10  import javax.net.ssl.HostnameVerifier;
11  import javax.net.ssl.SSLException;
12  import javax.net.ssl.SSLSession;
13  import javax.net.ssl.SSLSocket;
14  
15  import org.apache.http.client.methods.HttpDelete;
16  import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
17  import org.apache.http.client.methods.HttpGet;
18  import org.apache.http.client.methods.HttpHead;
19  import org.apache.http.client.methods.HttpOptions;
20  import org.apache.http.client.methods.HttpPost;
21  import org.apache.http.client.methods.HttpPut;
22  import org.apache.http.client.methods.HttpRequestBase;
23  import org.apache.http.client.methods.HttpTrace;
24  import org.apache.http.conn.ssl.X509HostnameVerifier;
25  import org.apache.http.entity.AbstractHttpEntity;
26  import org.apache.http.util.EntityUtils;
27  
28  import com.foxinmy.weixin4j.http.AbstractHttpClient;
29  import com.foxinmy.weixin4j.http.HttpClientException;
30  import com.foxinmy.weixin4j.http.HttpHeaders;
31  import com.foxinmy.weixin4j.http.HttpMethod;
32  import com.foxinmy.weixin4j.http.HttpRequest;
33  import com.foxinmy.weixin4j.http.entity.HttpEntity;
34  import com.foxinmy.weixin4j.util.StringUtil;
35  
36  /**
37   * Apache HttpComponents 4.x
38   * 
39   * @className HttpComponent4
40   * @author jinyu(foxinmy@gmail.com)
41   * @date 2015年8月18日
42   * @since JDK 1.6
43   * @see
44   */
45  public abstract class HttpComponent4 extends AbstractHttpClient {
46  
47  	/**
48  	 * Create HttpRequest
49  	 */
50  	protected HttpRequestBase createRequest(HttpRequest request)
51  			throws HttpClientException, IOException {
52  		HttpRequestBase httpRequest = createMethod(request);
53  		resolveHeaders(request.getHeaders(), httpRequest);
54  		resolveContent(request.getEntity(), httpRequest);
55  		return httpRequest;
56  	}
57  
58  	/**
59  	 * Create HttpMethod
60  	 */
61  	protected HttpRequestBase createMethod(HttpRequest request)
62  			throws HttpClientException {
63  		HttpMethod method = request.getMethod();
64  		URI uri = request.getURI();
65  		if (method == HttpMethod.GET) {
66  			return new HttpGet(uri);
67  		} else if (method == HttpMethod.HEAD) {
68  			return new HttpHead(uri);
69  		} else if (method == HttpMethod.POST) {
70  			return new HttpPost(uri);
71  		} else if (method == HttpMethod.PUT) {
72  			return new HttpPut(uri);
73  		} else if (method == HttpMethod.DELETE) {
74  			return new HttpDelete(uri);
75  		} else if (method == HttpMethod.OPTIONS) {
76  			return new HttpOptions(uri);
77  		} else if (method == HttpMethod.TRACE) {
78  			return new HttpTrace(uri);
79  		} else {
80  			throw new HttpClientException("unknown request method " + method
81  					+ " for " + uri);
82  		}
83  	}
84  
85  	/**
86  	 * Resolve Headers
87  	 */
88  	protected void resolveHeaders(HttpHeaders headers,
89  			HttpRequestBase httpRequest) {
90  		if (headers == null) {
91  			headers = new HttpHeaders();
92  		}
93  		// Add default accept headers
94  		if (!headers.containsKey(HttpHeaders.ACCEPT)) {
95  			headers.set(HttpHeaders.ACCEPT, "*/*");
96  		}
97  		// Add default user agent
98  		if (!headers.containsKey(HttpHeaders.USER_AGENT)) {
99  			headers.set(HttpHeaders.USER_AGENT, "apache/httpclient4");
100 		}
101 		for (Entry<String, List<String>> header : headers.entrySet()) {
102 			if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
103 				httpRequest.setHeader(header.getKey(),
104 						StringUtil.join(header.getValue(), ';'));
105 			} else {
106 				for (String headerValue : header.getValue()) {
107 					httpRequest.setHeader(header.getKey(),
108 							headerValue != null ? headerValue : "");
109 				}
110 			}
111 		}
112 	}
113 
114 	/**
115 	 * Resolve Content
116 	 */
117 	protected void resolveContent(HttpEntity entity, HttpRequestBase httpRequest)
118 			throws IOException {
119 		if (entity != null) {
120 			ByteArrayOutputStream os = new ByteArrayOutputStream();
121 			entity.writeTo(os);
122 			os.flush();
123 			AbstractHttpEntity httpEntity = new org.apache.http.entity.ByteArrayEntity(
124 					os.toByteArray());
125 			os.close();
126 			httpEntity.setContentType(entity.getContentType().toString());
127 			((HttpEntityEnclosingRequestBase) httpRequest)
128 					.setEntity(httpEntity);
129 		}
130 	}
131 
132 	protected byte[] getContent(org.apache.http.HttpResponse httpResponse)
133 			throws IOException {
134 		return EntityUtils.toByteArray(httpResponse.getEntity());
135 	}
136 
137 	protected static class CustomHostnameVerifier implements
138 			X509HostnameVerifier {
139 
140 		private final HostnameVerifier hostnameVerifier;
141 
142 		public CustomHostnameVerifier(HostnameVerifier hostnameVerifier) {
143 			this.hostnameVerifier = hostnameVerifier;
144 		}
145 
146 		@Override
147 		public boolean verify(String hostname, SSLSession session) {
148 			return hostnameVerifier.verify(hostname, session);
149 		}
150 
151 		@Override
152 		public void verify(String host, SSLSocket ssl) throws IOException {
153 		}
154 
155 		@Override
156 		public void verify(String host, X509Certificate cert)
157 				throws SSLException {
158 		}
159 
160 		@Override
161 		public void verify(String host, String[] cns, String[] subjectAlts)
162 				throws SSLException {
163 		}
164 	}
165 }