Skip to content

Commit e2333d6

Browse files
Revert "Handle boundaries in multipart request (#927)"
This reverts commit 74e9a8d.
1 parent 53588c4 commit e2333d6

File tree

4 files changed

+87
-141
lines changed

4 files changed

+87
-141
lines changed

common/src/main/java/com/genexus/CommonUtil.java

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
import java.math.BigDecimal;
1010
import java.io.*;
1111
import java.net.URLEncoder;
12-
import java.nio.file.Files;
13-
import java.nio.file.Path;
14-
import java.nio.file.Paths;
1512
import java.text.*;
1613
import java.util.*;
1714

@@ -179,62 +176,6 @@ public Object initialValue()
179176
throw new ExceptionInInitializerError("GXUtil static constructor error: " + e.getMessage());
180177
}
181178
}
182-
183-
public static String getContentType(String fileName) {
184-
Path path = Paths.get(fileName);
185-
String defaultContentType = "application/octet-stream";
186-
187-
try {
188-
String probedContentType = Files.probeContentType(path);
189-
if (probedContentType == null || probedContentType.equals(defaultContentType))
190-
return findContentTypeByExtension(fileName);
191-
return probedContentType;
192-
} catch (IOException ioe) {
193-
return findContentTypeByExtension(fileName);
194-
}
195-
}
196-
197-
private static String findContentTypeByExtension(String fileName) {
198-
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
199-
String contentType = contentTypes.get(fileExtension);
200-
return contentType != null ? contentTypes.get(fileExtension) : "application/octet-stream";
201-
}
202-
203-
private static Map<String, String> contentTypes = new HashMap<String, String>() {{
204-
put("txt" , "text/plain");
205-
put("rtx" , "text/richtext");
206-
put("htm" , "text/html");
207-
put("html" , "text/html");
208-
put("xml" , "text/xml");
209-
put("aif" , "audio/x-aiff");
210-
put("au" , "audio/basic");
211-
put("wav" , "audio/wav");
212-
put("bmp" , "image/bmp");
213-
put("gif" , "image/gif");
214-
put("jpe" , "image/jpeg");
215-
put("jpeg" , "image/jpeg");
216-
put("jpg" , "image/jpeg");
217-
put("jfif" , "image/pjpeg");
218-
put("tif" , "image/tiff");
219-
put("tiff" , "image/tiff");
220-
put("png" , "image/x-png");
221-
put("3gp" , "video/3gpp");
222-
put("3g2" , "video/3gpp2");
223-
put("mpg" , "video/mpeg");
224-
put("mpeg" , "video/mpeg");
225-
put("mov" , "video/quicktime");
226-
put("qt" , "video/quicktime");
227-
put("avi" , "video/x-msvideo");
228-
put("exe" , "application/octet-stream");
229-
put("dll" , "application/x-msdownload");
230-
put("ps" , "application/postscript");
231-
put("pdf" , "application/pdf");
232-
put("svg" , "image/svg+xml");
233-
put("tgz" , "application/x-compressed");
234-
put("zip" , "application/x-zip-compressed");
235-
put("gz" , "application/x-gzip");
236-
put("json" , "application/json");
237-
}};
238179

239180
public static String removeAllQuotes(String fileName)
240181
{

common/src/main/java/com/genexus/internet/GXHttpClient.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -568,81 +568,74 @@ protected String setPathUrl(String url) {
568568
return url;
569569
}
570570

571-
572-
protected File fileToPost;
573-
protected String fileToPostName;
574-
575571
@SuppressWarnings("unchecked")
576572
protected byte[] getData()
577573
{
578574
byte[] out = new byte[0];
579575

580-
for (Object key : getVariablesToSend().keySet())
576+
for (Object key: getVariablesToSend().keySet())
581577
{
582-
String value = getMultipartTemplate().getFormDataTemplate((String) key, (String) getVariablesToSend().get(key));
583-
getContentToSend().add(0, value); // Variables al principio
578+
String value = getMultipartTemplate().getFormDataTemplate((String)key, (String)getVariablesToSend().get(key));
579+
getContentToSend().add(0, value); //Variables al principio
584580
}
585581

586582
for (int idx = 0; idx < getContentToSend().size(); idx++)
587583
{
588584
Object curr = getContentToSend().elementAt(idx);
589585

590-
if (curr instanceof String)
586+
if (curr instanceof String)
591587
{
592588
try
593589
{
594-
if (contentEncoding != null)
590+
if(contentEncoding != null)
595591
{
596-
out = addToArray(out, ((String) curr).getBytes(contentEncoding));
597-
} else
592+
out = addToArray(out, ((String)curr).getBytes(contentEncoding));
593+
}else
598594
{
599595
out = addToArray(out, (String) curr);
600596
}
601-
} catch (UnsupportedEncodingException e)
597+
}catch(UnsupportedEncodingException e)
602598
{
603599
System.err.println(e.toString());
604600
out = addToArray(out, (String) curr);
605601
}
606602
}
607-
else if (curr instanceof Object[])
603+
else if (curr instanceof Object[])
608604
{
609-
StringWriter writer = (StringWriter) ((Object[]) curr)[0];
610-
StringBuffer encoding = (StringBuffer) ((Object[]) curr)[1];
605+
StringWriter writer = (StringWriter)((Object[])curr)[0];
606+
StringBuffer encoding = (StringBuffer)((Object[])curr)[1];
611607

612-
if (encoding == null || encoding.length() == 0)
608+
if(encoding == null || encoding.length() == 0)
613609
{
614610
encoding = new StringBuffer("UTF-8");
615611
}
616612
try
617613
{
618614
out = addToArray(out, writer.toString().getBytes(encoding.toString()));
619615
}
620-
catch (UnsupportedEncodingException e)
616+
catch(UnsupportedEncodingException e)
621617
{
622618
out = addToArray(out, writer.toString());
623619
}
624620
}
625-
else if (curr instanceof byte[])
621+
else if (curr instanceof byte[])
626622
{
627623
out = addToArray(out, (byte[]) curr);
628624
}
629-
else // File or FormFile
625+
else //File or FormFile
630626
{
631627
File file;
632628
if (curr instanceof FormFile)
633629
{
634-
FormFile formFile = (FormFile) curr;
630+
FormFile formFile = (FormFile)curr;
635631
out = startMultipartFile(out, formFile.name, formFile.file);
636632
file = new File(formFile.file);
637-
fileToPostName = formFile.name;
638633
}
639634
else
640635
{
641636
file = (File) curr;
642637
}
643-
fileToPost = file;
644-
645-
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)))
638+
try (BufferedInputStream bis = new java.io.BufferedInputStream(new FileInputStream(file)))
646639
{
647640
out = addToArray(out, CommonUtil.readToByteArray(bis));
648641
}

gxcloudstorage-awss3-v2/src/main/java/com/genexus/db/driver/ExternalProviderS3V2.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.apache.logging.log4j.Logger;
1818
import org.apache.logging.log4j.LogManager;
1919

20-
import com.genexus.CommonUtil;
2120
import com.genexus.util.GXService;
2221
import com.genexus.util.StorageUtils;
2322
import com.genexus.StructSdtMessages_Message;
@@ -293,6 +292,62 @@ public String copy(String objectUrl, String newName, String tableName, String fi
293292
copyWithoutACL(objectUrl, newName, tableName, fieldName);
294293
}
295294

295+
private String getContentType(String fileName) {
296+
Path path = Paths.get(fileName);
297+
String defaultContentType = "application/octet-stream";
298+
299+
try {
300+
String probedContentType = Files.probeContentType(path);
301+
if (probedContentType == null || probedContentType.equals(defaultContentType))
302+
return findContentTypeByExtension(fileName);
303+
return probedContentType;
304+
} catch (IOException ioe) {
305+
return findContentTypeByExtension(fileName);
306+
}
307+
}
308+
309+
private String findContentTypeByExtension(String fileName) {
310+
String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
311+
String contentType = contentTypes.get(fileExtension);
312+
return contentType != null ? contentTypes.get(fileExtension) : "application/octet-stream";
313+
}
314+
315+
private static Map<String, String> contentTypes = new HashMap<String, String>() {{
316+
put("txt" , "text/plain");
317+
put("rtx" , "text/richtext");
318+
put("htm" , "text/html");
319+
put("html" , "text/html");
320+
put("xml" , "text/xml");
321+
put("aif" , "audio/x-aiff");
322+
put("au" , "audio/basic");
323+
put("wav" , "audio/wav");
324+
put("bmp" , "image/bmp");
325+
put("gif" , "image/gif");
326+
put("jpe" , "image/jpeg");
327+
put("jpeg" , "image/jpeg");
328+
put("jpg" , "image/jpeg");
329+
put("jfif" , "image/pjpeg");
330+
put("tif" , "image/tiff");
331+
put("tiff" , "image/tiff");
332+
put("png" , "image/x-png");
333+
put("3gp" , "video/3gpp");
334+
put("3g2" , "video/3gpp2");
335+
put("mpg" , "video/mpeg");
336+
put("mpeg" , "video/mpeg");
337+
put("mov" , "video/quicktime");
338+
put("qt" , "video/quicktime");
339+
put("avi" , "video/x-msvideo");
340+
put("exe" , "application/octet-stream");
341+
put("dll" , "application/x-msdownload");
342+
put("ps" , "application/postscript");
343+
put("pdf" , "application/pdf");
344+
put("svg" , "image/svg+xml");
345+
put("tgz" , "application/x-compressed");
346+
put("zip" , "application/x-zip-compressed");
347+
put("gz" , "application/x-gzip");
348+
put("json" , "application/json");
349+
}};
350+
296351
private String buildPath(String... pathPart) {
297352
ArrayList<String> pathParts = new ArrayList<>();
298353
for (String part : pathPart) {
@@ -646,7 +701,7 @@ private String copyWithACL(String objectUrl, String newName, String tableName, S
646701
.bucket(bucket)
647702
.key(resourceKey)
648703
.metadata(metadata)
649-
.contentType(CommonUtil.getContentType(newName));
704+
.contentType(getContentType(newName));
650705
if (endpointUrl.contains(".amazonaws.com"))
651706
putObjectRequestBuilder = putObjectRequestBuilder.acl(internalToAWSACLWithACL(acl));
652707
PutObjectRequest putObjectRequest = putObjectRequestBuilder.build();
@@ -766,7 +821,7 @@ private String copyWithoutACL(String objectUrl, String newName, String tableName
766821
.bucket(bucket)
767822
.key(resourceKey)
768823
.metadata(metadata)
769-
.contentType(CommonUtil.getContentType(newName));
824+
.contentType(getContentType(newName));
770825
PutObjectRequest putObjectRequest = putObjectRequestBuilder.build();
771826
client.putObject(putObjectRequest, RequestBody.fromBytes(objectBytes.asByteArray()));
772827

java/src/main/java/com/genexus/internet/HttpClientJavaLib.java

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -511,67 +511,24 @@ public void execute(String method, String url) {
511511
HttpPost httpPost = new HttpPost(url.trim());
512512
httpPost.setConfig(reqConfig);
513513
Set<String> keys = getheadersToSend().keySet();
514-
boolean hasContentType = false;
515-
514+
boolean hasConentType = false;
516515
for (String header : keys) {
517516
httpPost.addHeader(header, getheadersToSend().get(header));
518-
if (header.equalsIgnoreCase("Content-Type")) {
519-
hasContentType = true;
520-
}
517+
if (header.equalsIgnoreCase("Content-type"))
518+
hasConentType = true;
521519
}
520+
if (!hasConentType) // Si no se setea Content-type, se pone uno default
521+
httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");
522522

523-
if (getIsMultipart()) {
524-
if (!hasContentType) {
525-
httpPost.addHeader("Content-Type", "multipart/form-data");
526-
}
527-
528-
String boundary = "----Boundary" + System.currentTimeMillis();
529-
httpPost.removeHeaders("Content-Type");
530-
httpPost.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
531-
532-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
533-
534-
for (Map.Entry<String, String> entry : ((Map<String, String>) getVariablesToSend()).entrySet()) {
535-
if ("fileFieldName".equals(entry.getKey())) {
536-
continue;
537-
}
538-
bos.write(("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8));
539-
bos.write(("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r\n\r\n").getBytes(StandardCharsets.UTF_8));
540-
bos.write(entry.getValue().getBytes(StandardCharsets.UTF_8));
541-
bos.write("\r\n".getBytes(StandardCharsets.UTF_8));
542-
}
543-
if (getData() != null && getData().length > 0) {
544-
String fileFieldName = getVariablesToSend().containsKey("fileFieldName") ?
545-
(String) getVariablesToSend().get("fileFieldName") : fileToPostName;
546-
String fileName = fileToPost != null ? fileToPost.getName() : "uploadedFile";
547-
String contentType = fileToPost != null ? CommonUtil.getContentType(fileName) : "application/octet-stream";
548-
549-
bos.write(("--" + boundary + "\r\n").getBytes(StandardCharsets.UTF_8));
550-
bos.write(("Content-Disposition: form-data; name=\"" + fileFieldName + "\"; filename=\"" + fileName + "\"\r\n").getBytes(StandardCharsets.UTF_8));
551-
bos.write(("Content-Type: " + contentType + "\r\n\r\n").getBytes(StandardCharsets.UTF_8));
552-
bos.write(getData());
553-
bos.write("\r\n".getBytes(StandardCharsets.UTF_8));
554-
}
555-
556-
bos.write(("--" + boundary + "--\r\n").getBytes(StandardCharsets.UTF_8));
557-
ByteArrayEntity dataToSend = new ByteArrayEntity(bos.toByteArray());
558-
httpPost.setEntity(dataToSend);
559-
} else {
560-
if (!hasContentType) {
561-
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
562-
}
563-
564-
ByteArrayEntity dataToSend;
565-
if (getVariablesToSend().size() > 0) {
566-
String formData = CommonUtil.hashtable2query(getVariablesToSend());
567-
dataToSend = new ByteArrayEntity(formData.getBytes(StandardCharsets.UTF_8));
568-
} else {
569-
dataToSend = new ByteArrayEntity(getData());
570-
}
571-
httpPost.setEntity(dataToSend);
572-
}
523+
ByteArrayEntity dataToSend;
524+
if (!getIsMultipart() && getVariablesToSend().size() > 0)
525+
dataToSend = new ByteArrayEntity(CommonUtil.hashtable2query(getVariablesToSend()).getBytes());
526+
else
527+
dataToSend = new ByteArrayEntity(getData());
528+
httpPost.setEntity(dataToSend);
573529

574530
response = httpClient.execute(httpPost, httpClientContext);
531+
575532
} else if (method.equalsIgnoreCase("PUT")) {
576533
HttpPut httpPut = new HttpPut(url.trim());
577534
httpPut.setConfig(reqConfig);

0 commit comments

Comments
 (0)