Skip to content

Commit 3ea82a6

Browse files
committed
Set up byte[] as a return type rather than String objects. Modify some exceptions
1 parent 60f87c0 commit 3ea82a6

6 files changed

Lines changed: 53 additions & 52 deletions

File tree

‎src/main/java/io/github/hikingc/matrixsdk/services/events/EventService.java‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.github.hikingc.matrixsdk.api.identifiers.RoomID;
1616
import io.github.hikingc.matrixsdk.context.ClientContext;
1717
import io.github.hikingc.matrixsdk.exceptions.MatrixIOException;
18+
import io.github.hikingc.matrixsdk.exceptions.MatrixSerializationException;
1819
import io.github.hikingc.matrixsdk.services.utils.HttpTransport;
1920
import io.github.hikingc.matrixsdk.services.utils.Mapper;
2021
import java.net.URI;
@@ -50,7 +51,7 @@ public EventService(ClientContext context, HttpTransport httpTransport) {
5051
@Override
5152
public ClientEvent<?> getEvent(RoomID roomId, String eventId) {
5253
Objects.requireNonNull(eventId, "The event ID must not be null");
53-
String response =
54+
var response =
5455
httpTransport.getRequest(
5556
URI.create(
5657
context.discoveryResponse().homeserver().baseUrl()
@@ -66,7 +67,7 @@ public ClientEvent<?> getEvent(RoomID roomId, String eventId) {
6667
@Override
6768
public RoomMembers getJoinedMembers(RoomID roomId) {
6869

69-
String response =
70+
var response =
7071
httpTransport.getRequest(
7172
URI.create(
7273
context.discoveryResponse().homeserver().baseUrl()
@@ -90,7 +91,7 @@ public List<RoomMemberEvent> getMembers(
9091
ROOM_ENDPOINT + roomId + "/members",
9192
args);
9293

93-
String response = httpTransport.getRequest(uri, context.token());
94+
var response = httpTransport.getRequest(uri, context.token());
9495
// We can skip the chunk parent, we don't use ObjectFromString because it is NOT a raw Array as
9596
// detailed
9697
// on the spec.
@@ -99,7 +100,7 @@ public List<RoomMemberEvent> getMembers(
99100

100101
@Override
101102
public List<StateEvent<?>> getStateEvents(RoomID roomId) {
102-
String response =
103+
var response =
103104
httpTransport.getRequest(
104105
URI.create(
105106
context.discoveryResponse().homeserver().baseUrl()
@@ -121,7 +122,7 @@ public StateEvent<?> getStateEvent(RoomID roomId, String eventType, String state
121122
context.discoveryResponse().homeserver().baseUrl(),
122123
ROOM_ENDPOINT + roomId + "/state/" + eventType + "/" + stateKey,
123124
args);
124-
String response = httpTransport.getRequest(uri, context.token());
125+
var response = httpTransport.getRequest(uri, context.token());
125126

126127
return Mapper.getObjectFromString(response, StateEvent.class);
127128
}
@@ -140,8 +141,8 @@ public Messages getMessages(
140141
context.discoveryResponse().homeserver().baseUrl(),
141142
ROOM_ENDPOINT + roomId + "/messages",
142143
args);
143-
String queryResponse = httpTransport.getRequest(uri, context.token());
144-
return Mapper.getObjectFromString(queryResponse, Messages.class);
144+
var response = httpTransport.getRequest(uri, context.token());
145+
return Mapper.getObjectFromString(response, Messages.class);
145146
}
146147

147148
@Override
@@ -159,14 +160,14 @@ public EventTimestamp getEventClosestToTimestamp(
159160
context.discoveryResponse().homeserver().baseUrl(),
160161
ROOM_ENDPOINT + roomId + "/timestamp_to_event",
161162
args);
162-
String response = httpTransport.getRequest(uri, context.token());
163+
var response = httpTransport.getRequest(uri, context.token());
163164
return Mapper.getObjectFromString(response, EventTimestamp.class);
164165
}
165166

166167
@Override
167168
public RoomInfo getInitialSync(RoomID roomId) {
168169

169-
String response =
170+
var response =
170171
httpTransport.getRequest(
171172
URI.create(
172173
context.discoveryResponse().homeserver().baseUrl()
@@ -183,7 +184,7 @@ public String sendStateEvent(RoomID roomId, String stateKey, StateEventContent c
183184
try {
184185
jsonPayload = Mapper.writeValueAsString(content);
185186
} catch (JacksonException e) {
186-
throw new MatrixIOException("Failed to parse input data", e);
187+
throw new MatrixSerializationException("Failed to parse input data", e);
187188
}
188189
String type = resolveStateWireType(content);
189190

@@ -192,7 +193,7 @@ public String sendStateEvent(RoomID roomId, String stateKey, StateEventContent c
192193
context.discoveryResponse().homeserver().baseUrl(),
193194
ROOM_ENDPOINT + roomId + "/state/" + type + "/" + stateKey,
194195
null);
195-
String response = httpTransport.putRequest(uri, jsonPayload, context.token());
196+
var response = httpTransport.putRequest(uri, jsonPayload, context.token());
196197
return Mapper.getStringValueOfAJsonKey(response, "event_id");
197198
}
198199

@@ -204,15 +205,15 @@ public String sendMessageEvent(RoomID roomId, String txnId, MessageEventContent
204205
try {
205206
jsonPayload = Mapper.writeValueAsString(content);
206207
} catch (JacksonException e) {
207-
throw new MatrixIOException("Failed to parse input data", e);
208+
throw new MatrixSerializationException("Failed to parse input data", e);
208209
}
209210

210211
URI uri =
211212
httpTransport.generateEncodedURI(
212213
context.discoveryResponse().homeserver().baseUrl(),
213214
ROOM_ENDPOINT + roomId + "/send/" + type + "/" + txnId,
214215
null);
215-
String response = httpTransport.putRequest(uri, jsonPayload, context.token());
216+
var response = httpTransport.putRequest(uri, jsonPayload, context.token());
216217
return Mapper.getStringValueOfAJsonKey(response, "event_id");
217218
}
218219

@@ -224,7 +225,7 @@ public String redactEvent(RoomID roomId, String eventId, String txnId, @Nullable
224225
if (reason != null) {
225226
json = Mapper.createObjectFromMap(Map.ofEntries(Map.entry("reason", reason)));
226227
}
227-
String response =
228+
var response =
228229
httpTransport.putRequest(
229230
URI.create(
230231
context.discoveryResponse().homeserver().baseUrl()
@@ -275,15 +276,15 @@ public Sync sync(QueryParametersSync params) {
275276
httpTransport.generateEncodedURI(
276277
context.discoveryResponse().homeserver().baseUrl(), "/_matrix/client/v3/sync", args);
277278

278-
String response = httpTransport.getRequest(query, context.token());
279+
var response = httpTransport.getRequest(query, context.token());
279280
return Mapper.getObjectFromString(response, Sync.class);
280281
}
281282

282283
/// Creates a new mxc:// for immediate usage.
283284
///
284285
/// @return a [String] representing the MXC
285-
private String createAndReserveMXC() throws JacksonException {
286-
String queryResponse =
286+
private String createAndReserveMXC() {
287+
var queryResponse =
287288
httpTransport.postRequest(
288289
URI.create(
289290
context.discoveryResponse().homeserver().baseUrl()

‎src/main/java/io/github/hikingc/matrixsdk/services/filtering/FilterService.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public String publishFilter(UserID userId, FilterDefinition filter) {
3636
context.discoveryResponse().homeserver().baseUrl(),
3737
USER_FILTER_ENDPOINT + userId + "/filter",
3838
null);
39-
String responseBody = httpTransport.postRequest(uri, serializedInputData, context.token());
39+
var responseBody = httpTransport.postRequest(uri, serializedInputData, context.token());
4040

4141
return Mapper.getStringValueOfAJsonKey(responseBody, "filter_id");
4242
}

‎src/main/java/io/github/hikingc/matrixsdk/services/rooms/RoomService.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public String create(InitialRoomConfiguration configuration) {
5353

5454
String jsonPayload = Mapper.writeValueAsString(configuration);
5555

56-
String responseBody;
57-
responseBody =
56+
var responseBody =
5857
httpTransport.postRequest(
5958
URI.create(
6059
context.discoveryResponse().homeserver().baseUrl()
@@ -104,7 +103,7 @@ public void deleteAlias(RoomAlias roomAlias) {
104103
@Override
105104
public List<String> getAliasesOfARoom(RoomID roomId) {
106105

107-
String response =
106+
var response =
108107
httpTransport.getRequest(
109108
URI.create(
110109
context.discoveryResponse().homeserver().baseUrl()
@@ -128,7 +127,7 @@ public List<String> getAliasesOfARoom(RoomID roomId) {
128127

129128
@Override
130129
public List<String> getJoinedRooms() {
131-
String response =
130+
var response =
132131
httpTransport.getRequest(
133132
URI.create(
134133
context.discoveryResponse().homeserver().baseUrl()
@@ -199,7 +198,7 @@ public String knockOn(Identifier roomIdOrAlias, String reason, List<String> via)
199198
Map<String, Object> map = new HashMap<>();
200199
map.put("reason", reason);
201200

202-
String responseBody =
201+
var responseBody =
203202
httpTransport.postRequest(uri, Mapper.createObjectFromMap(map), context.token());
204203
try {
205204
return Mapper.getStringValueOfAJsonKey(responseBody, ROOM_ID);

‎src/main/java/io/github/hikingc/matrixsdk/services/userdata/UserDataService.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public UsersFound searchUsersByTerm(@Nullable Integer limit, String searchTerm)
4545
"""
4646
.formatted(limitToUse, searchTerm);
4747

48-
String responseBody =
48+
var responseBody =
4949
httpTransport.postRequest(
5050
URI.create(context.discoveryResponse().homeserver().baseUrl() + USER_DIR),
5151
rawTextPayload,
@@ -55,7 +55,7 @@ public UsersFound searchUsersByTerm(@Nullable Integer limit, String searchTerm)
5555

5656
@Override
5757
public UserProfile getUserProfile(UserID userId) {
58-
String responseBody =
58+
var responseBody =
5959
httpTransport.getRequest(
6060
URI.create(context.discoveryResponse().homeserver().baseUrl() + PROFILE_DIR + userId),
6161
context.token());
@@ -65,7 +65,7 @@ public UserProfile getUserProfile(UserID userId) {
6565
@Override
6666
public String getUserProfileByProperty(UserID userId, @Nullable String keyName) {
6767
Objects.requireNonNull(keyName, "The key name must no be null");
68-
String responseBody =
68+
var responseBody =
6969
httpTransport.getRequest(
7070
URI.create(
7171
context.discoveryResponse().homeserver().baseUrl()

‎src/main/java/io/github/hikingc/matrixsdk/services/utils/HttpTransport.java‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.charset.StandardCharsets;
1212
import java.nio.file.Files;
1313
import java.nio.file.Path;
14+
import java.util.Arrays;
1415
import java.util.List;
1516
import java.util.Map;
1617
import java.util.Objects;
@@ -51,7 +52,7 @@ public HttpTransport(HttpClient httpClient) {
5152
///
5253
/// @param response the client response.
5354
/// @throws MatrixApiException when the server responds with an unsuccessful HTTP Code.
54-
private void validateResponse(HttpResponse<String> response) {
55+
private void validateResponse(HttpResponse<byte[]> response) {
5556
var code = response.statusCode();
5657
var body = response.body();
5758
var headers = response.headers();
@@ -60,7 +61,7 @@ private void validateResponse(HttpResponse<String> response) {
6061
return;
6162
}
6263

63-
if (body.isBlank()) {
64+
if (Arrays.toString(body).isBlank()) {
6465
throw new MatrixException("Server returned with HTTP Code:" + code);
6566
}
6667
ErrorResponse errorResponse;
@@ -96,7 +97,7 @@ private void validateResponse(HttpResponse<String> response) {
9697
/// @throws MatrixIOException if an I/O error has occurred while sending the request.
9798
/// @throws MatrixInterruptedException if the operation has been interrupted.
9899
/// @throws MatrixApiException when the response from the server is not successful.
99-
public String getRequest(URI path, @Nullable String authToken) {
100+
public byte[] getRequest(URI path, @Nullable String authToken) {
100101
var builderRequest =
101102
HttpRequest.newBuilder().uri(path).header(CONTENT_TYPE, APPLICATION_JSON).GET();
102103

@@ -105,9 +106,9 @@ public String getRequest(URI path, @Nullable String authToken) {
105106
}
106107
var request = builderRequest.build();
107108

108-
HttpResponse<String> response;
109+
HttpResponse<byte[]> response;
109110
try {
110-
response = client.send(request, HttpResponse.BodyHandlers.ofString());
111+
response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
111112
} catch (IOException e) {
112113
throw new MatrixIOException(
113114
"There has been an I/O error attempting to process this request", e);
@@ -128,7 +129,7 @@ public String getRequest(URI path, @Nullable String authToken) {
128129
/// @throws MatrixIOException if an I/O error has occurred while sending the request.
129130
/// @throws MatrixInterruptedException if the operation has been interrupted.
130131
/// @throws MatrixApiException when the response from the server is not successful.
131-
public String postRequest(URI path, @Nullable String body, @Nullable String authToken) {
132+
public byte[] postRequest(URI path, @Nullable String body, @Nullable String authToken) {
132133
var builderRequest = HttpRequest.newBuilder().uri(path);
133134

134135
if (body != null) {
@@ -145,9 +146,9 @@ public String postRequest(URI path, @Nullable String body, @Nullable String auth
145146
}
146147
var request = builderRequest.build();
147148

148-
HttpResponse<String> response;
149+
HttpResponse<byte[]> response;
149150
try {
150-
response = client.send(request, HttpResponse.BodyHandlers.ofString());
151+
response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
151152
} catch (IOException e) {
152153
throw new MatrixIOException(
153154
"There has been an I/O error attempting to process this request", e);
@@ -170,7 +171,7 @@ public String postRequest(URI path, @Nullable String body, @Nullable String auth
170171
/// @throws MatrixInterruptedException if the operation has been interrupted or a server returned
171172
/// with unsuccessful HTTP Code.
172173
/// @throws MatrixApiException when the response from the server is not successful.
173-
public String postAuth(URI path, @Nullable String body) {
174+
public byte[] postAuth(URI path, @Nullable String body) {
174175
var builderRequest = HttpRequest.newBuilder().uri(path);
175176

176177
builderRequest.header(CONTENT_TYPE, "application/x-www-form-urlencoded");
@@ -182,9 +183,9 @@ public String postAuth(URI path, @Nullable String body) {
182183

183184
var request = builderRequest.build();
184185

185-
HttpResponse<String> response;
186+
HttpResponse<byte[]> response;
186187
try {
187-
response = client.send(request, HttpResponse.BodyHandlers.ofString());
188+
response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
188189
} catch (IOException e) {
189190
throw new MatrixIOException(
190191
"There has been an I/O error attempting to process this request", e);
@@ -206,7 +207,7 @@ public String postAuth(URI path, @Nullable String body) {
206207
/// @throws MatrixInterruptedException if the operation has been interrupted or a server returned
207208
/// with unsuccessful HTTP Code.
208209
/// @throws MatrixApiException when the response from the server is not successful.
209-
public String putRequest(URI path, @Nullable String body, String authToken) {
210+
public byte[] putRequest(URI path, @Nullable String body, String authToken) {
210211

211212
var builderRequest =
212213
HttpRequest.newBuilder()
@@ -219,9 +220,9 @@ public String putRequest(URI path, @Nullable String body, String authToken) {
219220
: HttpRequest.BodyPublishers.noBody());
220221
var request = builderRequest.build();
221222

222-
HttpResponse<String> response;
223+
HttpResponse<byte[]> response;
223224
try {
224-
response = client.send(request, HttpResponse.BodyHandlers.ofString());
225+
response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
225226
} catch (IOException e) {
226227
throw new MatrixIOException(
227228
"There has been an I/O error attempting to process this request", e);
@@ -245,7 +246,7 @@ public String putRequest(URI path, @Nullable String body, String authToken) {
245246
/// @throws MatrixInterruptedException if the operation has been interrupted or a server returned
246247
/// with unsuccessful HTTP Code.
247248
/// @throws MatrixApiException when the response from the server is not successful.
248-
public String putResource(URI path, Path resource, String authToken) {
249+
public byte[] putResource(URI path, Path resource, String authToken) {
249250
HttpRequest uploadRequest;
250251
try {
251252
uploadRequest =
@@ -260,9 +261,9 @@ public String putResource(URI path, Path resource, String authToken) {
260261
"There has been an I/O error attempting to process this request", e);
261262
}
262263

263-
HttpResponse<String> response;
264+
HttpResponse<byte[]> response;
264265
try {
265-
response = client.send(uploadRequest, HttpResponse.BodyHandlers.ofString());
266+
response = client.send(uploadRequest, HttpResponse.BodyHandlers.ofByteArray());
266267
} catch (IOException e) {
267268
throw new MatrixIOException(
268269
"There has been an I/O error attempting to process this request", e);
@@ -283,17 +284,17 @@ public String putResource(URI path, Path resource, String authToken) {
283284
/// @throws MatrixInterruptedException if the operation has been interrupted or a server returned
284285
/// with unsuccessful HTTP Code.
285286
/// @throws MatrixApiException when the response from the server is not successful.
286-
public String deleteRequest(URI path, String authToken) {
287+
public byte[] deleteRequest(URI path, String authToken) {
287288
HttpRequest deleteRequest =
288289
HttpRequest.newBuilder()
289290
.uri(path)
290291
.header(AUTHORIZATION, BEARER + authToken)
291292
.DELETE()
292293
.build();
293294

294-
HttpResponse<String> response;
295+
HttpResponse<byte[]> response;
295296
try {
296-
response = client.send(deleteRequest, HttpResponse.BodyHandlers.ofString());
297+
response = client.send(deleteRequest, HttpResponse.BodyHandlers.ofByteArray());
297298
} catch (IOException e) {
298299
throw new MatrixIOException(
299300
"There has been an I/O error attempting to process this request", e);
@@ -328,7 +329,7 @@ public URI generateEncodedURI(String baseUrl, String path, @Nullable Map<String,
328329
return new URI(
329330
base.getScheme(), base.getAuthority(), path, query.isEmpty() ? null : query, null);
330331
} catch (URISyntaxException e) {
331-
throw new MatrixIOException("Failure parsing URI", e);
332+
throw new MatrixException("Failure parsing URI", e);
332333
}
333334
}
334335

@@ -347,7 +348,7 @@ public URI generateRawURI(String baseUrl, String path, @Nullable Map<String, Obj
347348
return new URI(
348349
base.getScheme(), base.getAuthority(), path, query.isEmpty() ? null : query, null);
349350
} catch (URISyntaxException e) {
350-
throw new MatrixIOException("Failure parsing URI", e);
351+
throw new MatrixException("Failure parsing URI", e);
351352
}
352353
}
353354

0 commit comments

Comments
 (0)