Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] dive deeper to picture #224

Merged
merged 7 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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