View Javadoc
1   /*
2    * To change this license header, choose License Headers in Project Properties.
3    * To change this template file, choose Tools | Templates
4    * and open the template in the editor.
5    */
6   package fr.paris.lutece.plugins.dataviz.web;
7   
8   import fr.paris.lutece.plugins.dataviz.service.StatNotFoundException;
9   import fr.paris.lutece.plugins.dataviz.service.StatsService;
10  import fr.paris.lutece.util.json.ErrorJsonResponse;
11  import fr.paris.lutece.util.json.JsonUtil;
12  
13  import java.io.IOException;
14  
15  import javax.servlet.ServletException;
16  import javax.servlet.ServletOutputStream;
17  import javax.servlet.http.HttpServlet;
18  import javax.servlet.http.HttpServletRequest;
19  import javax.servlet.http.HttpServletResponse;
20  
21  
22  /**
23   * Class for format stat into Json
24   */
25  public class StatServlet extends HttpServlet
26  {
27      private static final String CONTENT_TYPE_JSON = "application/json";
28      private static final String PARAMETER_NAME = "name";
29  
30      @Override
31      protected void service( HttpServletRequest req, HttpServletResponse resp )
32          throws ServletException, IOException
33      {
34          String strName = req.getParameter( PARAMETER_NAME );
35  
36          String strOutput;
37  
38          try
39          {
40              strOutput = StatsService.instance(  ).getStat( strName );
41          }
42          catch ( StatNotFoundException ex )
43          {
44              ErrorJsonResponse error = new ErrorJsonResponse( "1", ex.getMessage(  ) );
45              strOutput = JsonUtil.buildJsonResponse( error );
46          }
47  
48          ServletOutputStream out = resp.getOutputStream(  );
49  
50          resp.setContentType( CONTENT_TYPE_JSON );
51          out.print( strOutput );
52          out.flush(  );
53          out.close(  );
54      }
55  }