From 277e11b13ea468697804aeb76a1431446c7d3944 Mon Sep 17 00:00:00 2001 From: Gurmeen Bindra Date: Fri, 21 Aug 2015 14:33:46 +0100 Subject: Fix code to not throw StringPrepParseException that was part of ICU jar The IDNConverter interface now throws java strandard IllegalArg exception instead of the ICU specific StringPrepParseException. Users of stroke that are not using ICU will now be able to use it without the icu jar. Test-information: Sanity tested by connecting to an XMPP server using an XMPP client Change-Id: I6999ae0c690b70bc748f131908a758a01dac20b9 diff --git a/src/com/isode/stroke/idn/ICUConverter.java b/src/com/isode/stroke/idn/ICUConverter.java index bd347df..6f77983 100644 --- a/src/com/isode/stroke/idn/ICUConverter.java +++ b/src/com/isode/stroke/idn/ICUConverter.java @@ -19,18 +19,25 @@ import com.ibm.icu.text.StringPrepParseException; public class ICUConverter implements IDNConverter { - public String getStringPrepared(String s, StringPrepProfile profile) throws StringPrepParseException { + public String getStringPrepared(String s, StringPrepProfile profile) throws IllegalArgumentException { StringPrep str = StringPrep.getInstance(getICUProfileType(profile)); - - String preparedData = str.prepare(s, StringPrep.DEFAULT); - return preparedData; + try { + String preparedData = str.prepare(s, StringPrep.DEFAULT); + return preparedData; + }catch(StringPrepParseException e){ + throw new IllegalArgumentException(e); + } } - public SafeByteArray getStringPrepared(SafeByteArray s, StringPrepProfile profile) throws StringPrepParseException { + public SafeByteArray getStringPrepared(SafeByteArray s, StringPrepProfile profile) throws IllegalArgumentException { StringPrep str = StringPrep.getInstance(getICUProfileType(profile)); - String preparedData = str.prepare(s.toString(), StringPrep.DEFAULT); - return new SafeByteArray(preparedData); + try { + String preparedData = str.prepare(s.toString(), StringPrep.DEFAULT); + return new SafeByteArray(preparedData); + }catch(StringPrepParseException e){ + throw new IllegalArgumentException(e); + } } public String getIDNAEncoded(String s) { diff --git a/src/com/isode/stroke/idn/IDNConverter.java b/src/com/isode/stroke/idn/IDNConverter.java index 3566020..bc929f0 100644 --- a/src/com/isode/stroke/idn/IDNConverter.java +++ b/src/com/isode/stroke/idn/IDNConverter.java @@ -12,7 +12,6 @@ package com.isode.stroke.idn; import com.isode.stroke.base.SafeByteArray; -import com.ibm.icu.text.StringPrepParseException; public interface IDNConverter { @@ -21,10 +20,10 @@ public interface IDNConverter { XMPPNodePrep, XMPPResourcePrep, SASLPrep - }; + } - public String getStringPrepared(String s, StringPrepProfile profile) throws StringPrepParseException; - public SafeByteArray getStringPrepared(SafeByteArray s, StringPrepProfile profile) throws StringPrepParseException; + public String getStringPrepared(String s, StringPrepProfile profile) throws IllegalArgumentException; + public SafeByteArray getStringPrepared(SafeByteArray s, StringPrepProfile profile) throws IllegalArgumentException; // Thread-safe public String getIDNAEncoded(String s); diff --git a/src/com/isode/stroke/jid/JID.java b/src/com/isode/stroke/jid/JID.java index 6067afc..59eef52 100644 --- a/src/com/isode/stroke/jid/JID.java +++ b/src/com/isode/stroke/jid/JID.java @@ -160,7 +160,7 @@ public class JID implements Comparable { node_ = idnConverter.getStringPrepared(node, IDNConverter.StringPrepProfile.XMPPNodePrep); domain_ = idnConverter.getStringPrepared(domain, IDNConverter.StringPrepProfile.NamePrep); resource_ = resource != null ? idnConverter.getStringPrepared(resource, IDNConverter.StringPrepProfile.XMPPResourcePrep) : null; - } catch (StringPrepParseException e) { + } catch (IllegalArgumentException e) { valid_ = false; return; } diff --git a/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java b/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java index 1d2b4d9..2b302d0 100644 --- a/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java +++ b/src/com/isode/stroke/sasl/SCRAMSHA1ClientAuthenticator.java @@ -8,17 +8,15 @@ */ package com.isode.stroke.sasl; +import java.util.HashMap; +import java.util.Map; + import com.isode.stroke.base.ByteArray; import com.isode.stroke.base.SafeByteArray; -import com.ibm.icu.text.StringPrepParseException; +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.idn.IDNConverter; import com.isode.stroke.stringcodecs.Base64; import com.isode.stroke.stringcodecs.PBKDF2; -import com.isode.stroke.idn.IDNConverter; -import com.isode.stroke.crypto.CryptoProvider; -import java.text.Normalizer; -import java.text.Normalizer.Form; -import java.util.HashMap; -import java.util.Map; public class SCRAMSHA1ClientAuthenticator extends ClientAuthenticator { @@ -113,7 +111,7 @@ public class SCRAMSHA1ClientAuthenticator extends ClientAuthenticator { // Compute all the values needed for the server signature try { saltedPassword = PBKDF2.encode(idnConverter.getStringPrepared(getPassword(), IDNConverter.StringPrepProfile.SASLPrep), salt, iterations, crypto); - } catch (StringPrepParseException e) { + } catch (IllegalArgumentException e) { } authMessage = getInitialBareClientMessage().append(",").append(initialServerMessage).append(",").append(getFinalMessageWithoutProof()); @@ -161,7 +159,7 @@ public class SCRAMSHA1ClientAuthenticator extends ClientAuthenticator { String authenticationID = ""; try { authenticationID = idnConverter.getStringPrepared(getAuthenticationID(), IDNConverter.StringPrepProfile.SASLPrep); - } catch (StringPrepParseException e) { + } catch (IllegalArgumentException e) { } return new ByteArray("n=" + escape(authenticationID) + ",r=" + clientnonce); diff --git a/test/com/isode/stroke/idn/IDNConverterTest.java b/test/com/isode/stroke/idn/IDNConverterTest.java index a17affc..eb82a61 100644 --- a/test/com/isode/stroke/idn/IDNConverterTest.java +++ b/test/com/isode/stroke/idn/IDNConverterTest.java @@ -38,7 +38,7 @@ public class IDNConverterTest { try { String result = testling.getStringPrepared("tronçon", IDNConverter.StringPrepProfile.NamePrep); assertEquals("tronçon", result); - } catch (StringPrepParseException e) { + } catch (IllegalArgumentException e) { assertTrue("getStringPrep threw " + e, (e == null)); } } @@ -49,7 +49,7 @@ public class IDNConverterTest { assertEquals("", testling.getStringPrepared("", IDNConverter.StringPrepProfile.NamePrep)); assertEquals("", testling.getStringPrepared("", IDNConverter.StringPrepProfile.XMPPNodePrep)); assertEquals("", testling.getStringPrepared("", IDNConverter.StringPrepProfile.XMPPResourcePrep)); - } catch (StringPrepParseException e) { + } catch (IllegalArgumentException e) { assertTrue("getStringPrep threw " + e, (e == null)); } } -- cgit v0.10.2-6-g49f6