8
8
import com .messagebird .exceptions .UnauthorizedException ;
9
9
import com .messagebird .objects .ErrorReport ;
10
10
import com .messagebird .objects .PagedPaging ;
11
+ import org .apache .maven .artifact .versioning .ComparableVersion ;
12
+
11
13
import java .io .File ;
12
14
import java .io .FileOutputStream ;
13
15
import java .io .IOException ;
@@ -60,8 +62,7 @@ public class MessageBirdServiceImpl implements MessageBirdService {
60
62
private static final String [] PROTOCOL_LISTS = new String []{"http://" , "https://" };
61
63
private static final List <String > PROTOCOLS = Arrays .asList (PROTOCOL_LISTS );
62
64
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 ();
65
66
66
67
// Indicates whether we've overridden HttpURLConnection's behaviour to
67
68
// allow PATCH requests yet. Also see docs on allowPatchRequestsIfNeeded().
@@ -88,15 +89,17 @@ public MessageBirdServiceImpl(final String accessKey, final String serviceUrl) {
88
89
89
90
}
90
91
91
- private String determineUserAgentString () {
92
- double javaVersion = DEFAULT_JAVA_VERSION ;
92
+ private static ComparableVersion getJavaVersion () {
93
93
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" );
97
98
}
99
+ }
98
100
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 );
100
103
}
101
104
102
105
/**
@@ -360,7 +363,7 @@ private void handleHttpFailStatuses(final int status, String body) throws Unauth
360
363
<P > APIResponse doRequest (final String method , final String url , final Map <String , String > headers , final P payload ) throws GeneralException {
361
364
HttpURLConnection connection = null ;
362
365
InputStream inputStream = null ;
363
-
366
+
364
367
if (METHOD_PATCH .equalsIgnoreCase (method )) {
365
368
// It'd perhaps be cleaner to call this in the constructor, but
366
369
// we'd then need to throw GeneralExceptions from there. This means
@@ -473,13 +476,13 @@ private synchronized static void allowPatchRequestsIfNeeded() throws GeneralExce
473
476
Field modifiersField = Field .class .getDeclaredField ("modifiers" );
474
477
modifiersField .setAccessible (true );
475
478
modifiersField .setInt (methodsField , methodsField .getModifiers () & ~Modifier .FINAL );
476
-
479
+
477
480
Object noInstanceBecauseStaticField = null ;
478
-
481
+
479
482
// Determine what methods should be allowed.
480
483
String [] existingMethods = (String []) methodsField .get (noInstanceBecauseStaticField );
481
484
String [] allowedMethods = getAllowedMethods (existingMethods );
482
-
485
+
483
486
// Override the actual field to allow PATCH.
484
487
methodsField .set (noInstanceBecauseStaticField , allowedMethods );
485
488
@@ -631,34 +634,13 @@ private void setAdditionalHeaders(HttpURLConnection connection, Map<String, Stri
631
634
}
632
635
633
636
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 ) {
642
639
return new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssXXX" );
643
640
}
644
641
return new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssZZZ" );
645
642
}
646
643
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
-
662
644
/**
663
645
* Get the MessageBird error report data.
664
646
*
@@ -802,7 +784,7 @@ private String getPathVariables(final Map<String, Object> map) {
802
784
// the value is returned from the next() call
803
785
bpath .append (encodeKeyValuePair (param .getKey (), iterator .next ()));
804
786
count ++;
805
- }
787
+ }
806
788
} else {
807
789
// If the value is not a collection, create the querystring value directly.
808
790
bpath .append (encodeKeyValuePair (param .getKey (), param .getValue ()));
0 commit comments