Search code examples
javaapirestpostrestlet

Restlet: Retrieving http body in verifier


When I want retrieve the http body of a post in my Request Verifier it kinda resets my entity and I get a nullpointer exception when I want to get the http body in my resource class.

Verifier:

JsonRepresentation jsonrep;
        try {
            Representation entity = request.getEntity();
            jsonrep = new JsonRepresentation(entity);
            //bug: entity resets when getJsonObject is being called.
            JSONObject jsonobj = jsonrep.getJsonObject();
            if(companyId != jsonobj.getInt("id_companies")){
                return Verifier.RESULT_INVALID;
            }
...

AppResource:

@Post
public Representation addApp(Representation rep) throws Exception{
//rep is null
    JsonRepresentation jsonrep = new JsonRepresentation(rep);

When I dont't call:

                JSONObject jsonobj = jsonrep.getJsonObject();

it just works fine.

Is anybody facing the same issue or got a solution for it?

Thanks in advance!


Solution

  • In fact, by default, the representation is an InputRepresentation that doesn't store the representation content.

    In your case, the simplest way is to wrap the representation into a StringRepresentation within your verifier:

    Representation entity = request.getEntity();
    StringRepresentation sEntity = new StringRepresentation(entity);
    request.setEntity(sEntity);
    
    JsonRepresentation jsonrep = new JsonRepresentation(sEntity);

    Then, the string representation will be automatically provided to your server resource method...

    Hope it helps you. Thierry