Skip to content

Instantly share code, notes, and snippets.

@secondsun
Created November 1, 2012 20:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save secondsun/a5c6e16e4b8c54fbd042 to your computer and use it in GitHub Desktop.
Save secondsun/a5c6e16e4b8c54fbd042 to your computer and use it in GitHub Desktop.
Authentication Use Demo
public class AuthDemo {
private Pipeline pipeline;
private AuthenticationModule authModule;
public AuthDemo() {
// Set up Pipeline
pipeline = new Pipeline(Constants.ROOT_URL);
Authenticator auth = new DefaultAuthenticator();
try {
authModule = auth.auth(AuthType.REST, new URL(Constants.ROOT_URL))
.add("login");
} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
pipeline.add("tasks", Task.class).setAuthenticationModule(authModule);
pipeline.add("tags", Tag.class).setAuthenticationModule(authModule);
pipeline.add("projects", Project.class).setAuthenticationModule(authModule);
}
public Pipeline getPipeline() {
return pipeline;
}
public AuthenticationModule getAuthenticationModule() {
return authModule;
}
public void login(String username, String password, Callback<HeaderAndBody> callback) {
authModule.login(username, password, callback);
}
public void logout(Callback<Void> callback) {
authModule.logout(callback);
}
public void enroll(String firstName, String lastName, String emailAddress,
String username, String password, String role,
Callback<HeaderAndBody> callback) {
HashMap<String, String> userData = new HashMap<String, String>();
userData.put("firstname", firstName);
userData.put("lastname", lastName);
userData.put("email", emailAddress);
userData.put("username", username);
userData.put("password", password);
userData.put("role", role);
authModule.enroll(userData, callback);
}
}
public class AuthMain {
public static void main(String args) {
final AuthDemo demo = new AuthDemo();
demo.login("john","123", new Callback<HeaderAndBody> {
@Override
public void onSuccess(HeaderAndBody data) {
downloadData(demo);
}
@Override
public void onFailure(Exception e) {
try {
if (e instanceof HttpException) {
HttpException httpException = (HttpException) e;
switch (httpException.getStatusCode()) {
case 401:
System.out.println("Login failed");
break;
default:
throw new RuntimeException(new String(((HttpException) e)
.getData(), "UTF-8"), e);
}
} else {
throw new RuntimeException(e);
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public static void downloadData(AuthDemo demo){
Pipe pipe = authDemo.getPipeline().get("tasks");
pipe.read( new Callback<List<Task>>() {
@Override
public void onSuccess(List<Task> data) {
for (Task task : data) {
System.out.println(task);
}
demo.logout();
}
@Override
public void onFailure(Exception e) {
System.out.println("Error refreshing tasks: " + e.getMessage());
demo.logout();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment