Advertisement
Guest User

Restlet proxy example

a guest
Apr 16th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. import java.io.IOException;
  2.  
  3. import org.restlet.Application;
  4. import org.restlet.Client;
  5. import org.restlet.Component;
  6. import org.restlet.Restlet;
  7. import org.restlet.data.MediaType;
  8. import org.restlet.data.Parameter;
  9. import org.restlet.data.Protocol;
  10. import org.restlet.representation.Representation;
  11. import org.restlet.representation.StringRepresentation;
  12. import org.restlet.resource.ClientResource;
  13. import org.restlet.resource.ResourceException;
  14. import org.restlet.resource.ServerResource;
  15. import org.restlet.routing.Router;
  16. import org.restlet.util.Series;
  17.  
  18. public class ProxyTest extends Application
  19. {
  20.   public static class ConnectorTest extends ServerResource
  21.   {
  22.     @Override
  23.     protected Representation get() throws ResourceException {
  24.       ClientResource client = new ClientResource("https://www.google.com/");
  25.       Representation result = client.get();
  26.      
  27.       try {
  28.         System.out.println(client.getStatus());
  29.         System.out.println(result.getText().length());
  30.       }
  31.       catch (IOException e) {
  32.         e.printStackTrace();
  33.       }
  34.       finally {
  35.         client.release();
  36.       }
  37.      
  38.       return new StringRepresentation("Done.", MediaType.TEXT_PLAIN);
  39.     }
  40.   }
  41.  
  42.   @Override
  43.   public Restlet createInboundRoot() {
  44.     Router router = new Router(getContext());
  45.     router.attach("/connect", ConnectorTest.class);
  46.     return router;
  47.   }
  48.  
  49.   private static void attachClient(Component c, Protocol protocol) {
  50.     Client client = c.getClients().add(protocol);
  51.     Series<Parameter> parameters = client.getContext().getParameters();
  52.     parameters.add("proxyHost", "192.168.100.210");
  53.     parameters.add("proxyPort", "8888");
  54.   }
  55.  
  56.   public static void main(String[] args) throws Exception {
  57.     ProxyTest proxyTest = new ProxyTest();
  58.     Component c = new Component();
  59.    
  60.     c.getServers().add(Protocol.HTTP, 8080);
  61.    
  62.     attachClient(c, Protocol.HTTP);
  63.     attachClient(c, Protocol.HTTPS);
  64.     c.getDefaultHost().attachDefault(proxyTest);
  65.    
  66.     c.start();
  67.   }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement