Return proper http status code for unsupported media type

"Bad Request" (400) was returning instead of "Unsupported Media Type"
(415).

Change-Id: I7f660a0877bf4e6195a7b0b34f4acfd4bb4a9148
diff --git a/src/main/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServlet.java b/src/main/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServlet.java
index 2aece3d..eae220b 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServlet.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServlet.java
@@ -18,6 +18,7 @@
 import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
 import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
 import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
+import static javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE;
 
 import com.google.common.base.Supplier;
 import com.google.common.io.CharStreams;
@@ -62,6 +63,11 @@
     rsp.setCharacterEncoding("UTF-8");
     try {
       Context.setForwardedEvent();
+      if (!MediaType.parse(req.getContentType()).is(JSON_UTF_8)) {
+        sendError(rsp, SC_UNSUPPORTED_MEDIA_TYPE,
+            "Expecting " + JSON_UTF_8.toString() + " content type");
+        return;
+      }
       Event event = getEventFromRequest(req);
       dispatcher.postEvent(event);
       rsp.setStatus(SC_NO_CONTENT);
@@ -77,15 +83,12 @@
   }
 
   private Event getEventFromRequest(HttpServletRequest req) throws IOException {
-    if (MediaType.parse(req.getContentType()).is(JSON_UTF_8)) {
-      String jsonEvent = CharStreams.toString(req.getReader());
-      Gson gson = new GsonBuilder()
-          .registerTypeAdapter(Event.class, new EventDeserializer())
-          .registerTypeAdapter(Supplier.class, new SupplierDeserializer())
-          .create();
-      return gson.fromJson(jsonEvent, Event.class);
-    }
-    return null;
+    String jsonEvent = CharStreams.toString(req.getReader());
+    Gson gson = new GsonBuilder()
+        .registerTypeAdapter(Event.class, new EventDeserializer())
+        .registerTypeAdapter(Supplier.class, new SupplierDeserializer())
+        .create();
+    return gson.fromJson(jsonEvent, Event.class);
   }
 
   private static void sendError(HttpServletResponse rsp, int statusCode,
diff --git a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServletTest.java b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServletTest.java
index 8f417b3..e925e6f 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServletTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/syncevents/SyncEventsRestApiServletTest.java
@@ -14,9 +14,11 @@
 
 package com.ericsson.gerrit.plugins.syncevents;
 
+import static com.google.common.net.MediaType.JSON_UTF_8;
 import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
 import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
 import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT;
+import static javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE;
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.expectLastCall;
 import static org.easymock.EasyMock.isA;
@@ -105,6 +107,19 @@
   }
 
   @Test
+  public void testDoPostWrongMediaType() throws Exception {
+    resetAll();
+    expect(req.getContentType())
+        .andReturn(MediaType.APPLICATION_XML_UTF_8.toString()).anyTimes();
+    rsp.sendError(SC_UNSUPPORTED_MEDIA_TYPE,
+        "Expecting " + JSON_UTF_8.toString() + " content type");
+    expectLastCall().once();
+    replayAll();
+    syncEventsRestApiServlet.doPost(req, rsp);
+    verifyAll();
+  }
+
+  @Test
   public void testDoPostErrorWhileSendingErrorMessage() throws Exception {
     expect(req.getReader()).andThrow(new IOException("someError"));
     rsp.sendError(SC_BAD_REQUEST, "someError");