diff --git a/CHANGELOG.md b/CHANGELOG.md index 9baeb66b..884e46d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleUserProfile.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleUserProfile.java index 72cd09a5..a11cf15c 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleUserProfile.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleUserProfile.java @@ -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()); } diff --git a/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java b/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java index fb21dd9e..f275270f 100644 --- a/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java +++ b/sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java @@ -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)); } @@ -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(); @@ -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(); } } @@ -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; - } } \ No newline at end of file diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/UserEditorTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/UserEditorTests.java index 964169da..6a4aed96 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/UserEditorTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/UserEditorTests.java @@ -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())); } /** @@ -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))); } /**