|
| 1 | +# Office Internet Firewall (Proxy Design Pattern) |
| 2 | + |
| 3 | +A Spring Boot application that demonstrates the **Proxy Design Pattern**. This project simulates a corporate internet firewall where a "Proxy" stands between the employee and the real internet to filter out banned websites (like Facebook or TikTok) during work hours. |
| 4 | + |
| 5 | +## 📖 Project Overview |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +**The Problem:** |
| 10 | +Imagine you have a class `RealInternet` that connects to any website. |
| 11 | +* If you give this class directly to employees, they can surf social media all day. |
| 12 | +* Modifying the complex `RealInternet` class to add filtering logic violates the **Single Responsibility Principle** (it should only know how to connect, not how to police users). |
| 13 | + |
| 14 | +**The Solution:** |
| 15 | +The **Proxy Pattern** provides a surrogate or placeholder for another object to control access to it. |
| 16 | +* **The Interface:** Both the Real object and the Proxy implement `Internet`. |
| 17 | +* **The Proxy:** Intercepts the request. If the site is allowed, it passes the request to the `RealInternet`. If banned, it blocks it. |
| 18 | + |
| 19 | +## 🛠️ Tech Stack |
| 20 | + |
| 21 | +* **Language:** Java 17+ |
| 22 | +* **Framework:** Spring Boot 3.x |
| 23 | +* **Build Tool:** Maven |
| 24 | +* **Concepts:** Structural Design Patterns, Access Control, Delegation |
| 25 | + |
| 26 | +## 📂 Project Structure |
| 27 | + |
| 28 | +```text |
| 29 | +src/main/java/com/gautam/proxy/ |
| 30 | +│ |
| 31 | +├── service/ |
| 32 | +│ ├── Internet.java # <--- The Common Interface |
| 33 | +│ ├── RealInternet.java # <--- The Actual Service (Heavy lifter) |
| 34 | +│ └── ProxyInternet.java # <--- The Gatekeeper (Security logic) |
| 35 | +│ |
| 36 | +├── controller/ |
| 37 | +│ └── InternetController.java |
| 38 | +│ |
| 39 | +└── ProxyApplication.java |
| 40 | +``` |
| 41 | +## 🚀 How to Run |
| 42 | + |
| 43 | +Prerequisites |
| 44 | +* Java Development Kit (JDK) 17 or higher |
| 45 | +* Maven |
| 46 | + |
| 47 | +Steps |
| 48 | +1. Clone the repository |
| 49 | + ```bash |
| 50 | + git clone https://github.com/Astrogeek77/Spring_boot_projects.git |
| 51 | + cd Spring_boot_projects |
| 52 | + ``` |
| 53 | +2. Build the project |
| 54 | + ```bash |
| 55 | + mvn clean install |
| 56 | + ``` |
| 57 | +3. Run the application |
| 58 | + ```bash |
| 59 | + mvn spring-boot: run |
| 60 | + ``` |
| 61 | +4. **Test the Endpoint** Open your browser or Postman and go to: |
| 62 | +``` http://localhost:8080/api/proxy/browse?site=google.com ``` |
| 63 | + |
| 64 | +## 📡 API Reference |
| 65 | + |
| 66 | +### Browse Internet |
| 67 | +Simulates a user trying to access a website. The Proxy intercepts this request to check the URL against a banned list before deciding whether to complete the connection. |
| 68 | + |
| 69 | +* **URL:** `/api/proxy/browse` |
| 70 | +* **Method:** `GET` |
| 71 | +* **Query Param:** `site` (e.g., `google.com`, `facebook.com`) |
| 72 | + |
| 73 | +#### Scenario 1: Allowed Site |
| 74 | +If the site is safe (e.g., `google.com`), the Proxy delegates the work to the Real Internet service. |
| 75 | + |
| 76 | +* **Response:** |
| 77 | + ```json |
| 78 | + ["SUCCESS: Connected to google.com"] |
| 79 | + ``` |
| 80 | +* **Console Output:** |
| 81 | + ```text |
| 82 | + Connecting to google.com |
| 83 | + ``` |
| 84 | + |
| 85 | +#### Scenario 2: Banned Site |
| 86 | +If the site is blacklisted (e.g., `facebook.com`), the Proxy blocks the request immediately. The Real Internet service is **never** called. |
| 87 | + |
| 88 | +* **Response:** |
| 89 | + ```json |
| 90 | + ["BLOCKED: Access Denied: This site is blocked by corporate firewall."] |
| 91 | + ``` |
| 92 | +* **Console Output:** |
| 93 | + ```text |
| 94 | + Access Denied to facebook.com |
| 95 | + ``` |
| 96 | + |
| 97 | +## 🧠 Proxy Pattern Implementation Details |
| 98 | + |
| 99 | +1. **The Common Interface (`Internet.java`)**: |
| 100 | + This is crucial. By implementing the same interface, the Proxy looks exactly like the Real object to the client code. The client doesn't know (or care) that it is talking to a proxy instead of the real service. |
| 101 | + ```java |
| 102 | + public interface Internet { |
| 103 | + void connectTo(String host) throws Exception; |
| 104 | + } |
| 105 | + ``` |
| 106 | + |
| 107 | +2. **The Protection Proxy (`ProxyInternet.java`)**: |
| 108 | + The proxy adds a layer of security logic *before* delegating to the real object. It acts as a gatekeeper. |
| 109 | + ```java |
| 110 | + @Override |
| 111 | + public void connectTo(String host) throws Exception { |
| 112 | + // 1. Security Check |
| 113 | + if (bannedSites.contains(host)) { |
| 114 | + throw new Exception("Access Denied"); |
| 115 | + } |
| 116 | + |
| 117 | + // 2. Delegation to Real Object (only if check passes) |
| 118 | + realInternet.connectTo(host); |
| 119 | + } |
| 120 | + ``` |
| 121 | + |
| 122 | +3. **Separation of Concerns**: |
| 123 | + The `RealInternet` class stays clean and focused purely on networking logic. |
| 124 | + It doesn't need to know about corporate policies, banned lists, or user permissions. |
| 125 | + All that "administrative" logic is isolated inside the `ProxyInternet`. |
| 126 | + |
| 127 | +--- |
| 128 | +Created for learning the Proxy Design Pattern in Java Spring Boot. |
0 commit comments