Guest User

Untitled

a guest
Dec 11th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class ProxyInternet implements Internet {
  5.  
  6. private List<String> restrictedUrls;
  7. private ActualInternet actualInternet;
  8.  
  9. public ProxyInternet() {
  10. createRestrictedUrlList();
  11. }
  12.  
  13. private void createRestrictedUrlList() {
  14. restrictedUrls = new ArrayList<>();
  15.  
  16. restrictedUrls.add("www.facebook.com");
  17. restrictedUrls.add("www.twitter.com");
  18. restrictedUrls.add("www.youtube.com");
  19. }
  20.  
  21. @Override
  22. public void connectToUrl(String url) throws Exception {
  23.  
  24. //Example of Protection Proxy
  25. //The proxy object makes some condition checks to act as a firewall
  26. if (restrictedUrls.contains(url)) {
  27.  
  28. throw new Exception("Access Denied.\n" + url + " is a restricted url");
  29.  
  30. } else {
  31.  
  32. //Example of Virtual Proxy
  33. //The proxy object offloads multiple heavy object creations to a single time
  34. //Instantiates the real object when client requests for it the first time
  35. if (actualInternet == null) {
  36. actualInternet = new ActualInternet();
  37. }
  38.  
  39. actualInternet.connectToUrl(url);
  40. }
  41. }
  42. }
Add Comment
Please, Sign In to add comment