Changeset 7265


Ignore:
Timestamp:
01/05/22 22:26:06 (2 years ago)
Author:
Oliver Schonefeld
Message:
  • experimental support for processing authenticated requests
Location:
SRUServer/trunk/src/main/java/eu/clarin/sru/server
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRURequest.java

    r6934 r7265  
    2828 */
    2929public interface SRURequest {
     30    /**
     31     * Get the authentication information that was extracted from the the
     32     * request.
     33     *
     34     * @return the request authentication or <code>null</code>
     35     */
     36    public SRUAuthenticationInfo getAuthentication();
     37
     38
     39    /**
     40     * Get the subject of the request.
     41     *
     42     * @return the subject of the request or <code>null</code>
     43     */
     44    public String getAuthenticationSubject();
     45
    3046
    3147    /**
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRURequestImpl.java

    r6934 r7265  
    8080    private final SRUServerConfig config;
    8181    private final SRUQueryParserRegistry queryParsers;
     82    private final SRUAuthenticationInfoProvider authenticationInfoProvider;
    8283    private final HttpServletRequest request;
     84    private SRUAuthenticationInfo authenticationInfo;
    8385    private List<SRUDiagnostic> diagnostics;
    8486    private SRUOperation operation;
     
    249251    SRURequestImpl(SRUServerConfig config,
    250252            SRUQueryParserRegistry queryParsers,
     253            SRUAuthenticationInfoProvider authenticationInfoProvider,
    251254            HttpServletRequest request) {
    252         this.config       = config;
     255        this.config = config;
    253256        this.queryParsers = queryParsers;
    254         this.request      = request;
     257        this.authenticationInfoProvider = authenticationInfoProvider;
     258        this.request = request;
    255259    }
    256260
     
    676680            }
    677681
    678 
    679682            /*
    680683             *  check if any parameters where not consumed and
     
    694697
    695698        /*
     699         * extract authentication information from, if an authentication provider is set
     700         */
     701        if (authenticationInfoProvider != null) {
     702            try {
     703                authenticationInfo = authenticationInfoProvider.getAuthenticationInfo(request);
     704            } catch (SRUException e) {
     705                addDiagnostic(e.getDiagnostic());
     706            }
     707        }
     708
     709        /*
    696710         *  diagnostics == null -> consider as success
    697          *  FIXME: this should ne done nicer!
     711         *  FIXME: this should be done nicer!
    698712         */
    699713        return (diagnostics == null);
     
    773787
    774788    @Override
     789    public SRUAuthenticationInfo getAuthentication() {
     790        return authenticationInfo;
     791    }
     792
     793
     794    @Override
     795    public String getAuthenticationSubject() {
     796        if (authenticationInfo != null) {
     797            return authenticationInfo.getSubject();
     798        } else {
     799            return null;
     800        }
     801    }
     802
     803
     804    @Override
    775805    public SRUOperation getOperation() {
    776806        return operation;
     
    10141044    @Override
    10151045    public void addDiagnostic(String uri, String details, String message) {
    1016         final SRUDiagnostic diagnostic =
    1017                 new SRUDiagnostic(uri, details, message);
     1046        final SRUDiagnostic diagnostic = new SRUDiagnostic(uri, details,
     1047                message);
     1048        addDiagnostic(diagnostic);
     1049    }
     1050
     1051
     1052    private void addDiagnostic(SRUDiagnostic diagnostic) {
    10181053        if (diagnostics == null) {
    10191054            diagnostics = new ArrayList<SRUDiagnostic>();
     
    10211056        diagnostics.add(diagnostic);
    10221057    }
    1023 
    1024 
     1058   
     1059   
    10251060    private List<String> getParameterNames() {
    10261061        List<String> list = new ArrayList<String>();
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUServer.java

    r6934 r7265  
    5959    private final SRUServerConfig config;
    6060    private final SRUQueryParserRegistry queryParsers;
     61    private final SRUAuthenticationInfoProvider authenticationInfoProvider;
    6162    private final SRUSearchEngine searchEngine;
    6263    private final XMLOutputFactory writerFactory;
     
    7071     * @param queryParsers
    7172     *            a {@link SRUQueryParserRegistry} object
     73     * @param authenticationInfoProvider
     74     *            a {@link SRUAuthenticationInfoProvider} or <code>null</code>
    7275     * @param searchEngine
    7376     *            an object implementing the {@link SRUSearchEngine} interface
     
    7578     *             if config, queryParserRegistry or searchEngine is
    7679     *             <code>null</code>
    77      * @throws SRUException
    78      *             if an error occurred
     80     * @throws SRUConfigException
     81     *             if an error occurred with configuration or initialization of the server
    7982     */
    8083    public SRUServer(SRUServerConfig config,
    8184            SRUQueryParserRegistry queryParsers,
    82             SRUSearchEngine searchEngine) throws SRUException {
     85            SRUAuthenticationInfoProvider authenticationInfoProvider,
     86            SRUSearchEngine searchEngine) throws SRUConfigException {
    8387        if (config == null) {
    8488            throw new NullPointerException("config == null");
     
    9599        }
    96100        this.searchEngine = searchEngine;
     101
     102        this.authenticationInfoProvider = authenticationInfoProvider;
    97103
    98104        this.writerFactory = XMLOutputFactory.newInstance();
     
    111117            HttpServletResponse response) {
    112118        final SRURequestImpl req =
    113                 new SRURequestImpl(config, queryParsers, request);
     119                new SRURequestImpl(config, queryParsers, authenticationInfoProvider, request);
    114120        try {
    115121            // set response properties
  • SRUServer/trunk/src/main/java/eu/clarin/sru/server/utils/SRUServerServlet.java

    r6934 r7265  
    2222import java.net.MalformedURLException;
    2323import java.net.URL;
     24import java.util.Collections;
    2425import java.util.Enumeration;
    2526import java.util.HashMap;
     
    3637import org.slf4j.LoggerFactory;
    3738
     39import eu.clarin.sru.server.SRUAuthenticationInfoProvider;
    3840import eu.clarin.sru.server.SRUConfigException;
    39 import eu.clarin.sru.server.SRUException;
    4041import eu.clarin.sru.server.SRUQueryParserRegistry;
    4142import eu.clarin.sru.server.SRUServer;
     
    145146         * get init-parameters from ServletConfig ...
    146147         */
    147         final HashMap<String, String> params = new HashMap<String, String>();
     148        Map<String, String> params = new HashMap<>();
    148149        for (Enumeration<?> i = cfg.getInitParameterNames();
    149150                i.hasMoreElements();) {
     
    204205                    contextPath);
    205206        }
    206 
    207         // parse configuration
    208         SRUServerConfig sruServerConfig;
    209         try {
    210             sruServerConfig =
    211                     SRUServerConfig.parse(params, sruServerConfigFile);
    212         } catch (SRUConfigException e) {
    213             throw new ServletException("sru server configuration is invalid", e);
    214         }
     207        // seal parameters against tampering ...
     208        params = Collections.unmodifiableMap(params);
    215209
    216210        /*
    217          * create an instance of the search engine ...
     211         * now go ahead and setup everything ...
    218212         */
    219213        try {
    220             logger.debug("creating new search engine from class {}",
    221                     sruServerSearchEngineClass);
    222             @SuppressWarnings("unchecked")
    223             Class<SRUSearchEngineBase> clazz = (Class<SRUSearchEngineBase>)
    224                 Class.forName(sruServerSearchEngineClass);
    225             Constructor<SRUSearchEngineBase> constructor =
    226                     clazz.getConstructor();
    227             searchEngine = constructor.newInstance();
    228         } catch (ClassNotFoundException e) {
    229             throw new ServletException("error initializing sru server", e);
    230         } catch (ClassCastException e) {
    231             throw new ServletException("error initializing sru server", e);
    232         } catch (NoSuchMethodException e) {
    233             throw new ServletException("error initializing sru server", e);
    234         } catch (SecurityException e) {
    235             throw new ServletException("error initializing sru server", e);
    236         } catch (InstantiationException e) {
    237             throw new ServletException("error initializing sru server", e);
    238         } catch (IllegalAccessException e) {
    239             throw new ServletException("error initializing sru server", e);
    240         } catch (IllegalArgumentException e) {
    241             throw new ServletException("error initializing sru server", e);
    242         } catch (InvocationTargetException e) {
    243             throw new ServletException("error initializing sru server", e);
    244         }
    245 
    246         /*
    247          * finally initialize the SRU server ...
    248          */
    249         try {
     214            /*
     215             * parse SRU server configuration
     216             */
     217            SRUServerConfig sruServerConfig =
     218                    SRUServerConfig.parse(params, sruServerConfigFile);
     219
     220            /*
     221             * create an instance of the search engine ...
     222             */
     223            try {
     224                logger.debug("creating new search engine from class {}",
     225                        sruServerSearchEngineClass);
     226                @SuppressWarnings("unchecked")
     227                Class<SRUSearchEngineBase> clazz = (Class<SRUSearchEngineBase>)
     228                    Class.forName(sruServerSearchEngineClass);
     229                Constructor<SRUSearchEngineBase> constructor =
     230                        clazz.getConstructor();
     231                searchEngine = constructor.newInstance();
     232            } catch (ClassNotFoundException |
     233                    NoSuchMethodException |
     234                    SecurityException |
     235                    InstantiationException |
     236                    IllegalAccessException |
     237                    IllegalArgumentException |
     238                    InvocationTargetException e) {
     239                throw new SRUConfigException("error creating search engine", e);
     240            }
     241
     242            /*
     243             * initialize search engine ...
     244             */
    250245            final SRUQueryParserRegistry.Builder builder =
    251246                    new SRUQueryParserRegistry.Builder();
    252247            searchEngine.init(ctx, sruServerConfig, builder, params);
    253             final SRUQueryParserRegistry parsers = builder.build();
    254             sruServer = new SRUServer(sruServerConfig, parsers, searchEngine);
     248
     249            /*
     250             * create authentication provider
     251             */
     252            SRUAuthenticationInfoProvider authenticationProvider = null;
     253            if (SRUAuthenticationInfoProviderFactory.class.isInstance(searchEngine)) {
     254                logger.debug("creating new authentication info provider");
     255
     256                authenticationProvider = ((SRUAuthenticationInfoProviderFactory) searchEngine)
     257                        .createAuthenticationInfoProvider(ctx, params);
     258            }
     259
     260            /*
     261             * finally create the sru server ...
     262             */
     263            sruServer = new SRUServer(sruServerConfig,
     264                    builder.build(),
     265                    authenticationProvider,
     266                    searchEngine);
    255267        } catch (SRUConfigException e) {
    256             throw new ServletException("error initializing sru server", e);
    257         } catch (SRUException e) {
    258             throw new ServletException("error initializing sru server", e);
     268            String msg = (e.getMessage() != null)
     269                        ? "error configuring or inializing the server: " + e.getMessage()
     270                        : "error configuring or inializing the server";
     271            throw new ServletException(msg, e);
    259272        }
    260273    }
Note: See TracChangeset for help on using the changeset viewer.