001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.security.auth.http;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.registry.Registry;
019    import com.liferay.registry.RegistryUtil;
020    import com.liferay.registry.ServiceTracker;
021    
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
025    /**
026     * @author Tomas Polesovsky
027     */
028    public class HttpAuthManagerUtil {
029    
030            public static void generateChallenge(
031                    HttpServletRequest httpServletRequest,
032                    HttpServletResponse httpServletResponse,
033                    HttpAuthorizationHeader httpAuthorizationHeader) {
034    
035                    getHttpAuthManager().generateChallenge(
036                            httpServletRequest, httpServletResponse, httpAuthorizationHeader);
037            }
038    
039            public static long getBasicUserId(HttpServletRequest httpServletRequest)
040                    throws PortalException {
041    
042                    return getHttpAuthManager().getBasicUserId(httpServletRequest);
043            }
044    
045            public static long getDigestUserId(HttpServletRequest httpServletRequest)
046                    throws PortalException {
047    
048                    return getHttpAuthManager().getDigestUserId(httpServletRequest);
049            }
050    
051            public static long getUserId(
052                            HttpServletRequest httpServletRequest,
053                            HttpAuthorizationHeader httpAuthorizationHeader)
054                    throws PortalException {
055    
056                    return getHttpAuthManager().getUserId(
057                            httpServletRequest, httpAuthorizationHeader);
058            }
059    
060            public static HttpAuthorizationHeader parse(
061                    HttpServletRequest httpServletRequest) {
062    
063                    return getHttpAuthManager().parse(httpServletRequest);
064            }
065    
066            private static HttpAuthManager getHttpAuthManager() {
067                    return _instance._serviceTracker.getService();
068            }
069    
070            private HttpAuthManagerUtil() {
071                    Registry registry = RegistryUtil.getRegistry();
072    
073                    _serviceTracker = registry.trackServices(HttpAuthManager.class);
074    
075                    _serviceTracker.open();
076            }
077    
078            private static final HttpAuthManagerUtil _instance =
079                    new HttpAuthManagerUtil();
080    
081            private final ServiceTracker<?, HttpAuthManager> _serviceTracker;
082    
083    }