|
18 | 18 |
|
19 | 19 | package org.apache.jena.web;
|
20 | 20 |
|
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; |
25 | 28 | import java.util.ArrayList;
|
26 | 29 | import java.util.List;
|
27 | 30 | import java.util.UUID;
|
28 | 31 |
|
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; |
31 | 35 |
|
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 | + */ |
33 | 41 | public class FileSender {
|
34 | 42 |
|
35 |
| - class Entry { |
36 |
| - String fileName; |
37 |
| - String content; |
38 |
| - String contentType; |
39 |
| - } |
| 43 | + static record Entry(String fileName, String content, String contentType) {} |
40 | 44 |
|
41 | 45 | private List<Entry> items = new ArrayList<>();
|
42 | 46 |
|
43 |
| - private String url; |
| 47 | + private final String url; |
44 | 48 |
|
45 | 49 | public FileSender(String url ) { this.url = url; }
|
46 | 50 |
|
47 | 51 | 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); |
52 | 53 | items.add(e);
|
53 | 54 | }
|
54 | 55 |
|
55 | 56 | /** Return response code */
|
56 | 57 | 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); |
60 | 72 |
|
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(); |
82 | 85 | }
|
83 | 86 | }
|
84 |
| - |
|
0 commit comments