|
4 | 4 | import HTTPClient.URI;
|
5 | 5 | import com.genexus.CommonUtil;
|
6 | 6 | import com.genexus.common.interfaces.SpecificImplementation;
|
| 7 | +import json.org.json.JSONException; |
| 8 | +import json.org.json.JSONObject; |
| 9 | +import org.w3c.dom.Document; |
| 10 | +import org.xml.sax.InputSource; |
| 11 | +import org.xml.sax.SAXException; |
| 12 | +import org.xml.sax.helpers.DefaultHandler; |
| 13 | + |
| 14 | +import javax.xml.parsers.DocumentBuilder; |
| 15 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 16 | +import javax.xml.parsers.ParserConfigurationException; |
7 | 17 | import java.io.*;
|
8 | 18 | import java.util.Hashtable;
|
9 | 19 | import java.util.Vector;
|
@@ -776,8 +786,40 @@ String getHeaderTemplate(String name, String fileName, String mimeType){
|
776 | 786 | return "Content-Disposition: form-data; name=\""+ name + "\"; filename=\""+ fileName + "\"\r\n" + "Content-Type: " + mimeType + "\r\n\r\n";
|
777 | 787 | }
|
778 | 788 | String getFormDataTemplate(String varName, String value){
|
779 |
| - return "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + varName + "\";\r\n\r\n" + value; |
| 789 | + String contentType = getContentTypeFromString(value); |
| 790 | + return "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + varName + "\";\r\n" + ((contentType != null)? "Content-Type: " + contentType + "\r\n" : "") + "\r\n" + value; |
780 | 791 | }
|
781 |
| - } |
782 | 792 |
|
| 793 | + private String getContentTypeFromString(String value){ |
| 794 | + if (isJsonString(value)) |
| 795 | + return "application/json"; |
| 796 | + |
| 797 | + if (isXMLString(value)) |
| 798 | + return "text/xml"; |
| 799 | + |
| 800 | + return null; |
| 801 | + } |
| 802 | + |
| 803 | + private boolean isJsonString(String value){ |
| 804 | + try { |
| 805 | + JSONObject json = new JSONObject(value); |
| 806 | + return true; |
| 807 | + } catch (JSONException e) { |
| 808 | + return false; |
| 809 | + } |
| 810 | + } |
| 811 | + |
| 812 | + private boolean isXMLString(String value){ |
| 813 | + try { |
| 814 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 815 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 816 | + InputSource inputSource = new InputSource(new StringReader(value)); |
| 817 | + builder.setErrorHandler(new DefaultHandler()); |
| 818 | + Document document = builder.parse(inputSource); |
| 819 | + return true; |
| 820 | + } catch (ParserConfigurationException | SAXException | IOException e) { |
| 821 | + return false; |
| 822 | + } |
| 823 | + } |
| 824 | + } |
783 | 825 | }
|
0 commit comments