I am using Restlet 2.1.2 and am having an unusual problem of my request Representation text being null. I have looked at the forums and read that the entity is transient and will be destroyed if not cached correctly but while I have a guard providing security, it doesn't touch the entity as I know of. More interestingly, for one route /authenticate, which merely checks if the user can be authenticated, can get the entity text, a JSON object. However when the code for /account/create is run, both post requests using @Post("json:json"), I can not get the JSON data sent. Below I've added my code and the sample curl statements I'm issuing. for the results see the AuthenticationResource and Account Resource classes.
public class Executable{
public static void main(String[] args) throws Exception{
Component com = new Component();
com.getServers().add( Protocol.HTTP, 8182 );
com.getDefaultHost().attach( "/myapi", new RoutingApp() );
com.start();
}
}
public class RoutingApp extends Application {
public RoutingApp() {
super();
}
@Override
public synchronized Restlet createInboundRoot() {
Context context = getContext();
Router router = new Router(context);
router.attach( "/account/create", AccountResource.class );
router.attach( "/authenticate", AuthenticationResource.class );
ChallengeAuthenticator authenticator = new ChallengeAuthenticator(
context, ChallengeScheme.HTTP_BASIC, "myapi" );
MySecretVerifier verifier = new MySecretVerifier();
MyEnroler enroler = new MyEnroler();
authenticator.setVerifier( verifier );
authenticator.setEnroler( enroler );
MyAuthorizer authorizer = new MyAuthorizer();
authorizer.setNext( router );
authenticator.setNext( authorizer );
return authenticator;
}
}
public class MySecretVerifier extends SecretVerifier {
@Override
public int verify( String identifier, char[] secret ) {
[... Get User by identifier ...]
if( [... Check User Secret ...] ) {
Request request = Request.getCurrent();
request.getAttributes().put( "currentUser", user );
return SecretVerifier.RESULT_VALID;
} else {
return SecretVerifier.RESULT_INVALID;
}
}
}
public class MyEnroler implements Enroler {
@Override
public void enrole(ClientInfo ci) {
Request request = Request.getCurrent();
User user = (User) request.getAttributes().get( "currentUser" );
if( null != user ) {
[... Get User Roles ...]
for( [... Each Role ...] ) {
Role role = newRole( r.name, "" );
ci.getRoles().add( role );
}
}
}
}
public class MyAuthorizer extends Authorizer {
@Override
protected boolean authorize( Request request, Response response ) {
ClientInfo info = request.getClientInfo();
Method method = request.getMethod();
User user = (User) request.getAttributes().get( "currentUser" );
String uri = request.getResourceRef().getRemainingPart( true, false );
List<Role> userRoles = info.getRoles();
boolean authorized = false;
if( [... Check if User is Authorized ...] ) {
request.getAttributes().putAll( [... add some properties from a HashMap ...] );
authorized = false;
}
return authorized;
}
}
public class AuthenticationResource extends ServerResource {
@Post("json:json")
@Override
public String performPost(Representation entity) {
Request request = Request.getCurrent();
request.getEntityAsText(); //returns { "id" : "larry", "secret" : "test" }
request.getEntity().getText(); //returns null
entity.toString(); //returns [application/json]
entity.getText(); //returns null
}
}
public class AccountResource extends ServerResource {
@Post("json:json")
@Override
public String performPost( Representation entity ) {
Request request = Request.getCurrent();
request.getEntityAsText(); //returns null
request.getEntity().getText(); //returns null
entity.toString(); //returns [application/json]
entity.getText(); //returns null
}
}
My CURL statements are as follows:
curl -i --user larry:test -H "Content-Type: application/json" -X POST -d '{ "id" : "larry", "secret" : "test" }' http://localhost:8182/myapi/account/create
curl -i --user larry:test -H "Content-Type: application/json" -X POST -d '{ "id" : "larry", "secret" : "test" }' http://localhost:8182/myapi/authenticate