Skip to content

Commit

Permalink
server: set content-type when sending HTTP notifications
Browse files Browse the repository at this point in the history
This fixes #249.

Signed-off-by: Pierre-Alexandre Meyer <pierre@mouraf.org>
  • Loading branch information
pierre committed Mar 18, 2015
1 parent 862ca2f commit 242875e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Expand Up @@ -43,12 +43,18 @@
import com.ning.http.client.Response; import com.ning.http.client.Response;


import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.eventbus.Subscribe; import com.google.common.eventbus.Subscribe;


public class PushNotificationListener { public class PushNotificationListener {


private static final Logger log = LoggerFactory.getLogger(PushNotificationListener.class); private static final Logger log = LoggerFactory.getLogger(PushNotificationListener.class);


@VisibleForTesting
public static final String HTTP_HEADER_CONTENT_TYPE = "Content-Type";
@VisibleForTesting
public static final String CONTENT_TYPE_JSON = "application/json; charset=UTF-8";

private static final int TIMEOUT_NOTIFICATION = 15; // 15 seconds private static final int TIMEOUT_NOTIFICATION = 15; // 15 seconds


private final TenantUserApi tenantApi; private final TenantUserApi tenantApi;
Expand Down Expand Up @@ -88,6 +94,7 @@ private void dispatchCallback(final UUID tenantId, final ExtBusEvent event, fina
private boolean doPost(final UUID tenantId, final String url, final String body, final int timeoutSec) { private boolean doPost(final UUID tenantId, final String url, final String body, final int timeoutSec) {
final BoundRequestBuilder builder = httpClient.preparePost(url); final BoundRequestBuilder builder = httpClient.preparePost(url);
builder.setBody(body == null ? "{}" : body); builder.setBody(body == null ? "{}" : body);
builder.addHeader(HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON);


final Response response; final Response response;
try { try {
Expand Down
@@ -1,7 +1,7 @@
/* /*
* Copyright 2010-2013 Ning, Inc. * Copyright 2010-2013 Ning, Inc.
* Copyright 2014 Groupon, Inc * Copyright 2014-2015 Groupon, Inc
* Copyright 2014 The Billing Project, LLC * Copyright 2014-2015 The Billing Project, LLC
* *
* The Billing Project licenses this file to you under the Apache License, version 2.0 * The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the * (the "License"); you may not use this file except in compliance with the
Expand Down Expand Up @@ -33,6 +33,8 @@
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.killbill.billing.client.model.TenantKey; import org.killbill.billing.client.model.TenantKey;
import org.killbill.billing.jaxrs.json.NotificationJson; import org.killbill.billing.jaxrs.json.NotificationJson;
import org.killbill.billing.server.notifications.PushNotificationListener;
import org.killbill.billing.tenant.api.TenantKV;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.testng.Assert; import org.testng.Assert;
Expand All @@ -48,7 +50,7 @@ public class TestPushNotification extends TestJaxrsBase {
private CallbackServer callbackServer; private CallbackServer callbackServer;


private static final int SERVER_PORT = 8087; private static final int SERVER_PORT = 8087;
private static final String CALLBACK_ENDPPOINT = "/callmeback"; private static final String CALLBACK_ENDPOINT = "/callmeback";


private volatile boolean callbackCompleted; private volatile boolean callbackCompleted;
private volatile boolean callbackCompletedWithError; private volatile boolean callbackCompletedWithError;
Expand All @@ -57,7 +59,7 @@ public class TestPushNotification extends TestJaxrsBase {
@BeforeMethod(groups = "slow") @BeforeMethod(groups = "slow")
public void beforeMethod() throws Exception { public void beforeMethod() throws Exception {
super.beforeMethod(); super.beforeMethod();
callbackServer = new CallbackServer(this, SERVER_PORT, CALLBACK_ENDPPOINT); callbackServer = new CallbackServer(this, SERVER_PORT, CALLBACK_ENDPOINT);
callbackCompleted = false; callbackCompleted = false;
callbackCompletedWithError = false; callbackCompletedWithError = false;
callbackServer.startServer(); callbackServer.startServer();
Expand Down Expand Up @@ -92,9 +94,9 @@ public void retrieveAccountWithAsserts(final String accountId) {
@Test(groups = "slow") @Test(groups = "slow")
public void testPushNotification() throws Exception { public void testPushNotification() throws Exception {
// Register tenant for callback // Register tenant for callback
final String callback = "http://127.0.0.1:" + SERVER_PORT + CALLBACK_ENDPPOINT; final String callback = "http://127.0.0.1:" + SERVER_PORT + CALLBACK_ENDPOINT;
final TenantKey result0 = killBillClient.registerCallbackNotificationForTenant(callback, createdBy, reason, comment); final TenantKey result0 = killBillClient.registerCallbackNotificationForTenant(callback, createdBy, reason, comment);
Assert.assertEquals(result0.getKey(), org.killbill.billing.tenant.api.TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString()); Assert.assertEquals(result0.getKey(), TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString());
Assert.assertEquals(result0.getValues().size(), 1); Assert.assertEquals(result0.getValues().size(), 1);
Assert.assertEquals(result0.getValues().get(0), callback); Assert.assertEquals(result0.getValues().get(0), callback);


Expand All @@ -111,13 +113,13 @@ public void testPushNotification() throws Exception {
} }


final TenantKey result = killBillClient.getCallbackNotificationForTenant(); final TenantKey result = killBillClient.getCallbackNotificationForTenant();
Assert.assertEquals(result.getKey(), org.killbill.billing.tenant.api.TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString()); Assert.assertEquals(result.getKey(), TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString());
Assert.assertEquals(result.getValues().size(), 1); Assert.assertEquals(result.getValues().size(), 1);
Assert.assertEquals(result.getValues().get(0), callback); Assert.assertEquals(result.getValues().get(0), callback);


killBillClient.unregisterCallbackNotificationForTenant(createdBy, reason, comment); killBillClient.unregisterCallbackNotificationForTenant(createdBy, reason, comment);
final TenantKey result2 = killBillClient.getCallbackNotificationForTenant(); final TenantKey result2 = killBillClient.getCallbackNotificationForTenant();
Assert.assertEquals(result2.getKey(), org.killbill.billing.tenant.api.TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString()); Assert.assertEquals(result2.getKey(), TenantKV.TenantKey.PUSH_NOTIFICATION_CB.toString());
Assert.assertEquals(result2.getValues().size(), 0); Assert.assertEquals(result2.getValues().size(), 0);
} }


Expand Down Expand Up @@ -176,8 +178,6 @@ protected void doPost(final HttpServletRequest request, final HttpServletRespons
final int current = receivedCalls.incrementAndGet(); final int current = receivedCalls.incrementAndGet();


final String body = CharStreams.toString(new InputStreamReader(request.getInputStream(), "UTF-8")); final String body = CharStreams.toString(new InputStreamReader(request.getInputStream(), "UTF-8"));

response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK); response.setStatus(HttpServletResponse.SC_OK);


log.info("Got body {}", body); log.info("Got body {}", body);
Expand All @@ -191,6 +191,8 @@ protected void doPost(final HttpServletRequest request, final HttpServletRespons
Assert.assertEquals(notification.getObjectId(), notification.getAccountId()); Assert.assertEquals(notification.getObjectId(), notification.getAccountId());


test.retrieveAccountWithAsserts(notification.getObjectId()); test.retrieveAccountWithAsserts(notification.getObjectId());

Assert.assertEquals(request.getHeader(PushNotificationListener.HTTP_HEADER_CONTENT_TYPE), PushNotificationListener.CONTENT_TYPE_JSON);
} catch (final AssertionError e) { } catch (final AssertionError e) {
withError = true; withError = true;
} }
Expand Down

1 comment on commit 242875e

@sbrossie
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.