From b68b95cbbf48931c3e96ee0fcf938c6c42b845f1 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer <paul-christian.volkmer@mni.thm.de> Date: Wed, 5 Sep 2012 14:13:11 +0200 Subject: [PATCH] Task #3805: Major changes related to Task #3805 * Added session controller with method to get session by id * Created session class to be used as entity object * Created session service class and interface * Added content negotiation view resolver to map entity objects to JSON --- .../de/thm/arsnova/SessionController.java | 28 +++++++++ .../java/de/thm/arsnova/entities/Session.java | 55 +++++++++++++++++ .../thm/arsnova/services/ISessionService.java | 10 ++++ .../thm/arsnova/services/SessionService.java | 37 ++++++++++++ src/main/webapp/WEB-INF/arsnova-servlet.xml | 60 ++++++++++++------- src/main/webapp/config.properties.example | 6 +- 6 files changed, 175 insertions(+), 21 deletions(-) create mode 100644 src/main/java/de/thm/arsnova/SessionController.java create mode 100644 src/main/java/de/thm/arsnova/entities/Session.java create mode 100644 src/main/java/de/thm/arsnova/services/ISessionService.java create mode 100644 src/main/java/de/thm/arsnova/services/SessionService.java diff --git a/src/main/java/de/thm/arsnova/SessionController.java b/src/main/java/de/thm/arsnova/SessionController.java new file mode 100644 index 00000000..33496989 --- /dev/null +++ b/src/main/java/de/thm/arsnova/SessionController.java @@ -0,0 +1,28 @@ +package de.thm.arsnova; + +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; + +import de.thm.arsnova.entities.Session; +import de.thm.arsnova.services.ISessionService; + +@Controller +public class SessionController { + + @Autowired + ISessionService sessionService; + + @RequestMapping("/session/{sessionkey}") + public Session getSession(@PathVariable String sessionkey, HttpServletResponse response) { + Session session = sessionService.getSession(sessionkey); + if (session != null) return session; + + response.setStatus(HttpStatus.NOT_FOUND.value()); + return null; + } +} diff --git a/src/main/java/de/thm/arsnova/entities/Session.java b/src/main/java/de/thm/arsnova/entities/Session.java new file mode 100644 index 00000000..431b2213 --- /dev/null +++ b/src/main/java/de/thm/arsnova/entities/Session.java @@ -0,0 +1,55 @@ +package de.thm.arsnova.entities; + + +public class Session { + + private String type; + private String name; + private String shortName; + private String keyword; + private String creator; + private boolean active; + private String _id; + private String _rev; + + public String getType() { + return type; + } + public void setType(String type) { + this.type = type; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public String getShortName() { + return shortName; + } + public void setShortName(String shortName) { + this.shortName = shortName; + } + public String getKeyword() { + return keyword; + } + public void setKeyword(String keyword) { + this.keyword = keyword; + } + public String getCreator() { + return creator; + } + public void setCreator(String creator) { + this.creator = creator; + } + public boolean isActive() { + return active; + } + public void setActive(boolean active) { + this.active = active; + } + + public void set_id(String id) {} + public void set_rev(String rev) {} + +} diff --git a/src/main/java/de/thm/arsnova/services/ISessionService.java b/src/main/java/de/thm/arsnova/services/ISessionService.java new file mode 100644 index 00000000..a40d08b1 --- /dev/null +++ b/src/main/java/de/thm/arsnova/services/ISessionService.java @@ -0,0 +1,10 @@ +package de.thm.arsnova.services; + +import de.thm.arsnova.entities.Session; + + +public interface ISessionService { + + public Session getSession(String keyword); + +} \ No newline at end of file diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java new file mode 100644 index 00000000..b4737211 --- /dev/null +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -0,0 +1,37 @@ +package de.thm.arsnova.services; + +import java.net.URLEncoder; + +import net.sf.json.JSONObject; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import com.fourspaces.couchdb.Database; +import com.fourspaces.couchdb.View; +import com.fourspaces.couchdb.ViewResults; + +import de.thm.arsnova.entities.Session; + +@Service +public class SessionService implements ISessionService { + + private final com.fourspaces.couchdb.Session session = new com.fourspaces.couchdb.Session("localhost", 5984); + public static final Logger logger = LoggerFactory + .getLogger(SessionService.class); + + @Override + public Session getSession(String keyword) { + Database db = session.getDatabase("arsnova"); + View view = new View("session/by_keyword"); + view.setKey(URLEncoder.encode("\""+keyword+"\"")); + ViewResults results = db.view(view); + + if (results.getJSONArray("rows").optJSONObject(0) == null) return null; + + Session result = (Session)JSONObject.toBean(results.getJSONArray("rows").optJSONObject(0).optJSONObject("value"), Session.class); + return result; + } + +} diff --git a/src/main/webapp/WEB-INF/arsnova-servlet.xml b/src/main/webapp/WEB-INF/arsnova-servlet.xml index 32eddb27..9b8f4814 100644 --- a/src/main/webapp/WEB-INF/arsnova-servlet.xml +++ b/src/main/webapp/WEB-INF/arsnova-servlet.xml @@ -12,28 +12,48 @@ <context:annotation-config /> <mvc:resources mapping="/**.html" location="/" /> - <mvc:resources mapping="/**.png" location="/"/> - <mvc:resources mapping="/**.manifest" location="/"/> - <mvc:resources mapping="/**.json" location="/"/> - <mvc:resources mapping="/**.xml" location="/"/> - <mvc:resources mapping="/dojo/**" location="/dojo/"/> - <mvc:resources mapping="/dijit/**" location="/dijit/"/> - <mvc:resources mapping="/dojox/**" location="/dojox/"/> - <mvc:resources mapping="/app/**" location="/app/"/> - <mvc:resources mapping="/views/**" location="/views/"/> - <mvc:resources mapping="/lib/**" location="/lib/"/> - <mvc:resources mapping="/resources/**" location="/resources/"/> - <mvc:resources mapping="/screenshots/**" location="/screenshots/"/> - <mvc:resources mapping="/templates/**" location="/templates/"/> - + <mvc:resources mapping="/**.png" location="/" /> + <mvc:resources mapping="/**.manifest" location="/" /> + <mvc:resources mapping="/**.json" location="/" /> + <mvc:resources mapping="/**.xml" location="/" /> + <mvc:resources mapping="/dojo/**" location="/dojo/" /> + <mvc:resources mapping="/dijit/**" location="/dijit/" /> + <mvc:resources mapping="/dojox/**" location="/dojox/" /> + <mvc:resources mapping="/app/**" location="/app/" /> + <mvc:resources mapping="/views/**" location="/views/" /> + <mvc:resources mapping="/lib/**" location="/lib/" /> + <mvc:resources mapping="/resources/**" location="/resources/" /> + <mvc:resources mapping="/screenshots/**" location="/screenshots/" /> + <mvc:resources mapping="/templates/**" location="/templates/" /> + <mvc:annotation-driven /> - <bean id="viewResolver" - class="org.springframework.web.servlet.view.InternalResourceViewResolver"> - <property name="viewClass" - value="org.springframework.web.servlet.view.JstlView" /> - <property name="prefix" value="/WEB-INF/views/" /> - <property name="suffix" value=".jsp" /> + <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/config.properties.example b/src/main/webapp/config.properties.example index f781cbac..280d47da 100644 --- a/src/main/webapp/config.properties.example +++ b/src/main/webapp/config.properties.example @@ -1,3 +1,7 @@ security.openid-target-url=http://localhost:8080/arsnova-war/doOpenIdLogin security.cas-check-url=http://localhost:8080/arsnova-war/j_spring_cas_security_check -security.cas-service-url=https://cas.thm.de/cas \ No newline at end of file +security.cas-service-url=https://cas.thm.de/cas + +couchdb.host=localhost +couchdb.port=5984 +couchdb.name=arsnova \ No newline at end of file -- GitLab