From 784260a80cfa7eb08e1934a11ccd5f737155f324 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer <paul-christian.volkmer@mni.thm.de> Date: Tue, 27 May 2014 20:24:23 +0200 Subject: [PATCH] Changed configuration to enable XML based responses Having content negotiation with more than just JSON content might follow REST principles. All entities annotated with e.g. @XmlRootElement can be send as XML data. This requires client to send Accept header with value "application/json" to fetch JSON responses. Default Accept header send by client is something like: "text/html,application/xhtml+xml,application/xml" which requests an XML document, not JSON document but JSON is expected. --- .../de/thm/arsnova/entities/Statistics.java | 26 +++++++++---------- .../webapp/WEB-INF/spring/arsnova-servlet.xml | 2 ++ .../controller/StatisticsControllerTest.java | 13 +++++----- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/thm/arsnova/entities/Statistics.java b/src/main/java/de/thm/arsnova/entities/Statistics.java index d8d53a225..d1faaa10c 100644 --- a/src/main/java/de/thm/arsnova/entities/Statistics.java +++ b/src/main/java/de/thm/arsnova/entities/Statistics.java @@ -11,50 +11,50 @@ public class Statistics { public int getAnswers() { return answers; } - public void setAnswers(int answers) { + public void setAnswers(final int answers) { this.answers = answers; } public int getQuestions() { return questions; } - public void setQuestions(int questions) { + public void setQuestions(final int questions) { this.questions = questions; } public int getOpenSessions() { return openSessions; } - public void setOpenSessions(int openSessions) { + public void setOpenSessions(final int openSessions) { this.openSessions = openSessions; } public int getClosedSessions() { return closedSessions; } - public void setClosedSessions(int closedSessions) { + public void setClosedSessions(final int closedSessions) { this.closedSessions = closedSessions; } public int getActiveUsers() { return activeUsers; } - public void setActiveUsers(int activeUsers) { + public void setActiveUsers(final int activeUsers) { this.activeUsers = activeUsers; } @Override public int hashCode() { return (this.getClass().getName() - + this.activeUsers - + this.answers - + this.closedSessions - + this.openSessions - + this.questions).hashCode(); + + activeUsers + + answers + + closedSessions + + openSessions + + questions).hashCode(); } @Override - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (obj == null) { return false; } @@ -62,8 +62,8 @@ public class Statistics { return true; } if (obj instanceof Statistics) { - Statistics other = (Statistics) obj; - return this.hashCode() == other.hashCode(); + final Statistics other = (Statistics) obj; + return hashCode() == other.hashCode(); } return false; diff --git a/src/main/webapp/WEB-INF/spring/arsnova-servlet.xml b/src/main/webapp/WEB-INF/spring/arsnova-servlet.xml index 586f28e30..c06457d54 100644 --- a/src/main/webapp/WEB-INF/spring/arsnova-servlet.xml +++ b/src/main/webapp/WEB-INF/spring/arsnova-servlet.xml @@ -22,10 +22,12 @@ class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="false" /> <property name="favorParameter" value="true" /> + <property name="defaultContentType" value="application/json" /> <property name="mediaTypes"> <value> html=text/html json=application/json + xml=application/xml </value> </property> </bean> diff --git a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java index d41bdee6b..cc9f7dbdd 100644 --- a/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java +++ b/src/test/java/de/thm/arsnova/controller/StatisticsControllerTest.java @@ -10,6 +10,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -43,35 +44,35 @@ public class StatisticsControllerTest { @Test public final void testShouldGetCurrentOnlineUsers() throws Exception { - mockMvc.perform(get("/statistics/activeusercount")) + mockMvc.perform(get("/statistics/activeusercount").accept(MediaType.TEXT_PLAIN)) .andExpect(status().isOk()) .andExpect(content().string("0")); } @Test public final void testShouldSendXDeprecatedApiForGetCurrentOnlineUsers() throws Exception { - mockMvc.perform(get("/statistics/activeusercount")) + mockMvc.perform(get("/statistics/activeusercount").accept(MediaType.TEXT_PLAIN)) .andExpect(status().isOk()) .andExpect(header().string(AbstractController.X_DEPRECATED_API,"1")); } @Test public final void testShouldGetSessionCount() throws Exception { - mockMvc.perform(get("/statistics/sessioncount")) + mockMvc.perform(get("/statistics/sessioncount").accept(MediaType.TEXT_PLAIN)) .andExpect(status().isOk()) .andExpect(content().string("3")); } @Test public final void testShouldSendXDeprecatedApiForGetSessionCount() throws Exception { - mockMvc.perform(get("/statistics/sessioncount")) + mockMvc.perform(get("/statistics/sessioncount").accept(MediaType.TEXT_PLAIN)) .andExpect(status().isOk()) .andExpect(header().string(AbstractController.X_DEPRECATED_API,"1")); } @Test public final void testShouldGetStatistics() throws Exception { - mockMvc.perform(get("/statistics")) + mockMvc.perform(get("/statistics").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$.answers").value(0)) .andExpect(jsonPath("$.questions").value(0)) @@ -82,7 +83,7 @@ public class StatisticsControllerTest { @Test public final void testShouldGetCacheControlHeaderForStatistics() throws Exception { - mockMvc.perform(get("/statistics")) + mockMvc.perform(get("/statistics").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(header().string("cache-control", "public, max-age=60")); } -- GitLab