Junior
What types of proxy servers do you know?
sobes.tech AI
Answer from AI
The following types of proxy servers are known:
- Forward Proxy: Located between clients and the internet. Intercepts requests from clients and forwards them to the internet on their behalf. Used for access control, caching, traffic filtering, and ensuring client anonymity.
- Reverse Proxy: Located in front of web servers. Intercepts requests from the internet to web servers and forwards them to the appropriate server. Used for load balancing, SSL encryption, caching, data compression, and web server protection.
- Transparent Proxy: Operates in such a way that clients are unaware of its existence. Often used by providers for content filtering or caching. Traffic is redirected to the proxy at the router level.
- Anonymous Proxy: Hides the client's IP address and identifies itself as a proxy. The level of anonymity can vary.
- Distorting Proxy: Identifies itself as a proxy but reports an incorrect (distorted) IP address of the client.
- Elite Proxy: Fully hides the fact that the request passes through a proxy. The destination server only sees the proxy server's IP address.
In the context of Java, proxy concepts are often used to implement certain design patterns or to interact with external systems:
- Virtual Proxy: Delays the creation of a costly object until it is actually needed.
- Protection Proxy: Controls access to the original object.
- Remote Proxy: Represents an object located in another address space.
A proxy in Java can be implemented using:
java.lang.reflect.Proxyfor dynamically creating proxies for interfaces.- Libraries such as CGLIB for class proxying.
// Example of using java.lang.reflect.Proxy
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Subject {
void request();
}
class RealSubject implements Subject {
@Override
public void request() {
System.out.println("RealSubject: Handling request.");
}
}
class SubjectInvocationHandler implements InvocationHandler {
private final Subject realSubject;
public SubjectInvocationHandler(Subject realSubject) {
this.realSubject = realSubject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("Proxy: Before calling request.");
Object result = method.invoke(realSubject, args); // Call method on real object
System.out.println("Proxy: After calling request.");
return result;
}
}
// Usage:
// Subject realSubject = new RealSubject();
// Subject proxySubject = (Subject) Proxy.newProxyInstance(
// Subject.class.getClassLoader(),
// new Class<?>[]{Subject.class},
// new SubjectInvocationHandler(realSubject));
// proxySubject.request();