View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * Servlet to manage progress feeds
53   * 
54   * @author lutece team
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       * Initialize the servlet
75       * 
76       * @param config
77       *            The servlet config
78       * @throws ServletException
79       *             If an exception occurs that interrupts the servlet's normal operation
80       */
81      public void init( ServletConfig config ) throws ServletException
82      {
83          super.init( config );
84      }
85  
86      /**
87       * Process HTTP Post request
88       * 
89       * @param request
90       *            The http request
91       * @param response
92       *            The http response
93       * @throws ServletException
94       *             If an exception occurs that interrupts the servlet's normal operation
95       * @throws IOException
96       *             If an I/O exception occurs
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         // token
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 }