Skip to content

Commit

Permalink
[Java] dive deeper to picture (#224)
Browse files Browse the repository at this point in the history
* fix: local changes fixes

* revert: example things

* feat: trasnport revert

* fix: test thingies

* refactor: delete unsued func

* fix: codacy final func param

* fix: was not possible
  • Loading branch information
arifBurakDemiray authored Jan 23, 2024
1 parent b32c2b0 commit 9b94dd9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

* Fixed a bug where setting custom user properties would not work.
* Fixed a bug where setting organization of the user would not work.
* Fixed a bug where sending a user profile picture with checksum was not possible.

* Deprecated "Countly::backendMode()" call, use "Countly::backendM" instead via "instance()" call.
* Deprecated "Usage::addLocation(double, double)" call, use "Countly::location::setLocation" instead via "instance()" call.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ private Params prepareRequestParamsForUserProfile() {
Params params = new Params();
final JSONObject json = new JSONObject();
perform(json, params);
if (!json.isEmpty()) {

if (!json.isEmpty() || params.has(PICTURE_BYTES) || params.has(PredefinedUserPropertyKeys.PICTURE_PATH)) {
params.add("user_details", json.toString());
}

Expand Down
42 changes: 5 additions & 37 deletions sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ HttpURLConnection connection(final Request request) throws IOException {
byte[] maybePictureData = getPictureDataFromRequest(request);
boolean usingGET = !config.isHTTPPostForced() && request.isGettable(config.getServerURL()) && maybePictureData == null;

if (!usingGET && maybePictureData != null) {
path = setProfilePicturePathRequestParams(path, request.params);
}

if (usingGET && config.getParameterTamperingProtectionSalt() != null) {
request.params.add(CHECKSUM, Utils.digestHex(PARAMETER_TAMPERING_DIGEST, request.params + config.getParameterTamperingProtectionSalt(), L));
}
Expand Down Expand Up @@ -179,17 +175,17 @@ HttpURLConnection connection(final Request request) throws IOException {
addMultipart(output, writer, boundary, "text/plain", CHECKSUM, Utils.digestHex(PARAMETER_TAMPERING_DIGEST, salting.substring(0, salting.length() - 1) + config.getParameterTamperingProtectionSalt(), L), null);
}

writer.append(Utils.CRLF).append("--").append(boundary).append("--").append(Utils.CRLF).flush();
writer.append("--").append(boundary).append("--").append(Utils.CRLF).flush();
} else {
//picture data is "null". If it was sent, we send "null" to server to clear the image there
//we send a normal request in HTTP POST
if (config.getParameterTamperingProtectionSalt() != null) {
request.params.add(CHECKSUM, Utils.digestHex(PARAMETER_TAMPERING_DIGEST, request.params.toString() + config.getParameterTamperingProtectionSalt(), L));
request.params.add(CHECKSUM, Utils.digestHex(PARAMETER_TAMPERING_DIGEST, request.params + config.getParameterTamperingProtectionSalt(), L));
}
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

output = connection.getOutputStream();
writer = new PrintWriter(new OutputStreamWriter(output, Utils.UTF8), true);
writer = new PrintWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8), true);

writer.write(request.params.toString());
writer.flush();
Expand All @@ -213,19 +209,17 @@ HttpURLConnection connection(final Request request) throws IOException {
return connection;
}

void addMultipart(OutputStream output, PrintWriter writer, String boundary, String contentType, String name, String value, Object file) throws IOException {
void addMultipart(OutputStream output, PrintWriter writer, final String boundary, final String contentType, final String name, final String value, final byte[] file) throws IOException {
writer.append("--").append(boundary).append(Utils.CRLF);
if (file != null) {
writer.append("Content-Disposition: form-data; name=\"").append(name).append("\"; filename=\"").append(value).append("\"").append(Utils.CRLF);
writer.append("Content-Type: ").append(contentType).append(Utils.CRLF);
writer.append("Content-Transfer-Encoding: binary").append(Utils.CRLF);
writer.append(Utils.CRLF).flush();
output.write((byte[]) file);
output.write(file);
output.flush();
writer.append(Utils.CRLF).flush();
} else {
writer.append("Content-Disposition: form-data; name=\"").append(name).append("\"").append(Utils.CRLF);
writer.append("Content-Type: ").append(contentType).append("; charset=").append(Utils.UTF8).append(Utils.CRLF);
writer.append(Utils.CRLF).append(value).append(Utils.CRLF).flush();
}
}
Expand Down Expand Up @@ -502,30 +496,4 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) throws
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

private String setProfilePicturePathRequestParams(String path, Params params) {
Params tempParams = new Params();

tempParams.add("device_id", params.remove("device_id"));
tempParams.add("app_key", params.remove("app_key"));
tempParams.add("timestamp", params.remove("timestamp"));
tempParams.add("sdk_name", params.remove("sdk_name"));
tempParams.add("sdk_version", params.remove("sdk_version"));
tempParams.add("tz", params.remove("tz"));
tempParams.add("hour", params.remove("hour"));
tempParams.add("dow", params.remove("dow"));
tempParams.add("rr", params.remove("rr"));

if (params.has("av")) {
tempParams.add("av", params.remove("av"));
}
//if no user details, add empty user details to indicate that we are sending a picture
if (!params.has("user_details")) {
tempParams.add("user_details", "{}");
} else {
tempParams.add("user_details", params.remove("user_details"));
}

return path + tempParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void setPicturePath_localPath() {
//set profile picture url and commit it
sessionHandler(() -> Countly.instance().user().edit().setPicturePath(imgFile.getAbsolutePath()).commit());
validatePictureAndPath(imgFile.getAbsolutePath(), null);
validateUserDetailsRequestInRQ(TestUtils.map("picturePath", imgFile.getAbsolutePath()));
validateUserDetailsRequestInRQ(TestUtils.map("user_details", "{}", "picturePath", imgFile.getAbsolutePath()));
}

/**
Expand Down Expand Up @@ -111,7 +111,7 @@ public void setPicture_binaryData() {
sessionHandler(() -> Countly.instance().user().edit().setPicture(imgData).commit());
validatePictureAndPath(null, imgData);
Countly.session().end();
validateUserDetailsRequestInRQ(TestUtils.map(ModuleUserProfile.PICTURE_BYTES, Utils.Base64.encode(imgData)));
validateUserDetailsRequestInRQ(TestUtils.map("user_details", "{}", ModuleUserProfile.PICTURE_BYTES, Utils.Base64.encode(imgData)));
}

/**
Expand Down

0 comments on commit 9b94dd9

Please sign in to comment.