Search in sources :

Example 1 with ServiceException

use of com.google.api.ads.common.lib.exception.ServiceException in project googleads-java-lib by googleads.

the class AdManagerAxisHeaderHandler method setHeaders.

/**
 * @see HeaderHandler#setHeaders(Object, com.google.api.ads.common.lib.client.AdsSession,
 *     com.google.api.ads.common.lib.client.AdsServiceDescriptor)
 */
@Override
public void setHeaders(Object soapClient, AdManagerSession adManagerSession, AdManagerServiceDescriptor adManagerServiceDescriptor) throws AuthenticationException, ServiceException {
    try {
        Preconditions.checkArgument(soapClient instanceof Stub, "soapClient must be Stub but was: %s", soapClient);
        Stub stub = (Stub) soapClient;
        adManagerHttpHeaderHandler.setHttpHeaders(soapClient, adManagerSession);
        Object soapHeader = createSoapHeader(adManagerServiceDescriptor);
        BeanUtils.setProperty(soapHeader, "applicationName", userAgentCombiner.getUserAgent(adManagerSession.getApplicationName()));
        if (adManagerSession.getNetworkCode() != null) {
            BeanUtils.setProperty(soapHeader, "networkCode", adManagerSession.getNetworkCode());
        }
        setAuthenticationHeaders(soapClient, adManagerSession);
        String namespace = adManagerApiConfiguration.getNamespacePrefix() + "/" + adManagerServiceDescriptor.getVersion();
        soapClientHandler.setHeader(stub, namespace, "RequestHeader", soapHeader);
        soapClientHandler.setCompression(stub, adsLibConfiguration.isCompressionEnabled());
        soapClientHandler.setRequestTimeout(stub, adsLibConfiguration.getSoapRequestTimeout());
    } catch (InstantiationException e) {
        throw new ServiceException("Unexpected exception.", e);
    } catch (IllegalAccessException e) {
        throw new ServiceException("Unexpected exception.", e);
    } catch (ClassNotFoundException e) {
        throw new ServiceException("Unexpected exception.", e);
    } catch (InvocationTargetException e) {
        throw new ServiceException("Unexpected exception.", e);
    } catch (IllegalArgumentException e) {
        throw new ServiceException("Unexpected exception.", e);
    } catch (SecurityException e) {
        throw new ServiceException("Unexpected exception.", e);
    }
}
Also used : ServiceException(com.google.api.ads.common.lib.exception.ServiceException) Stub(org.apache.axis.client.Stub) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ServiceException

use of com.google.api.ads.common.lib.exception.ServiceException in project googleads-java-lib by googleads.

the class AdManagerJaxWsHeaderHandler method constructSoapHeader.

/**
 * Constructs a SOAP header object ready to be attached to a JAX-WS client.
 *
 * @param headerData a map of SOAP header element names to values
 * @param adManagerServiceDescriptor the descriptor of which service's headers are being created
 * @return a SOAP header object
 */
private SOAPElement constructSoapHeader(Map<String, Object> headerData, AdManagerServiceDescriptor adManagerServiceDescriptor) {
    String requestHeaderNamespace = adManagerApiConfiguration.getNamespacePrefix() + "/" + adManagerServiceDescriptor.getVersion();
    try {
        SOAPFactory soapFactory = SOAPFactory.newInstance();
        SOAPElement requestHeader = soapFactory.createElement(REQUEST_HEADER_LOCAL_PART, "ns1", requestHeaderNamespace);
        for (String headerElementName : headerData.keySet()) {
            if (headerData.get(headerElementName) != null) {
                SOAPElement newElement = requestHeader.addChildElement(headerElementName, "ns1", requestHeaderNamespace);
                newElement.addTextNode(headerData.get(headerElementName).toString());
            }
        }
        return requestHeader;
    } catch (SOAPException e) {
        throw new ServiceException("Unexpected exception.", e);
    }
}
Also used : ServiceException(com.google.api.ads.common.lib.exception.ServiceException) SOAPException(javax.xml.soap.SOAPException) SOAPElement(javax.xml.soap.SOAPElement) SOAPFactory(javax.xml.soap.SOAPFactory)

Example 3 with ServiceException

use of com.google.api.ads.common.lib.exception.ServiceException in project googleads-java-lib by googleads.

the class AdWordsAxisHeaderHandler method setHeaders.

/**
 * @see HeaderHandler#setHeaders(Object,
 *      com.google.api.ads.common.lib.client.AdsSession,
 *      com.google.api.ads.common.lib.client.AdsServiceDescriptor)
 */
@Override
public void setHeaders(Object soapClient, AdWordsSession adWordsSession, AdWordsServiceDescriptor adWordsServiceDescriptor) throws AuthenticationException, ServiceException {
    try {
        Preconditions.checkArgument(soapClient instanceof Stub, "soapClient must be Stub but was: %s", soapClient);
        Stub stub = (Stub) soapClient;
        Object soapHeader = soapHeaderFactory.createSoapHeader(adWordsServiceDescriptor);
        String namespace = adWordsApiConfiguration.getNamespacePrefix() + "/" + adWordsServiceDescriptor.getPackageGroup() + "/" + adWordsServiceDescriptor.getVersion();
        soapClientHandler.setHeader(stub, namespace, REQUEST_HEADER_LOCAL_PART, soapHeader);
        soapClientHandler.setHeaderChild(stub, REQUEST_HEADER_LOCAL_PART, "developerToken", adWordsSession.getDeveloperToken());
        soapClientHandler.setHeaderChild(stub, REQUEST_HEADER_LOCAL_PART, "clientCustomerId", adWordsSession.getClientCustomerId());
        soapClientHandler.setHeaderChild(stub, REQUEST_HEADER_LOCAL_PART, "userAgent", userAgentCombiner.getUserAgent(adWordsSession.getUserAgent()));
        soapClientHandler.setHeaderChild(stub, REQUEST_HEADER_LOCAL_PART, "validateOnly", adWordsSession.isValidateOnly());
        soapClientHandler.setHeaderChild(stub, REQUEST_HEADER_LOCAL_PART, "partialFailure", adWordsSession.isPartialFailure());
        soapClientHandler.setCompression(stub, adsLibConfiguration.isCompressionEnabled());
        soapClientHandler.setRequestTimeout(stub, adsLibConfiguration.getSoapRequestTimeout());
        authorizationHeaderHandler.setAuthorization(soapClient, adWordsSession);
    } catch (InstantiationException | IllegalAccessException | ServiceException | ClassNotFoundException | InvocationTargetException | NoSuchMethodException e) {
        throw new ServiceException("Unexpected exception setting headers for: " + adWordsServiceDescriptor, e);
    }
}
Also used : ServiceException(com.google.api.ads.common.lib.exception.ServiceException) Stub(org.apache.axis.client.Stub) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with ServiceException

use of com.google.api.ads.common.lib.exception.ServiceException in project googleads-java-lib by googleads.

the class BaseAdsServiceClientFactoryHelper method createAdsServiceClient.

/**
 * Creates an {@link AdsServiceClient} given an {@link AdsServiceDescriptor}
 * descriptor and an {@link AdsSession}.
 *
 * @param adsServiceDescriptor descriptor with information on ads service
 * @param adsSession the session associated with the desired
 *     client
 * @return the created {@link AdsServiceClient}
 * @throws ServiceException if the ads service client could not be created
 */
@Override
public C createAdsServiceClient(D adsServiceDescriptor, S adsSession) throws ServiceException {
    Object soapClient = createSoapClient(adsServiceDescriptor);
    C adsServiceClient = createServiceClient(soapClient, adsServiceDescriptor, adsSession);
    try {
        adsServiceClient.setEndpointAddress(adsServiceDescriptor.getEndpointAddress(adsSession.getEndpoint()));
    } catch (MalformedURLException e) {
        throw new ServiceException("Unexpected exception", e);
    }
    return adsServiceClient;
}
Also used : MalformedURLException(java.net.MalformedURLException) ServiceException(com.google.api.ads.common.lib.exception.ServiceException)

Example 5 with ServiceException

use of com.google.api.ads.common.lib.exception.ServiceException in project googleads-java-lib by googleads.

the class JaxWsHandler method setHeaderChildString.

/**
 * Adds a child text node named childName to the existing header named headerName.
 *
 * @param soapClient the binding provider
 * @param headerName the name of the existing header
 * @param childNamespace the namespace of the new child
 * @param childName the name of the new child
 * @param childValue the value of the new child
 *
 * @throws NullPointerException if no header exists named headerName
 */
public void setHeaderChildString(BindingProvider soapClient, final String headerName, String childNamespace, String childName, String childValue) {
    // Find the parent header SOAPElement
    SOAPElement parentHeader = (SOAPElement) getHeader(soapClient, headerName);
    Preconditions.checkNotNull(parentHeader, "No header element found with name: %s", headerName);
    // Add a SOAPElement for the child
    try {
        SOAPElement childElement = parentHeader.addChildElement(new QName(childNamespace, childName));
        childElement.setTextContent(childValue);
    } catch (SOAPException e) {
        throw new ServiceException("Failed to set header for child " + childName, e);
    }
}
Also used : ServiceException(com.google.api.ads.common.lib.exception.ServiceException) QName(javax.xml.namespace.QName) SOAPException(javax.xml.soap.SOAPException) SOAPElement(javax.xml.soap.SOAPElement)

Aggregations

ServiceException (com.google.api.ads.common.lib.exception.ServiceException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 SOAPException (javax.xml.soap.SOAPException)5 SOAPElement (javax.xml.soap.SOAPElement)4 QName (javax.xml.namespace.QName)3 Stub (org.apache.axis.client.Stub)3 SOAPFactory (javax.xml.soap.SOAPFactory)2 SOAPHeaderElement (org.apache.axis.message.SOAPHeaderElement)2 RequestInfo (com.google.api.ads.common.lib.client.RequestInfo)1 ResponseInfo (com.google.api.ads.common.lib.client.ResponseInfo)1 SoapClientHandler (com.google.api.ads.common.lib.soap.SoapClientHandler)1 AxisCompatible (com.google.api.ads.common.lib.soap.compatability.AxisCompatible)1 JaxWsCompatible (com.google.api.ads.common.lib.soap.compatability.JaxWsCompatible)1 MalformedURLException (java.net.MalformedURLException)1 SOAPHeader (javax.xml.soap.SOAPHeader)1 SOAPMessage (javax.xml.soap.SOAPMessage)1 BindingProvider (javax.xml.ws.BindingProvider)1 Handler (javax.xml.ws.handler.Handler)1 EngineConfiguration (org.apache.axis.EngineConfiguration)1 Service (org.apache.axis.client.Service)1