Search code examples
javaandroidjsonrestletvarnish

Error by using restlet behind varnish (Reverse proxy)


I am using restlet server has a RESTful provider for my mobile application. Is there anything to do with varnish to make restlet working ?

Indeed, without using varnish and making my app contacting directly my rest server everything works fine.

Here the exception returned by restlet server :

May 14, 2012 7:48:10 PM org.restlet.resource.UniformResource doCatch
WARNING: Exception or error caught in resource
java.lang.NullPointerException
        at fr.evoxmusic.ebackupserver.rest.IsGoogleRegistered.isGoogleRegistered(IsGoogleRegistered.java:30)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.restlet.resource.ServerResource.doHandle(ServerResource.java:449)
        at org.restlet.resource.ServerResource.post(ServerResource.java:1114)
        at org.restlet.resource.ServerResource.doHandle(ServerResource.java:533)
        at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:590)
        at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:302)
        at org.restlet.resource.ServerResource.handle(ServerResource.java:849)

Note : I am using Reslet client. Here my code to contact the server.

public class RestletClient extends ClientResource {
    private static final String TAG = "RestletClient";
    private static final String UserAgent = "eBackupSMS";

    public static JSONObject SendHttpPost(String URL, JSONObject entity) {
        JSONObject result = new JSONObject();
        JsonRepresentation jr = new JsonRepresentation(entity);

        Client client = new Client(new Context(), Protocol.HTTP);
        client.getContext().getParameters().add("useForwardedForHeader", "true");
        ClientResource cr = new ClientResource(URL);
        cr.setNext(client);

        List<Preference<Encoding>> preflist = cr.getClientInfo().getAcceptedEncodings();
        preflist.add(new Preference<Encoding>(Encoding.GZIP));
        preflist.add(new Preference<Encoding>(Encoding.DEFLATE));

        cr.getClientInfo().setAgent(UserAgent);

        // Others params.
        cr.setRetryDelay(10000);
        cr.setRetryAttempts(3);
        cr.setRetryOnError(true);

        try {

            cr.post(jr);
            jr = new JsonRepresentation(cr.getResponseEntity());
            cr.release();

            result = jr.getJsonObject();
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }

        return result;
    }

}

Here the isGoogleRegistered resource where the error happens.

public class IsGoogleRegistered extends ServerResource {

    /**
     * Method used to verify if the token provided by the client is valid for
     * the google server.
     * 
     * @param entity as json representation
     * @return a jsonobject containing the response
     * @throws Exception
     * 
     * @example : {"google":{"registrationid":"client token id"}}
     */
    @Post("json")
    public JsonRepresentation isGoogleRegistered(JsonRepresentation entity) throws Exception {
        JSONObject wrapper = new JSONObject();
        JSONObject content = new JSONObject();

        wrapper = entity.getJsonObject().getJSONObject("google");

        try {
            if (C2DMMessage.isGoogleRegistered(JBackupServer.c2dmToken, wrapper.getString("registrationid")))
                content.put("isgoogleregistered", true);
            else
                content.put("isgoogleregistered", false);
        } catch (IOException e) {
            content.put("error", "connection to google servers not available");
        }

        wrapper = new JSONObject();
        wrapper.put("result", content);

        JsonRepresentation jr = new JsonRepresentation(wrapper);
        return jr;
    }

}

line 30 :

wrapper = entity.getJsonObject().getJSONObject("google");

Thanks.


Solution

  • By using socat I saw that varnish seems not correctly forward chunked http request to the backend..