public static URLConnection post ( String url , String encodedCredentials , String jsonPayloadObject , Charset charset , ProxyConfig proxy , TrustStoreConfig customTrustStore , ConnectionSettings connectionSettings ) throws Exception { if ( url == null || encodedCredentials == null || jsonPayloadObject == null ) { throw new IllegalArgumentException ( "arguments cannot be null" ) ; } byte [ ] bytes = jsonPayloadObject . getBytes ( charset ) ; URLConnection conn = getConnection ( url , proxy ) ; if ( customTrustStore != null && customTrustStore . getTrustStorePath ( ) != null && conn instanceof HttpsURLConnection ) { KeyStore trustStore = TrustStoreManagerService . getInstance ( ) . getTrustStoreManager ( ) . loadTrustStore ( customTrustStore . getTrustStorePath ( ) , customTrustStore . getTrustStoreType ( ) , customTrustStore . getTrustStorePassword ( ) ) ; TrustManagerFactory tmf = TrustManagerFactory . getInstance ( TrustManagerFactory . getDefaultAlgorithm ( ) ) ; tmf . init ( trustStore ) ; SSLContext ctx = SSLContext . getInstance ( "TLS" ) ; ctx . init ( null , tmf . getTrustManagers ( ) , null ) ; SSLSocketFactory sslSocketFactory = ctx . getSocketFactory ( ) ; ( ( HttpsURLConnection ) conn ) . setSSLSocketFactory ( sslSocketFactory ) ; } conn . setDoOutput ( true ) ; conn . setUseCaches ( false ) ; ( ( HttpURLConnection ) conn ) . setFixedLengthStreamingMode ( bytes . length ) ; conn . setRequestProperty ( "Authorization" , "Basic " + encodedCredentials ) ; conn . setRequestProperty ( "Content-Type" , "application/json" ) ; conn . setRequestProperty ( "Accept" , "application/json, text/plain" ) ; if ( connectionSettings . getReadTimeout ( ) != null ) { conn . setReadTimeout ( connectionSettings . getReadTimeout ( ) ) ; } if ( connectionSettings . getConnectTimeout ( ) != null ) { conn . setConnectTimeout ( connectionSettings . getConnectTimeout ( ) ) ; } // custom header, for UPS conn . setRequestProperty ( "aerogear-sender" , "AeroGear Java Sender" ) ; ( ( HttpURLConnection ) conn ) . setRequestMethod ( "POST" ) ; OutputStream out = null ; try { out = conn . getOutputStream ( ) ; out . write ( bytes ) ; } finally { // in case something blows up, while writing // the payload, we wanna close the stream: if ( out != null ) { out . close ( ) ; } } return conn ; }
Returns URLConnection that posts the given JSON to the given UnifiedPush Server URL .