Skip to content
Snippets Groups Projects
Commit 0c661e6c authored by Julian Hochstetter's avatar Julian Hochstetter
Browse files

Merge commit 'refs/merge-requests/6' of git://scm.thm.de/arsnova/arsnova-war into merge-requests/6

Conflicts:
	src/main/java/de/thm/arsnova/controller/SessionController.java
	src/main/java/de/thm/arsnova/dao/CouchDBDao.java
	src/main/java/de/thm/arsnova/dao/IDatabaseDao.java
	src/main/java/de/thm/arsnova/services/ISessionService.java
	src/main/java/de/thm/arsnova/services/SessionService.java
	src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java
parents 020cd22a ebb8d87b
No related merge requests found
Showing
with 288 additions and 90 deletions
/*
* Copyright (C) 2012 THM webMedia
*
* This file is part of ARSnova.
*
* ARSnova is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARSnova is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.thm.arsnova.controller;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
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.Feedback;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.services.ISessionService;
import de.thm.arsnova.services.IUserService;
import de.thm.arsnova.socket.ARSnovaSocketIOServer;
@Controller
public class FeedbackController extends AbstractController {
public static final Logger logger = LoggerFactory.getLogger(FeedbackController.class);
@Autowired
ISessionService sessionService;
@Autowired
IUserService userService;
@Autowired
ARSnovaSocketIOServer server;
@RequestMapping(value="/session/{sessionkey}/feedback", method=RequestMethod.GET)
@ResponseBody
public Feedback getFeedback(@PathVariable String sessionkey) {
return sessionService.getFeedback(sessionkey);
}
@RequestMapping(value="/session/{sessionkey}/feedback", method=RequestMethod.POST)
@ResponseBody
public Feedback postFeedback(@PathVariable String sessionkey, @RequestBody int value, HttpServletResponse response) {
User user = userService.getUser(SecurityContextHolder.getContext().getAuthentication());
if (sessionService.saveFeedback(sessionkey, value, user)) {
Feedback feedback = sessionService.getFeedback(sessionkey);
if (feedback != null) {
// TODO: Broadcast feedback changes via websocket
response.setStatus(HttpStatus.CREATED.value());
return feedback;
}
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
return null;
}
response.setStatus(HttpStatus.BAD_REQUEST.value());
return null;
}
}
/*
* Copyright (C) 2012 THM webMedia
*
* This file is part of ARSnova.
*
* ARSnova is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ARSnova is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.thm.arsnova.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.services.ISessionService;
import de.thm.arsnova.services.IUserService;
import de.thm.arsnova.socket.ARSnovaSocketIOServer;
@Controller
public class QuestionController extends AbstractController {
public static final Logger logger = LoggerFactory.getLogger(QuestionController.class);
@Autowired
ISessionService sessionService;
@Autowired
IUserService userService;
@Autowired
ARSnovaSocketIOServer server;
@RequestMapping(value="/session/{sessionkey}/question/{questionId}", method=RequestMethod.GET)
@ResponseBody
public Question getQuestion(@PathVariable String sessionkey, @PathVariable String questionId, HttpServletResponse response) {
Question question = sessionService.getQuestion(questionId);
if (question != null && question.getSession().equals(sessionkey)) {
return question;
}
response.setStatus(HttpStatus.NOT_FOUND.value());
return null;
}
@RequestMapping(value="/session/{sessionkey}/question", method=RequestMethod.POST)
@ResponseBody
public void postQuestion(@PathVariable String sessionkey, @RequestBody Question question, HttpServletResponse response) {
if (! sessionkey.equals(question.getSession())) {
response.setStatus(HttpStatus.PRECONDITION_FAILED.value());
return;
}
if (sessionService.saveQuestion(question)) {
response.setStatus(HttpStatus.CREATED.value());
return;
}
response.setStatus(HttpStatus.BAD_REQUEST.value());
return;
}
@RequestMapping(
value={
"/getSkillQuestions/{sessionkey}",
"/session/{sessionkey}/skillquestions"
},
method=RequestMethod.GET
)
@ResponseBody
public List<Question> getSkillQuestions(@PathVariable String sessionkey, @RequestParam(value="sort", required=false) String sort, HttpServletResponse response) {
List<Question> questions = sessionService.getSkillQuestions(sessionkey, sort);
if(questions == null || questions.isEmpty()) {
response.setStatus(HttpStatus.NOT_FOUND.value());
return null;
}
logger.info(questions.toString());
return questions;
}
}
......@@ -172,7 +172,7 @@ public class SessionController extends AbstractController {
return url.toString();
}
@RequestMapping(value="/mySessions", method=RequestMethod.GET)
@RequestMapping(value={"/mySessions","/session/mysessions"}, method=RequestMethod.GET)
@ResponseBody
public List<Session> getMySession(HttpServletResponse response) {
String username = userService.getUser(SecurityContextHolder.getContext().getAuthentication()).getUsername();
......
......@@ -34,11 +34,15 @@ import java.util.Set;
import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.bean.BeanMorpher;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -55,6 +59,8 @@ import com.fourspaces.couchdb.View;
import com.fourspaces.couchdb.ViewResults;
import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.PossibleAnswer;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
......@@ -515,45 +521,4 @@ public class CouchDBDao implements IDatabaseDao {
}
return null;
}
@Override
public LoggedIn registerAsOnlineUser(User u, Session s) {
try {
View view = new View("logged_in/all");
view.setKey(URLEncoder.encode("\"" + u.getUsername() + "\"", "UTF-8"));
ViewResults results = this.getDatabase().view(view);
LoggedIn loggedIn = new LoggedIn();
if (results.getJSONArray("rows").optJSONObject(0) != null) {
JSONObject json = results.getJSONArray("rows").optJSONObject(0).optJSONObject("value");
loggedIn = (LoggedIn) JSONObject.toBean(json, LoggedIn.class);
Collection<VisitedSession> visitedSessions = JSONArray.toCollection(json.getJSONArray("visitedSessions"), VisitedSession.class);
loggedIn.setVisitedSessions(new ArrayList<VisitedSession>(visitedSessions));
}
loggedIn.setUser(u.getUsername());
loggedIn.setSessionId(s.get_id());
loggedIn.addVisitedSession(s);
JSONObject json = JSONObject.fromObject(loggedIn);
Document doc = new Document(json);
if (doc.getId() == "") {
// If this is a new user without a logged_in document, we have to remove the following
// pre-filled fields. Otherwise, CouchDB will take these empty fields as genuine
// identifiers, and will throw errors afterwards.
doc.remove("_id");
doc.remove("_rev");
}
this.getDatabase().saveDocument(doc);
LoggedIn l = (LoggedIn) JSONObject.toBean(doc.getJSONObject(), LoggedIn.class);
Collection<VisitedSession> visitedSessions = JSONArray.toCollection(doc.getJSONObject().getJSONArray("visitedSessions"), VisitedSession.class);
l.setVisitedSessions(new ArrayList<VisitedSession>(visitedSessions));
return l;
} catch (UnsupportedEncodingException e) {
return null;
} catch (IOException e) {
return null;
}
}
}
......@@ -22,10 +22,10 @@ package de.thm.arsnova.dao;
import java.util.List;
import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.socket.message.Question;
public interface IDatabaseDao {
public void cleanFeedbackVotes(int cleanupFeedbackDelay);
......
package de.thm.arsnova.socket.message;
package de.thm.arsnova.entities;
public class Authorize {
private String user;
......
......@@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.thm.arsnova.socket.message;
package de.thm.arsnova.entities;
public class PossibleAnswer {
......
......@@ -16,7 +16,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.thm.arsnova.socket.message;
package de.thm.arsnova.entities;
import java.util.List;
......
......@@ -24,12 +24,11 @@ import java.util.Map;
import java.util.Set;
import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.socket.message.Question;
public interface ISessionService {
......@@ -50,5 +49,4 @@ public interface ISessionService {
public Question getQuestion(String id);
public List<Question> getSkillQuestions(String sessionkey, String sort);
public int getSkillQuestionCount(String sessionkey);
public LoggedIn registerAsOnlineUser(User user, String sessionkey);
}
\ No newline at end of file
......@@ -36,11 +36,11 @@ import org.springframework.transaction.annotation.Transactional;
import de.thm.arsnova.annotation.Authenticated;
import de.thm.arsnova.dao.IDatabaseDao;
import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.socket.ARSnovaSocketIOServer;
import de.thm.arsnova.socket.message.Question;
@Service
public class SessionService implements ISessionService {
......
......@@ -23,10 +23,10 @@ import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.services.ISessionService;
import de.thm.arsnova.socket.message.Feedback;
import de.thm.arsnova.socket.message.Question;
public class ARSnovaSocketIOServer {
......
package de.thm.arsnova.controller;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import javax.inject.Inject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import de.thm.arsnova.exceptions.NotFoundException;
import de.thm.arsnova.exceptions.UnauthorizedException;
import de.thm.arsnova.services.StubUserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"file:src/main/webapp/WEB-INF/arsnova-servlet.xml",
"file:src/main/webapp/WEB-INF/spring/spring-main.xml",
"file:src/test/resources/test-config.xml" })
public class FeedbackControllerTest {
@Inject
private ApplicationContext applicationContext;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private HandlerAdapter handlerAdapter;
@Autowired
private FeedbackController feedbackController;
@Autowired
private StubUserService userService;
@Before
public void setUp() {
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
handlerAdapter = applicationContext
.getBean(AnnotationMethodHandlerAdapter.class);
}
@Test(expected=NotFoundException.class)
public void testShouldNotGetFeedbackForUnknownSession() throws Exception {
userService.setUserAuthenticated(true);
request.setMethod("GET");
request.setRequestURI("/session/00000000/feedback");
final ModelAndView mav = handlerAdapter.handle(request, response, feedbackController);
assertNull(mav);
assertTrue(response.getStatus() == 404);
}
@Test(expected=UnauthorizedException.class)
public void testShouldNotGetFeedbackIfUnauthorized() throws Exception {
userService.setUserAuthenticated(false);
request.setMethod("GET");
request.setRequestURI("/session/00000000/feedback");
final ModelAndView mav = handlerAdapter.handle(request, response, feedbackController);
assertNull(mav);
assertTrue(response.getStatus() == 401);
}
@Test(expected=UnauthorizedException.class)
public void testShouldNotSaveFeedbackIfUnauthorized() throws Exception {
userService.setUserAuthenticated(false);
request.setMethod("POST");
request.setRequestURI("/session/00000000/feedback");
request.setContentType("application/json");
request.setContent("0".getBytes());
final ModelAndView mav = handlerAdapter.handle(request, response, feedbackController);
assertNull(mav);
assertTrue(response.getStatus() == 401);
}
}
......@@ -100,42 +100,4 @@ public class SessionControllerTest {
assertNull(mav);
assertTrue(response.getStatus() == 401);
}
@Test(expected=NotFoundException.class)
public void testShouldNotGetFeedbackForUnknownSession() throws Exception {
userService.setUserAuthenticated(true);
request.setMethod("GET");
request.setRequestURI("/session/00000000/feedback");
final ModelAndView mav = handlerAdapter.handle(request, response, sessionController);
assertNull(mav);
assertTrue(response.getStatus() == 404);
}
@Test(expected=UnauthorizedException.class)
public void testShouldNotGetFeedbackIfUnauthorized() throws Exception {
userService.setUserAuthenticated(false);
request.setMethod("GET");
request.setRequestURI("/session/00000000/feedback");
final ModelAndView mav = handlerAdapter.handle(request, response, sessionController);
assertNull(mav);
assertTrue(response.getStatus() == 401);
}
@Test(expected=UnauthorizedException.class)
public void testShouldNotSaveFeedbackIfUnauthorized() throws Exception {
userService.setUserAuthenticated(false);
request.setMethod("POST");
request.setRequestURI("/session/00000000/feedback");
request.setContentType("application/json");
request.setContent("0".getBytes());
final ModelAndView mav = handlerAdapter.handle(request, response, sessionController);
assertNull(mav);
assertTrue(response.getStatus() == 401);
}
}
......@@ -10,12 +10,12 @@ import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.Session;
import de.thm.arsnova.entities.User;
import de.thm.arsnova.exceptions.ForbiddenException;
import de.thm.arsnova.exceptions.NotFoundException;
import de.thm.arsnova.socket.message.Question;
@Component
@Scope("singleton")
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment