Skip to content

Commit 96468f5

Browse files
Handle boundaries in multipart request (#927)
(cherry picked from commit 74e9a8d)
1 parent 7783ce5 commit 96468f5

File tree

4 files changed

+396
-86
lines changed

4 files changed

+396
-86
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
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;
1215
import java.text.*;
1316
import java.util.*;
1417

@@ -176,6 +179,62 @@ public Object initialValue()
176179
throw new ExceptionInInitializerError("GXUtil static constructor error: " + e.getMessage());
177180
}
178181
}
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+
}};
179238

180239
public static String removeAllQuotes(String fileName)
181240
{

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

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

571+
572+
protected File fileToPost;
573+
protected String fileToPostName;
574+
571575
@SuppressWarnings("unchecked")
572576
protected byte[] getData()
573577
{
574578
byte[] out = new byte[0];
575579

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

582586
for (int idx = 0; idx < getContentToSend().size(); idx++)
583587
{
584588
Object curr = getContentToSend().elementAt(idx);
585589

586-
if (curr instanceof String)
590+
if (curr instanceof String)
587591
{
588592
try
589593
{
590-
if(contentEncoding != null)
594+
if (contentEncoding != null)
591595
{
592-
out = addToArray(out, ((String)curr).getBytes(contentEncoding));
593-
}else
596+
out = addToArray(out, ((String) curr).getBytes(contentEncoding));
597+
} else
594598
{
595599
out = addToArray(out, (String) curr);
596600
}
597-
}catch(UnsupportedEncodingException e)
601+
} catch (UnsupportedEncodingException e)
598602
{
599603
System.err.println(e.toString());
600604
out = addToArray(out, (String) curr);
601605
}
602606
}
603-
else if (curr instanceof Object[])
607+
else if (curr instanceof Object[])
604608
{
605-
StringWriter writer = (StringWriter)((Object[])curr)[0];
606-
StringBuffer encoding = (StringBuffer)((Object[])curr)[1];
609+
StringWriter writer = (StringWriter) ((Object[]) curr)[0];
610+
StringBuffer encoding = (StringBuffer) ((Object[]) curr)[1];
607611

608-
if(encoding == null || encoding.length() == 0)
612+
if (encoding == null || encoding.length() == 0)
609613
{
610614
encoding = new StringBuffer("UTF-8");
611615
}
612616
try
613617
{
614618
out = addToArray(out, writer.toString().getBytes(encoding.toString()));
615619
}
616-
catch(UnsupportedEncodingException e)
620+
catch (UnsupportedEncodingException e)
617621
{
618622
out = addToArray(out, writer.toString());
619623
}
620624
}
621-
else if (curr instanceof byte[])
625+
else if (curr instanceof byte[])
622626
{
623627
out = addToArray(out, (byte[]) curr);
624628
}
625-
else //File or FormFile
629+
else // File or FormFile
626630
{
627631
File file;
628632
if (curr instanceof FormFile)
629633
{
630-
FormFile formFile = (FormFile)curr;
634+
FormFile formFile = (FormFile) curr;
631635
out = startMultipartFile(out, formFile.name, formFile.file);
632636
file = new File(formFile.file);
637+
fileToPostName = formFile.name;
633638
}
634639
else
635640
{
636641
file = (File) curr;
637642
}
638-
try (BufferedInputStream bis = new java.io.BufferedInputStream(new FileInputStream(file)))
643+
fileToPost = file;
644+
645+
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)))
639646
{
640647
out = addToArray(out, CommonUtil.readToByteArray(bis));
641648
}

0 commit comments

Comments
 (0)