001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.auth.login;
020
021import org.apache.wiki.api.core.Engine;
022
023import javax.security.auth.callback.Callback;
024import javax.security.auth.callback.CallbackHandler;
025import javax.security.auth.callback.UnsupportedCallbackException;
026import javax.servlet.http.HttpServletRequest;
027import java.io.IOException;
028
029
030/**
031 * Handles logins made from within JSPWiki.
032 *
033 * @see org.apache.wiki.api.spi.SessionSPI#find(Engine, HttpServletRequest)
034 * @since 2.3
035 */
036public final class WebContainerCallbackHandler implements CallbackHandler {
037
038    private final HttpServletRequest m_request;
039    private final Engine m_engine;
040
041    /**
042     * Create a new handler.
043     *
044     * @param engine  The Engine
045     * @param request The request to look into
046     */
047    public WebContainerCallbackHandler( final Engine engine, final HttpServletRequest request ) {
048        m_engine = engine;
049        m_request = request;
050    }
051
052    /**
053     * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
054     * 
055     * {@inheritDoc}
056     */
057    @Override
058    public void handle( final Callback[] callbacks ) throws IOException, UnsupportedCallbackException {
059        for( final Callback callback : callbacks ) {
060            if( callback instanceof HttpRequestCallback ) {
061                ( ( HttpRequestCallback )callback ).setRequest( m_request );
062            } else if( callback instanceof WikiEngineCallback ) {
063                ( ( WikiEngineCallback )callback ).setEngine( m_engine );
064            } else {
065                throw new UnsupportedCallbackException( callback );
066            }
067        }
068    }
069
070}