Skip to content

Update FileSender #3213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
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
85 changes: 43 additions & 42 deletions jena-arq/src/main/java/org/apache/jena/web/FileSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,68 @@

package org.apache.jena.web;

import java.io.IOException;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.jena.atlas.io.IO;
import org.apache.jena.atlas.web.HttpException;
import org.apache.jena.http.HttpLib;
import org.apache.jena.riot.WebContent;
import org.apache.jena.riot.web.HttpNames;

/** Multipart HTTP PUT/POST. */
/**
* @deprecated To be removed - do not use.
*/
@Deprecated(forRemoval = true)
public class FileSender {

class Entry {
String fileName;
String content;
String contentType;
}
static record Entry(String fileName, String content, String contentType) {}

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

private String url;
private final String url;

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

public void add(String filename, String content, String type) {
Entry e = new Entry();
e.fileName = filename;
e.content = content;
e.contentType = type;
Entry e = new Entry(filename, content, type);
items.add(e);
}

/** Return response code */
public int send(String method) {
try {
String WNL = "\r\n"; // Web newline
String boundary = UUID.randomUUID().toString();
String WNL = "\r\n"; // Web newline
String boundary = UUID.randomUUID().toString();

// This is for testing so build a body.
StringBuilder strBuidler = new StringBuilder();
for ( Entry e : items ) {
strBuidler.append("--" + boundary+WNL);
strBuidler.append("Content-Disposition: form-data; name=\"FILE\"; filename=\""+e.fileName+"\""+WNL);
strBuidler.append("Content-Type: "+e.contentType+";charset=UTF-8"+WNL);
strBuidler.append(WNL);
strBuidler.append(e.content);
strBuidler.append(WNL);
}
strBuidler.append("--" + boundary + "--"+WNL);

HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
try ( PrintStream ps = new PrintStream(connection.getOutputStream()); ) {
for ( Entry e : items ) {
ps.print("--" + boundary+WNL);
ps.print("Content-Disposition: form-data; name=\"FILE\"; filename=\""+e.fileName+"\""+WNL);
ps.print("Content-Type: "+e.contentType+";charset=UTF-8"+WNL);
ps.print(WNL);
ps.print(e.content);
ps.print(WNL);
}
ps.print("--" + boundary + "--"+WNL);
}
connection.connect();
int responseCode = connection.getResponseCode();
if ( responseCode >= 300 )
throw new HttpException(responseCode);
return responseCode;
} catch (IOException ex) { IO.exception(ex); return -1;}
URI uri = HttpLib.toRequestURI(url);
String body = strBuidler.toString();
String ctHeaderValue = WebContent.contentTypeMultipartFormData+"; boundary="+boundary;

HttpRequest request = HttpRequest
.newBuilder(uri)
.setHeader(HttpNames.hContentType, ctHeaderValue)
.method(method, BodyPublishers.ofString(body))
.build();
HttpResponse<InputStream> response = HttpLib.executeJDK(HttpClient.newHttpClient(), request, BodyHandlers.ofInputStream());
HttpLib.handleResponseNoBody(response);
return response.statusCode();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jena.fuseki.main;

import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.jena.http.HttpLib;
import org.apache.jena.riot.WebContent;
import org.apache.jena.riot.web.HttpNames;

/**
* Simple multipart form data HTTP PUT/POST sender, primarily for.
* This class does not stream the content.
*/
public class FileSender {
// This class is in main/src to enable sharing without needing to depend on a test artifact.

private static record Entry(String fileName, String content, String contentType) {}
private List<Entry> items = new ArrayList<>();
private final String url;

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

public void add(String filename, String content, String type) {
Entry e = new Entry(filename, content, type);
items.add(e);
}

/** Return response code */
public int send(String method) {
String WNL = "\r\n"; // Web newline
String boundary = UUID.randomUUID().toString();

// This is for testing so build a body.
StringBuilder strBuidler = new StringBuilder();
for ( Entry e : items ) {
strBuidler.append("--" + boundary+WNL);
strBuidler.append("Content-Disposition: form-data; name=\"FILE\"; filename=\""+e.fileName+"\""+WNL);
strBuidler.append("Content-Type: "+e.contentType+";charset=UTF-8"+WNL);
strBuidler.append(WNL);
strBuidler.append(e.content);
strBuidler.append(WNL);
}
strBuidler.append("--" + boundary + "--"+WNL);

URI uri = HttpLib.toRequestURI(url);
String body = strBuidler.toString();
String ctHeaderValue = WebContent.contentTypeMultipartFormData+"; boundary="+boundary;

HttpRequest request = HttpRequest
.newBuilder(uri)
.setHeader(HttpNames.hContentType, ctHeaderValue)
.method(method, BodyPublishers.ofString(body))
.build();
HttpResponse<InputStream> response = HttpLib.executeJDK(HttpClient.newHttpClient(), request, BodyHandlers.ofInputStream());
HttpLib.handleResponseNoBody(response);
return response.statusCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.apache.jena.sparql.exec.http.DSP;
import org.apache.jena.sparql.exec.http.GSP;
import org.apache.jena.sparql.graph.GraphFactory;
import org.apache.jena.web.FileSender;
import org.apache.jena.web.HttpSC;
import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.jena.fuseki;

import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.jena.http.HttpLib;
import org.apache.jena.riot.WebContent;
import org.apache.jena.riot.web.HttpNames;

/**
* Simple multipart form data HTTP PUT/POST sender, primarily for.
* This class does not stream the content.
*/
public class FileSender {
// This class is in main/src to enable sharing without needing to depend on a test artifact.

private static record Entry(String fileName, String content, String contentType) {}
private List<Entry> items = new ArrayList<>();
private final String url;

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

public void add(String filename, String content, String type) {
Entry e = new Entry(filename, content, type);
items.add(e);
}

/** Return response code */
public int send(String method) {
String WNL = "\r\n"; // Web newline
String boundary = UUID.randomUUID().toString();

// This is for testing so build a body.
StringBuilder strBuidler = new StringBuilder();
for ( Entry e : items ) {
strBuidler.append("--" + boundary+WNL);
strBuidler.append("Content-Disposition: form-data; name=\"FILE\"; filename=\""+e.fileName+"\""+WNL);
strBuidler.append("Content-Type: "+e.contentType+";charset=UTF-8"+WNL);
strBuidler.append(WNL);
strBuidler.append(e.content);
strBuidler.append(WNL);
}
strBuidler.append("--" + boundary + "--"+WNL);

URI uri = HttpLib.toRequestURI(url);
String body = strBuidler.toString();
String ctHeaderValue = WebContent.contentTypeMultipartFormData+"; boundary="+boundary;

HttpRequest request = HttpRequest
.newBuilder(uri)
.setHeader(HttpNames.hContentType, ctHeaderValue)
.method(method, BodyPublishers.ofString(body))
.build();
HttpResponse<InputStream> response = HttpLib.executeJDK(HttpClient.newHttpClient(), request, BodyHandlers.ofInputStream());
HttpLib.handleResponseNoBody(response);
return response.statusCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.apache.jena.sparql.exec.http.DSP;
import org.apache.jena.sparql.exec.http.GSP;
import org.apache.jena.sparql.graph.GraphFactory;
import org.apache.jena.web.FileSender;
import org.junit.Test;

/**
Expand Down
Loading