Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2013 Google Inc. All Rights Reserved.
  //
  // Licensed 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 com.google.api.ads.adwords.axis.utility.extension.util;
 
 
Helper class to copy the com.google.api.ads.adwords.lib.client.AdWordsSession.

Author(s):
Julian Toledo
 
 public class AdWordsSessionUtil {
 
   static Logger log = LoggerFactory.getLogger(AdWordsSessionUtil.class);
 
   private static final String USER_AGENT = "AdWordsAxisUtilityExtension";

  
Creates a copy of the AdWordsSession.

Parameters:
adWordsSession to copy from
Returns:
a new copy of the AdWordsSession
 
   public static AdWordsSession copy(AdWordsSession adWordsSession) {
     return copy(adWordsSessionadWordsSession.getUserAgent());
   }

  
Copies the AdWordsSession and Adds USER_AGENT to the userAgent.

Parameters:
adWordsSession to copy from
Returns:
a new copy of the AdWordsSession with the new userAgent
 
   public static AdWordsSession copyWithUtilityUserAgent(AdWordsSession adWordsSession) {
     String userAgent = "";
     if (adWordsSession.getUserAgent() != null) {
       userAgent = adWordsSession.getUserAgent();
     }
     if (!userAgent.startsWith()) {
       return copy(adWordsSession + userAgent);
     } else {
       return copy(adWordsSession);
     }
   }

  
Copies (if needed) the AdWordsSession and Adds USER_AGENT to the userAgent.

Parameters:
adWordsSession to copy from
Returns:
a new copy of the AdWordsSession with the new userAgent only if needed
 
   public static AdWordsSession copyIfNeededWithUtilityUserAgent(AdWordsSession adWordsSession) {
     String userAgent = "";
     if (adWordsSession.getUserAgent() != null) {
       userAgent = adWordsSession.getUserAgent();
     }
     if (!userAgent.startsWith()) {
       return copy(adWordsSession + userAgent);
     } else {
       return adWordsSession;
     }
   }

  
Creates a copy of the AdWordsSession and changes the userAgent.

Parameters:
adWordsSession to copy from
userAgent the new User Agent for the session
Returns:
a new copy of the AdWordsSession
 
   public static AdWordsSession copy(AdWordsSession adWordsSessionString userAgent) {
     AdWordsSession.Builder builder = new Builder();
     if (adWordsSession.getEndpoint() != null) {
       builder = builder.withEndpoint(adWordsSession.getEndpoint());
     }
     if (userAgent != null) {
      builder = builder.withUserAgent(userAgent);
    } else {
      builder = builder.withUserAgent();
    }
    if (adWordsSession.getDeveloperToken() != null) {
      builder = builder.withDeveloperToken(adWordsSession.getDeveloperToken());
    }
    if (adWordsSession.getClientCustomerId() != null) {
      builder = builder.withClientCustomerId(adWordsSession.getClientCustomerId());
    }
    if (adWordsSession.getOAuth2Credential() != null) {
      builder = builder.withOAuth2Credential(adWordsSession.getOAuth2Credential());
    }
    if (adWordsSession.getReportingConfiguration() != null) {
      ReportingConfiguration reportingConfig =
          new ReportingConfiguration.Builder()
              .skipReportHeader(adWordsSession.getReportingConfiguration().isSkipReportHeader())
              .skipColumnHeader(adWordsSession.getReportingConfiguration().isSkipColumnHeader())
              .skipReportSummary(adWordsSession.getReportingConfiguration().isSkipReportSummary())
              .includeZeroImpressions(
                  adWordsSession.getReportingConfiguration().isIncludeZeroImpressions())
              .build();
      builder = builder.withReportingConfiguration(reportingConfig);
    }
    try {
      AdWordsSession newAdWordsSession;
      newAdWordsSession = builder.build();
      newAdWordsSession.setPartialFailure(adWordsSession.isPartialFailure());
      newAdWordsSession.setValidateOnly(adWordsSession.isValidateOnly());
      return newAdWordsSession;
    } catch (ValidationException e) {
      .warn("Error @addUtilityUserAgent, returning unchanged AdWordsSession");
      return adWordsSession;
    }
  }

  
Creates a new AdWordsSession using the ads.properties file and using OAuth2.

Returns:
the AdWordsSession associated with the ads.properties file
Throws:
com.google.api.ads.common.lib.exception.OAuthException if problem with OAuth2
com.google.api.ads.common.lib.conf.ConfigurationLoadException if problems loading the ad.properties file
com.google.api.ads.common.lib.exception.ValidationException if the AdWordsSession did not validate
  public static AdWordsSession fromFileWithOAuth2()
    // Generate a refreshable OAuth2 credential 
    // and can be used in place of a service account.
    Credential oAuth2Credential = new OfflineCredentials.Builder()
        .forApi(.)
        .fromFile()
        .build()
        .generateCredential();
    // Construct and return an AdWordsSession.
    return new AdWordsSession.Builder()
        .fromFile()
        .withOAuth2Credential(oAuth2Credential)
        .build();
  }
New to GrepCode? Check out our FAQ X