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 java.io.IOException;
022
023import javax.security.auth.callback.Callback;
024import javax.security.auth.callback.CallbackHandler;
025import javax.security.auth.callback.UnsupportedCallbackException;
026import javax.servlet.http.HttpServletRequest;
027
028import org.apache.wiki.WikiEngine;
029
030/**
031 * Handles logins made from within JSPWiki.
032 * @see org.apache.wiki.WikiSession#getWikiSession(WikiEngine,HttpServletRequest)
033 * @since 2.3
034 */
035public final class WebContainerCallbackHandler implements CallbackHandler
036{
037    private final HttpServletRequest m_request;
038
039    private final WikiEngine         m_engine;
040
041    /**
042     *  Create a new handler.
043     *  
044     *  @param engine The WikiEngine
045     *  @param request The request to look into
046     */
047    public WebContainerCallbackHandler( WikiEngine engine, HttpServletRequest request )
048    {
049        m_engine  = engine;
050        m_request = request;
051    }
052
053    /**
054     * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
055     * 
056     * {@inheritDoc}
057     */
058    public void handle( Callback[] callbacks ) throws IOException, UnsupportedCallbackException
059    {
060        for( int i = 0; i < callbacks.length; i++ )
061        {
062            Callback callback = callbacks[i];
063            if ( callback instanceof HttpRequestCallback )
064            {
065                ( (HttpRequestCallback) callback ).setRequest( m_request );
066            }
067            else if( callback instanceof WikiEngineCallback )
068            {
069                ( (WikiEngineCallback) callback ).setEngine( m_engine );
070            }
071            else
072            {
073                throw new UnsupportedCallbackException( callback );
074            }
075        }
076    }
077
078}