1111import java .net .UnknownHostException ;
1212import java .security .cert .CertificateException ;
1313import java .security .cert .X509Certificate ;
14- import java .util .regex .Pattern ;
1514
1615import javax .net .ssl .HostnameVerifier ;
1716import javax .net .ssl .HttpsURLConnection ;
2221import org .slf4j .Logger ;
2322import org .slf4j .LoggerFactory ;
2423
25- import cn .jpush .api .utils .Base64 ;
2624import cn .jpush .api .utils .StringUtils ;
2725
2826import com .google .gson .Gson ;
2927import com .google .gson .GsonBuilder ;
3028
31- public class BaseHttpClient {
32- private static final Logger LOG = LoggerFactory .getLogger (BaseHttpClient .class );
29+ public class NativeHttpClient implements IHttpClient {
30+ private static final Logger LOG = LoggerFactory .getLogger (NativeHttpClient .class );
3331
34- private static final String CHARSET = "UTF-8" ;
35- private static final String RATE_LIMIT_QUOTA = "X-Rate-Limit-Limit" ;
36- private static final String RATE_LIMIT_Remaining = "X-Rate-Limit-Remaining" ;
37- private static final String RATE_LIMIT_Reset = "X-Rate-Limit-Reset" ;
38-
39- protected static final int RESPONSE_OK = 200 ;
4032 protected static Gson _gson = new GsonBuilder ().excludeFieldsWithoutExposeAnnotation ().create ();
41-
42- //设置连接超时时间
43- private final int DEFAULT_CONNECTION_TIMEOUT = (20 * 1000 ); // milliseconds
44-
45- //设置读取超时时间
46- private final int DEFAULT_SOCKET_TIMEOUT = (30 * 1000 ); // milliseconds
4733
4834
49- protected ResponseWrapper sendGet (String url , String params , String authCode ) {
50- return sendRequest (url , params , "GET" , authCode );
35+ public ResponseWrapper sendGet (String url , String params , String authCode ) {
36+ return sendRequest (url , params , METHOD_GET , authCode );
5137 }
5238
53- protected ResponseWrapper sendPost (String url , String content , String authCode ) {
54- return sendRequest (url , content , "POST" , authCode );
39+ public ResponseWrapper sendPost (String url , String content , String authCode ) {
40+ return sendRequest (url , content , METHOD_POST , authCode );
5541 }
5642
57- protected ResponseWrapper sendRequest (String url , String content , String method , String authCode ) {
43+ public ResponseWrapper sendRequest (String url , String content , String method , String authCode ) {
5844 LOG .debug ("Send request to - " + url + ", with content - " + content );
5945 HttpURLConnection conn = null ;
6046 OutputStream out = null ;
@@ -77,7 +63,7 @@ protected ResponseWrapper sendRequest(String url, String content, String method,
7763 conn .setRequestProperty ("Authorization" , authCode );
7864 }
7965
80- if (method .equals ("POST" )) {
66+ if (METHOD_POST .equals (method )) {
8167 conn .setDoOutput (true ); //POST Request
8268 conn .setRequestProperty ("Content-Type" , "application/x-www-form-urlencoded" );
8369 byte [] data = content .getBytes (CHARSET );
@@ -188,31 +174,9 @@ protected void initSSL() {
188174 LOG .error ("" , e );
189175 }
190176 }
191-
192- public static String getAuthorizationBase64 (String appKey , String masterSecret ) {
193- String encodeKey = appKey + ":" + masterSecret ;
194- return String .valueOf (Base64 .encode (encodeKey .getBytes ()));
195- }
196-
197- private final static Pattern PUSH_PATTERNS = Pattern .compile ("[^a-zA-Z0-9]" );
198-
199- public static void checkBasic (String appKey , String masterSecret ) {
200- if (StringUtils .isEmpty (appKey )
201- || StringUtils .isEmpty (masterSecret )) {
202- throw new IllegalArgumentException ("appKey and masterSecret are both required." );
203- }
204- if (appKey .length () != 24
205- || masterSecret .length () != 24
206- || PUSH_PATTERNS .matcher (appKey ).find ()
207- || PUSH_PATTERNS .matcher (masterSecret ).find ()) {
208- throw new IllegalArgumentException ("appKey and masterSecret format is incorrect. "
209- + "They should be 24 size, and be composed with alphabet and numbers. "
210- + "Please confirm that they are coming from JPush Web Portal." );
211- }
212- }
213-
214-
215- public class SimpleHostnameVerifier implements HostnameVerifier {
177+
178+
179+ public static class SimpleHostnameVerifier implements HostnameVerifier {
216180
217181 @ Override
218182 public boolean verify (String hostname , SSLSession session ) {
@@ -221,7 +185,7 @@ public boolean verify(String hostname, SSLSession session) {
221185
222186 }
223187
224- public class SimpleTrustManager implements TrustManager , X509TrustManager {
188+ public static class SimpleTrustManager implements TrustManager , X509TrustManager {
225189
226190 @ Override
227191 public void checkClientTrusted (X509Certificate [] chain , String authType )
0 commit comments