From ebc1ea29bfd5e26e635c1f8d66570dc0970c9724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Sexenian?= <99925035+tomas-sexenian@users.noreply.github.com> Date: Tue, 11 Feb 2025 10:48:47 -0300 Subject: [PATCH] Revert "Handle boundaries in multipart request (#927)" This reverts commit 74e9a8dca8e235abf19a802a04875f6d8884c367. --- .../src/main/java/com/genexus/CommonUtil.java | 59 ---------------- .../com/genexus/internet/GXHttpClient.java | 41 +++++------- .../db/driver/ExternalProviderS3V2.java | 61 ++++++++++++++++- .../genexus/internet/HttpClientJavaLib.java | 67 ++++--------------- 4 files changed, 87 insertions(+), 141 deletions(-) diff --git a/common/src/main/java/com/genexus/CommonUtil.java b/common/src/main/java/com/genexus/CommonUtil.java index 979c1b765..42af5b2e8 100644 --- a/common/src/main/java/com/genexus/CommonUtil.java +++ b/common/src/main/java/com/genexus/CommonUtil.java @@ -9,9 +9,6 @@ import java.math.BigDecimal; import java.io.*; import java.net.URLEncoder; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.text.*; import java.util.*; @@ -179,62 +176,6 @@ public Object initialValue() throw new ExceptionInInitializerError("GXUtil static constructor error: " + e.getMessage()); } } - - public static String getContentType(String fileName) { - Path path = Paths.get(fileName); - String defaultContentType = "application/octet-stream"; - - try { - String probedContentType = Files.probeContentType(path); - if (probedContentType == null || probedContentType.equals(defaultContentType)) - return findContentTypeByExtension(fileName); - return probedContentType; - } catch (IOException ioe) { - return findContentTypeByExtension(fileName); - } - } - - private static String findContentTypeByExtension(String fileName) { - String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); - String contentType = contentTypes.get(fileExtension); - return contentType != null ? contentTypes.get(fileExtension) : "application/octet-stream"; - } - - private static Map contentTypes = new HashMap() {{ - put("txt" , "text/plain"); - put("rtx" , "text/richtext"); - put("htm" , "text/html"); - put("html" , "text/html"); - put("xml" , "text/xml"); - put("aif" , "audio/x-aiff"); - put("au" , "audio/basic"); - put("wav" , "audio/wav"); - put("bmp" , "image/bmp"); - put("gif" , "image/gif"); - put("jpe" , "image/jpeg"); - put("jpeg" , "image/jpeg"); - put("jpg" , "image/jpeg"); - put("jfif" , "image/pjpeg"); - put("tif" , "image/tiff"); - put("tiff" , "image/tiff"); - put("png" , "image/x-png"); - put("3gp" , "video/3gpp"); - put("3g2" , "video/3gpp2"); - put("mpg" , "video/mpeg"); - put("mpeg" , "video/mpeg"); - put("mov" , "video/quicktime"); - put("qt" , "video/quicktime"); - put("avi" , "video/x-msvideo"); - put("exe" , "application/octet-stream"); - put("dll" , "application/x-msdownload"); - put("ps" , "application/postscript"); - put("pdf" , "application/pdf"); - put("svg" , "image/svg+xml"); - put("tgz" , "application/x-compressed"); - put("zip" , "application/x-zip-compressed"); - put("gz" , "application/x-gzip"); - put("json" , "application/json"); - }}; public static String removeAllQuotes(String fileName) { diff --git a/common/src/main/java/com/genexus/internet/GXHttpClient.java b/common/src/main/java/com/genexus/internet/GXHttpClient.java index 4ffa47c49..a939b164e 100644 --- a/common/src/main/java/com/genexus/internet/GXHttpClient.java +++ b/common/src/main/java/com/genexus/internet/GXHttpClient.java @@ -568,48 +568,44 @@ protected String setPathUrl(String url) { return url; } - - protected File fileToPost; - protected String fileToPostName; - @SuppressWarnings("unchecked") protected byte[] getData() { byte[] out = new byte[0]; - for (Object key : getVariablesToSend().keySet()) + for (Object key: getVariablesToSend().keySet()) { - String value = getMultipartTemplate().getFormDataTemplate((String) key, (String) getVariablesToSend().get(key)); - getContentToSend().add(0, value); // Variables al principio + String value = getMultipartTemplate().getFormDataTemplate((String)key, (String)getVariablesToSend().get(key)); + getContentToSend().add(0, value); //Variables al principio } for (int idx = 0; idx < getContentToSend().size(); idx++) { Object curr = getContentToSend().elementAt(idx); - if (curr instanceof String) + if (curr instanceof String) { try { - if (contentEncoding != null) + if(contentEncoding != null) { - out = addToArray(out, ((String) curr).getBytes(contentEncoding)); - } else + out = addToArray(out, ((String)curr).getBytes(contentEncoding)); + }else { out = addToArray(out, (String) curr); } - } catch (UnsupportedEncodingException e) + }catch(UnsupportedEncodingException e) { System.err.println(e.toString()); out = addToArray(out, (String) curr); } } - else if (curr instanceof Object[]) + else if (curr instanceof Object[]) { - StringWriter writer = (StringWriter) ((Object[]) curr)[0]; - StringBuffer encoding = (StringBuffer) ((Object[]) curr)[1]; + StringWriter writer = (StringWriter)((Object[])curr)[0]; + StringBuffer encoding = (StringBuffer)((Object[])curr)[1]; - if (encoding == null || encoding.length() == 0) + if(encoding == null || encoding.length() == 0) { encoding = new StringBuffer("UTF-8"); } @@ -617,32 +613,29 @@ else if (curr instanceof Object[]) { out = addToArray(out, writer.toString().getBytes(encoding.toString())); } - catch (UnsupportedEncodingException e) + catch(UnsupportedEncodingException e) { out = addToArray(out, writer.toString()); } } - else if (curr instanceof byte[]) + else if (curr instanceof byte[]) { out = addToArray(out, (byte[]) curr); } - else // File or FormFile + else //File or FormFile { File file; if (curr instanceof FormFile) { - FormFile formFile = (FormFile) curr; + FormFile formFile = (FormFile)curr; out = startMultipartFile(out, formFile.name, formFile.file); file = new File(formFile.file); - fileToPostName = formFile.name; } else { file = (File) curr; } - fileToPost = file; - - try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) + try (BufferedInputStream bis = new java.io.BufferedInputStream(new FileInputStream(file))) { out = addToArray(out, CommonUtil.readToByteArray(bis)); } diff --git a/gxcloudstorage-awss3-v2/src/main/java/com/genexus/db/driver/ExternalProviderS3V2.java b/gxcloudstorage-awss3-v2/src/main/java/com/genexus/db/driver/ExternalProviderS3V2.java index f9a12ee72..878f4947a 100644 --- a/gxcloudstorage-awss3-v2/src/main/java/com/genexus/db/driver/ExternalProviderS3V2.java +++ b/gxcloudstorage-awss3-v2/src/main/java/com/genexus/db/driver/ExternalProviderS3V2.java @@ -17,7 +17,6 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; -import com.genexus.CommonUtil; import com.genexus.util.GXService; import com.genexus.util.StorageUtils; import com.genexus.StructSdtMessages_Message; @@ -293,6 +292,62 @@ public String copy(String objectUrl, String newName, String tableName, String fi copyWithoutACL(objectUrl, newName, tableName, fieldName); } + private String getContentType(String fileName) { + Path path = Paths.get(fileName); + String defaultContentType = "application/octet-stream"; + + try { + String probedContentType = Files.probeContentType(path); + if (probedContentType == null || probedContentType.equals(defaultContentType)) + return findContentTypeByExtension(fileName); + return probedContentType; + } catch (IOException ioe) { + return findContentTypeByExtension(fileName); + } + } + + private String findContentTypeByExtension(String fileName) { + String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); + String contentType = contentTypes.get(fileExtension); + return contentType != null ? contentTypes.get(fileExtension) : "application/octet-stream"; + } + + private static Map contentTypes = new HashMap() {{ + put("txt" , "text/plain"); + put("rtx" , "text/richtext"); + put("htm" , "text/html"); + put("html" , "text/html"); + put("xml" , "text/xml"); + put("aif" , "audio/x-aiff"); + put("au" , "audio/basic"); + put("wav" , "audio/wav"); + put("bmp" , "image/bmp"); + put("gif" , "image/gif"); + put("jpe" , "image/jpeg"); + put("jpeg" , "image/jpeg"); + put("jpg" , "image/jpeg"); + put("jfif" , "image/pjpeg"); + put("tif" , "image/tiff"); + put("tiff" , "image/tiff"); + put("png" , "image/x-png"); + put("3gp" , "video/3gpp"); + put("3g2" , "video/3gpp2"); + put("mpg" , "video/mpeg"); + put("mpeg" , "video/mpeg"); + put("mov" , "video/quicktime"); + put("qt" , "video/quicktime"); + put("avi" , "video/x-msvideo"); + put("exe" , "application/octet-stream"); + put("dll" , "application/x-msdownload"); + put("ps" , "application/postscript"); + put("pdf" , "application/pdf"); + put("svg" , "image/svg+xml"); + put("tgz" , "application/x-compressed"); + put("zip" , "application/x-zip-compressed"); + put("gz" , "application/x-gzip"); + put("json" , "application/json"); + }}; + private String buildPath(String... pathPart) { ArrayList pathParts = new ArrayList<>(); for (String part : pathPart) { @@ -646,7 +701,7 @@ private String copyWithACL(String objectUrl, String newName, String tableName, S .bucket(bucket) .key(resourceKey) .metadata(metadata) - .contentType(CommonUtil.getContentType(newName)); + .contentType(getContentType(newName)); if (endpointUrl.contains(".amazonaws.com")) putObjectRequestBuilder = putObjectRequestBuilder.acl(internalToAWSACLWithACL(acl)); PutObjectRequest putObjectRequest = putObjectRequestBuilder.build(); @@ -766,7 +821,7 @@ private String copyWithoutACL(String objectUrl, String newName, String tableName .bucket(bucket) .key(resourceKey) .metadata(metadata) - .contentType(CommonUtil.getContentType(newName)); + .contentType(getContentType(newName)); PutObjectRequest putObjectRequest = putObjectRequestBuilder.build(); client.putObject(putObjectRequest, RequestBody.fromBytes(objectBytes.asByteArray())); diff --git a/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java b/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java index 7ff054f56..d96e86690 100644 --- a/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java +++ b/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java @@ -511,67 +511,24 @@ public void execute(String method, String url) { HttpPost httpPost = new HttpPost(url.trim()); httpPost.setConfig(reqConfig); Set keys = getheadersToSend().keySet(); - boolean hasContentType = false; - + boolean hasConentType = false; for (String header : keys) { httpPost.addHeader(header, getheadersToSend().get(header)); - if (header.equalsIgnoreCase("Content-Type")) { - hasContentType = true; - } + if (header.equalsIgnoreCase("Content-type")) + hasConentType = true; } + if (!hasConentType) // Si no se setea Content-type, se pone uno default + httpPost.addHeader("Content-type", "application/x-www-form-urlencoded"); - if (getIsMultipart()) { - if (!hasContentType) { - httpPost.addHeader("Content-Type", "multipart/form-data"); - } - - String boundary = "----Boundary" + System.currentTimeMillis(); - httpPost.removeHeaders("Content-Type"); - httpPost.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - - for (Map.Entry entry : ((Map) getVariablesToSend()).entrySet()) { - if ("fileFieldName".equals(entry.getKey())) { - continue; - } - bos.write(("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8)); - bos.write(("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r\n\r\n").getBytes(StandardCharsets.UTF_8)); - bos.write(entry.getValue().getBytes(StandardCharsets.UTF_8)); - bos.write("\r\n".getBytes(StandardCharsets.UTF_8)); - } - if (getData() != null && getData().length > 0) { - String fileFieldName = getVariablesToSend().containsKey("fileFieldName") ? - (String) getVariablesToSend().get("fileFieldName") : fileToPostName; - String fileName = fileToPost != null ? fileToPost.getName() : "uploadedFile"; - String contentType = fileToPost != null ? CommonUtil.getContentType(fileName) : "application/octet-stream"; - - bos.write(("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8)); - bos.write(("Content-Disposition: form-data; name=\"" + fileFieldName + "\"; filename=\"" + fileName + "\"\r\n").getBytes(StandardCharsets.UTF_8)); - bos.write(("Content-Type: " + contentType + "\r\n\r\n").getBytes(StandardCharsets.UTF_8)); - bos.write(getData()); - bos.write("\r\n".getBytes(StandardCharsets.UTF_8)); - } - - bos.write(("--" + boundary + "--\r\n").getBytes(StandardCharsets.UTF_8)); - ByteArrayEntity dataToSend = new ByteArrayEntity(bos.toByteArray()); - httpPost.setEntity(dataToSend); - } else { - if (!hasContentType) { - httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); - } - - ByteArrayEntity dataToSend; - if (getVariablesToSend().size() > 0) { - String formData = CommonUtil.hashtable2query(getVariablesToSend()); - dataToSend = new ByteArrayEntity(formData.getBytes(StandardCharsets.UTF_8)); - } else { - dataToSend = new ByteArrayEntity(getData()); - } - httpPost.setEntity(dataToSend); - } + ByteArrayEntity dataToSend; + if (!getIsMultipart() && getVariablesToSend().size() > 0) + dataToSend = new ByteArrayEntity(CommonUtil.hashtable2query(getVariablesToSend()).getBytes()); + else + dataToSend = new ByteArrayEntity(getData()); + httpPost.setEntity(dataToSend); response = httpClient.execute(httpPost, httpClientContext); + } else if (method.equalsIgnoreCase("PUT")) { HttpPut httpPut = new HttpPut(url.trim()); httpPut.setConfig(reqConfig);