/**
 * Licensed to the Austrian Association for Software Tool Integration (AASTI)
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. The AASTI licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.core.security.filter;

import java.util.Map;

import org.apache.shiro.authc.AuthenticationException;
import org.openengsb.core.api.OsgiUtilsService;
import org.openengsb.core.api.remote.FilterAction;
import org.openengsb.core.api.remote.FilterConfigurationException;
import org.openengsb.core.api.remote.FilterException;
import org.openengsb.core.api.security.Credentials;
import org.openengsb.core.api.security.model.SecureRequest;
import org.openengsb.core.api.security.model.SecureResponse;
import org.openengsb.core.common.remote.AbstractFilterChainElement;
import org.openengsb.core.security.SecurityContext;
import org.openengsb.labs.delegation.service.ClassProvider;
import org.openengsb.labs.delegation.service.Constants;
import org.osgi.framework.Filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This filter does no actual transformation. It takes a {@link SecureRequest} extracts the
 * {@link org.apache.shiro.authc.AuthenticationInfo} and tries to authenticate. If authentication was successful, the
 * filter-chain will proceed. The result of the next filter is just passed through.
 *
 * This filter is intended for incoming ports.
 *
 * <code>
 * <pre>
 *      [SecureRequest]  > Filter > [SecureRequest]    > ...
 *                                                        |
 *                                                        v
 *      [SecureResponse] < Filter < [SecureResponse]   < ...
 * </pre>
 * </code>
 */
public class MessageAuthenticatorFilter extends AbstractFilterChainElement<SecureRequest, SecureResponse> {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageAuthenticatorFilter.class);

    private OsgiUtilsService utilsService;
    private FilterAction next;

    public MessageAuthenticatorFilter(OsgiUtilsService utilsService) {
        this.utilsService = utilsService;
    }

    @Override
    protected SecureResponse doFilter(SecureRequest input, Map<String, Object> metaData) {
        LOGGER.debug("recieved authentication info: " + input.getPrincipal() + " " + input.getCredentials());

        String className = input.getCredentials().getClassName();
        Class<? extends Credentials> credentialType;
        try {
            credentialType = loadCredentialsType(className);
        } catch (ClassNotFoundException e) {
            throw new FilterException(e);
        }
        try {
            SecurityContext.login(input.getPrincipal(), input.getCredentials().toObject(credentialType));
        } catch (AuthenticationException e) {
            throw new FilterException(e);
        }

        LOGGER.debug("authenticated");
        return (SecureResponse) next.filter(input, metaData);
    }

    @SuppressWarnings("unchecked")
    private Class<? extends Credentials> loadCredentialsType(String className)
        throws ClassNotFoundException {
        Filter filter =
            utilsService.makeFilter(ClassProvider.class,
                String.format("(%s=%s)", Constants.PROVIDED_CLASSES_KEY, className));
        Class<? extends Credentials> credentialType;
        credentialType =
            (Class<? extends Credentials>) utilsService.getOsgiServiceProxy(filter, ClassProvider.class)
                .loadClass(className);
        return credentialType;
    }

    @Override
    public void setNext(FilterAction next) throws FilterConfigurationException {
        checkNextInputAndOutputTypes(next, SecureRequest.class, SecureResponse.class);
        this.next = next;
    }

}

@@ -1564,25 +1564,30 @@
 service.
-Constants
+DelegationUtil
 ;%0Aimport
@@ -3729,139 +3729,50 @@
 er =
-%0A            utilsService.makeFilter(ClassProvider.class,%0A                String.format(%22(%25s=%25s)%22, Constants.PROVIDED_CLASSES_KEY, 
+ DelegationUtil.createClassProviderFilter(
 clas
@@ -3777,17 +3777,16 @@
 assName)
-)
 ;%0A      
@@ -3791,89 +3791,14 @@
     
-Class%3C? extends Credentials%3E credentialType;%0A        credentialType =%0A           
+return
  (Cl
@@ -3899,20 +3899,16 @@
         
-    
 .loadCla
@@ -3926,39 +3926,8 @@
 e);%0A
-        return credentialType;%0A
     

use utils-class for loading credential-type in messageAuthenticatorFilter