Skip to content

Commit 5207b8e

Browse files
committed
GH-3212: Rewrite FileSender for java.net.http networking
1 parent ae8c14a commit 5207b8e

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

jena-arq/src/main/java/org/apache/jena/web/FileSender.java

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,67 +18,69 @@
1818

1919
package org.apache.jena.web;
2020

21-
import java.io.IOException;
22-
import java.io.PrintStream;
23-
import java.net.HttpURLConnection;
24-
import java.net.URL;
21+
import java.io.InputStream;
22+
import java.net.URI;
23+
import java.net.http.HttpClient;
24+
import java.net.http.HttpRequest;
25+
import java.net.http.HttpRequest.BodyPublishers;
26+
import java.net.http.HttpResponse;
27+
import java.net.http.HttpResponse.BodyHandlers;
2528
import java.util.ArrayList;
2629
import java.util.List;
2730
import java.util.UUID;
2831

29-
import org.apache.jena.atlas.io.IO;
30-
import org.apache.jena.atlas.web.HttpException;
32+
import org.apache.jena.http.HttpLib;
33+
import org.apache.jena.riot.WebContent;
34+
import org.apache.jena.riot.web.HttpNames;
3135

32-
/** Multipart HTTP PUT/POST. */
36+
/**
37+
* Simple Multipart HTTP PUT/POST sender for testing.
38+
* THis class does not stream the content.
39+
* It is in main/src to enable sharing without needing to depend on a test artifact.
40+
*/
3341
public class FileSender {
3442

35-
class Entry {
36-
String fileName;
37-
String content;
38-
String contentType;
39-
}
43+
static record Entry(String fileName, String content, String contentType) {}
4044

4145
private List<Entry> items = new ArrayList<>();
4246

43-
private String url;
47+
private final String url;
4448

4549
public FileSender(String url ) { this.url = url; }
4650

4751
public void add(String filename, String content, String type) {
48-
Entry e = new Entry();
49-
e.fileName = filename;
50-
e.content = content;
51-
e.contentType = type;
52+
Entry e = new Entry(filename, content, type);
5253
items.add(e);
5354
}
5455

5556
/** Return response code */
5657
public int send(String method) {
57-
try {
58-
String WNL = "\r\n"; // Web newline
59-
String boundary = UUID.randomUUID().toString();
58+
String WNL = "\r\n"; // Web newline
59+
String boundary = UUID.randomUUID().toString();
60+
61+
// This is for testing so build a body.
62+
StringBuilder strBuidler = new StringBuilder();
63+
for ( Entry e : items ) {
64+
strBuidler.append("--" + boundary+WNL);
65+
strBuidler.append("Content-Disposition: form-data; name=\"FILE\"; filename=\""+e.fileName+"\""+WNL);
66+
strBuidler.append("Content-Type: "+e.contentType+";charset=UTF-8"+WNL);
67+
strBuidler.append(WNL);
68+
strBuidler.append(e.content);
69+
strBuidler.append(WNL);
70+
}
71+
strBuidler.append("--" + boundary + "--"+WNL);
6072

61-
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
62-
connection.setRequestMethod(method);
63-
connection.setDoOutput(true);
64-
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
65-
try ( PrintStream ps = new PrintStream(connection.getOutputStream()); ) {
66-
for ( Entry e : items ) {
67-
ps.print("--" + boundary+WNL);
68-
ps.print("Content-Disposition: form-data; name=\"FILE\"; filename=\""+e.fileName+"\""+WNL);
69-
ps.print("Content-Type: "+e.contentType+";charset=UTF-8"+WNL);
70-
ps.print(WNL);
71-
ps.print(e.content);
72-
ps.print(WNL);
73-
}
74-
ps.print("--" + boundary + "--"+WNL);
75-
}
76-
connection.connect();
77-
int responseCode = connection.getResponseCode();
78-
if ( responseCode >= 300 )
79-
throw new HttpException(responseCode);
80-
return responseCode;
81-
} catch (IOException ex) { IO.exception(ex); return -1;}
73+
URI uri = HttpLib.toRequestURI(url);
74+
String body = strBuidler.toString();
75+
String ctHeaderValue = WebContent.contentTypeMultipartFormData+"; boundary="+boundary;
76+
77+
HttpRequest request = HttpRequest
78+
.newBuilder(uri)
79+
.setHeader(HttpNames.hContentType, ctHeaderValue)
80+
.method(method, BodyPublishers.ofString(body))
81+
.build();
82+
HttpResponse<InputStream> response = HttpLib.executeJDK(HttpClient.newHttpClient(), request, BodyHandlers.ofInputStream());
83+
HttpLib.handleResponseNoBody(response);
84+
return response.statusCode();
8285
}
8386
}
84-

0 commit comments

Comments
 (0)