|
1 | 1 | package com.nfl.glitr.relay;
|
2 | 2 |
|
| 3 | +import com.nfl.glitr.exception.GlitrException; |
3 | 4 | import com.nfl.glitr.registry.TypeRegistry;
|
4 | 5 | import graphql.relay.ConnectionCursor;
|
5 | 6 | import graphql.relay.DefaultConnection;
|
|
12 | 13 | import graphql.schema.GraphQLObjectType;
|
13 | 14 | import graphql.schema.GraphQLOutputType;
|
14 | 15 |
|
15 |
| -import javax.xml.bind.DatatypeConverter; |
16 |
| -import java.io.UnsupportedEncodingException; |
17 |
| -import java.nio.charset.Charset; |
| 16 | +import java.nio.charset.StandardCharsets; |
18 | 17 | import java.util.ArrayList;
|
19 | 18 | import java.util.List;
|
20 | 19 |
|
21 | 20 | import static graphql.Assert.assertNotNull;
|
| 21 | +import static java.lang.String.format; |
| 22 | +import static java.util.Base64.getDecoder; |
| 23 | +import static java.util.Base64.getEncoder; |
22 | 24 |
|
23 | 25 | public class RelayHelper {
|
24 | 26 |
|
@@ -80,31 +82,28 @@ public static graphql.relay.Connection buildConnection(Iterable<?> col, int skip
|
80 | 82 | }
|
81 | 83 |
|
82 | 84 | public static String createCursor(int offset) {
|
83 |
| - return Base64.toBase64(DUMMY_CURSOR_PREFIX + Integer.toString(offset)); |
| 85 | + byte[] bytes = (DUMMY_CURSOR_PREFIX + Integer.toString(offset)).getBytes(StandardCharsets.UTF_8); |
| 86 | + return getEncoder().encodeToString(bytes); |
84 | 87 | }
|
85 | 88 |
|
86 | 89 | public static int getOffsetFromCursor(String cursor, int defaultValue) {
|
87 |
| - if (cursor == null) return defaultValue; |
88 |
| - String string = Base64.fromBase64(cursor); |
89 |
| - return Integer.parseInt(string.substring(DUMMY_CURSOR_PREFIX.length())); |
90 |
| - } |
91 |
| - |
92 |
| - |
93 |
| - static public class Base64 { |
94 |
| - |
95 |
| - private Base64() { |
| 90 | + if (cursor == null) { |
| 91 | + return defaultValue; |
96 | 92 | }
|
97 |
| - |
98 |
| - public static String toBase64(String string) { |
99 |
| - try { |
100 |
| - return DatatypeConverter.printBase64Binary(string.getBytes("utf-8")); |
101 |
| - } catch (UnsupportedEncodingException e) { |
102 |
| - throw new RuntimeException(e); |
103 |
| - } |
| 93 | + byte[] decode; |
| 94 | + try { |
| 95 | + decode = getDecoder().decode(cursor); |
| 96 | + } catch (IllegalArgumentException e) { |
| 97 | + throw new GlitrException(format("The cursor is not in base64 format : '%s'", cursor), e); |
104 | 98 | }
|
105 |
| - |
106 |
| - public static String fromBase64(String string) { |
107 |
| - return new String(DatatypeConverter.parseBase64Binary(string), Charset.forName("UTF-8")); |
| 99 | + String string = new String(decode, StandardCharsets.UTF_8); |
| 100 | + if (DUMMY_CURSOR_PREFIX.length() > string.length()) { |
| 101 | + throw new GlitrException(format("The cursor prefix is missing from the cursor : '%s'", cursor)); |
| 102 | + } |
| 103 | + try { |
| 104 | + return Integer.parseInt(string.substring(DUMMY_CURSOR_PREFIX.length())); |
| 105 | + } catch (NumberFormatException nfe) { |
| 106 | + throw new GlitrException(format("The cursor was not created by this class : '%s'", cursor), nfe); |
108 | 107 | }
|
109 | 108 | }
|
110 | 109 | }
|
0 commit comments