All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cxf.jaxrs.spring.AbstractSpringComponentScanServer Maven / Gradle / Ivy

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF 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.apache.cxf.jaxrs.spring;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Path;
import javax.ws.rs.ext.Provider;

import org.apache.cxf.annotations.Provider.Scope;
import org.apache.cxf.common.util.ClasspathScanner;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.feature.Feature;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.ResourceProvider;
import org.apache.cxf.message.Message;
import org.apache.cxf.service.factory.ServiceConstructionException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

@ComponentScan(
    includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, 
                                           value = {Path.class, 
                                                    Provider.class,
                                                    org.apache.cxf.annotations.Provider.class})
)
public abstract class AbstractSpringComponentScanServer extends AbstractSpringConfigurationFactory {

    @Value("${cxf.jaxrs.classes-scan-packages:}")
    private String classesScanPackages;
    
    private List resourceProviders = new LinkedList();
    private List jaxrsProviders = new LinkedList();
    private List cxfFeatures = new LinkedList();
    private List> cxfInInterceptors = new LinkedList>();
    private List> cxfOutInterceptors = new LinkedList>();
    private Class serviceAnnotation;
    
    protected AbstractSpringComponentScanServer() {
        
    }
    protected AbstractSpringComponentScanServer(Class serviceAnnotation) {
        this.serviceAnnotation = serviceAnnotation;
    }
    protected void setJaxrsResources(JAXRSServerFactoryBean factory) {
        boolean checkJaxrsRoots = checkJaxrsRoots();
        boolean checkJaxrsProviders = checkJaxrsProviders();
        boolean checkCxfProviders = checkCxfProviders();
        
        for (String beanName : applicationContext.getBeanDefinitionNames()) {
            if (checkJaxrsRoots && isAnnotationAvailable(beanName, Path.class)
                && matchesServiceAnnotation(beanName)) {
                SpringResourceFactory resourceFactory = new SpringResourceFactory(beanName);
                resourceFactory.setApplicationContext(applicationContext);
                resourceProviders.add(resourceFactory);
            } else if (checkJaxrsProviders && isAnnotationAvailable(beanName, Provider.class)
                && matchesServiceAnnotation(beanName)) {
                jaxrsProviders.add(getProviderBean(beanName));
            } else if (checkCxfProviders && isAnnotationAvailable(beanName, 
                org.apache.cxf.annotations.Provider.class) && matchesServiceAnnotation(beanName)) {
                addCxfProvider(getProviderBean(beanName));
            }
        }

        if (!StringUtils.isEmpty(classesScanPackages)) {
            try {
                final Map< Class< ? extends Annotation >, Collection< Class< ? > > > classes =
                    ClasspathScanner.findClasses(classesScanPackages, Provider.class, 
                                                 org.apache.cxf.annotations.Provider.class);
                                          
                jaxrsProviders.addAll(JAXRSServerFactoryBeanDefinitionParser
                    .createBeansFromDiscoveredClasses(super.applicationContext, classes.get(Provider.class), null));
                List cxfProviders = JAXRSServerFactoryBeanDefinitionParser
                    .createBeansFromDiscoveredClasses(super.applicationContext, 
                                                      classes.get(org.apache.cxf.annotations.Provider.class), 
                                                      null);
                for (Object cxfProvider : cxfProviders) {
                    addCxfProvider(cxfProvider);
                }
            } catch (Exception ex) {
                throw new ServiceConstructionException(ex);
            }
        }
        
        factory.setResourceProviders(getResourceProviders());
        factory.setProviders(getJaxrsProviders());
        factory.setFeatures(getFeatures());
        factory.setInInterceptors(getInInterceptors());
        factory.setOutInterceptors(getOutInterceptors());
        
    }
    
    private Object getProviderBean(String beanName) {
        return applicationContext.getBean(beanName);
    }
    protected void addCxfProvider(Object bean) {
        org.apache.cxf.annotations.Provider ann = 
            bean.getClass().getAnnotation(org.apache.cxf.annotations.Provider.class);
        if (ann.scope() == Scope.Client) {
            return;
        }
        if (ann.value() == org.apache.cxf.annotations.Provider.Type.Feature) {
            cxfFeatures.add((Feature)bean);
        } else if (ann.value() == org.apache.cxf.annotations.Provider.Type.InInterceptor) {
            cxfInInterceptors.add((Interceptor)bean);
        } else if (ann.value() == org.apache.cxf.annotations.Provider.Type.OutInterceptor) {
            cxfOutInterceptors.add((Interceptor)bean);
        }
        
    }
    protected boolean matchesServiceAnnotation(String beanName) {
        return serviceAnnotation == null || isAnnotationAvailable(beanName, serviceAnnotation);
    }
    protected  boolean isAnnotationAvailable(String beanName, Class annClass) {
        return applicationContext.findAnnotationOnBean(beanName, annClass) != null;
    }
    
    protected boolean checkCxfProviders() {
        return true;    
    }
    
    protected boolean checkJaxrsProviders() {
        return true;    
    }
    
    protected boolean checkJaxrsRoots() {
        return true;    
    }

    protected List getResourceProviders() {
        return resourceProviders;
    }
    
    protected List getJaxrsProviders() {
        return jaxrsProviders;
    }
    @Override
    public List getFeatures() {
        return cxfFeatures;
    }
    @Override
    public List> getInInterceptors() {
        return cxfInInterceptors;
    }
    public List> getOutInterceptors() {
        return cxfOutInterceptors;
    }
    
}