View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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  /*
35   * To change this license header, choose License Headers in Project Properties.
36   * To change this template file, choose Tools | Templates
37   * and open the template in the editor.
38   */
39  package fr.paris.lutece.plugins.lutecetools.web.rs;
40  
41  import fr.paris.lutece.plugins.lutecetools.business.Stats;
42  import fr.paris.lutece.plugins.lutecetools.service.StatsService;
43  import fr.paris.lutece.plugins.rest.service.RestConstants;
44  import fr.paris.lutece.util.xml.XmlUtil;
45  import java.io.IOException;
46  import javax.ws.rs.GET;
47  import javax.ws.rs.HeaderParam;
48  import javax.ws.rs.Path;
49  import javax.ws.rs.QueryParam;
50  import javax.ws.rs.core.HttpHeaders;
51  import javax.ws.rs.core.MediaType;
52  import javax.ws.rs.core.Response;
53  import com.fasterxml.jackson.databind.ObjectMapper;
54  
55  /**
56   *
57   * @author pierre
58   */
59  @Path( RestConstants.BASE_PATH + Constants.PATH_PLUGIN + Constants.PATH_STATS )
60  public class StatsRest
61  {
62      private static final String KEY_STATS = "stats";
63      private static final String KEY_MAVEN_COUNT = "maven-count";
64      private static final String KEY_GITHUB_COUNT = "github-count";
65      private static final String KEY_GITHUB_OK = "github-ok";
66      private static final String KEY_JIRA_OK = "jira-ok";
67      private static final String KEY_README_OK = "readme-ok";
68  
69      private static final ObjectMapper _mapper = new ObjectMapper( );
70  
71      @GET
72      @Path( "/" )
73      public Response getStats( @HeaderParam( HttpHeaders.ACCEPT ) String strAccept, @QueryParam( Constants.PARAMETER_FORMAT ) String strFormat )
74              throws IOException
75      {
76          String entity;
77          String mediaType = MediaType.APPLICATION_JSON;
78  
79          if ( strFormat != null )
80          {
81              if ( strFormat.equals( Constants.MEDIA_TYPE_XML ) )
82              {
83                  entity = getStatsXml( );
84                  mediaType = MediaType.APPLICATION_XML;
85              }
86              else
87              {
88                  entity = getStatsJson( );
89              }
90          }
91          else
92              if ( ( strAccept != null ) && strAccept.contains( MediaType.APPLICATION_XML ) )
93              {
94                  entity = getStatsXml( );
95                  mediaType = MediaType.APPLICATION_XML;
96              }
97              else
98              {
99                  entity = getStatsJson( );
100             }
101 
102         return Response.ok( entity, mediaType ).build( );
103     }
104 
105     private String getStatsJson( ) throws IOException
106     {
107         Stats stats = StatsService.getStats( );
108         return _mapper.writeValueAsString( stats );
109     }
110 
111     private String getStatsXml( )
112     {
113         Stats stats = StatsService.getStats( );
114         StringBuffer sbXML = new StringBuffer( XmlUtil.getXmlHeader( ) );
115         XmlUtil.beginElement( sbXML, KEY_STATS );
116         XmlUtil.addElement( sbXML, KEY_MAVEN_COUNT, stats.getMavenCount( ) );
117         XmlUtil.addElement( sbXML, KEY_GITHUB_COUNT, stats.getGithubCount( ) );
118         XmlUtil.addElement( sbXML, KEY_GITHUB_OK, stats.getGithubOK( ) );
119         XmlUtil.addElement( sbXML, KEY_JIRA_OK, stats.getJiraOK( ) );
120         XmlUtil.addElement( sbXML, KEY_README_OK, stats.getReadmeOK( ) );
121         XmlUtil.endElement( sbXML, KEY_STATS );
122 
123         return sbXML.toString( );
124     }
125 
126 }