ProgressManagerServlet.java

  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. import fr.paris.lutece.portal.service.progressmanager.ProgressManagerService;
  36. import fr.paris.lutece.util.json.ErrorJsonResponse;
  37. import fr.paris.lutece.util.json.JsonResponse;
  38. import fr.paris.lutece.util.json.JsonUtil;
  39. import java.io.IOException;
  40. import java.io.PrintWriter;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import java.util.Map;
  44. import javax.servlet.ServletConfig;
  45. import javax.servlet.ServletException;
  46. import javax.servlet.http.HttpServlet;
  47. import javax.servlet.http.HttpServletRequest;
  48. import javax.servlet.http.HttpServletResponse;

  49. /**
  50.  * Servlet to manage progress feeds
  51.  *
  52.  * @author lutece team
  53.  */
  54. public class ProgressManagerServlet extends HttpServlet
  55. {

  56.     private static final long serialVersionUID = -6400074588556875395L;

  57.     private static final String PARAMETER_PROGRESS = "progress";
  58.     private static final String PARAMETER_REPORT = "report";
  59.     private static final String PARAMETER_TOKEN = "token";
  60.     private static final String PARAMETER_FROM_LINE = "fromLine";

  61.     private static final String CONTENT_TYPE = "application/json";
  62.     private static final String STATUS_NOT_FOUND = "not found";
  63.     private static final String STATUS_BAD_PARAMETER = "bad parameter";

  64.     private static final String ATTRIBUTE_NAME_LAST_LINE = "lastLine";
  65.     private static final String ATTRIBUTE_NAME_LINES = "lines";

  66.     /**
  67.      * Initialize the servlet
  68.      *
  69.      * @param config
  70.      *            The servlet config
  71.      * @throws ServletException
  72.      *             If an exception occurs that interrupts the servlet's normal operation
  73.      */
  74.     public void init( ServletConfig config ) throws ServletException
  75.     {
  76.         super.init( config );
  77.     }

  78.     /**
  79.      * Process HTTP Post request
  80.      *
  81.      * @param request
  82.      *            The http request
  83.      * @param response
  84.      *            The http response
  85.      * @throws ServletException
  86.      *             If an exception occurs that interrupts the servlet's normal operation
  87.      * @throws IOException
  88.      *             If an I/O exception occurs
  89.      */
  90.     @Override
  91.     public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
  92.     {
  93.         response.setContentType( CONTENT_TYPE );
  94.         PrintWriter out = response.getWriter( );

  95.         // token
  96.         String strToken = (String) request.getParameter( PARAMETER_TOKEN );

  97.         if ( strToken == null || strToken.isEmpty( ) )
  98.         {
  99.             out.println( JsonUtil.buildJsonResponse( new ErrorJsonResponse( STATUS_NOT_FOUND ) ) );
  100.             out.flush( );
  101.             out.close( );
  102.             return;
  103.         }

  104.         ProgressManagerService progressManagerService = ProgressManagerService.getInstance( );

  105.         if ( !progressManagerService.isRegistred( strToken ) )
  106.         {
  107.             out.println( JsonUtil.buildJsonResponse( new ErrorJsonResponse( STATUS_NOT_FOUND ) ) );
  108.             out.flush( );
  109.             out.close( );
  110.             return;
  111.         }

  112.         if ( request.getParameter( PARAMETER_PROGRESS ) != null )
  113.         {
  114.             out.println( JsonUtil.buildJsonResponse( new JsonResponse( progressManagerService.getProgressStatus( strToken ) ) ) );
  115.             out.flush( );
  116.             out.close( );
  117.             return;
  118.         }
  119.         else
  120.             if ( request.getParameter( PARAMETER_REPORT ) != null )
  121.             {
  122.                 int iFromLine = -1;
  123.                 try
  124.                 {
  125.                     iFromLine = Integer.parseInt( request.getParameter( PARAMETER_FROM_LINE ) );
  126.                 }
  127.                 catch( NumberFormatException e )
  128.                 {
  129.                     out.println( JsonUtil.buildJsonResponse( new ErrorJsonResponse( STATUS_BAD_PARAMETER ) ) );
  130.                 }

  131.                 Map<String, Object> mapResponse = new HashMap<>( );
  132.                 List<String> reportLines = progressManagerService.getReport( strToken, iFromLine );
  133.                 mapResponse.put( ATTRIBUTE_NAME_LAST_LINE, iFromLine + reportLines.size( ) );
  134.                 mapResponse.put( ATTRIBUTE_NAME_LINES, reportLines );
  135.                 out.println( JsonUtil.buildJsonResponse( new JsonResponse( mapResponse ) ) );
  136.                 out.flush( );
  137.                 out.close( );
  138.                 return;
  139.             }
  140.             else
  141.             {
  142.                 out.println( JsonUtil.buildJsonResponse( new ErrorJsonResponse( STATUS_NOT_FOUND ) ) );
  143.             }

  144.         out.flush( );
  145.         out.close( );
  146.     }
  147. }