diff --git a/common/src/main/java/com/genexus/CommonUtil.java b/common/src/main/java/com/genexus/CommonUtil.java index 42af5b2e8..979c1b765 100644 --- a/common/src/main/java/com/genexus/CommonUtil.java +++ b/common/src/main/java/com/genexus/CommonUtil.java @@ -9,6 +9,9 @@ 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.*; @@ -176,6 +179,62 @@ 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 a939b164e..4ffa47c49 100644 --- a/common/src/main/java/com/genexus/internet/GXHttpClient.java +++ b/common/src/main/java/com/genexus/internet/GXHttpClient.java @@ -568,44 +568,48 @@ 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"); } @@ -613,29 +617,32 @@ 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; } - try (BufferedInputStream bis = new java.io.BufferedInputStream(new FileInputStream(file))) + fileToPost = file; + + try (BufferedInputStream bis = new 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 878f4947a..f9a12ee72 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,6 +17,7 @@ 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; @@ -292,62 +293,6 @@ 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) { @@ -701,7 +646,7 @@ private String copyWithACL(String objectUrl, String newName, String tableName, S .bucket(bucket) .key(resourceKey) .metadata(metadata) - .contentType(getContentType(newName)); + .contentType(CommonUtil.getContentType(newName)); if (endpointUrl.contains(".amazonaws.com")) putObjectRequestBuilder = putObjectRequestBuilder.acl(internalToAWSACLWithACL(acl)); PutObjectRequest putObjectRequest = putObjectRequestBuilder.build(); @@ -821,7 +766,7 @@ private String copyWithoutACL(String objectUrl, String newName, String tableName .bucket(bucket) .key(resourceKey) .metadata(metadata) - .contentType(getContentType(newName)); + .contentType(CommonUtil.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 d96e86690..7ff054f56 100644 --- a/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java +++ b/java/src/main/java/com/genexus/internet/HttpClientJavaLib.java @@ -511,24 +511,67 @@ public void execute(String method, String url) { HttpPost httpPost = new HttpPost(url.trim()); httpPost.setConfig(reqConfig); Set keys = getheadersToSend().keySet(); - boolean hasConentType = false; + boolean hasContentType = false; + for (String header : keys) { httpPost.addHeader(header, getheadersToSend().get(header)); - if (header.equalsIgnoreCase("Content-type")) - hasConentType = true; + if (header.equalsIgnoreCase("Content-Type")) { + hasContentType = true; + } } - if (!hasConentType) // Si no se setea Content-type, se pone uno default - httpPost.addHeader("Content-type", "application/x-www-form-urlencoded"); - ByteArrayEntity dataToSend; - if (!getIsMultipart() && getVariablesToSend().size() > 0) - dataToSend = new ByteArrayEntity(CommonUtil.hashtable2query(getVariablesToSend()).getBytes()); - else - dataToSend = new ByteArrayEntity(getData()); - httpPost.setEntity(dataToSend); + if (getIsMultipart()) { + if (!hasContentType) { + httpPost.addHeader("Content-Type", "multipart/form-data"); + } - response = httpClient.execute(httpPost, httpClientContext); + 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); + } + + response = httpClient.execute(httpPost, httpClientContext); } else if (method.equalsIgnoreCase("PUT")) { HttpPut httpPut = new HttpPut(url.trim()); httpPut.setConfig(reqConfig);