private void addCredentialsIfAny(ClientResource clientResource){ if (!StringUtils.isAnyBlank(login, password)) clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, login, password); } }
public class ActivitiRestClient { private static String REST_URI = "http://localhost:8080/activiti-rest/service"; private static Logger logger = Logger.getLogger(ActivitiRestClient.class); private static ClientResource getClientResource(String uri) { ClientResource clientResource = new ClientResource(uri); clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "kermit", "kermit"); return clientResource; } .... }
ClientResource resource = new ClientResource(resouceURL); String token = "myToken"; ChallengeResponse cr = new ChallengeResponse( ChallengeScheme.HTTP_OAUTH_BEARER); cr.setRawValue(token); resource.setChallengeResponse(cr); (...)
/** * Sets the authentication response sent by a client to an origin server * given a scheme, identifier and secret. * * @param scheme * The challenge scheme. * @param identifier * The user identifier, such as a login name or an access key. * @param secret * The user secret, such as a password or a secret key. */ public void setChallengeResponse(ChallengeScheme scheme, final String identifier, String secret) { setChallengeResponse(new ChallengeResponse(scheme, identifier, secret)); }
protected ClientResource createClient(final String path) { String assembledPath = assembleEndpoint(path); log.debug("Endpoint URL: " + assembledPath); final ClientResource service = new ClientResource(new Context(getClass().getName()), assembledPath); service.setNext(client); ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; if (context.getEmailAddress() != null) { ChallengeResponse authentication = new ChallengeResponse(scheme, context.getEmailAddress(), context.getCredentials()); service.setChallengeResponse(authentication); } else { service.setChallengeResponse(null); } return service; }
public static ClientResource createAuthenticatedClientResource(String url, String userName, String password) { ClientResource cr = new ClientResource(url); cr.setNext(client); cr.accept(MediaType.APPLICATION_JSON); if (!StringUtils.isNullOrEmpty(userName) && !StringUtils.isNullOrEmpty(password)) { cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, userName, password); } return cr; }
public static ClientResource createAuthenticatedClientResource(String url, String userName, String password) { ClientResource cr = new ClientResource(url); cr.setNext(client); cr.accept(MediaType.APPLICATION_JSON); if (!StringUtils.isNullOrEmpty(userName) && !StringUtils.isNullOrEmpty(password)) { cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, userName, password); } return cr; }
public static ClientResource createAuthenticatedClientResource(String url, String userName, String password) { ClientResource cr = new ClientResource(url); cr.setNext(client); cr.accept(MediaType.APPLICATION_JSON); if (!StringUtils.isNullOrEmpty(userName) && !StringUtils.isNullOrEmpty(password)) { cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, userName, password); } return cr; }
public static ClientResource createAuthenticatedClientResource(String url, String userName, String password) { ClientResource cr = new ClientResource(url); cr.setNext(client); cr.accept(MediaType.APPLICATION_JSON); if (!StringUtils.isNullOrEmpty(userName) && !StringUtils.isNullOrEmpty(password)) { cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, userName, password); } return cr; }
public static ClientResource createAuthenticatedClientResource(String url, String userName, String password) { ClientResource cr = new ClientResource(url); cr.setNext(client); cr.accept(MediaType.APPLICATION_JSON); if (!StringUtils.isNullOrEmpty(userName) && !StringUtils.isNullOrEmpty(password)) { cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC, userName, password); } return cr; }
public static void main(String[] args) { // Prepare the request ClientResource resource = new ClientResource("http://localhost:8182/"); // Add the client authentication to the call ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; ChallengeResponse authentication = new ChallengeResponse(scheme, "scott", "tiger"); resource.setChallengeResponse(authentication); try { // Send the HTTP GET request resource.get(); // Output the response entity on the JVM console resource.getResponseEntity().write(System.out); } catch (Exception e) { if (Status.CLIENT_ERROR_UNAUTHORIZED.equals(resource.getStatus())) { // Unauthorized access System.out .println("Access unauthorized by the server, check your credentials"); } else { // Unexpected status System.out.println("An unexpected status was returned: " + resource.getStatus()); } } } }
public static void main(String[] args) throws Exception { // Prepare the request ClientResource resource = new ClientResource("http://localhost:8111/"); // Add the client authentication to the call ChallengeScheme scheme = ChallengeScheme.HTTP_BASIC; ChallengeResponse authentication = new ChallengeResponse(scheme, "scott", "tiger"); resource.setChallengeResponse(authentication); // Send the HTTP GET request resource.get(); if (resource.getStatus().isSuccess()) { // Output the response entity on the JVM console resource.getResponseEntity().write(System.out); } else if (resource.getStatus() .equals(Status.CLIENT_ERROR_UNAUTHORIZED)) { // Unauthorized access System.out .println("Access authorized by the server, check your credentials"); } else { // Unexpected status System.out.println("An unexpected status was returned: " + resource.getStatus()); } }
@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); } }
public static void main(String[] args) throws ResourceException, IOException { // Prepare the request ClientResource cr = new ClientResource("http://localhost:8182/"); ChallengeRequest c1 = null; // first try: unauthenticated request try { cr.get(); } catch (ResourceException re) { if (Status.CLIENT_ERROR_UNAUTHORIZED.equals(cr.getStatus())) { c1 = getDigestChallengeRequest(cr); } } // second try: authenticated request if (c1 != null) { ChallengeResponse c2 = new ChallengeResponse(c1, cr.getResponse(), "scott", "tiger".toCharArray()); cr.setChallengeResponse(c2); cr.get().write(System.out); } }
clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, restletCloudConfig.getAgentLogin(), restletCloudConfig.getAgentPassword());
clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC, restletCloudConfig.getAgentLogin(), restletCloudConfig.getAgentPassword());
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(); }