From 804a6da8239d16b444cd28d8a72a4296a47bd6ea Mon Sep 17 00:00:00 2001 From: Julian Hochstetter <julian.hochstetter@mni.thm.de> Date: Mon, 17 Sep 2012 09:26:43 +0200 Subject: [PATCH] Task #3868: Refactor Servlets * @ResponseBody Annotation for JSON Response * Old ARSnova Servlet does nothing * New Spring Servlet loads Beans, Services, Security and SocketServer * One Property Loader which is used in XML Spring Configuration and with @Value Annotation --- .../de/thm/arsnova/SessionController.java | 3 ++ .../thm/arsnova/services/SessionService.java | 9 ++-- src/main/webapp/WEB-INF/api-servlet.xml | 47 ----------------- src/main/webapp/WEB-INF/arsnova-servlet.xml | 25 ++------- .../webapp/WEB-INF/spring/spring-main.xml | 51 ++++++++++++++++--- .../webapp/WEB-INF/spring/spring-security.xml | 4 -- src/main/webapp/WEB-INF/web.xml | 9 ---- 7 files changed, 56 insertions(+), 92 deletions(-) delete mode 100644 src/main/webapp/WEB-INF/api-servlet.xml diff --git a/src/main/java/de/thm/arsnova/SessionController.java b/src/main/java/de/thm/arsnova/SessionController.java index d92affcf..51335593 100644 --- a/src/main/java/de/thm/arsnova/SessionController.java +++ b/src/main/java/de/thm/arsnova/SessionController.java @@ -34,6 +34,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; import de.thm.arsnova.entities.Session; import de.thm.arsnova.services.ISessionService; @@ -66,6 +67,7 @@ public class SessionController { } @RequestMapping(value="/session/{sessionkey}", method=RequestMethod.GET) + @ResponseBody public Session getSession(@PathVariable String sessionkey, HttpServletResponse response) { Session session = sessionService.getSession(sessionkey); if (session != null) return session; @@ -75,6 +77,7 @@ public class SessionController { } @RequestMapping(value="/session", method=RequestMethod.POST) + @ResponseBody public Session postNewSession(@RequestBody Session session, HttpServletResponse response) { Session newSession = sessionService.saveSession(session); if (session != null) { diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java index ee141d02..55a22c3e 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -74,18 +74,21 @@ public class SessionService implements ISessionService { public static final Logger logger = LoggerFactory.getLogger(SessionService.class); - @Value("#{props['couchdb.host']}") + @Value("${couchdb.host}") public final void setDatabaseHost(String databaseHost) { + logger.info(databaseHost); this.databaseHost = databaseHost; } - @Value("#{props['couchdb.port']}") + @Value("${couchdb.port}") public final void setDatabasePort(String databasePort) { + logger.info(databasePort); this.databasePort = Integer.parseInt(databasePort); } - @Value("#{props['couchdb.name']}") + @Value("${couchdb.name}") public final void setDatabaseName(String databaseName) { + logger.info(databaseName); this.databaseName = databaseName; } diff --git a/src/main/webapp/WEB-INF/api-servlet.xml b/src/main/webapp/WEB-INF/api-servlet.xml deleted file mode 100644 index 345e7710..00000000 --- a/src/main/webapp/WEB-INF/api-servlet.xml +++ /dev/null @@ -1,47 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" - xmlns:security="http://www.springframework.org/schema/security" - xmlns:mvc="http://www.springframework.org/schema/mvc" - xmlns:task="http://www.springframework.org/schema/task" - xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd - http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd - http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> - - <context:component-scan base-package="de.thm.arsnova" /> - <context:annotation-config /> - - <mvc:annotation-driven /> - <task:annotation-driven /> - - <bean - class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> - <bean - class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> - <property name="messageConverters"> - <list> - <bean - class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> - </list> - </property> - </bean> - - <bean - class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> - <property name="mediaTypes"> - <map> - <entry key="json" value="application/json" /> - </map> - </property> - - <property name="defaultViews"> - <list> - <bean - class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> - </list> - </property> - </bean> - -</beans> diff --git a/src/main/webapp/WEB-INF/arsnova-servlet.xml b/src/main/webapp/WEB-INF/arsnova-servlet.xml index 1c79a718..065ede77 100644 --- a/src/main/webapp/WEB-INF/arsnova-servlet.xml +++ b/src/main/webapp/WEB-INF/arsnova-servlet.xml @@ -1,26 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:context="http://www.springframework.org/schema/context" - xmlns:security="http://www.springframework.org/schema/security" - xmlns:mvc="http://www.springframework.org/schema/mvc" - xmlns:p="http://www.springframework.org/schema/p" - xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd - http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd - http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> - - <context:component-scan base-package="de.thm.arsnova" /> - <context:annotation-config /> - - <mvc:resources mapping="/**" location="/" /> - - <mvc:annotation-driven /> - - <bean id="viewResolver" - class="org.springframework.web.servlet.view.InternalResourceViewResolver" - p:viewClass="org.springframework.web.servlet.view.JstlView" - p:prefix="/WEB-INF/views/" - p:suffix=".jsp" /> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> + <!-- ARSnova Root Context: defines shared resources visible to all OTHER web components --> </beans> diff --git a/src/main/webapp/WEB-INF/spring/spring-main.xml b/src/main/webapp/WEB-INF/spring/spring-main.xml index 688a7fd2..8f11efeb 100644 --- a/src/main/webapp/WEB-INF/spring/spring-main.xml +++ b/src/main/webapp/WEB-INF/spring/spring-main.xml @@ -2,9 +2,12 @@ <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd + xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" + xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd + http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" @@ -16,14 +19,48 @@ </list> </property> </bean> - - <util:properties id="props" - location="file:///etc/arsnova/arsnova.properties" /> - + <import resource="spring-security.xml" /> <context:component-scan base-package="de.thm.arsnova" /> - <context:annotation-config /> + <mvc:annotation-driven /> + <mvc:resources mapping="/**" location="/" /> + + <task:annotation-driven /> + + <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> + <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> + <property name="messageConverters"> + <list> + <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> + </list> + </property> + </bean> + + <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> + <property name="mediaTypes"> + <map> + <entry key="html" value="text/html"/> + <entry key="json" value="application/json" /> + </map> + </property> + <property name="viewResolvers"> + <list> + <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> + <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> + <property name="prefix" value="/WEB-INF/jsp/"/> + <property name="suffix" value=".jsp"/> + </bean> + </list> + </property> + <property name="defaultViews"> + <list> + <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"> + <property name="prefixJson" value="false"/> + </bean> + </list> + </property> + </bean> <bean id="socketServer" class="de.thm.arsnova.socket.ARSnovaSocketIOServer" init-method="startServer" destroy-method="stopServer" scope="singleton" diff --git a/src/main/webapp/WEB-INF/spring/spring-security.xml b/src/main/webapp/WEB-INF/spring/spring-security.xml index 40e16c94..bc0a0d1f 100644 --- a/src/main/webapp/WEB-INF/spring/spring-security.xml +++ b/src/main/webapp/WEB-INF/spring/spring-security.xml @@ -9,10 +9,6 @@ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> - <context:component-scan base-package="de.thm.arsnova" /> - <context:annotation-config /> - - <security:authentication-manager alias="authenticationManager"> <security:authentication-provider ref="facebookAuthProvider" /> <security:authentication-provider ref="twitterAuthProvider" /> diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index b289f010..cb7155c3 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -22,15 +22,6 @@ <servlet-name>arsnova</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> - <servlet> - <servlet-name>api</servlet-name> - <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> - <load-on-startup>1</load-on-startup> - </servlet> - <servlet-mapping> - <servlet-name>api</servlet-name> - <url-pattern>/api/*</url-pattern> - </servlet-mapping> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> -- GitLab