Skip to content

Commit 7f87a0b

Browse files
committed
GH-3212: Move FileSender to Fuseki test area
1 parent 5207b8e commit 7f87a0b

File tree

5 files changed

+170
-5
lines changed

5 files changed

+170
-5
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@
3434
import org.apache.jena.riot.web.HttpNames;
3535

3636
/**
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.
37+
* @deprecated To be removed - do not use.
4038
*/
39+
@Deprecated(forRemoval = true)
4140
public class FileSender {
4241

4342
static record Entry(String fileName, String content, String contentType) {}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.jena.fuseki.main;
20+
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;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import java.util.UUID;
31+
32+
import org.apache.jena.http.HttpLib;
33+
import org.apache.jena.riot.WebContent;
34+
import org.apache.jena.riot.web.HttpNames;
35+
36+
/**
37+
* Simple multipart form data HTTP PUT/POST sender, primarily for.
38+
* This class does not stream the content.
39+
*/
40+
public class FileSender {
41+
// This class is in main/src to enable sharing without needing to depend on a test artifact.
42+
43+
private static record Entry(String fileName, String content, String contentType) {}
44+
private List<Entry> items = new ArrayList<>();
45+
private final String url;
46+
47+
public FileSender(String url ) { this.url = url; }
48+
49+
public void add(String filename, String content, String type) {
50+
Entry e = new Entry(filename, content, type);
51+
items.add(e);
52+
}
53+
54+
/** Return response code */
55+
public int send(String method) {
56+
String WNL = "\r\n"; // Web newline
57+
String boundary = UUID.randomUUID().toString();
58+
59+
// This is for testing so build a body.
60+
StringBuilder strBuidler = new StringBuilder();
61+
for ( Entry e : items ) {
62+
strBuidler.append("--" + boundary+WNL);
63+
strBuidler.append("Content-Disposition: form-data; name=\"FILE\"; filename=\""+e.fileName+"\""+WNL);
64+
strBuidler.append("Content-Type: "+e.contentType+";charset=UTF-8"+WNL);
65+
strBuidler.append(WNL);
66+
strBuidler.append(e.content);
67+
strBuidler.append(WNL);
68+
}
69+
strBuidler.append("--" + boundary + "--"+WNL);
70+
71+
URI uri = HttpLib.toRequestURI(url);
72+
String body = strBuidler.toString();
73+
String ctHeaderValue = WebContent.contentTypeMultipartFormData+"; boundary="+boundary;
74+
75+
HttpRequest request = HttpRequest
76+
.newBuilder(uri)
77+
.setHeader(HttpNames.hContentType, ctHeaderValue)
78+
.method(method, BodyPublishers.ofString(body))
79+
.build();
80+
HttpResponse<InputStream> response = HttpLib.executeJDK(HttpClient.newHttpClient(), request, BodyHandlers.ofInputStream());
81+
HttpLib.handleResponseNoBody(response);
82+
return response.statusCode();
83+
}
84+
}

jena-fuseki2/jena-fuseki-main/src/test/java/org/apache/jena/fuseki/main/TestFileUpload.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.apache.jena.sparql.exec.http.DSP;
3434
import org.apache.jena.sparql.exec.http.GSP;
3535
import org.apache.jena.sparql.graph.GraphFactory;
36-
import org.apache.jena.web.FileSender;
3736
import org.apache.jena.web.HttpSC;
3837
import org.junit.jupiter.api.Test;
3938

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.jena.fuseki;
20+
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;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
import java.util.UUID;
31+
32+
import org.apache.jena.http.HttpLib;
33+
import org.apache.jena.riot.WebContent;
34+
import org.apache.jena.riot.web.HttpNames;
35+
36+
/**
37+
* Simple multipart form data HTTP PUT/POST sender, primarily for.
38+
* This class does not stream the content.
39+
*/
40+
public class FileSender {
41+
// This class is in main/src to enable sharing without needing to depend on a test artifact.
42+
43+
private static record Entry(String fileName, String content, String contentType) {}
44+
private List<Entry> items = new ArrayList<>();
45+
private final String url;
46+
47+
public FileSender(String url ) { this.url = url; }
48+
49+
public void add(String filename, String content, String type) {
50+
Entry e = new Entry(filename, content, type);
51+
items.add(e);
52+
}
53+
54+
/** Return response code */
55+
public int send(String method) {
56+
String WNL = "\r\n"; // Web newline
57+
String boundary = UUID.randomUUID().toString();
58+
59+
// This is for testing so build a body.
60+
StringBuilder strBuidler = new StringBuilder();
61+
for ( Entry e : items ) {
62+
strBuidler.append("--" + boundary+WNL);
63+
strBuidler.append("Content-Disposition: form-data; name=\"FILE\"; filename=\""+e.fileName+"\""+WNL);
64+
strBuidler.append("Content-Type: "+e.contentType+";charset=UTF-8"+WNL);
65+
strBuidler.append(WNL);
66+
strBuidler.append(e.content);
67+
strBuidler.append(WNL);
68+
}
69+
strBuidler.append("--" + boundary + "--"+WNL);
70+
71+
URI uri = HttpLib.toRequestURI(url);
72+
String body = strBuidler.toString();
73+
String ctHeaderValue = WebContent.contentTypeMultipartFormData+"; boundary="+boundary;
74+
75+
HttpRequest request = HttpRequest
76+
.newBuilder(uri)
77+
.setHeader(HttpNames.hContentType, ctHeaderValue)
78+
.method(method, BodyPublishers.ofString(body))
79+
.build();
80+
HttpResponse<InputStream> response = HttpLib.executeJDK(HttpClient.newHttpClient(), request, BodyHandlers.ofInputStream());
81+
HttpLib.handleResponseNoBody(response);
82+
return response.statusCode();
83+
}
84+
}

jena-fuseki2/jena-fuseki-webapp/src/test/java/org/apache/jena/fuseki/TestWebappFileUpload.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.apache.jena.sparql.exec.http.DSP;
3434
import org.apache.jena.sparql.exec.http.GSP;
3535
import org.apache.jena.sparql.graph.GraphFactory;
36-
import org.apache.jena.web.FileSender;
3736
import org.junit.Test;
3837

3938
/**

0 commit comments

Comments
 (0)