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

org.apache.camel.component.linkedin.LinkedInComponent Maven / Gradle / Ivy

The newest version!
/*
 * 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.camel.component.linkedin;

import java.io.IOException;
import java.security.GeneralSecurityException;
import javax.net.ssl.SSLContext;

import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.component.linkedin.api.LinkedInOAuthRequestFilter;
import org.apache.camel.component.linkedin.api.OAuthParams;
import org.apache.camel.component.linkedin.api.OAuthSecureStorage;
import org.apache.camel.component.linkedin.internal.CachingOAuthSecureStorage;
import org.apache.camel.component.linkedin.internal.LinkedInApiCollection;
import org.apache.camel.component.linkedin.internal.LinkedInApiName;
import org.apache.camel.spi.annotations.Component;
import org.apache.camel.support.component.AbstractApiComponent;
import org.apache.camel.support.jsse.SSLContextParameters;
import org.apache.camel.util.ObjectHelper;

/**
 * Represents the component that manages {@link LinkedInEndpoint}.
 */
@Component("linkedin")
public class LinkedInComponent extends AbstractApiComponent {

    private LinkedInOAuthRequestFilter requestFilter;

    public LinkedInComponent() {
        super(LinkedInEndpoint.class, LinkedInApiName.class, LinkedInApiCollection.getCollection());
    }

    public LinkedInComponent(CamelContext context) {
        super(context, LinkedInEndpoint.class, LinkedInApiName.class, LinkedInApiCollection.getCollection());
    }

    @Override
    protected LinkedInApiName getApiName(String apiNameStr) throws IllegalArgumentException {
        return LinkedInApiName.fromValue(apiNameStr);
    }

    /**
     * To use the shared configuration
     */
    @Override
    public void setConfiguration(LinkedInConfiguration configuration) {
        super.setConfiguration(configuration);
    }

    /**
     * To use the shared configuration
     */
    @Override
    public LinkedInConfiguration getConfiguration() {
        return super.getConfiguration();
    }

    @Override
    protected Endpoint createEndpoint(String uri, String methodName, LinkedInApiName apiName,
                                      LinkedInConfiguration endpointConfiguration) {
        endpointConfiguration.setApiName(apiName);
        endpointConfiguration.setMethodName(methodName);
        return new LinkedInEndpoint(uri, this, apiName, methodName, endpointConfiguration);
    }

    public synchronized LinkedInOAuthRequestFilter getRequestFilter(LinkedInConfiguration endpointConfiguration) {
        if (endpointConfiguration.equals(configuration)) {
            if (requestFilter == null) {
                requestFilter = createRequestFilter(this.configuration);
            }
            return requestFilter;
        } else {
            return createRequestFilter(endpointConfiguration);
        }
    }

    private LinkedInOAuthRequestFilter createRequestFilter(LinkedInConfiguration configuration) {
        // validate configuration
        configuration.validate();

        final String[] enabledProtocols;
        try {
            // use default SSP to create supported non-SSL protocols list
            final SSLContext sslContext = new SSLContextParameters().createSSLContext(getCamelContext());
            enabledProtocols = sslContext.createSSLEngine().getEnabledProtocols();
        } catch (GeneralSecurityException e) {
            throw RuntimeCamelException.wrapRuntimeCamelException(e);
        } catch (IOException e) {
            throw RuntimeCamelException.wrapRuntimeCamelException(e);
        }
        return new LinkedInOAuthRequestFilter(getOAuthParams(configuration),
            configuration.getHttpParams(), configuration.isLazyAuth(), enabledProtocols);
    }

    private static OAuthParams getOAuthParams(LinkedInConfiguration configuration) {
        OAuthSecureStorage secureStorage = configuration.getSecureStorage();
        if (secureStorage == null && !ObjectHelper.isEmpty(configuration.getAccessToken())) {
            secureStorage = new DefaultOAuthSecureStorage(configuration.getAccessToken(), configuration.getExpiryTime());
        }
        return new OAuthParams(configuration.getUserName(), configuration.getUserPassword(),
            new CachingOAuthSecureStorage(secureStorage), configuration.getClientId(), configuration.getClientSecret(),
            configuration.getRedirectUri(), configuration.getScopes());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy