diff --git a/src/main/java/de/thm/arsnova/SessionController.java b/src/main/java/de/thm/arsnova/SessionController.java index 57572481fdfdc0b382c7898ca45067d405b85cbd..89bab167b29f79a105899d978ac39afc4c881b2b 100644 --- a/src/main/java/de/thm/arsnova/SessionController.java +++ b/src/main/java/de/thm/arsnova/SessionController.java @@ -38,7 +38,7 @@ public class SessionController { @Autowired ISessionService sessionService; - @RequestMapping("/session/{sessionkey}") + @RequestMapping(value="/session/{sessionkey}", method=RequestMethod.GET) public Session getSession(@PathVariable String sessionkey, HttpServletResponse response) { Session session = sessionService.getSession(sessionkey); if (session != null) return session; diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java index ff7897917c5430132a43448800ba92b11643dbf4..e40e0d342af577ee769db8f5732199ce7ece8d7e 100644 --- a/src/main/java/de/thm/arsnova/services/SessionService.java +++ b/src/main/java/de/thm/arsnova/services/SessionService.java @@ -27,6 +27,7 @@ import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; @@ -45,18 +46,34 @@ 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); - private final Database database = session.getDatabase("arsnova"); + private String databaseHost; + private int databasePort; + private String databaseName; + + private Database database; + + public static final Logger logger = LoggerFactory.getLogger(SessionService.class); - public static final Logger logger = LoggerFactory - .getLogger(SessionService.class); + @Value("#{props['couchdb.host']}") + public final void setDatabaseHost(String databaseHost) { + this.databaseHost = databaseHost; + } + + @Value("#{props['couchdb.port']}") + public final void setDatabasePort(String databasePort) { + this.databasePort = Integer.parseInt(databasePort); + } + + @Value("#{props['couchdb.name']}") + public final void setDatabaseName(String databaseName) { + this.databaseName = databaseName; + } @Override public Session getSession(String keyword) { View view = new View("session/by_keyword"); view.setKey(URLEncoder.encode("\"" + keyword + "\"")); - ViewResults results = database.view(view); + ViewResults results = this.getDatabase().view(view); if (results.getJSONArray("rows").optJSONObject(0) == null) return null; @@ -103,7 +120,7 @@ public class SessionService implements ISessionService { view.setGroup(true); view.setStartKey(URLEncoder.encode("[\"" + sessionId + "\"]")); view.setEndKey(URLEncoder.encode("[\"" + sessionId + "\",{}]")); - ViewResults results = database.view(view); + ViewResults results = this.getDatabase().view(view); logger.info("Feedback: {}", results.getJSONArray("rows")); @@ -173,7 +190,7 @@ public class SessionService implements ISessionService { } try { - database.saveDocument(feedback); + this.getDatabase().saveDocument(feedback); } catch (IOException e) { return false; } @@ -185,7 +202,7 @@ public class SessionService implements ISessionService { @Transactional(isolation=Isolation.READ_COMMITTED) public boolean sessionKeyAvailable(String keyword) { View view = new View("session/by_keyword"); - ViewResults results = database.view(view); + ViewResults results = this.getDatabase().view(view); return ! results.containsKey(keyword); } @@ -193,7 +210,7 @@ public class SessionService implements ISessionService { private String getSessionId(String keyword) { View view = new View("session/by_keyword"); view.setKey(URLEncoder.encode("\"" + keyword + "\"")); - ViewResults results = database.view(view); + ViewResults results = this.getDatabase().view(view); if (results.getJSONArray("rows").optJSONObject(0) == null) return null; @@ -224,4 +241,28 @@ public class SessionService implements ISessionService { if (this.sessionKeyAvailable(keyword)) return keyword; return generateKeyword(); } + + private Database getDatabase() { + if (database == null) { + try { + com.fourspaces.couchdb.Session session = new com.fourspaces.couchdb.Session( + databaseHost, + databasePort + ); + + database = session.getDatabase(databaseName); + } catch (Exception e) { + logger.error( + "Cannot connect to CouchDB database '" + + databaseName + +"' on host '" + + databaseHost + + "' using port " + + databasePort + ); + } + } + + return database; + } } diff --git a/src/main/webapp/WEB-INF/spring/spring-main.xml b/src/main/webapp/WEB-INF/spring/spring-main.xml index 1c3c4fd43b867a15036cf0edd2c666c3792048c8..d46fe5909cf684c6c94424c4b061df33f3879fba 100644 --- a/src/main/webapp/WEB-INF/spring/spring-main.xml +++ b/src/main/webapp/WEB-INF/spring/spring-main.xml @@ -3,7 +3,9 @@ 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 + 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"> <bean id="propertyPlaceholderConfigurer" @@ -12,12 +14,13 @@ p:ignoreResourceNotFound="true"> <property name="locations" > <list> - <value>config.properties.example</value> <value>config.properties</value> </list> </property> </bean> + <util:properties id="props" location="config.properties"/> + <import resource="spring-security.xml" /> <context:component-scan base-package="de.thm.arsnova" />