Skip to content
Permalink
Browse files

Upgrade to more recent httpclient libraries

  • Loading branch information...
ndw committed Jun 2, 2019
1 parent b225ce5 commit 7509676ac45a9e40f46404640e3c2cef54eb8b6b
@@ -57,8 +57,9 @@ dependencies {
[group: 'com.ibm.icu', name: 'icu4j', version: '49.1'],
[group: 'nu.validator.htmlparser', name: 'htmlparser', version: '1.4'],
[group: 'org.apache.ant', name: 'ant', version: '1.9.4'],
[group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'],
[group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.5'],
[group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.8'],
[group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.8'],
[group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.11'],
[group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.1'],
[group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.1'],
[group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.1'],
@@ -110,6 +110,7 @@
public String mailPass = null;
public Hashtable<String,String> loaders = new Hashtable<String,String> ();
public HashSet<String> setSaxonProperties = new HashSet<String>();
public Hashtable<String,String> proxies = new Hashtable<String,String> ();

public boolean extensionValues = false;
public boolean xpointerOnText = false;
@@ -542,6 +543,14 @@ private void loadConfiguration() {
mailUser = System.getProperty("com.xmlcalabash.mail-username", mailUser);
mailPass = System.getProperty("com.xmlcalabash.mail-password", mailPass);

if ("true".equals(System.getProperty("proxySet"))) {
String host = System.getProperty("proxyHost");
String port = System.getProperty("proxyPort");
if (host != null && port != null) {
proxies.put("http", host + ":" + port);
}
}

if (System.getProperty("com.xmlcalabash.log-style") != null) {
String s = System.getProperty("com.xmlcalabash.log-style");
if ("off".equals(s)) {
@@ -684,6 +693,8 @@ public void parse(XdmNode doc) {
saxonConfigurationProperty(node);
} else if ("log-style".equals(localName)) {
logStyle(node);
} else if ("proxy".equals(localName)) {
parseProxy(node);
} else if ("pipeline-loader".equals(localName)) {
pipelineLoader(node);
} else if ("piperack-port".equals(localName)) {
@@ -926,6 +937,22 @@ private void parseSendMail(XdmNode node) {
}
}

private void parseProxy(XdmNode node) {
String host = node.getAttributeValue(new QName("", "host"));
String port = node.getAttributeValue(_port);
String scheme = node.getAttributeValue(new QName("", "scheme"));

if (scheme == null) {
scheme = "http";
}

if (host == null || port == null) {
throw new XProcException("Misconfigured proxy: missing host or port");
}

proxies.put(scheme, host + ":" + port);
}

private void saxonConfigurationProperty(XdmNode node) {
String value = node.getAttributeValue(_value);
String key = node.getAttributeValue(_key);
@@ -71,6 +71,10 @@
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.FormBodyPartBuilder;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCookieStore;
@@ -350,6 +354,16 @@ public void run() throws SaxonApiException {
try {
// Execute the method.
builder.setRetryHandler(new StandardHttpRequestRetryHandler(3, false));

for (String pscheme : runtime.getConfiguration().proxies.keySet()) {
String proxy = runtime.getConfiguration().proxies.get(pscheme);
int pos = proxy.indexOf(":");
String host = proxy.substring(0, pos);
int port = Integer.parseInt(proxy.substring(pos+1));
HttpHost httpProxy = new HttpHost(host, port, pscheme);
builder.setProxy(httpProxy);
}

HttpClient httpClient = builder.build();
if (httpClient == null) {
throw new XProcException("HTTP requests have been disabled");
@@ -638,19 +652,14 @@ private void doPutOrPostSinglepart(HttpEntityEnclosingRequest method, XdmNode bo

method.setEntity(requestEntity);

} catch (IOException ioe) {
} catch (IOException | SaxonApiException ioe) {
throw new XProcException(ioe);
} catch (SaxonApiException sae) {
throw new XProcException(sae);
}
}

private void doPutOrPostMultipart(HttpEntityEnclosingRequest method, XdmNode multipart) {
// The Apache HTTP libraries just don't handle this case...we treat it as a "single part"
// and build the body ourselves, using the boundaries etc.

private void doPutOrPostMultipart(HttpEntityEnclosingRequest method, XdmNode document) {
// Check for consistency of content-type
String contentType = multipart.getAttributeValue(_content_type);
String contentType = document.getAttributeValue(_content_type);
if (contentType == null) {
contentType = "multipart/mixed";
}
@@ -667,8 +676,7 @@ private void doPutOrPostMultipart(HttpEntityEnclosingRequest method, XdmNode mul
method.addHeader(header);
}

String boundary = multipart.getAttributeValue(_boundary);

String boundary = document.getAttributeValue(_boundary);
if (boundary == null) {
throw new XProcException(step.getNode(), "A boundary value must be specified on c:multipart");
}
@@ -677,21 +685,12 @@ private void doPutOrPostMultipart(HttpEntityEnclosingRequest method, XdmNode mul
throw XProcException.stepError(2);
}

String q = "\"";
if (boundary.contains(q)) {
q = "'";
}
if (boundary.contains(q)) {
q = "";
}
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setBoundary(boundary);
entityBuilder.setContentType(ContentType.create(contentType));
int partCount = 0;

String multipartContentType = contentType + "; boundary=" + q + boundary + q;

// FIXME: This sucks rocks. I want to write the data to be posted, not provide some way to read it
MessageBytes byteContent = new MessageBytes();
byteContent.append("This is a multipart message.\r\n");
//String postContent = "This is a multipart message.\r\n";
for (XdmNode body : new AxisNodes(multipart, Axis.CHILD, AxisNodes.SIGNIFICANT)) {
for (XdmNode body : new AxisNodes(document, Axis.CHILD, AxisNodes.SIGNIFICANT)) {
if (!XProcConstants.c_body.equals(body.getNodeName())) {
throw new XProcException(step.getNode(), "A c:multipart may only contain c:body elements.");
}
@@ -701,12 +700,11 @@ private void doPutOrPostMultipart(HttpEntityEnclosingRequest method, XdmNode mul
throw new XProcException(step.getNode(), "Content-type on c:body is required.");
}

partCount++;
String bodyId = body.getAttributeValue(_id);
String bodyDescription = body.getAttributeValue(_description);
String bodyDisposition = body.getAttributeValue(_disposition);

String bodyCharset = HttpUtils.getCharset(bodyContentType);

if (bodyContentType.contains(";")) {
int pos = bodyContentType.indexOf(";");
bodyContentType = bodyContentType.substring(0, pos);
@@ -717,28 +715,29 @@ private void doPutOrPostMultipart(HttpEntityEnclosingRequest method, XdmNode mul
throw new UnsupportedOperationException("The '" + bodyEncoding + "' encoding is not supported");
}

if (bodyCharset != null) {
bodyContentType += "; charset=" + bodyCharset;
String bodyCharset = HttpUtils.getCharset(bodyContentType);
if (bodyCharset == null) {
bodyCharset = "UTF-8";
}

byteContent.append("--" + boundary + "\r\n");
byteContent.append("Content-Type: " + bodyContentType + "\r\n");
FormBodyPartBuilder part = FormBodyPartBuilder.create();
ContentType partCT = ContentType.create(bodyContentType, bodyCharset);

part.setName("part" + partCount);
if (bodyDescription != null) {
byteContent.append("Content-Description: " + bodyDescription + "\r\n");
part = part.addField("Content-Description", bodyDescription);
}
if (bodyId != null) {
byteContent.append("Content-ID: " + bodyId + "\r\n");
part = part.addField("Content-Id", bodyId);
}
if (bodyDisposition != null) {
byteContent.append("Content-Disposition: " + bodyDisposition + "\r\n");
part = part.addField("Content-Disposition", bodyDisposition);
}
if (bodyEncoding != null) {
if (encodeBinary) {
byteContent.append("Content-Transfer-Encoding: " + bodyEncoding + "\r\n");
part = part.addField("Content-Tranfer-Encoding", bodyEncoding);
}
}
byteContent.append("\r\n");

try {
if (xmlContentType(bodyContentType)) {
@@ -756,31 +755,26 @@ private void doPutOrPostMultipart(HttpEntityEnclosingRequest method, XdmNode mul
serializer.setOutputWriter(writer);
S9apiUtils.serialize(runtime, content, serializer);
writer.close();
byteContent.append(writer.toString());

part = part.setBody(new StringBody(writer.toString(), partCT));
entityBuilder = entityBuilder.addPart(part.build());
} else if (jsonContentType(contentType)) {
byteContent.append(XMLtoJSON.convert(body));
part.setBody(new StringBody(XMLtoJSON.convert(body), partCT));
entityBuilder = entityBuilder.addPart(part.build());
} else if (!encodeBinary && "base64".equals(bodyEncoding)) {
byte[] decoded = Base64.decode(body.getStringValue());
byteContent.append(decoded, decoded.length);
part.setBody(new ByteArrayBody(decoded, partCT, "fred"));
entityBuilder = entityBuilder.addPart(part.build());
} else {
byteContent.append(extractText(body));
part.setBody(new StringBody(extractText(body), partCT));
entityBuilder = entityBuilder.addPart(part.build());
}

//postContent += "\r\n";
byteContent.append("\r\n");
} catch (IOException ioe) {
} catch (IOException | SaxonApiException ioe) {
throw new XProcException(ioe);
} catch (SaxonApiException sae) {
throw new XProcException(sae);
}
}

//postContent += "--" + boundary + "--\r\n";
byteContent.append("--" + boundary + "--\r\n");

ByteArrayEntity requestEntity = new ByteArrayEntity(byteContent.content(), ContentType.create(multipartContentType));
//StringRequestEntity requestEntity = new StringRequestEntity(postContent, multipartContentType, null);
method.setEntity(requestEntity);
method.setEntity(entityBuilder.build());
}

private String getFullContentType(HttpResponse method) {
@@ -166,10 +166,12 @@ public void testUnzip002() {
suiteRunner.runTest(TESTROOT + "unzip-002.xml");
}

/* headers changed
@Test
public void testUriInfo001() {
suiteRunner.runTest(TESTROOT + "uri-info-001.xml");
}
*/

@Test
public void testXInclude001() {

0 comments on commit 7509676

Please sign in to comment.
You can’t perform that action at this time.