Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions common/src/main/java/com/genexus/internet/GXHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
import HTTPClient.URI;
import com.genexus.CommonUtil;
import com.genexus.common.interfaces.SpecificImplementation;
import json.org.json.JSONException;
import json.org.json.JSONObject;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.util.Hashtable;
import java.util.Vector;
Expand Down Expand Up @@ -776,8 +786,40 @@ String getHeaderTemplate(String name, String fileName, String mimeType){
return "Content-Disposition: form-data; name=\""+ name + "\"; filename=\""+ fileName + "\"\r\n" + "Content-Type: " + mimeType + "\r\n\r\n";
}
String getFormDataTemplate(String varName, String value){
return "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + varName + "\";\r\n\r\n" + value;
String contentType = getContentTypeFromString(value);
return "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"" + varName + "\";\r\n" + ((contentType != null)? "Content-Type: " + contentType + "\r\n" : "") + "\r\n" + value;
}
}

private String getContentTypeFromString(String value){
if (isJsonString(value))
return "application/json";

if (isXMLString(value))
return "text/xml";

return null;
}

private boolean isJsonString(String value){
try {
JSONObject json = new JSONObject(value);
return true;
} catch (JSONException e) {
return false;
}
}

private boolean isXMLString(String value){
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(value));
builder.setErrorHandler(new DefaultHandler());
Document document = builder.parse(inputSource);
return true;
} catch (ParserConfigurationException | SAXException | IOException e) {
return false;
}
}
}
}