ClientResource cr = new ClientResource(PATH_TO_URL); AuthorizationResource proxy = cr.wrap(AuthorizationResource.class) proxy.login(auth);
ClientResource cr = new ClientResource("http://your-api.com/address/123"); AddressService service = cr.wrap(AddressService.class); // use the service transparently
/** * Creates a client resource that proxy calls to the given Java interface * into Restlet method calls. It basically creates a new instance of * {@link ClientResource} and invokes the {@link #wrap(Class)} method. * * @param <T> * @param context * The context. * @param reference * The target reference. * @param resourceInterface * The annotated resource interface class to proxy. * @return The proxy instance. */ public static <T> T create(Context context, Reference reference, Class<? extends T> resourceInterface) { ClientResource clientResource = new ClientResource(context, reference); return clientResource.wrap(resourceInterface); }
/** * Wraps the parent client resource to proxy calls to the given Java * interface into Restlet method calls. The parent resource is defined in * the sense of hierarchical URIs. If the resource URI is not hierarchical, * then an exception is thrown. * * @param <T> * @param resourceInterface * The annotated resource interface class to proxy. * @return The proxy instance. */ public <T> T getParent(Class<? extends T> resourceInterface) throws ResourceException { T result = null; ClientResource parentResource = getParent(); if (parentResource != null) { result = parentResource.wrap(resourceInterface); } return result; }
/** * Wraps the child client resource to proxy calls to the given Java * interface into Restlet method calls. The child resource is defined in the * sense of hierarchical URIs. If the resource URI is not hierarchical, then * an exception is thrown. * * @param <T> * @param relativeRef * The URI reference of the child resource relatively to the * current resource seen as the parent resource. * @param resourceInterface * The annotated resource interface class to proxy. * @return The proxy instance. */ public <T> T getChild(Reference relativeRef, Class<? extends T> resourceInterface) throws ResourceException { T result = null; ClientResource childResource = getChild(relativeRef); if (childResource != null) { result = childResource.wrap(resourceInterface); } return result; }
ClientResource cr = new ClientResource("http://10.0.2.2:8888/download/"); cr.setRequestEntityBuffering(true); DownloadResource downloadResource = cr.wrap(DownloadResourceProtocol.class); // Remote invocation - seamless: Representation representation = downloadResource.download(); // Using data: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); IOUtils.copy(representation.getStream(), byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); Log.i("Byte array: " + Arrays.toString(byteArray));
String url = "http://localhost:8182/test"; ClientResource cr = new ClientResource(url); MyResource resource = cr.wrap(MyResource.class); SomeInputBean input = new SomeInputBean(); SomeOutputBean output = resource.handleBean(input);
ClientResource cr = new ClientResource("http://.../getlistofobjects"); cr.accept(MediaType.APPLICATION_JSON); MyService myService = cr.wrap(MyService.class); List<MyObject> objs = myService.getObjs();
/** * Wraps the client resource to proxy calls to the given Java interface into * Restlet method calls. Use the {@link org.restlet.engine.Engine} * classloader in order to generate the proxy. * * @param <T> * @param resourceInterface * The annotated resource interface class to proxy. * @return The proxy instance. */ public <T> T wrap(Class<? extends T> resourceInterface) { return wrap(resourceInterface, org.restlet.engine.Engine.getInstance() .getClassLoader()); }
@Override public List<Car> list() { Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", System.getProperty("javax.net.ssl.trustStore")); ClientResource clientResource = new ClientResource("https://localhost:8043/api/cars/cars"); clientResource.setNext(client); ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER); challenge.setRawValue(Request.getCurrent().getAttributes().getOrDefault("token", "").toString()); clientResource.setChallengeResponse(challenge); CarServiceInterface carServiceInterface = clientResource.wrap(CarServiceInterface.class); Car[] allCars = carServiceInterface.getAllCars(); try { client.stop(); } catch (Exception e) { throw new RuntimeException(e); } return asList(allCars); } }
return clientResource.wrap(resourceClass, AgentUtils.class.getClassLoader());
return clientResource.wrap(resourceClass, AgentUtils.class.getClassLoader());
return clientResource.wrap(resourceClass, AgentUtils.class.getClassLoader());
return clientResource.wrap(resourceClass, AgentUtils.class.getClassLoader());
public static void main(String[] args) throws Exception { Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", "src/org/restlet/example/book/restlet/ch05/clientTrust.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); ClientResource clientResource = new ClientResource( "https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); MailResource mailClient = clientResource.wrap(MailResource.class); mailClient.store(mailClient.retrieve()); client.stop(); }
public static void main(String[] args) throws Exception { // Create and configure HTTPS client Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", "src/org/restlet/example/book/restlet/ch05/clientTrust.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); // Create and configure client resource ClientResource clientResource = new ClientResource( "https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); // Preemptively configure the authentication credentials ChallengeResponse authentication = new ChallengeResponse( ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd"); clientResource.setChallengeResponse(authentication); // Communicate with remote resource MailResource mailClient = clientResource.wrap(MailResource.class); mailClient.store(mailClient.retrieve()); // Store HTTPS client client.stop(); }
public static void main(String[] args) throws Exception { // Create and configure HTTPS client Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", "src/org/restlet/example/book/restlet/ch05/clientTrust.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); // Create and configure client resource ClientResource clientResource = new ClientResource( "https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); // Preemptively configure the authentication credentials ChallengeResponse authentication = new ChallengeResponse( ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd"); clientResource.setChallengeResponse(authentication); // Communicate with remote resource MailResource mailClient = clientResource.wrap(MailResource.class); mailClient.store(mailClient.retrieve()); // Store HTTPS client client.stop(); }
public static void main(String[] args) throws Exception { // Create and configure HTTPS client Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("truststorePath", "src/org/restlet/example/book/restlet/ch05/clientTrust.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); // Create and configure client resource ClientResource clientResource = new ClientResource( "https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); // Preemptively configure the authentication credentials ChallengeResponse authentication = new ChallengeResponse( ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd"); clientResource.setChallengeResponse(authentication); // Communicate with remote resource MailResource mailClient = clientResource.wrap(MailResource.class); mailClient.store(mailClient.retrieve()); // Store HTTPS client client.stop(); }
public static void main(String[] args) throws Exception { // Create and configure HTTPS client Client client = new Client(new Context(), Protocol.HTTPS); Series<Parameter> parameters = client.getContext().getParameters(); parameters.add("keystorePath", "src/org/restlet/example/book/restlet/ch05/clientKey.jks"); parameters.add("keystorePassword", "password"); parameters.add("keystoreType", "JKS"); parameters.add("keyPassword", "password"); parameters.add("truststorePath", "src/org/restlet/example/book/restlet/ch05/clientTrust.jks"); parameters.add("truststorePassword", "password"); parameters.add("truststoreType", "JKS"); // Create and configure client resource ClientResource clientResource = new ClientResource( "https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); // Preemptively configure the authentication credentials ChallengeResponse authentication = new ChallengeResponse( ChallengeScheme.HTTP_BASIC, "chunkylover53", "pwd"); clientResource.setChallengeResponse(authentication); // Communicate with remote resource MailResource mailClient = clientResource.wrap(MailResource.class); mailClient.store(mailClient.retrieve()); // Store HTTPS client client.stop(); }
"https://localhost:8183/accounts/chunkylover53/mails/123"); clientResource.setNext(client); MailResource mailClient = clientResource.wrap(MailResource.class);