Run Spring Boot Scheduler as Heroku Worker

1. Setup Application
2. Setup Scheduler
3. Add Spring Boot dependency
4. Add Procfile
5. Scale Heroku worker

1. Setup Application

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * Created by tmichels on 9/20/15.
 */

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

2. Setup Scheduler

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * Created by tmichels on 9/18/15.
 */

@Component
public class Scheduler {

    private static final Logger logger = LoggerFactory.getLogger(Scheduler.class);

    @Scheduled(cron = "0 0 13 * * *")
    public void buyHighPercentageReturnNotes(){
       System.out.println("Scheduler is running");
    }
}

3. Add Spring Boot dependency

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.6.RELEASE</version>
    </parent>
  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
      </dependency>
  </dependencies>

4. Add Procfile

worker: java $JAVA_OPTS -jar target/*.jar

5. Scale Heroku worker

heroku ps:scale worker=1

Microsoft Bing Adwords Java Integration

Step 1: Setup Bing BulkServiceManager Configuration

package com.integration.adwords.config;

import com.microsoft.bingads.*;
import com.microsoft.bingads.bulk.BulkServiceManager;
import com.microsoft.bingads.campaignmanagement.ICampaignManagementService;
import com.microsoft.bingads.internal.OAuthService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by tmichels on 7/31/15.
 */
@Configuration
public class BingAdwordsConfig {

    @Value("${BING_ADS_CLIENTID}")
    private String bingAdsClientId;

    @Value("${BING_ADS_CLIENT_SECRET}")
    private String bingAdsClientSecret;

    @Value("${BING_ADS_ACCOUNTID}")
    private String bingAdsAccountId;

    @Value("${BING_ADS_CUSTOMERID}")
    private String bingAdsCustomerId;

    @Value("${BING_ADS_DEVELOPER_TOKEN}")
    private String bingAdsDeveloperToken;

    @Value("${BING_ADS_USERNAME}")
    private String bingAdsUsername;

    @Value("${BING_ADS_PASSWORD}")
    private String bingAdsPassword;

    @Value("${BING_ADS_REFRESH_TOKEN}")
    private String bingAdsRefreshToken;


    @Bean
    public OAuthDesktopMobileAuthCodeGrant oAuthWebAuthCodeGrant() throws MalformedURLException {
        OAuthDesktopMobileAuthCodeGrant oAuthWebAuthCodeGrant = new OAuthDesktopMobileAuthCodeGrant(bingAdsClientId, bingAdsRefreshToken);
        return oAuthWebAuthCodeGrant;
    }
    
    @Bean
    public AuthorizationData authorizationData(OAuthDesktopMobileAuthCodeGrant authentication){
        AuthorizationData authorizationData = new AuthorizationData();
        authorizationData.setAuthentication(authentication);
        authorizationData.setCustomerId(new Long(bingAdsCustomerId));
        authorizationData.setAccountId(new Long(bingAdsAccountId));
        authorizationData.setDeveloperToken(bingAdsDeveloperToken);
        return authorizationData;
    }

    @Bean
    public BulkServiceManager bulkServiceManager(AuthorizationData authorizationData){
        BulkServiceManager bulkServiceManager = new BulkServiceManager(authorizationData);
        bulkServiceManager.setStatusPollIntervalInMilliseconds(5000);
        return bulkServiceManager;
    }
}

Step 2: Setup Bing Adwords Service

package com.integration.adwords.service;

import com.microsoft.bingads.AuthorizationData;
import com.microsoft.bingads.bulk.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

/**
 * Created by tmichels on 7/31/15.
 */

@Service
public class BingAdwordService {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(BingAdwordService.class);

    private static final String OUTPUTFILENAME="bingAdwords.csv";
    private static final String FILEDIRECTORY="adwords/bing/";

    @Autowired
    BulkServiceManager bulkService;

    public void submitAndDownloadWithBulkServiceManager() throws IOException, URISyntaxException {
        try {
            logger.info("Start BingAdwordService submitAndDownloadWithBulkServiceManager");
            SubmitDownloadParameters submitDownloadParameters = new SubmitDownloadParameters();
            submitDownloadParameters.setCampaignIds(null);
            ArrayList<DataScope> dataScope = new ArrayList<DataScope>();
            dataScope.add(DataScope.ENTITY_DATA);
            submitDownloadParameters.setDataScope(dataScope);
            submitDownloadParameters.setFileType(DownloadFileType.CSV);
            submitDownloadParameters.setLastSyncTimeInUTC(null);
            ArrayList<BulkDownloadEntity> bulkDownloadEntities = new ArrayList<BulkDownloadEntity>();
            bulkDownloadEntities.add(BulkDownloadEntity.CAMPAIGNS);
            bulkDownloadEntities.add(BulkDownloadEntity.AD_GROUPS);
            bulkDownloadEntities.add(BulkDownloadEntity.KEYWORDS);
            bulkDownloadEntities.add(BulkDownloadEntity.ADS);
            submitDownloadParameters.setEntities(bulkDownloadEntities);

            BulkDownloadOperation bulkDownloadOperation = bulkService.submitDownloadAsync(submitDownloadParameters, null).get();

            BulkOperationStatus<DownloadStatus> downloadStatus;
            int waitTime = 5000;

            for (int i = 0; i < 24; i++) {
                Thread.sleep(waitTime);

                downloadStatus = bulkDownloadOperation.getStatusAsync(null).get();

                if (downloadStatus.getStatus() == DownloadStatus.COMPLETED) {
                    break;
                }
            }

            File resultFile = bulkDownloadOperation.downloadResultFileAsync(
                    new File(FILEDIRECTORY),
                    OUTPUTFILENAME,
                    true,
                    true,  // Set this value true if you want to overwrite the same file.
                    null).get();

            logger.info("Download BingAdwords file success %s\n", resultFile.getName());

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

Apache Camel Kafka Spring Integration

1. Configure Camel Kafka Component

package com.integration.camel.component;

import org.apache.camel.component.kafka.KafkaComponent;
import org.apache.camel.component.kafka.KafkaConfiguration;
import org.apache.camel.component.kafka.KafkaEndpoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 9/3/15.
 */

@Configuration
public class KafkaCamelComponent {
    
    @Bean
    public KafkaEndpoint kafkaEndpoint(){
        KafkaEndpoint kafkaEndpoint = new KafkaEndpoint();
        kafkaEndpoint.setZookeeperHost("localhost");
        kafkaEndpoint.setZookeeperPort(2181);
        kafkaEndpoint.setTopic("test");
        return kafkaEndpoint;
    }

    @Bean
    public KafkaComponent kafkaComponent(KafkaEndpoint kafkaEndpoint){
        KafkaComponent kafkaComponent = new KafkaComponent();
        kafkaComponent.setEndpointClass(kafkaEndpoint.getClass());
        return kafkaComponent;
    }
}

2. Configure Kafka Consume and Producer Route

package com.integration.camel.route;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 9/3/15.
 */

@Configuration
public class KafkaCamelRoute {

    @Bean(name = "KafkaRouteProducer")
    public RouteBuilder kafkaRouteProducer() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:kafkaRoute").to("kafka:localhost:9092?topic=test&zookeeperHost=localhost&zookeeperPort=2181&groupId=group1&serializerClass=kafka.serializer.StringEncoder").bean(kafkaOutputBean.class);
            }
        };
    }

    @Bean(name="KafkaRouteConsumer")
    public RouteBuilder kafkaRouteConsumer() {
        return new RouteBuilder() {
            public void configure() {
                from("kafka:localhost:9092?topic=test&zookeeperHost=localhost&zookeeperPort=2181&groupId=group1&serializerClass=kafka.serializer.StringEncoder").bean(kafkaOutputBean.class);
            }
        };
    }

    public static class kafkaOutputBean {
        public void printKafkaBody(String body) {
            System.out.println("KafkaBody result >>>>> " + body);
        }
    }
}

3. Start Kafka test by adding routes to CamelContext and starting

package com.integration.camel.route;

import com.integration.camel.component.KafkaCamelComponent;
import com.integration.camel.context.CamelContextConfig;
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.kafka.KafkaComponent;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by tmichels on 9/3/15.
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {KafkaCamelComponent.class,
                                KafkaCamelRoute.class,
                                CamelContextConfig.class})
public class KafkaCamelIntegrationTest {

    @Autowired
    CamelContext camelContext;

    @Autowired
    KafkaComponent kafkaComponent;

    @Autowired
    @Qualifier("KafkaRouteProducer")
    RouteBuilder kafkaRouteProducer;

    @Autowired
    @Qualifier("KafkaRouteConsumer")
    RouteBuilder kafkaRouteConsumer;

    @EndpointInject(uri = "direct:kafkaRoute")
    ProducerTemplate kafkaProducer;
    
    @Before
    public void before() throws Exception {
        camelContext.addRoutes(kafkaRouteProducer);
        camelContext.addRoutes(kafkaRouteConsumer);
    }

    @Test
    public void testKafkaRoute(){
        kafkaProducer.sendBody("direct:KafkaRouteProducer", "testKafkaMessage");
        try {
            camelContext.start();
            camelContext.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Google Adwords Java Integration

Step 1: Create Google AdwordSession configuration

package com.integration.adwords.config;

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration;
import com.google.api.ads.common.lib.auth.OfflineCredentials;
import com.google.api.ads.common.lib.exception.OAuthException;
import com.google.api.ads.common.lib.exception.ValidationException;
import com.google.api.client.auth.oauth2.Credential;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 7/31/15.
 */

@Configuration
public class GoogleAdwordsConfig {

    @Value("${GOOGLE_ADWORD_CLIENTID}")
    private String googleAdWordClientId;

    @Value("${GOOGLE_ADWORD_SECRET}")
    private String googleAdwordSecet;

    @Value("${GOOGLE_ADWORDS_DEV_TOKEN}")
    private String googleAdwordDevToken;

    @Value("${GOOGLE_ADWORDS_REFRESH_TOKEN}")
    private String googleAdwordRefreshToken;

    @Value("${GOOGLE_ADWORDS_USER_AGENT}")
    private String googleAdwordUserAgent;

    @Value("${GOOGLE_ADWORDS_CLIENT_CUSTOMER_ID}")
    private String googleAdwordClientCustomerId;

    @Bean
    public Credential credential() throws ValidationException, OAuthException {
        Credential credential = new OfflineCredentials.Builder()
                                .forApi(OfflineCredentials.Api.ADWORDS)
                                .withClientSecrets(googleAdWordClientId, googleAdwordSecet)
                                .withRefreshToken(googleAdwordRefreshToken)
                                .build()
                                .generateCredential();
        return credential;
    }

    @Bean
    public AdWordsSession adWordsSession(Credential credential) throws ValidationException {
        AdWordsSession adWordsSession = new AdWordsSession.Builder()
                                        .withDeveloperToken(googleAdwordDevToken)
                                        .withOAuth2Credential(credential)
                                        .withClientCustomerId(googleAdwordClientCustomerId)
                                        .withUserAgent(googleAdwordUserAgent)


                .build();
        return adWordsSession;
    }
}

2. Create AdWord Service to export KeyWords, Campaigns, AdGroups and Ads.

package com.integration.adwords.service;

import com.google.api.ads.adwords.axis.factory.AdWordsServices;
import com.google.api.ads.adwords.axis.utils.v201506.SelectorBuilder;
import com.google.api.ads.adwords.axis.v201506.cm.*;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.selectorfields.v201502.cm.CampaignField;
import com.google.api.ads.adwords.lib.selectorfields.v201506.cm.AdGroupAdField;
import com.google.api.ads.adwords.lib.selectorfields.v201506.cm.AdGroupCriterionField;
import com.google.api.ads.adwords.lib.selectorfields.v201506.cm.AdGroupField;
import com.integration.file.FileConverter;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tmichels on 7/31/15.
 */

@Service
public class GoogleAdwordService {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(GoogleAdwordService.class);

    private static final int PAGE_SIZE = 100;

    private static final String CAMPAIGNFILENAME="adwords/google/googleAdwordCampaign.csv";
    private static final String KEYWORDFILENAME="adwords/google/googleAdwordKeywords.csv";
    private static final String ADGROUPFILENAME="adwords/google/googleAdwordAdGroup.csv";
    private static final String ADSFILENAME="adwords/google/googleAdwordAds.csv";

    @Autowired
    AdWordsSession adWordsSession;

    @Autowired
    FileConverter fileConverter;

    public void getCampaigns() throws RemoteException {
        List<String> campaignColumns = new ArrayList<>();
        List<String> campaignData = new ArrayList<>();

        // Get the CampaignService.
        CampaignServiceInterface campaignService =
                new AdWordsServices().get(adWordsSession, CampaignServiceInterface.class);

        int offset = 0;

        // Create selector.
        SelectorBuilder builder = new SelectorBuilder();
        Selector selector = builder
                .fields(CampaignField.Id,
                        CampaignField.Name,
                        CampaignField.AdServingOptimizationStatus,
                        CampaignField.AdvertisingChannelSubType,
                        CampaignField.AdvertisingChannelType,
                        CampaignField.BiddingStrategyId,
                        CampaignField.BiddingStrategyName,
                        CampaignField.BiddingStrategyType,
                        CampaignField.BidType,
                        CampaignField.BudgetId,
                        CampaignField.BudgetName,
                        CampaignField.BudgetReferenceCount,
                        CampaignField.BudgetStatus,
                        CampaignField.DeliveryMethod,
                        CampaignField.Eligible,
                        CampaignField.EndDate,
                        CampaignField.FrequencyCapMaxImpressions,
                        CampaignField.Labels,
                        CampaignField.StartDate,
                        CampaignField.TrackingUrlTemplate,
                        CampaignField.UrlCustomParameters)
                .orderAscBy(CampaignField.Id)
                .offset(offset)
                .limit(PAGE_SIZE)
                .build();

        campaignColumns.add(CampaignField.Id.toString());
        campaignColumns.add(CampaignField.Name.toString());
        campaignColumns.add(CampaignField.AdServingOptimizationStatus.toString());
        campaignColumns.add(CampaignField.AdvertisingChannelSubType.toString());
        campaignColumns.add(CampaignField.AdvertisingChannelType.toString());
        campaignColumns.add(CampaignField.BiddingStrategyId.toString());
        campaignColumns.add(CampaignField.BiddingStrategyName.toString());
        campaignColumns.add(CampaignField.BiddingStrategyType.toString());
        campaignColumns.add(CampaignField.BidType.toString());
        campaignColumns.add(CampaignField.BudgetId.toString());
        campaignColumns.add(CampaignField.BudgetName.toString());
        campaignColumns.add(CampaignField.BudgetReferenceCount.toString());
        campaignColumns.add(CampaignField.BudgetStatus.toString());
        campaignColumns.add(CampaignField.DeliveryMethod.toString());
        campaignColumns.add(CampaignField.Eligible.toString());
        campaignColumns.add(CampaignField.EndDate.toString());
        campaignColumns.add(CampaignField.FrequencyCapMaxImpressions.toString());
        campaignColumns.add(CampaignField.Labels.toString());
        campaignColumns.add(CampaignField.StartDate.toString());
        campaignColumns.add(CampaignField.TrackingUrlTemplate.toString());
        campaignColumns.add(CampaignField.UrlCustomParameters.toString());

        CampaignPage page = null;
        do {
            // Get all campaigns.
            page = campaignService.get(selector);

            // Display campaigns.
            if (page.getEntries() != null) {
                for (Campaign campaign : page.getEntries()) {
                    campaignData.add(campaign.getId().toString());
                    campaignData.add(campaign.getName());
                    campaignData.add(campaign.getAdServingOptimizationStatus()!=null? campaign.getAdServingOptimizationStatus().toString() : "");
                    campaignData.add(campaign.getAdvertisingChannelSubType() != null ? campaign.getAdvertisingChannelSubType().toString() : "");
                    campaignData.add(campaign.getAdvertisingChannelType().toString());
                    campaignData.add(campaign.getBiddingStrategyConfiguration().getBiddingStrategyId() != null ? campaign.getBiddingStrategyConfiguration().getBiddingStrategyId().toString() : "");
                    campaignData.add(campaign.getBiddingStrategyConfiguration().getBiddingStrategyName());
                    campaignData.add(campaign.getBiddingStrategyConfiguration().getBiddingStrategyType() != null ? campaign.getBiddingStrategyConfiguration().getBiddingStrategyType().getValue() : "");
                    campaignData.add(campaign.getBudget().getBudgetId() != null ? campaign.getBudget().getBudgetId().toString() : "");
                    campaignData.add(campaign.getBudget().getName());
                    campaignData.add(campaign.getBudget().getReferenceCount() != null ? campaign.getBudget().getReferenceCount().toString() : "");
                    campaignData.add(campaign.getBudget().getStatus() != null ? campaign.getBudget().getStatus().getValue() : "");
                    campaignData.add(campaign.getBudget().getDeliveryMethod() != null ? campaign.getBudget().getDeliveryMethod().getValue() : "");
                    campaignData.add(campaign.getBudget().getIsExplicitlyShared() != null ? campaign.getBudget().getIsExplicitlyShared().toString() : "");
                    campaignData.add(campaign.getEndDate());
                    campaignData.add(campaign.getFrequencyCap()!=null && campaign.getFrequencyCap().getImpressions() != null ? campaign.getFrequencyCap().getImpressions().toString() : "");
                    campaignData.add(campaign.getLabels() != null ? campaign.getLabels().toString() : "");
                    campaignData.add(campaign.getStartDate());
                    campaignData.add(campaign.getTrackingUrlTemplate());
                    campaignData.add(campaign.getUrlCustomParameters()!=null &&  campaign.getUrlCustomParameters().getParameters() != null ? campaign.getUrlCustomParameters().getParameters().toString() : "");
                }
            } else {
                logger.info("No campaigns were found.");
            }

            offset += PAGE_SIZE;
            selector = builder.increaseOffsetBy(PAGE_SIZE).build();
        } while (offset < page.getTotalNumEntries());

        fileConverter.convertToCSV(campaignData, campaignColumns, CAMPAIGNFILENAME);
    }

    public void getKeyWords() throws RemoteException {
        List<String> campaignColumns = new ArrayList<>();
        List<String> campaignData = new ArrayList<>();

        // Get the AdGroupCriterionService.
        AdGroupCriterionServiceInterface adGroupCriterionService =
                new AdWordsServices().get(adWordsSession, AdGroupCriterionServiceInterface.class);

        int offset = 0;
        boolean morePages = true;

        // Create selector.
        SelectorBuilder builder = new SelectorBuilder();
        Selector selector = builder
                .fields(
                        AdGroupCriterionField.Id,
                        AdGroupCriterionField.AdGroupId,
                        AdGroupCriterionField.KeywordMatchType,
                        AdGroupCriterionField.KeywordText)
                .offset(offset)
                .limit(PAGE_SIZE)
                .equals(AdGroupCriterionField.CriteriaType, "KEYWORD")
                .build();

        campaignColumns.add(AdGroupCriterionField.Id.toString());
        campaignColumns.add(AdGroupCriterionField.AdGroupId.toString());
        campaignColumns.add(AdGroupCriterionField.KeywordMatchType.toString());
        campaignColumns.add(AdGroupCriterionField.KeywordText.toString());

        while (morePages) {
            AdGroupCriterionPage page = adGroupCriterionService.get(selector);

            if (page.getEntries() != null && page.getEntries().length > 0) {
                for (AdGroupCriterion adGroupCriterionResult : page.getEntries()) {
                    campaignData.add(adGroupCriterionResult.getAdGroupId().toString());
                    campaignData.add(adGroupCriterionResult.getCriterion().getId().toString());
                    campaignData.add(((Keyword) adGroupCriterionResult.getCriterion()).getText());
                    campaignData.add(((Keyword) adGroupCriterionResult.getCriterion()).getMatchType().toString());
                }
            } else {
                logger.info("No keywords were found.");
            }
            offset += PAGE_SIZE;
            selector = builder.increaseOffsetBy(PAGE_SIZE).build();
            morePages = offset < page.getTotalNumEntries();
        }

        fileConverter.convertToCSV(campaignData, campaignColumns, KEYWORDFILENAME);
    }

    public void getTextAds() throws RemoteException {
        List<String> campaignColumns = new ArrayList<>();
        List<String> campaignData = new ArrayList<>();

        AdGroupAdServiceInterface adGroupAdService =
                new AdWordsServices().get(adWordsSession, AdGroupAdServiceInterface.class);

        int offset = 0;
        boolean morePages = true;

        // Create selector.
        SelectorBuilder builder = new SelectorBuilder();
        Selector selector = builder
                .fields(AdGroupAdField.Id,
                        AdGroupAdField.AdGroupId,
                        AdGroupAdField.Status)
                .orderAscBy(AdGroupAdField.Id)
                .offset(offset)
                .limit(PAGE_SIZE)
                .in(AdGroupAdField.Status, "ENABLED", "PAUSED", "DISABLED")
                .build();

        campaignColumns.add(AdGroupAdField.Id.toString());
        campaignColumns.add(AdGroupAdField.AdGroupId.toString());
        campaignColumns.add(AdGroupAdField.Status.toString());

        while (morePages) {
            AdGroupAdPage page = adGroupAdService.get(selector);

            if (page.getEntries() != null && page.getEntries().length > 0) {
                for (AdGroupAd adGroupAd : page.getEntries()) {
                    campaignData.add(adGroupAd.getAd().getId().toString());
                    campaignData.add(adGroupAd.getAd().getAdType().toString());
                    campaignData.add(adGroupAd.getStatus().toString());
                }
            } else {
                logger.info("No text ads were found");
            }

            offset += PAGE_SIZE;
            selector = builder.increaseOffsetBy(PAGE_SIZE).build();
            morePages = offset < page.getTotalNumEntries();
        }

        fileConverter.convertToCSV(campaignData, campaignColumns, ADSFILENAME);

    }
    public void getAdGroups() throws RemoteException {
        List<String> campaignColumns = new ArrayList<>();
        List<String> campaignData = new ArrayList<>();

        AdGroupServiceInterface adGroupService = new AdWordsServices().get(adWordsSession, AdGroupServiceInterface.class);

        int offset = 0;
        boolean morePages = true;

        // Create selector.
        SelectorBuilder builder = new SelectorBuilder();
        Selector selector = builder
                .fields(AdGroupField.Id,
                        AdGroupField.Name,
                        AdGroupField.Status)
                .orderAscBy(AdGroupField.Name)
                .offset(offset)
                .limit(PAGE_SIZE)
                .build();

        campaignColumns.add(AdGroupField.Id.toString());
        campaignColumns.add(AdGroupField.Name.toString());
        campaignColumns.add(AdGroupField.Status.toString());

        while (morePages) {
            AdGroupPage page = adGroupService.get(selector);
            // Display ad groups.
            if (page.getEntries() != null) {
                for (AdGroup adGroup : page.getEntries()) {
                    campaignData.add(adGroup.getId().toString());
                    campaignData.add(adGroup.getName().toString());
                    campaignData.add(adGroup.getStatus()!=null ? adGroup.getStatus().getValue() : "");
                }
            } else {
                logger.info("No group ads were found.");
            }

            offset += PAGE_SIZE;
            selector = builder.increaseOffsetBy(PAGE_SIZE).build();
            morePages = offset < page.getTotalNumEntries();
        }
        fileConverter.convertToCSV(campaignData, campaignColumns, ADGROUPFILENAME);
    }
}

Apache Camel Salesforce Integration

1. Setup SalesforceLoginConfig
2. Setup SalesforceCamelEndpointConfig
3. Setup SalesforceCamelComponent
4. Setup SalesforceCamelRouteConfig
5. Run SalesforceCamelIntegrationTest

1. Setup SalesforceLoginConfig

package com.sforce.core.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.component.salesforce.SalesforceComponent;
import org.apache.camel.component.salesforce.SalesforceEndpointConfig;
import org.apache.camel.component.salesforce.SalesforceLoginConfig;
import org.apache.camel.impl.DefaultCamelContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 4/8/15.
 */

@Configuration
public class SalesforceCamelConfig {

    @Value("${SF_USERNAME}")
    private String username;

    @Value("${SF_PASSWORD}")
    private String password;

    @Value("${SF_TOKEN}")
    private String token;

    @Value("${SF_BASE_URL}")
    private String url;

    @Value("${SF_VERSION}")
    private String version;

    @Value("${SF_CLIENT_ID}")
    private String clientId;

    @Value("${SF_CLIENT_SECRET}")
    private String clientSecret;


    @Bean
    public SalesforceLoginConfig salesforceLoginConfig(){
        SalesforceLoginConfig salesforceLoginConfig = new SalesforceLoginConfig();
        salesforceLoginConfig.setUserName(username);
        salesforceLoginConfig.setPassword(password+token);
        salesforceLoginConfig.setLoginUrl(url);
        salesforceLoginConfig.setClientId(clientId);
        salesforceLoginConfig.setClientSecret(clientSecret);
        salesforceLoginConfig.setLazyLogin(false);
        return salesforceLoginConfig;
    }
}

2. Setup SalesforceCamelEndpointConfig

package com.sforce.core.camel;

import org.apache.camel.Endpoint;
import org.apache.camel.component.salesforce.SalesforceComponent;
import org.apache.camel.component.salesforce.SalesforceEndpoint;
import org.apache.camel.component.salesforce.SalesforceEndpointConfig;
import org.apache.camel.component.salesforce.internal.OperationName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by tmichels on 4/13/15.
 */

@Configuration
public class SalesforceCamelEndpointConfig {
    
    @Bean
    public SalesforceEndpointConfig salesforceEndpointConfig(){
        SalesforceEndpointConfig salesforceEndpointConfig = new SalesforceEndpointConfig();
        salesforceEndpointConfig.setApiVersion("33.0");
        return salesforceEndpointConfig;
    }
}

3. Setup SalesforceCamelComponent

package com.sforce.core.camel;


import org.apache.camel.component.salesforce.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Created by tmichels on 4/13/15.
 */

@Configuration
@Import({SalesforceCamelEndpointConfig.class, SalesforceCamelConfig.class})
public class SalesforceCamelComponent {

    @Autowired
    SalesforceEndpointConfig salesforceEndpointConfig;

    @Autowired
    SalesforceLoginConfig salesforceLoginConfig;

    @Bean
    public SalesforceComponent salesforceComponent(){
        SalesforceComponent salesforceComponent = new SalesforceComponent();
        salesforceComponent.setConfig(salesforceEndpointConfig);
        salesforceComponent.setLoginConfig(salesforceLoginConfig);
        salesforceComponent.setPackages("com.sfore.core.camel");
        return salesforceComponent;
    }
}

4. Setup SalesforceCamelRouteConfig

package com.sforce.core.camel;

import com.sforce.core.config.LcGroovyPropertiesConfig;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.salesforce.SalesforceComponent;
import org.apache.camel.spring.SpringCamelContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
import org.apache.camel.spring.javaconfig.Main;

/**
 * Created by tmichels on 4/10/15.
 */

@Configuration
@ComponentScan({"com.sforce.core.camel"})
public class SalesforceCamelRouteConfig extends SingleRouteCamelConfiguration implements InitializingBean {

    @Autowired
    SalesforceComponent salesforceCamelComponent;

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.run(args);
    }


    @Override
    protected CamelContext createCamelContext() throws Exception {
        SpringCamelContext camelContext = new SpringCamelContext();
        camelContext.setApplicationContext(getApplicationContext());
        camelContext.addComponent("salesforce", salesforceCamelComponent);
        camelContext.addRoutes(route());
        camelContext.start();
        return camelContext;
    }


    @Override
    public void afterPropertiesSet() throws Exception {}

    @Bean
    @Override
    public RouteBuilder route() {
        return new RouteBuilder() {
            public void configure() {
                from("direct:getBasicInfo").to("salesforce:getBasicInfo").bean(BasicInfoBean.class);
            }
        };
    }

    public static class BasicInfoBean {
        public void someMethod(String body) {
            System.out.println(">>>>>>> Salesforce Basics Info: " + body);
        }
    }
}

5. Run SalesforceCamelIntegrationTest

package com.sforce.core.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by tmichels on 7/27/15.
 */

@ContextConfiguration(classes = {SalesforceCamelRouteConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class SalesforceCamelIntegrationTest {

    @Autowired
    CamelContext camelContext;

    @EndpointInject(uri = "direct:getBasicInfo")
    ProducerTemplate producer;

    @Test
    public void testCamelContext() throws Exception {
        String body = "Account";
        producer.sendBody(body);
    }
}

OUTPUT:

>>>>>>> Salesforce Basics Info: {"objectDescribe":{"name":"Account","label":"Account","updateable":true,"keyPrefix":"001","custom":false,"urls":{"sobject":"/services/data/v33.0/sobjects/Account","describe":"/services/data/v33.0/sobjects/Account/describe","rowTemplate":"/services/data/v33.0/sobjects/Account/{ID}","approvalLayouts":"/services/data/v33.0/sobjects/Account/describe/approvalLayouts","quickActions":"/services/data/v33.0/sobjects/Account/quickActions","listviews":"/services/data/v33.0/sobjects/Account/listviews","layouts":"/services/data/v33.0/sobjects/Account/describe/layouts","compactLayouts":"/services/data/v33.0/sobjects/Account/describe/compactLayouts"},"searchable":true,"labelPlural":"Accounts","layoutable":true,"activateable":false,"createable":true,"deprecatedAndHidden":false,"deletable":true,"customSetting":false,"feedEnabled":true,"mergeable":true,"queryable":true,"replicateable":true,"retrieveable":true,"undeletable":true,"triggerable":true},"recentItems":[{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhUzlIAF"},"Name":"Pope John XXIII Regional High School","Id":"001W000000JhUzlIAF"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhZFQIA3"},"Name":"A PLAZA DRIVING SCHOOL","Id":"001W000000JhZFQIA3"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JgxxXIAR"},"Name":"Advanced Reproductive Care, Inc. (ARC)","Id":"001W000000JgxxXIAR"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JrwqvIAB"},"Name":"name","Id":"001W000000JrwqvIAB"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhXSnIAN"},"Name":"Stella Niagara Education Park","Id":"001W000000JhXSnIAN"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jr1WOIAZ"},"Name":"A J DIANA SONS INC","Id":"001W000000Jr1WOIAZ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jr1TIIAZ"},"Name":"A J DIANA SONS COMPANY","Id":"001W000000Jr1TIIAZ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jr1WNIAZ"},"Name":"A J DIANA SONS INC","Id":"001W000000Jr1WNIAZ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JqzmSIAR"},"Name":"A J DIANA SONS INC","Id":"001W000000JqzmSIAR"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jq8TeIAJ"},"Name":"A J DIANA SONS COMPANY","Id":"001W000000Jq8TeIAJ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhZLTIA3"},"Name":"name","Id":"001W000000JhZLTIA3"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000Jq8LVIAZ"},"Name":"A PLAZA DRIVING SCHOOL","Id":"001W000000Jq8LVIAZ"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhZBTIA3"},"Name":"A J DIANA SONS INC","Id":"001W000000JhZBTIA3"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JgxwjIAB"},"Name":"[First]Name [Last]Name","Id":"001W000000JgxwjIAB"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhUlVIAV"},"Name":"Central Texas Christian School","Id":"001W000000JhUlVIAV"},{"attributes":{"type":"Account","url":"/services/data/v33.0/sobjects/Account/001W000000JhKbtIAF"},"Name":"Sylvan Learning Center - 112","Id":"001W000000JhKbtIAF"}]}

Jax-rs Basic Authentication using HeaderParam

@Path("/v1/docs")
@Consumes({ MediaType.APPLICATION_JSON })
@Named
@Api(value = "/v1/docs", description = "Docs Service" )
public interface DocumentService {

    @POST
    @Path("/{caseId}/{emailMessageid}")
    @PermitAll
    @ApiOperation(value = "Docs Service",
            notes = "Create Salesforce Account, Contact"
    )
    @ApiResponses(value = {
            @ApiResponse(code = 500, message = "Unexpected failure")
    })
    void uploadAndDeleteDocumentFromSalesforceAndUpdateCase(@PathParam("caseId") String caseId, 
                                                            @PathParam("emailMessageid") String emailMessageid,  
                                                            @HeaderParam("authorization") String authentication);
}
@Named
public class DocumentServiceImpl implements DocumentService {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(DocumentServiceImpl.class);

    @Value("${SF_DOCS_USERNAME}")
    private String sfDocsUsername;

    @Value("${SF_DOCS_PASSWORD}")
    private String sfDocsPassword;

    @Override
    public synchronized void uploadAndDeleteDocumentFromSalesforceAndUpdateCase(String caseId, String emailMessageId, String authentication) {
        if(isUserAuthenticated(authentication)){
          
        } else {
            logger.error("caseId {} emailId {} authentication failed {}", caseId, emailMessageId, authentication);
        }
    }

    public boolean isUserAuthenticated(String authString){
        if (authString!=null){
            String[] authParts = authString.split("\\s+");
            String authInfo = authParts[1];
            byte[] bytes  =  DatatypeConverter.parseBase64Binary(authInfo);
            String decodedAuth = new String(bytes);
            String[] userNameAndPassword = decodedAuth.split(":");
            if (userNameAndPassword[0].equals(sfDocsUsername) && userNameAndPassword[1].equals(sfDocsPassword)){
                return true;
            } else
                logger.info("isUserAuthenticated did not match decodedAuth {} username {}", decodedAuth, sfDocsUsername+":"+sfDocsPassword);
        }
        return false;
    }
}

Setup Spring EnableCaching for Memcached


1. Setup CacheConfiguration
2. Extend AbstractCacheManager
3. Implement Cache
4. Implement CachingConfigurer

1. Setup CacheConfiguration

package com.example.cache;

import java.io.IOException;
import java.net.URISyntaxException;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.ConnectionFactory;
import net.spy.memcached.ConnectionFactoryBuilder;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.auth.AuthDescriptor;
import net.spy.memcached.auth.PlainCallbackHandler;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CacheConfiguration {

    @Bean
    public MemcachedClient memcachedCloudConfiguration() {
        try {
            AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" },
                    new PlainCallbackHandler(System.getenv("MEMCACHEDCLOUD_USERNAME"), System.getenv("MEMCACHEDCLOUD_PASSWORD")));

            MemcachedClient mc = new MemcachedClient(
                    new ConnectionFactoryBuilder()
                            .setTranscoder(new CustomSerializingTranscoder())
                            .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
                            .setAuthDescriptor(ad).build(),
                    AddrUtil.getAddresses(System.getenv("MEMCACHEDCLOUD_SERVERS")));

            return mc;
        } catch (IOException ex) {
            // the Memcached client could not be initialized.
        }
        return null;
    }
}

2. Extend AbstractCacheManager

package com.example.cache;

import org.springframework.cache.Cache;
import org.springframework.cache.support.AbstractCacheManager;
import org.springframework.util.Assert;

import java.util.Collection;

/**
 * Created by tmichels on 3/19/15.
 */
public class MemCacheManager extends AbstractCacheManager {
    private final Collection<MemCache> internalCaches;

    public MemCacheManager(final Collection<MemCache> internalCaches) {
        this.internalCaches = internalCaches;
    }

    @Override
    protected Collection<? extends Cache> loadCaches() {
        Assert.notNull(internalCaches, "A collection caches is required and cannot be empty");
        return internalCaches;
    }
}

3. Implement Cache

package com.example.cache;

import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.transcoders.SerializingTranscoder;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * Created by tmichels on 3/19/15.
 */
public class MemCache implements Cache {

    @Autowired
    MemcachedClient cache;

    private final String name = "";
    private static final Logger LOGGER = Logger.getLogger(MemCache.class);

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Object getNativeCache() {
        return cache;
    }

    @Override
    public ValueWrapper get(final Object key) {
        Object value = null;
        try {
            value = cache.get(key.toString());
        } catch (final Exception e) {
            LOGGER.warn(e);
        }
        if (value == null) {
            return null;
        }
        return new SimpleValueWrapper(value);
    }

    @Override
    public void put(final Object key, final Object value) {
        cache.set(key.toString(), 7 * 24 * 3600, value);
        Assert.assertNotNull(get(key)); //This fails on the viewCache
    }

    @Override
    public void evict(final Object key) {
        this.cache.delete(key.toString());
    }

    @Override
    public void clear() {
        cache.flush();
    }
 }

4. Implement CachingConfigurer

package com.example.cache;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.interceptor.DefaultKeyGenerator;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableCaching
public class SimpleCacheConfiguration implements CachingConfigurer {

    @Override
    @Bean
    public CacheManager cacheManager() {
        CacheManager cacheManager;
        try {
            cacheManager = new MemCacheManager(internalCaches());
            return cacheManager;
        } catch (final URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    @Bean
    public Collection<MemCache> internalCaches() throws URISyntaxException {
        final Collection<MemCache> caches = new ArrayList<MemCache>();
        caches.add(new MemCache());
        return caches;
    }

    @Override
    public KeyGenerator keyGenerator() {
        return new DefaultKeyGenerator();
    }

}

Metrics Dashboard using STOMP WebSocket

http://cloudsole-metrics.herokuapp.com
https://github.com/thysmichels/cloudsole-metrics

1. Basic WebAppInitializer
2. WebConfiguration with EnableScheduling
3. WebSocketConfiguration with EnableWebSocketMessageBroker
4. Random Number Generator with ApplicationListener
5. Sock.js Javascript

1. Basic WebAppInitializer

package  com.thysmichels.websockets.configuration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(final ServletContext context) throws ServletException {

        final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();

        root.scan("com.thysmichels.websockets");

        context.addListener(new ContextLoaderListener(root));

        final ServletRegistration.Dynamic appServlet = context.addServlet("appServlet",new DispatcherServlet(new GenericWebApplicationContext()));
        appServlet.setAsyncSupported(true);
        appServlet.setLoadOnStartup(1);
        appServlet.addMapping("/*");
    }
}

2. WebConfiguration with EnableScheduling

package  com.thysmichels.websockets.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
@EnableScheduling
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(
        final DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

3. WebSocketConfiguration with EnableWebSocketMessageBroker

package  com.thysmichels.websockets.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
    }

    @Override
    public void registerStompEndpoints(final StompEndpointRegistry registry) {
        registry.addEndpoint("/metrics").withSockJS();
    }

    @Override
    public void configureClientInboundChannel(final ChannelRegistration registration) {}

    @Override
    public void configureClientOutboundChannel(final ChannelRegistration registration) { }

}

4. Random Number Generator with ApplicationListener

package  com.thysmichels.websockets.utils;

import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.messaging.core.MessageSendingOperations;
import org.springframework.messaging.simp.broker.BrokerAvailabilityEvent;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class RandomDataGenerator implements ApplicationListener<BrokerAvailabilityEvent> {

    private final MessageSendingOperations<String> messagingTemplate;

    @Autowired
    public RandomDataGenerator(final MessageSendingOperations<String> messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    @Override
    public void onApplicationEvent(final BrokerAvailabilityEvent event) {
    }

    @Scheduled(fixedDelay = 1000)
    public void sendDataUpdates() {
        this.messagingTemplate.convertAndSend("/data", new Random().nextInt(100));
    }
}

5. Sock.js Javascript

/**
 * Created by tmichels on 8/19/14.
 */

//var stompClient = null;
var updateOpts = {'minVal':'0','maxVal':'100','newVal':'1'};

var randomData;
var socket = new SockJS('/metrics');
var client = Stomp.over(socket);

client.connect('admin', 'password', function(frame) {

    client.subscribe("/data", function(message) {
        var point = [ (new Date()).getTime(), parseInt(message.body) ];
        gaugeUpdate('cf-gauge-1', {'minVal':'0','maxVal':'100','newVal':parseInt(message.body)})
        $('#spark-1').each(function(){

            customSparkOptions = {};
            customSparkOptions.minSpotColor = true;
            var sparkOptions = cf_defaultSparkOpts;
            var sparkOptions = $.extend({}, cf_defaultSparkOpts, customSparkOptions);

            data.push(parseInt(message.body));
            createSparkline($(this), data, sparkOptions);
        });

        $('#metric-1 .metric').html(message.body);
        $('#metric-1 .large').html(message.body);
        $('#metric-2 .metric').html(message.body);
        $('#metric-2 .large').html(message.body);
        var element = $(this).data('update');
        cf_rSVPs[$('#svp-1').attr('id')].chart.update(parseInt(message.body));
        $('#svp-1 .chart').data('percent', parseInt(message.body));
        $('#svp-1 .metric').html(message.body);
       // $('#cf-svmc-sparkline .metric').html(message.body);

        $('#cf-rag-1').each(function(){
            // Dummy data for RAG
            ragData = [60,30,parseInt(message.body)];
            ragLabels = ['Success','Bounce','Abandoned'];
            ragOpts = {postfix:'%'}

            cf_rRags[$(this).prop('id')] = new RagChart($(this).prop('id'), ragData, ragLabels, ragOpts);
        });

        $('#cf-pie-1').each(function(){

            var pdata = [
                {
                    value : parseInt(message.body),
                    color : pieSegColors[3],
                    label: 'Success'
                },
                {
                    value : parseInt(message.body)+50,
                    color : pieSegColors[2],
                    label: 'Bounce'
                },
                {
                    value: parseInt(message.body)+100,
                    color: pieSegColors[1],
                    label: 'Abandoned'
                }
            ]

            var $container = $(this);
            var pId = $container.prop('id');

            // Store chart information
            cf_rPs[pId] = {};
            cf_rPs[pId].data = pdata;


             // Set options per chart
             customOptions = {};
             customOptions.animation = false;
             cf_rPs[pId].options = customOptions;

            // Create chart
            createPieChart($container);
        });

        $('#cf-funnel-1').each(function(){
            funData = [parseInt(message.body)+3000,parseInt(message.body)+1500,parseInt(message.body)+500,parseInt(message.body)+250,parseInt(message.body)];
            funLabels = ['Visits','Cart','Checkout','Purchase','Refund'];
            funOptions = {barOpacity:true, layout:'left'};

            cf_rFunnels[$(this).prop('id')] = new FunnelChart($(this).prop('id'), funData, funLabels, funOptions);
        });

    });
});


LendingClub Java Client

https://github.com/thysmichels/lendingclub-java-client

The minimum requirements to get started using LendingClub API:
1. Setup wsimport maven plugin
2. Run mvn clean install to generate LendingClub API Java classes from wsdl
3. Create basic LendingClub Main Class
4. Output

1. Setup wsimport maven plugin

         <plugin>
            <groupId>org.jvnet.jax-ws-commons</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <wsdlUrls><wsdlUrl>https://www.lendingclub.com/ws/1.4?wsdl</wsdlUrl></wsdlUrls>
                <verbose>true</verbose>
            </configuration>
        </plugin>

2. Run mvn clean install to generate LendingClub API Java classes from wsdl

mvn clean install
...

[INFO] --- jaxws-maven-plugin:2.3:wsimport (default) @ lendingclub-java-client ---
[INFO] Processing: https://www.lendingclub.com/ws/1.4?wsdl
[INFO] jaxws:wsimport args: [-keep, -s, /Users/tmichels/git/lendingclub-java-client/target/generated-sources/wsimport, -d, /Users/tmichels/git/lendingclub-java-client/target/classes, -verbose, -encoding, UTF-8, -Xnocompile, "https://www.lendingclub.com/ws/1.4?wsdl"]
parsing WSDL...



Generating code...

com/lendingclub/ws/BadArgumentException.java
com/lendingclub/ws/BadArgumentExceptionInfo.java
com/lendingclub/ws/BrowseLoansResult.java
com/lendingclub/ws/CreatePortfolioResult.java
com/lendingclub/ws/CreditInfo.java
com/lendingclub/ws/ExcessiveRequestsException.java
com/lendingclub/ws/ExcessiveRequestsExceptionInfo.java
com/lendingclub/ws/GetPortfoliosResult.java
com/lendingclub/ws/HomeOwnership.java
com/lendingclub/ws/IncomeVerification.java
com/lendingclub/ws/InvalidCredentialsException.java
com/lendingclub/ws/InvalidCredentialsExceptionInfo.java
com/lendingclub/ws/LendingClubV14.java
com/lendingclub/ws/LendingClubV14_Service.java
com/lendingclub/ws/LoanBrowseLoans.java
com/lendingclub/ws/LoanBrowseLoansResponse.java
com/lendingclub/ws/LoanGrade.java
com/lendingclub/ws/LoanListing.java
com/lendingclub/ws/LoanPurpose.java
com/lendingclub/ws/LoanSubGrade.java
com/lendingclub/ws/MissingCredentialsException.java
com/lendingclub/ws/MissingCredentialsExceptionInfo.java
com/lendingclub/ws/ObjectFactory.java
com/lendingclub/ws/Order.java
com/lendingclub/ws/OrderConfirmation.java
com/lendingclub/ws/OrderCreatePortfolio.java
com/lendingclub/ws/OrderCreatePortfolioResponse.java
com/lendingclub/ws/OrderExecutionStatus.java
com/lendingclub/ws/OrderGetPortfolios.java
com/lendingclub/ws/OrderGetPortfoliosResponse.java
com/lendingclub/ws/OrderInstruct.java
com/lendingclub/ws/OrderInstructConfirmation.java
com/lendingclub/ws/OrderSubmitOrders.java
com/lendingclub/ws/OrderSubmitOrdersResponse.java
com/lendingclub/ws/Portfolio.java
com/lendingclub/ws/PortfolioResult.java
com/lendingclub/ws/ReviewStatus.java
com/lendingclub/ws/SubmitOrderResult.java
com/lendingclub/ws/SystemGetVersion.java
com/lendingclub/ws/SystemGetVersionResponse.java
com/lendingclub/ws/UnauthorizedWebserviceUserException.java
com/lendingclub/ws/UnauthorizedWebserviceUserExceptionInfo.java
com/lendingclub/ws/VersionInfo.java
com/lendingclub/ws/VersionStatus.java
com/lendingclub/ws/package-info.java

3. Create basic LendingClub Main Class

package com.lendingclub.main;

import com.lendingclub.exception.WebRequestException;
import com.lendingclub.ws.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

/**
 * Created by tmichels on 8/21/14.
 */
public class Main {

   private static final String LENDINGCLUB_API_KEY = "PASTE_YOUR_API_KEY_HERE";

    public static void main(String[] args) throws WebRequestException, UnauthorizedWebserviceUserException, ExcessiveRequestsException, InvalidCredentialsException, MissingCredentialsException {
        final String endpointUrl = "https://api.lendingclub.com:443/ws/1.4";
        Map<String, List<String>> headers = new HashMap<String,List<String>>();
        headers.put("Authorization", Collections.singletonList(LENDINGCLUB_API_KEY));
        LendingClubV14_Service lcSvc = new LendingClubV14_Service();
        LendingClubV14 lcWs = lcSvc.getLendingClubV14Port();
        Map<String, Object> requestContext = ((BindingProvider) lcWs).getRequestContext();
        requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointUrl);

        BrowseLoansResult browseLoansResult = lcWs.loanBrowseLoans(true);
        for (LoanListing ll : browseLoansResult.getLoans()){
            System.out.println("Loan: " + ll.getTitle() + " " + ll.getGrade());
        }
    }
}

4. Output

Loan: Debt consolidation B
Loan: Debt consolidation D
Loan: Credit card refinancing A
Loan: Credit card refinancing D

Using LendingClub Java Client library
1. LendingClub API Connection Config
2. LendingClub Client Service
3. LendingClub Client Main Class
4. LendingClub Client Config Test
5. LendingClub Client Tests

1. LendingClub API Connection Config

package com.lendingclub.config;

import com.lendingclub.ws.LendingClubV14;
import com.lendingclub.ws.LendingClubV14_Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;
import java.util.*;

/**
 * Created by tmichels on 8/21/14.
 */

@Configuration
@PropertySource("classpath:/system.properties")
public class ConnectionConfig {

    @Value("${lendingclub_api_key}")
    private String lendingclubKey;

    @Value("${lendingclub_api_url:https://api.lendingclub.com:443/ws/1.4}")
    private String lendingclubEndpoint;

    @Bean
    public LendingClubV14 lcConnection(){

        Map<String, List<String>> headers = new HashMap<String,List<String>>();
        headers.put("Authorization", Arrays.asList(lendingclubKey));

        LendingClubV14_Service lcSvc = new LendingClubV14_Service();
        LendingClubV14 lcWs = lcSvc.getLendingClubV14Port();
        Map<String, Object> requestContext = ((BindingProvider) lcWs).getRequestContext();

        // 1 minute for connection
        ((BindingProvider) lcWs).getRequestContext().put("com.sun.xml.ws.connect.timeout", 1 * 60 * 1000);
        // 1 minute for request
        ((BindingProvider) lcWs).getRequestContext().put("com.sun.xml.ws.request.timeout", 1 * 60 * 1000);

        requestContext.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, lendingclubEndpoint);
        return lcWs;
    }
}

2. LendingClub Client Service

@Service
public class LendingClubClient {

    @Autowired
    private LendingClubV14 lcConnection;

    public BrowseLoansResult getLoans(boolean recentlyListed) throws Exception {
        return lcConnection.loanBrowseLoans(!recentlyListed);
    }

    public VersionInfo getSystemVersion() throws ExcessiveRequestsException {
        return lcConnection.systemGetVersion();
    }

    public void submitOrders(List<OrderInstruct> orderInstructList) throws Exception {
        lcConnection.orderSubmitOrders(orderInstructList);
    }

    public GetPortfoliosResult getPortfolios(Long portfolioId) throws Exception {
        return lcConnection.orderGetPortfolios(portfolioId);
    }

    public CreatePortfolioResult orderCreatePortfolio(Portfolio portfolio) throws Exception {
        return lcConnection.orderCreatePortfolio(portfolio);
    }
}

3. LendingClub Client Main Class

public class MainSpring {

    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.scan("com.lendingclub");
        applicationContext.refresh();

        LendingClubClient loanService = applicationContext.getBean(LendingClubClient.class);

        BrowseLoansResult browseLoansResult = loanService.getLoans(true);
        for (LoanListing loanListing : browseLoansResult.getLoans()){
            System.out.println("Loans: " + loanListing.getTitle() + " " + loanListing.getGrade());
        }
    }
}

4. Lending Club Client Config Test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WSConnectionConfig.class, ClientConfig.class} , loader = AnnotationConfigContextLoader.class)
public class ConnectionConfigTest {

    @Autowired
    LendingClubV14 lcConnection;

    @Test
    public void testlcWSConnection() throws ExcessiveRequestsException {
        Assert.assertEquals(lcConnection.systemGetVersion().getNumber(), "1.4");
        Assert.assertEquals(lcConnection.systemGetVersion().getStatus().value(), "ACTIVE");
    }
}

5. Lending Club Client Tests

package com.lendingclub.client;

import com.lendingclub.ClientConfig;
import com.lendingclub.config.WSConnectionConfig;
import com.lendingclub.ws.Order;
import com.lendingclub.ws.OrderInstruct;
import com.lendingclub.ws.Portfolio;
import com.lendingclub.ws.SubmitOrderResult;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.Arrays;

/**
 * Created by tmichels on 8/22/14.
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WSConnectionConfig.class, ClientConfig.class, LendingClubClient.class} , loader = AnnotationConfigContextLoader.class)
public class LendingClubClientTest {

    @Autowired
    LendingClubClient lcClient;

    @Test
    public void testGetLoans() throws Exception {
        Assert.assertNotNull(lcClient.getLoans(true));
    }

    @Test
    public void testGetPortfolios() throws Exception {
        Assert.assertNotNull(lcClient.getPortfolios(new Long(6817950)));
    }

    @Test
    public void testOrderCreatePortfolio() throws Exception {
        Portfolio portfolio = new Portfolio();
        portfolio.setAid(new Long(6817950));
        portfolio.setPortfolioDescription("Dummy portfolio description");
        portfolio.setPortfolioName("Dummy portfilio");
        Assert.assertNotNull(lcClient.orderCreatePortfolio(portfolio));
    }

    @Test
    public void testSubmitOrders() throws Exception {
        OrderInstruct orderInstruct = new OrderInstruct();
        orderInstruct.setAid(new Long(6817950));
        lcClient.submitOrders(Arrays.asList(orderInstruct));
    }
}

Spring MVC AngularJS Address Book

Try it: http://cloudsole-angular.herokuapp.com
Clone it: https://github.com/thysmichels/cloudsole-angular

1. Address Book Model
2. Address Book Service
3. Address Book Service Implementation
4. Address Book Controller
5. Address Book Angular Controller
6. Address Book HTML

1. Address Book Model

package com.cloudsole.angular.model;

/**
 * Created by tmichels on 8/3/14.
 */
public class AddressBook {

    private long id;
    private String firstName;
    private String lastName;
    private String phone;
    private String email;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

2. Address Book Service

package com.cloudsole.angular.service;

import com.cloudsole.angular.model.AddressBook;

import java.util.List;

/**
 * Created by tmichels on 8/3/14.
 */
public interface AddressBookService {
     List<AddressBook> viewAllAddressBook();
     void createAddressBook(AddressBook addressBook);
     void updateAddressBook(int pos, AddressBook updateAddressBook);
     void deleteAddressBook(int id);
     void deleteAllAddressBook();
     AddressBook findAddressBook(int id);
}

3. Address Book Service Implementation

package com.cloudsole.angular.service;

import com.cloudsole.angular.model.AddressBook;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tmichels on 8/3/14.
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    List<AddressBook> addressBooks = new ArrayList<AddressBook>();
    private static Long id = 0L;

    @Override
    public List<AddressBook> viewAllAddressBook() {
        return addressBooks;
    }

    @Override
    public void createAddressBook(AddressBook addressBook) {
        addressBook.setId(id);
        addressBooks.add(addressBook);
        ++id;
    }

    @Override
    public void updateAddressBook(int pos, AddressBook updateAddressBook) {
        addressBooks.set(pos, updateAddressBook);
    }

    @Override
    public void deleteAddressBook(int id) {
        addressBooks.remove(id);
    }

    @Override
    public void deleteAllAddressBook() {
        addressBooks.clear();
        id = 0L;
    }

    @Override
    public AddressBook findAddressBook(int id) {
        return addressBooks.get(id);
    }
}

4. Address Book Controller

package com.cloudsole.angular.controller;

import com.cloudsole.angular.model.AddressBook;
import com.cloudsole.angular.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by tmichels on 8/3/14.
 */

@Controller
@RequestMapping("/address")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    @RequestMapping(value = "/all.json", method = RequestMethod.GET)
    public @ResponseBody List<AddressBook> viewAllAddressBook(){
        return addressBookService.viewAllAddressBook();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody void addAddressBookEntry(@RequestBody AddressBook addressBook){
        addressBookService.createAddressBook(addressBook);
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public @ResponseBody void deleteAddressBookEntry(@PathVariable("id") String id){
        addressBookService.deleteAddressBook(Integer.valueOf(id));
    }

    @RequestMapping(value = "/update/{pos}", method = RequestMethod.PUT)
    public @ResponseBody void updateAddressBook(@RequestBody AddressBook addressBook, @PathVariable("pos") String pos){
        addressBookService.updateAddressBook(Integer.valueOf(pos), addressBook);
    }

    @RequestMapping(value="/delete/all", method = RequestMethod.DELETE)
    public @ResponseBody void deleteAllAddressBook(){
        addressBookService.deleteAllAddressBook();
    }

    @RequestMapping("/layout")
    public String getTodoPartialPage() {
        return "addressbook/layout";
    }
}

5. Address Book Angular Controller

/**
 * Created by tmichels on 8/3/14.
 */


var AddressBookController = function($scope, $http){

    $scope.editMode = false;
    $scope.position = '';

    $scope.viewAllAddressBook = function(){
        $http.get('address/all.json').success(function(response){
            $scope.addressBooks = response;
        })
    }

    $scope.resetAddressBookField = function(){
        $scope.ab.firstName='';
        $scope.ab.lastName='';
        $scope.ab.phone = '';
        $scope.ab.email = '';
        $scope.editMode = false;
    }

    $scope.addAddressBook = function(ab) {
        $http.post('address/add', ab).success(function(response){
            $scope.viewAllAddressBook();
            $scope.ab.firstName='';
            $scope.ab.lastName='';
            $scope.ab.phone = '';
            $scope.ab.email = '';
        }).error(function(response){
            console.log(response);
        })
    }

    $scope.updateAddressBook = function(ab) {
        $http.put('address/update/'+$scope.position, ab).success(function(response){
            $scope.ab.firstName='';
            $scope.ab.lastName='';
            $scope.ab.phone = '';
            $scope.ab.email = '';
            $scope.viewAllAddressBook();
            $scope.editMode = false;
        }).error(function(response){
            console.log(response);
        })
    }

    $scope.deleteAddressBook = function(id) {
        $http.delete('address/delete/' + id).success(function(response){
            $scope.viewAllAddressBook();
        }).error(function(response){
            console.log(response);
        })
    }

    $scope.deleteAllAddressBook = function(){
        $http.delete('address/delete/all').success(function(response){
            $scope.viewAllAddressBook();
        })
    }

    $scope.editAddressBook = function(pos, addressBook){
        $scope.position = pos;
        $scope.ab = addressBook;
        $scope.editMode = true;
    }

    $scope.viewAllAddressBook();
}

6. Address Book HTML

<div class="alert alert-error" ng-show="error">{{errorMessage}}</div>
<div class="row">
    <form ng-submit="addAddressBook(ab)">
        <div class="col-lg-8">
            <input class="form-control" placeholder="Enter First Name" type="text" ng-model="ab.firstName" required min="1" />
            <input class="form-control" placeholder="Enter Last Name" type="text" ng-model="ab.lastName" required min="1" />
            <input class="form-control" placeholder="Enter Phone" type="text" ng-model="ab.phone" required min="1" />
            <input class="form-control" placeholder="Enter Email" type="text" ng-model="ab.email" required min="1" />
        </div>
    </form>

    <button class="btn btn-primary" ng-disabled="!ab" ng-hide="editMode" ng-click="addAddressBook(ab)">Add Entry</button>
    <button type="btn btn-primary" class="btn btn-primary"
            ng-disabled="!ab" ng-show="editMode"
            ng-click="updateAddressBook(ab)">Save</button>
    <button type="btn btn-primary" class="btn btn-primary" ng-click="resetAddressBookField()">Reset</button>
</div>
<hr />

<div class="row">
    <div class="col-lg-8">
        <div class="form-group">
            <div class="input-group">
                <input type="text" class="form-control" placeholder="Search" id="search-query-3" ng-model="searchAddressBook">
                  <span class="input-group-btn">
                    <button type="submit" class="btn"><span class="fui-search"></span></button>
                  </span>
            </div>
        </div>
    </div>
</div>
<hr />

<div class="alert alert-info" style="width:400px;margin-left:100px;" ng-show="addressBooks.length == 0">
    No address book entry found
</div>
<table class="table table-bordered table-striped" ng-show="addressBooks.length > 0">
    <thead>
    <tr>
        <th style="text-align: center; width: 25px;">Delete</th>
        <th style="text-align: center; width: 25px;">Update</th>
        <th style="text-align: center;">First Name</th>
        <th style="text-align: center;">Last Name</th>
        <th style="text-align: center;">Phone Number</th>
        <th style="text-align: center;">Email</th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="addressBook in addressBooks">
        <td  style="width:70px;text-align:center;"><button class="btn btn-mini btn-danger" ng-click="deleteAddressBook(addressBooks.indexOf(addressBook))">Delete</button></td>
        <td  style="width:70px;text-align:center;"><button class="btn btn-mini btn-danger" ng-click="editAddressBook(addressBooks.indexOf(addressBook), addressBook)">Update</button></td>
        <td>{{addressBook.firstName}}</td><td>{{addressBook.lastName}}</td><td>{{addressBook.phone}}</td><td>{{addressBook.email}}</td>
    </tr>
    </tbody>
</table>
<button class="btn btn-danger"  ng-show="addressBooks.length >= 1" ng-click="deleteAllAddressBook()">Delete All Address Book</button>
%d bloggers like this: