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

com.microsoft.bingads.internal.ServiceUtils Maven / Gradle / Ivy

Go to download

The Bing Ads Java SDK is a library improving developer experience when working with the Bing Ads services by providing high-level access to features such as Bulk API, OAuth Authorization and SOAP API.

There is a newer version: 13.0.22.1
Show newest version
package com.microsoft.bingads.internal;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;


import jakarta.xml.ws.Response;

import com.microsoft.bingads.ApiEnvironment;
import com.microsoft.bingads.InternalException;

/**
 * Reserved for internal use. 
 */
public class ServiceUtils {
    private static String PROPRETY_PREFIX = "com.microsoft.bingads.";

    public static String TRACKING_KEY = "com.microsoft.bingads.trackingId";

    public static String REQUEST_HEADERS_KEY = "com.microsoft.bingads.requestHeaders";

    public static String TRACKING_HEADER_NAME = "TrackingId";    

    private static Properties FileProperties;

    public static String getPropertyFileName() {
        return System.getProperty("com.microsoft.bingads.propertyFileName", "bingads.properties");
    }

    static {
        String propertyFileName = getPropertyFileName();

        try(InputStream input =  ServiceUtils.class.getClassLoader().getResourceAsStream(propertyFileName)) {            
            if (input != null) {
                FileProperties = new Properties();

                FileProperties.load(input);                
            }
        } catch (IOException ex) {
            throw new InternalException(ex);
        }
    }

    public static String getPropertyValue(String name) {
        String systemPropertyName = name.startsWith(PROPRETY_PREFIX) 
            ? name 
            : PROPRETY_PREFIX + name;

        return System.getProperty(systemPropertyName, FileProperties != null ? FileProperties.getProperty(name) : null);
    }

    public static String getPropertyValue(String name, String valueIfNotFound) {
        String propertyValue = getPropertyValue(name);

        if (propertyValue == null) {
            return valueIfNotFound;
        }

        return propertyValue;
    }

    public static String GetTrackingId(Response response) {        
        Map context = response.getContext();
        return context != null ? context.get(TRACKING_KEY).toString() : "";
    }
    
    public static ApiEnvironment getEnvironmentFromConfig() {
        String envString = getPropertyValue("environment");

        if (envString == null) {
            return null;
        }

        return ApiEnvironment.fromValue(envString);
    }
    
    public static String getServiceUrlFromConfig(Class serviceInterface) {
        return getPropertyValue(serviceInterface.getCanonicalName() + ".url");
    }
    
    public static boolean getFallbackFlag() {
        String propertyValue = getPropertyValue("EnableFallbackToSoap");

        if (propertyValue == null) {
            return true;
        }

        return Boolean.parseBoolean(propertyValue);
    }

    public static String getClientName() {
        return getPropertyValue("ClientName");
    }

    public static boolean getEnableRestApi() {
        String propertyValue = getPropertyValue("EnableRestApi");

        if (propertyValue == null) {
            return false;
        }

        return Boolean.parseBoolean(propertyValue);
    }

    public static boolean getDisableRestApi(Class serviceInterface) {
        String propertyValue = getPropertyValue(serviceInterface.getSimpleName() + ".DisableRestApi");

        if (propertyValue == null) {
            return false;
        }

        return Boolean.parseBoolean(propertyValue);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy