Created
July 2, 2012 23:49
-
-
Save tbml/3036465 to your computer and use it in GitHub Desktop.
Qi4j forum sample rest client test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ForumClient | |
{ | |
private ContextResourceClient crc; | |
private final Module module; | |
private String username = "rickard"; | |
private String password = "secret"; | |
public ForumClient() throws AssemblyException | |
{ | |
Energy4Java is = new Energy4Java( ); | |
Application app = is.newApplication( new ApplicationAssembler() | |
{ | |
@Override | |
public ApplicationAssembly assemble(ApplicationAssemblyFactory applicationFactory) throws AssemblyException | |
{ | |
ApplicationAssembly assembly = applicationFactory.newApplicationAssembly(); | |
ForumClient.this.assemble(assembly); | |
return assembly; | |
} | |
}); | |
Client client = new Client( Protocol.HTTP ); | |
module = app.findModule("Client", "REST"); | |
ContextResourceClientFactory contextResourceClientFactory = module.newObject(ContextResourceClientFactory.class, client); | |
contextResourceClientFactory.setAcceptedMediaTypes( MediaType.APPLICATION_JSON ); | |
contextResourceClientFactory.setErrorHandler( new ErrorHandler().onError( ErrorHandler.AUTHENTICATION_REQUIRED, new ResponseHandler() | |
{ | |
boolean tried = false; | |
@Override | |
public HandlerCommand handleResponse( Response response, ContextResourceClient client ) | |
{ | |
if (tried) | |
throw new ResourceException( response.getStatus() ); | |
tried = true; | |
client.getContextResourceClientFactory().getInfo().setUser( new User(username, password) ); | |
System.out.println("authenticating as: " + username); | |
// Try again | |
return refresh(); | |
} | |
} ).onError( ErrorHandler.RECOVERABLE_ERROR, new ResponseHandler() | |
{ | |
@Override | |
public HandlerCommand handleResponse( Response response, ContextResourceClient client ) | |
{ | |
// Try to restart | |
return refresh(); | |
} | |
} ) ); | |
Reference ref = new Reference( "http://localhost:8888/" ); | |
crc = contextResourceClientFactory.newClient( ref ); | |
} | |
private void assemble(ApplicationAssembly assembly) throws AssemblyException | |
{ | |
LayerAssembly client = assembly.layer("Client"); | |
ModuleAssembly module = client.module("REST"); | |
// General setup of client | |
new ClientAssembler().assemble( module ); | |
new ValueAssembler().assemble( module ); | |
// Forum | |
module.values( Registration.class); | |
} | |
List<Link> rootResources() { | |
final List<Link> resources = new ArrayList<Link>(); | |
crc.onResource( new ResultHandler<Resource>() | |
{ | |
@Override | |
public HandlerCommand handleResult(Resource result, ContextResourceClient client) | |
{ | |
resources.addAll(result.resources().get()); | |
return null; | |
} | |
}); | |
crc.start(); | |
return resources; | |
} | |
void signupNewUser() throws Exception | |
{ | |
crc.newClient( "signup/").onResource(new ResultHandler<Resource>() | |
{ | |
@Override | |
public HandlerCommand handleResult(Resource result, ContextResourceClient client) | |
{ | |
ValueBuilder<Registration> vb = module.newValueBuilder(Registration.class); | |
vb.prototype().name().set(username); | |
vb.prototype().realName().set(" "); | |
vb.prototype().email().set("nomail@nomail.com"); | |
vb.prototype().password().set(password); | |
return command("signup", vb.newInstance()).onSuccess(new ResultHandler<Object>() | |
{ | |
@Override | |
public HandlerCommand handleResult(Object result, ContextResourceClient client) | |
{ | |
System.out.println( "signup: " + username ); | |
return null; | |
} | |
}); | |
} | |
}).start(); | |
} | |
void adminListUsers() throws Exception | |
{ | |
crc.newClient("administration/users/").onResource( new ResultHandler<Resource>() | |
{ | |
@Override | |
public HandlerCommand handleResult(Resource result, ContextResourceClient client) | |
{ | |
return query("index"); | |
} | |
}).onQuery("index", new ResultHandler<Object>() | |
{ | |
@Override | |
public HandlerCommand handleResult(Object result, ContextResourceClient client) | |
{ | |
return null; | |
} | |
}) | |
.start(); | |
} | |
public static void main( String[] args ) | |
throws Exception | |
{ | |
ForumClient client = new ForumClient(); | |
// root resource links | |
List<Link> links = client.rootResources(); | |
for (Link link : links) { | |
System.out.println("root resource: " + link.rel()); | |
} | |
// register new user | |
client.signupNewUser(); | |
// TODO: failure, UsersResource.index() handler missing ( Query type) | |
client.adminListUsers(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment