restlet / restlet-framework-java

The first REST API framework for Java
https://restlet.talend.com
Other
654 stars 284 forks source link

POST request from Google Pub/Sub has no content - GAE edition #1234

Open jamespercy opened 8 years ago

jamespercy commented 8 years ago

I'm trying to set up a POST endpoint to receive messages from Google Pub/Sub using Restlet GAE edition. For some reason the request is received but the content is always missing. As a test I set up a standard servlet to handle the request and the content was delivered correctly so I'm 99% sure the issue is with Restlet.

here is how I set up my service:

public class WebAppEventApi extends Application {

@Override
public Restlet createInboundRoot() {
    Context context = getContext();
    Router router = new Router(context);
    router.setFinderClass(ModuleInjectResourceFinder.class);

    router.attach("/signup-engagement-handler", SignupEngagementHandler.class);
    router.attach("/welcome-notification-handler", WelcomeNotificationHandler.class);
    router.attach("/low-toner-notification-handler", LowTonerNotificationHandler.class);

    return router;
}
}

and here's an example of one of the resources

public class WelcomeNotificationHandler extends ServerResource {

    private static final Logger LOG = LoggerFactory.getLogger(WelcomeNotificationHandler.class);

    @Post("json")
    public void handleWelcomeNotification(PubSubDelivery delivery) {
        try {
            LOG.info("RECEIVED EVENT: handling welcome {}", new String(delivery.getMessage().decodeData(), "UTF-8"));
        } catch (Exception e) {
            LOG.error("error handling delivery ", e);
        }
        setStatus(Status.SUCCESS_NO_CONTENT);
    }
}

Here is the class I'm mapping to:

public class PubSubDelivery {
private PubsubMessage message;
private String subscription;

public PubsubMessage getMessage() {
    return message;
}

public void setMessage(PubsubMessage message) {
    this.message = message;
}

public String getSubscription() {
    return subscription;
}

public void setSubscription(String subscription) {
    this.subscription = subscription;
}
}

I've tried various other representations with no success, so I'm sure it isn't a mapping issue.

I'm using the GAE edition of restlet 2.3.7 with the gson extension

thboileau commented 8 years ago

Hello, I'm really sorry for the delay... I see no issue with the code you provided. This kind of issue remains me another one: as GAE does not support request with chunked encoding, the client has to set the proper content-length header. In this case, the ClientResource class provides the "setEntityBuffering" method. Did you try to update the method as follow:

public void handleWelcomeNotification(Representation representation) {
LOG.info("representation.getText());
}

Another case is where the input entity has already been read, for example by a log, or Servlet filter. As the input stream can only be read once, this may be the cause of your issue.