1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.quiz.modules.rest.rs;
35
36 import fr.paris.lutece.plugins.quiz.service.QuizService;
37 import fr.paris.lutece.plugins.rest.service.RestConstants;
38 import fr.paris.lutece.plugins.rest.util.json.JSONUtil;
39 import fr.paris.lutece.portal.service.spring.SpringContextService;
40
41 import java.util.Map;
42
43 import javax.servlet.http.HttpServletResponse;
44
45 import javax.ws.rs.GET;
46 import javax.ws.rs.Path;
47 import javax.ws.rs.PathParam;
48 import javax.ws.rs.Produces;
49 import javax.ws.rs.core.Context;
50 import javax.ws.rs.core.MediaType;
51
52
53
54
55
56 @Path( RestConstants.BASE_PATH + "quiz" )
57 public class QuizRest
58 {
59 private static final String BEAN_QUIZ_SERVICE = "quiz.quizService";
60
61
62
63
64
65 @GET
66 @Path( "" )
67 @Produces( {MediaType.TEXT_PLAIN,
68 MediaType.APPLICATION_JSON
69 } )
70 public String getQuizListJson( )
71 {
72 QuizService serviceQuiz = (QuizService) SpringContextService.getBean( BEAN_QUIZ_SERVICE );
73
74 return JSONUtil.model2Json( serviceQuiz.getQuizList( ) );
75 }
76
77
78
79
80
81
82 @GET
83 @Path( "{id}" )
84 @Produces( {MediaType.TEXT_PLAIN,
85 MediaType.APPLICATION_JSON
86 } )
87 public String getQuizJson( @PathParam( "id" )
88 String strId, @Context
89 HttpServletResponse response )
90 {
91 String strResponse = "";
92
93 try
94 {
95 strResponse = JSONUtil.model2Json( getQuiz( strId ) );
96 }
97 catch ( NumberFormatException e )
98 {
99 response.setStatus( HttpServletResponse.SC_BAD_REQUEST );
100 strResponse = JSONUtil.formatError( "Invalid quiz id", 1 );
101 }
102 catch ( NullPointerException e )
103 {
104 response.setStatus( HttpServletResponse.SC_NOT_FOUND );
105 strResponse = JSONUtil.formatError( "Quiz id not found", 1 );
106 }
107
108 return strResponse;
109 }
110
111 private Map getQuiz( String strId )
112 {
113 QuizService serviceQuiz = (QuizService) SpringContextService.getBean( BEAN_QUIZ_SERVICE );
114
115 return serviceQuiz.getQuiz( Integer.parseInt( strId ) );
116 }
117 }