2929import java .io .*;
3030import java .net .HttpURLConnection ;
3131import java .net .URL ;
32+ import java .net .URLConnection ;
3233import java .nio .charset .StandardCharsets ;
3334import java .security .KeyManagementException ;
3435import java .security .NoSuchAlgorithmException ;
@@ -732,13 +733,6 @@ public String getItemState(String item) {
732733 AstericsErrorHandling .instance .reportDebugInfo (this , "Get item (name: " + item + ": " + protocol + "://"
733734 + hostname + ":" + port + "/rest/items/" + item + "/state" );
734735 return httpGet (protocol + "://" + hostname + ":" + port + "/rest/items/" + item + "/state" );
735- } catch (KeyManagementException e ) {
736- tg .stop ();
737- AstericsErrorHandling .instance .reportDebugInfo (this ,
738- "KeyManagement exception, try to use lazyCertificate option (property)" );
739- } catch (NoSuchAlgorithmException e ) {
740- tg .stop ();
741- AstericsErrorHandling .instance .reportDebugInfo (this , "Algortihm exception, please contact the AsTeRICS team" );
742736 } catch (IOException e ) {
743737 // catch a wrong item name
744738 if (e .getMessage ().equalsIgnoreCase ("Not Found" )) {
@@ -769,6 +763,9 @@ public String setItemState(String item, String state) {
769763
770764 HttpURLConnection con = (HttpURLConnection ) url .openConnection ();
771765
766+ //add authentication header, if username and password is set.
767+ con =addAuthenticationHeader (con );
768+
772769 con .setDoOutput (true );
773770 con .setRequestMethod ("POST" );
774771 con .setRequestProperty ("Accept" , "application/json" );
@@ -797,7 +794,6 @@ public String setItemState(String item, String state) {
797794 AstericsErrorHandling .instance .reportDebugInfo (this , "change to :" + state );
798795 return content .toString ();
799796 //return httpGet(protocol + "://" + hostname + ":" + port + "/CMD?" + item + "=" + state);
800-
801797 } catch (IOException e ) {
802798 if (e .getMessage ().equalsIgnoreCase ("Not Found" )) {
803799 tg .stop ();
@@ -812,22 +808,42 @@ public String setItemState(String item, String state) {
812808 return "" ;
813809 }
814810
815- public List <String > getList (String hostname , String type ) throws IOException {
816- List <String > response = new ArrayList <String >();
817-
818- try {
819- AstericsErrorHandling .instance .reportDebugInfo (this ,
820- "Get list (type: " + type + ": " + hostname + "/rest/" + type + "s" );
821-
822- //create JSON Array
823- JSONArray jsonArray = new JSONArray (httpGet (hostname + "/rest/" + type + "s" ));
824-
825- // parse all objects, and extract name
826- for (int i =0 ; i <jsonArray .length ();i ++){
827- JSONObject jsonObject = jsonArray .getJSONObject (i );
828- String name = jsonObject .getString ("name" );
829- response .add (name );
830- }
811+ /**
812+ * Adds Authentication HTTP-Header field to a given HttpURLConnection variable.
813+ * @param conn
814+ * @return
815+ */
816+ private HttpURLConnection addAuthenticationHeader (HttpURLConnection conn ) {
817+ // if we wan't to ignore any certificate errors (not recommended!!!!),
818+ // we need to do additional stuff here
819+ // Based on
820+ // http://www.rgagnon.com/javadetails/java-fix-certificate-problem-in-HTTPS.html
821+ try {
822+ if (lazyCertificate == true ) {
823+ // Install the all-trusting host verifier
824+ HttpsURLConnection .setDefaultHostnameVerifier (hostnameValid );
825+
826+ TrustManager [] trustAllCerts = new TrustManager [] { new X509TrustManager () {
827+ @ Override
828+ public java .security .cert .X509Certificate [] getAcceptedIssuers () {
829+ return null ;
830+ }
831+
832+ @ Override
833+ public void checkClientTrusted (X509Certificate [] certs , String authType ) {
834+ }
835+
836+ @ Override
837+ public void checkServerTrusted (X509Certificate [] certs , String authType ) {
838+ }
839+
840+ } };
841+
842+ SSLContext sc = SSLContext .getInstance ("SSL" );
843+ sc .init (null , trustAllCerts , new java .security .SecureRandom ());
844+ HttpsURLConnection .setDefaultSSLSocketFactory (sc .getSocketFactory ());
845+ }
846+ System .setProperty ("sun.security.ssl.allowUnsafeRenegotiation" , "true" );
831847 } catch (KeyManagementException e ) {
832848 tg .stop ();
833849 AstericsErrorHandling .instance .reportDebugInfo (this ,
@@ -837,52 +853,44 @@ public List<String> getList(String hostname, String type) throws IOException {
837853 AstericsErrorHandling .instance .reportDebugInfo (this , "Algortihm exception, please contact the AsTeRICS team" );
838854 }
839855
840- return response ;
841- }
856+ // check for an username, if given, authenticate via HTTP BASIC
857+ if (this .username .length () != 0 ) {
858+ String userPassword = username + ":" + password ;
859+ String passphraseEncoded = MyBase64 .encode (userPassword .getBytes ());
842860
843- public String httpGet (String urlStr ) throws IOException , KeyManagementException , NoSuchAlgorithmException {
844- // if we wan't to ignore any certificate errors (not recommended!!!!),
845- // we need to do additional stuff here
846- // Based on
847- // http://www.rgagnon.com/javadetails/java-fix-certificate-problem-in-HTTPS.html
848- if (lazyCertificate == true ) {
849- // Install the all-trusting host verifier
850- HttpsURLConnection .setDefaultHostnameVerifier (hostnameValid );
851-
852- TrustManager [] trustAllCerts = new TrustManager [] { new X509TrustManager () {
853- @ Override
854- public java .security .cert .X509Certificate [] getAcceptedIssuers () {
855- return null ;
856- }
861+ conn .setRequestProperty ("Authorization" , "Basic " + passphraseEncoded );
862+ }
863+ return conn ;
864+ }
857865
858- @ Override
859- public void checkClientTrusted (X509Certificate [] certs , String authType ) {
860- }
866+ public List <String > getList (String hostname , String type ) throws IOException {
867+ List <String > response = new ArrayList <String >();
861868
862- @ Override
863- public void checkServerTrusted (X509Certificate [] certs , String authType ) {
864- }
869+ AstericsErrorHandling .instance .reportDebugInfo (this ,
870+ "Get list (type: " + type + ": " + hostname + "/rest/" + type + "s" );
865871
866- } };
872+ //create JSON Array
873+ JSONArray jsonArray = new JSONArray (httpGet (hostname + "/rest/" + type + "s" ));
867874
868- SSLContext sc = SSLContext .getInstance ("SSL" );
869- sc .init (null , trustAllCerts , new java .security .SecureRandom ());
870- HttpsURLConnection .setDefaultSSLSocketFactory (sc .getSocketFactory ());
875+ // parse all objects, and extract name
876+ for (int i =0 ; i <jsonArray .length ();i ++){
877+ JSONObject jsonObject = jsonArray .getJSONObject (i );
878+ String name = jsonObject .getString ("name" );
879+ response .add (name );
871880 }
872- System .setProperty ("sun.security.ssl.allowUnsafeRenegotiation" , "true" );
881+ return response ;
882+ }
883+
884+ public String httpGet (String urlStr ) throws IOException {
873885 URL url = new URL (urlStr );
874886 HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
875- //Bug fix openhab3 REST API: If the Accept types are not explicitly specified, the request fails with error code 400 or 401.
876- conn .setRequestProperty ("Accept" ,"text/plain,application/json" );
877887
878- // check for an username, if given, authenticate via HTTP BASIC
879- if (this .username .length () != 0 ) {
880- String userPassword = username + ":" + password ;
881- String passphraseEncoded = MyBase64 .encode (userPassword .getBytes ());
888+ //add authentication header, if username and password is set.
889+ conn =addAuthenticationHeader (conn );
882890
883- conn . setRequestProperty ( "Authorization" , "Basic " + passphraseEncoded );
884- conn .connect ( );
885- }
891+ //Bug fix openhab3 REST API: If the Accept types are not explicitly specified, the request fails with error code 400 or 401.
892+ conn .setRequestProperty ( "Accept" , "text/plain,application/json" );
893+ conn . connect ();
886894
887895 if (conn .getResponseCode () != 200 ) {
888896 throw new IOException (conn .getResponseMessage ());
0 commit comments