Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.ozone.test.InputSubstream;

Expand Down Expand Up @@ -94,4 +100,32 @@ public static String extractUploadId(String xml) {
}
return null;
}

/**
* Open an HttpURLConnection with the given parameters.
*
* @param url The URL to connect to.
* @param httpMethod The HTTP method to use (e.g., "GET", "PUT", "POST", etc.).
* @param headers A map of request headers to set. Can be null.
* @param body The request body as a byte array. Can be null.
* @return An open HttpURLConnection.
* @throws IOException If an I/O error occurs.
*/
public static HttpURLConnection openHttpURLConnection(URL url, String httpMethod, Map<String, List<String>> headers,
byte[] body) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(httpMethod);
if (headers != null) {
headers.forEach((key, values) -> values.forEach(value -> connection.addRequestProperty(key, value)));
}

if (body != null) {
connection.setDoOutput(true);
try (OutputStream os = connection.getOutputStream()) {
IOUtils.write(body, os);
os.flush();
}
}
return connection;
}
}
Loading