88import com .messagebird .exceptions .UnauthorizedException ;
99import com .messagebird .objects .ErrorReport ;
1010import com .messagebird .objects .PagedPaging ;
11+ import org .apache .maven .artifact .versioning .ComparableVersion ;
12+
1113import java .io .File ;
1214import java .io .FileOutputStream ;
1315import java .io .IOException ;
@@ -60,8 +62,7 @@ public class MessageBirdServiceImpl implements MessageBirdService {
6062 private static final String [] PROTOCOL_LISTS = new String []{"http://" , "https://" };
6163 private static final List <String > PROTOCOLS = Arrays .asList (PROTOCOL_LISTS );
6264
63- // Used when the actual version can not be parsed.
64- private static final double DEFAULT_JAVA_VERSION = 0.0 ;
65+ private static final ComparableVersion JAVA_VERSION = getJavaVersion ();
6566
6667 // Indicates whether we've overridden HttpURLConnection's behaviour to
6768 // allow PATCH requests yet. Also see docs on allowPatchRequestsIfNeeded().
@@ -88,15 +89,17 @@ public MessageBirdServiceImpl(final String accessKey, final String serviceUrl) {
8889
8990 }
9091
91- private String determineUserAgentString () {
92- double javaVersion = DEFAULT_JAVA_VERSION ;
92+ private static ComparableVersion getJavaVersion () {
9393 try {
94- javaVersion = getVersion ();
95- } catch (GeneralException e ) {
96- // Do nothing: leave the version at its default.
94+ String version = System .getProperty ("java.version" );
95+ return new ComparableVersion (version );
96+ } catch (IllegalArgumentException e ) {
97+ return new ComparableVersion ("0.0" );
9798 }
99+ }
98100
99- return String .format ("MessageBird Java/%s ApiClient/%s" , javaVersion , clientVersion );
101+ private String determineUserAgentString () {
102+ return String .format ("MessageBird Java/%s ApiClient/%s" , JAVA_VERSION , clientVersion );
100103 }
101104
102105 /**
@@ -360,7 +363,7 @@ private void handleHttpFailStatuses(final int status, String body) throws Unauth
360363 <P > APIResponse doRequest (final String method , final String url , final Map <String , String > headers , final P payload ) throws GeneralException {
361364 HttpURLConnection connection = null ;
362365 InputStream inputStream = null ;
363-
366+
364367 if (METHOD_PATCH .equalsIgnoreCase (method )) {
365368 // It'd perhaps be cleaner to call this in the constructor, but
366369 // we'd then need to throw GeneralExceptions from there. This means
@@ -473,13 +476,13 @@ private synchronized static void allowPatchRequestsIfNeeded() throws GeneralExce
473476 Field modifiersField = Field .class .getDeclaredField ("modifiers" );
474477 modifiersField .setAccessible (true );
475478 modifiersField .setInt (methodsField , methodsField .getModifiers () & ~Modifier .FINAL );
476-
479+
477480 Object noInstanceBecauseStaticField = null ;
478-
481+
479482 // Determine what methods should be allowed.
480483 String [] existingMethods = (String []) methodsField .get (noInstanceBecauseStaticField );
481484 String [] allowedMethods = getAllowedMethods (existingMethods );
482-
485+
483486 // Override the actual field to allow PATCH.
484487 methodsField .set (noInstanceBecauseStaticField , allowedMethods );
485488
@@ -631,34 +634,13 @@ private void setAdditionalHeaders(HttpURLConnection connection, Map<String, Stri
631634 }
632635
633636 private DateFormat getDateFormat () {
634- double javaVersion = DEFAULT_JAVA_VERSION ;
635- try {
636- javaVersion = getVersion ();
637- } catch (GeneralException e ) {
638- // Do nothing: leave the version at its default.
639- }
640-
641- if (javaVersion > 1.6 ) {
637+ ComparableVersion java6 = new ComparableVersion ("1.6" );
638+ if (JAVA_VERSION .compareTo (java6 ) > 0 ) {
642639 return new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssXXX" );
643640 }
644641 return new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssZZZ" );
645642 }
646643
647- private double getVersion () throws GeneralException {
648- String version = System .getProperty ("java.version" );
649-
650- try {
651- int pos = version .indexOf ('.' );
652- pos = version .indexOf ('.' , pos + 1 );
653-
654- return Double .parseDouble (version .substring (0 , pos ));
655- } catch (RuntimeException e ) {
656- // Thrown if the index is out of bounds, or when we can't parse a
657- // double for some reason.
658- throw new GeneralException (e );
659- }
660- }
661-
662644 /**
663645 * Get the MessageBird error report data.
664646 *
@@ -802,7 +784,7 @@ private String getPathVariables(final Map<String, Object> map) {
802784 // the value is returned from the next() call
803785 bpath .append (encodeKeyValuePair (param .getKey (), iterator .next ()));
804786 count ++;
805- }
787+ }
806788 } else {
807789 // If the value is not a collection, create the querystring value directly.
808790 bpath .append (encodeKeyValuePair (param .getKey (), param .getValue ()));
0 commit comments