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.solrserver;
35  
36  import fr.paris.lutece.portal.business.user.AdminUser;
37  import fr.paris.lutece.portal.service.admin.AdminUserService;
38  import fr.paris.lutece.portal.service.util.AppPropertiesService;
39  
40  import org.apache.solr.common.util.FastInputStream;
41  import org.apache.solr.servlet.SolrDispatchFilter;
42  
43  import java.io.IOException;
44  
45  import javax.servlet.Filter;
46  import javax.servlet.FilterChain;
47  import javax.servlet.FilterConfig;
48  import javax.servlet.ServletException;
49  import javax.servlet.ServletInputStream;
50  import javax.servlet.ServletRequest;
51  import javax.servlet.ServletResponse;
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletRequestWrapper;
54  
55  
56  public class SolrServerFilter extends SolrDispatchFilter
57  {
58      public static final String SOLR_DATA_DIR = "solr.data.dir";
59      public static final String SOLR_HOME_LABEL = "solr.solr.home";
60      public static final String SOLR_URI = AppPropertiesService.getProperty( "solrserver.solr.uri" );
61      public static final String SOLR_URI_UPDATE = AppPropertiesService.getProperty( "solrserver.solr.uri.update" );
62      public static final String SOLR_URI_SELECT = AppPropertiesService.getProperty( "solrserver.solr.uri.select" );
63      public static final String SOLR_URI_AUTOCOMPLETE = AppPropertiesService.getProperty( "solrserver.solr.uri.autoComplete" );
64      
65      public static final String SOLR_HOME = AppPropertiesService.getProperty( "solrserver.solr.home" );
66      public static final String SOLR_ABSOLUTE_DATA = AppPropertiesService.getProperty( "solrserver.solr.absolute.data" );
67      public static final String SOLR_RELATIVE_DATA = AppPropertiesService.getProperty( "solrserver.solr.relative.data" );
68      public static final String SOLR_ADMIN_CLIENT = AppPropertiesService.getProperty("solrserver.solr.host.client", "127.0.0.1" );
69     // private SolrDispatchFilter solrDispatchFilter = new SolrDispatchFilter(  );
70  
71      @Override
72      public  void init( FilterConfig filterConfig ) throws ServletException
73      {
74          String realPath = filterConfig.getServletContext(  ).getRealPath( "/" );
75  
76          System.setProperty( SOLR_HOME_LABEL, realPath + SOLR_HOME );
77  
78          if ( ( SOLR_ABSOLUTE_DATA == null ) || ( SOLR_ABSOLUTE_DATA.length(  ) == 0 ) )
79          {
80              System.setProperty( SOLR_DATA_DIR, realPath + SOLR_RELATIVE_DATA );
81          }
82          else
83          {
84              System.setProperty( SOLR_DATA_DIR, SOLR_ABSOLUTE_DATA );
85          }
86  
87          super.init( filterConfig );
88      }
89      @Override
90      public  void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
91          throws IOException, ServletException
92      {
93          String strURI = ( (HttpServletRequest) request ).getRequestURI(  );
94          boolean bCallSolr = false;
95        
96          if ( strURI.indexOf( SOLR_URI_UPDATE ) > 0 )
97          {
98              AdminUser adminUser = AdminUserService.getAdminUser( (HttpServletRequest) request );
99              String strRemoteAddr = ( (HttpServletRequest) request ).getRemoteAddr(  );
100 
101             if ( ( adminUser != null ) || ( strRemoteAddr.compareTo( SOLR_ADMIN_CLIENT ) == 0 ) )
102             {
103                 bCallSolr = true;
104             }
105         }
106         else if ( strURI.indexOf( SOLR_URI_SELECT ) > 0 )
107         {
108             bCallSolr = true;
109         }
110         else if ( strURI.indexOf( SOLR_URI_AUTOCOMPLETE ) > 0 )
111         {
112             bCallSolr = true;
113         }
114 
115         if ( bCallSolr ) {
116             request = new HttpServletRequestWrapper((HttpServletRequest)request) {
117                 @Override public String getServletPath() {
118                     String path = ((HttpServletRequest) getRequest()).getServletPath();
119                     path = path.substring( SOLR_URI.length() );
120                     path = "/collection1" + path;
121 
122                     return path;
123                 };
124             };
125             super.doFilter( request, response, chain );
126         }
127         
128     }
129     @Override
130     public  void destroy(  )
131     {
132         super.destroy(  );
133     }
134 }