BAEL-7444 create custom url connection (#15717)

This commit is contained in:
Wynn Teo 2024-02-05 07:21:26 +08:00 committed by GitHub
parent 6e3cb9b030
commit 08d29f73b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,55 @@
package com.baeldung.customurlconnection;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
public class CustomURLConnection extends URLConnection {
private final String simulatedData = "This is the simulated data from the resource.";
private URL url;
private boolean connected = false;
private String headerValue = "SimulatedHeaderValue";
protected CustomURLConnection(URL url) {
super(url);
this.url = url;
}
@Override
public void connect() throws IOException {
connected = true;
System.out.println("Connection established to: " + url);
}
@Override
public InputStream getInputStream() throws IOException {
if (!connected) {
connect();
}
return new ByteArrayInputStream(simulatedData.getBytes());
}
@Override
public OutputStream getOutputStream() throws IOException {
ByteArrayOutputStream simulatedOutput = new ByteArrayOutputStream();
return simulatedOutput;
}
@Override
public int getContentLength() {
return simulatedData.length();
}
@Override
public String getHeaderField(String name) {
if ("SimulatedHeader".equalsIgnoreCase(name)) { // Example header name
return headerValue;
} else {
return null; // Header not found
}
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.customurlconnection;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
public class CustomURLStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL u) throws IOException {
// Create and return an instance of CustomURLConnection
return new CustomURLConnection(u);
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.customurlconnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
public class CustomURLStreamHandlerFactory implements URLStreamHandlerFactory {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
// Check if the requested protocol is our custom protocol
if ("myprotocol".equals(protocol)) {
return new CustomURLStreamHandler();
}
return null; // Let the default handler deal with other protocols
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.customurlconnection;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
URL.setURLStreamHandlerFactory(new CustomURLStreamHandlerFactory());
try {
URL url = new URL("myprotocol://example.com/resource");
CustomURLConnection customConnection = (CustomURLConnection) url.openConnection();
customConnection.connect();
InputStream inputStream = customConnection.getInputStream();
String content = new Scanner(inputStream).useDelimiter("\\A").next();
System.out.println(content);
int contentLength = customConnection.getContentLength();
System.out.println("Content Length: " + contentLength);
String headerValue = customConnection.getHeaderField("SimulatedHeader");
System.out.println("Header Value: " + headerValue);
} catch (IOException e) {
e.printStackTrace();
}
}
}