View Javadoc

1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.plugins.elasticsearch.modules.statsfilter.service;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  
38  import org.elasticsearch.action.index.IndexResponse;
39  
40  import org.elasticsearch.client.Client;
41  
42  import org.elasticsearch.common.xcontent.XContentBuilder;
43  import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
44  
45  import org.elasticsearch.node.Node;
46  import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
47  
48  import java.io.IOException;
49  
50  import javax.servlet.Filter;
51  import javax.servlet.FilterChain;
52  import javax.servlet.FilterConfig;
53  import javax.servlet.ServletException;
54  import javax.servlet.ServletRequest;
55  import javax.servlet.ServletResponse;
56  import javax.servlet.http.HttpServletRequest;
57  
58  /**
59   *
60   */
61  public class StatsFilter implements Filter
62  {
63      private static Boolean _bInit = Boolean.FALSE;
64      private Node _node;
65      private Client _client;
66  
67      @Override
68      public void init( FilterConfig fc ) throws ServletException
69      {
70          _node = nodeBuilder(  ).clusterName( Constants.CLUSTER_NAME ).node(  );
71          _client = _node.client(  );
72      }
73  
74      private void initIndex(  )
75      {
76          //        synchronized (_bInit)
77          {
78              if ( !_bInit )
79              {
80                  if ( ElasticsearchUtils.isIndexExist( _client, Constants.INDEX_NAME ) )
81                  {
82                      AppLogService.info( "Elasticsearch index '" + Constants.INDEX_NAME +
83                          "' exists, should delete and recreate it !" );
84                      ElasticsearchUtils.deleteIndex( _client, Constants.INDEX_NAME );
85                      ElasticsearchUtils.createIndex( _client, Constants.INDEX_NAME, Constants.DOCUMENT_TYPE );
86                  }
87                  else
88                  {
89                      AppLogService.info( "Elasticsearch index '" + Constants.INDEX_NAME + "' doesn't exist, should create it !" );
90                      ElasticsearchUtils.createIndex( _client, Constants.INDEX_NAME, Constants.DOCUMENT_TYPE );
91                  }
92  
93                  _bInit = Boolean.TRUE;
94              }
95          }
96      }
97  
98      @Override
99      public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
100         throws IOException, ServletException
101     {
102         HttpServletRequest r = (HttpServletRequest) request;
103         System.out.println( r.getRequestURI(  ) + r.getQueryString(  ) );
104 
105         initIndex();
106         XContentBuilder jsonSource = jsonBuilder(  ).startObject(  ).field( "method", r.getMethod(  ) )
107                                    .field( "uri", r.getRequestURI(  ) ).field( "query_string", r.getQueryString(  ) )
108                                    .endObject(  );
109 
110         IndexResponse ir = _client.prepareIndex( Constants.INDEX_NAME, Constants.DOCUMENT_TYPE ).setSource( jsonSource.string(  ) ).execute(  )
111                                   .actionGet(  );
112 
113         chain.doFilter( request, response );
114     }
115 
116     @Override
117     public void destroy(  )
118     {
119         _node.close(  );
120     }
121 }