diff --git a/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/CustomURLConnection.java b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/CustomURLConnection.java new file mode 100644 index 0000000000..231a3173be --- /dev/null +++ b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/CustomURLConnection.java @@ -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 + } + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/CustomURLStreamHandler.java b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/CustomURLStreamHandler.java new file mode 100644 index 0000000000..3749fd42d7 --- /dev/null +++ b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/CustomURLStreamHandler.java @@ -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); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/CustomURLStreamHandlerFactory.java b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/CustomURLStreamHandlerFactory.java new file mode 100644 index 0000000000..e9ef59451a --- /dev/null +++ b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/CustomURLStreamHandlerFactory.java @@ -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 + } +} diff --git a/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/Main.java b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/Main.java new file mode 100644 index 0000000000..52981fe160 --- /dev/null +++ b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/customurlconnection/Main.java @@ -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(); + } + } +}