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.portal.web.progressmanager;
35
36 import fr.paris.lutece.portal.service.progressmanager.ProgressManagerService;
37 import fr.paris.lutece.util.json.ErrorJsonResponse;
38 import fr.paris.lutece.util.json.JsonResponse;
39 import fr.paris.lutece.util.json.JsonUtil;
40 import java.io.IOException;
41 import java.io.PrintWriter;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import javax.servlet.ServletConfig;
46 import javax.servlet.ServletException;
47 import javax.servlet.http.HttpServlet;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51
52
53
54
55
56 public class ProgressManagerServlet extends HttpServlet
57 {
58
59 private static final long serialVersionUID = -6400074588556875395L;
60
61 private static final String PARAMETER_PROGRESS = "progress";
62 private static final String PARAMETER_REPORT = "report";
63 private static final String PARAMETER_TOKEN = "token";
64 private static final String PARAMETER_FROM_LINE = "fromLine";
65
66 private static final String CONTENT_TYPE = "application/json";
67 private static final String STATUS_NOT_FOUND = "not found";
68 private static final String STATUS_BAD_PARAMETER = "bad parameter";
69
70 private static final String ATTRIBUTE_NAME_LAST_LINE = "lastLine";
71 private static final String ATTRIBUTE_NAME_LINES = "lines";
72
73
74
75
76
77
78
79
80
81 public void init( ServletConfig config ) throws ServletException
82 {
83 super.init( config );
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98 @Override
99 public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
100 {
101 response.setContentType( CONTENT_TYPE );
102 PrintWriter out = response.getWriter( );
103
104
105 String strToken = (String) request.getParameter( PARAMETER_TOKEN );
106
107 if ( strToken == null || strToken.isEmpty( ) )
108 {
109 out.println( JsonUtil.buildJsonResponse( new ErrorJsonResponse( STATUS_NOT_FOUND ) ) );
110 out.flush( );
111 out.close( );
112 return;
113 }
114
115 ProgressManagerService progressManagerService = ProgressManagerService.getInstance( );
116
117 if ( !progressManagerService.isRegistred( strToken ) )
118 {
119 out.println( JsonUtil.buildJsonResponse( new ErrorJsonResponse( STATUS_NOT_FOUND ) ) );
120 out.flush( );
121 out.close( );
122 return;
123 }
124
125 if ( request.getParameter( PARAMETER_PROGRESS ) != null )
126 {
127 out.println( JsonUtil.buildJsonResponse( new JsonResponse( progressManagerService.getProgressStatus( strToken ) ) ) );
128 out.flush( );
129 out.close( );
130 return;
131 }
132 else
133 if ( request.getParameter( PARAMETER_REPORT ) != null )
134 {
135 int iFromLine = -1;
136 try
137 {
138 iFromLine = Integer.parseInt( request.getParameter( PARAMETER_FROM_LINE ) );
139 }
140 catch( NumberFormatException e )
141 {
142 out.println( JsonUtil.buildJsonResponse( new ErrorJsonResponse( STATUS_BAD_PARAMETER ) ) );
143 }
144
145 Map<String, Object> mapResponse = new HashMap<>( );
146 List<String> reportLines = progressManagerService.getReport( strToken, iFromLine );
147 mapResponse.put( ATTRIBUTE_NAME_LAST_LINE, iFromLine + reportLines.size( ) );
148 mapResponse.put( ATTRIBUTE_NAME_LINES, reportLines );
149 out.println( JsonUtil.buildJsonResponse( new JsonResponse( mapResponse ) ) );
150 out.flush( );
151 out.close( );
152 return;
153 }
154 else
155 {
156 out.println( JsonUtil.buildJsonResponse( new ErrorJsonResponse( STATUS_NOT_FOUND ) ) );
157 }
158
159 out.flush( );
160 out.close( );
161 }
162 }