Search in sources :

Example 6 with ServiceException

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

the class AdWordsJaxWsHeaderHandler 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 adWordsServiceDescriptor the descriptor of which service's headers
 *     are being created
 * @return a SOAP header object
 */
private SOAPElement constructSoapHeader(Map<String, Object> headerData, AdWordsServiceDescriptor adWordsServiceDescriptor) {
    String requestHeaderNamespace = adWordsApiConfiguration.getNamespacePrefix() + "/" + adWordsServiceDescriptor.getPackageGroup() + "/" + adWordsServiceDescriptor.getVersion();
    String requestElementsNamespace = adWordsApiConfiguration.getNamespacePrefix() + "/cm/" + adWordsServiceDescriptor.getVersion();
    try {
        SOAPFactory soapFactory = SOAPFactory.newInstance();
        SOAPElement requestHeader = soapFactory.createElement(new QName(requestHeaderNamespace, REQUEST_HEADER_LOCAL_PART));
        for (String headerElementName : headerData.keySet()) {
            if (headerData.get(headerElementName) != null) {
                SOAPElement newElement = requestHeader.addChildElement(headerElementName, null, requestElementsNamespace);
                newElement.addTextNode(headerData.get(headerElementName).toString());
            }
        }
        return requestHeader;
    } catch (SOAPException e) {
        throw new ServiceException("Unexpected exception constructing SOAP header for: " + adWordsServiceDescriptor, 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) SOAPFactory(javax.xml.soap.SOAPFactory)

Example 7 with ServiceException

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

the class AxisHandler method setHeader.

/**
 * @see  SoapClientHandler#setHeader(Object, String, String, Object)
 */
@Override
public void setHeader(Stub soapClient, String namespace, String headerName, Object headerValue) {
    try {
        QName qName = new QName(namespace, headerName);
        SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement(qName);
        soapHeaderElement.setObjectValue(headerValue);
        soapHeaderElement.setActor(null);
        soapClient.setHeader(soapHeaderElement);
    } catch (SOAPException e) {
        throw new ServiceException("Could not set header.", e);
    }
}
Also used : SOAPHeaderElement(org.apache.axis.message.SOAPHeaderElement) ServiceException(com.google.api.ads.common.lib.exception.ServiceException) QName(javax.xml.namespace.QName) SOAPException(javax.xml.soap.SOAPException)

Example 8 with ServiceException

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

the class AxisHandler method setHeaderChild.

/**
 * Updates the child attribute of headerName named childName to childValue.
 *
 * @param soapClient the stub
 * @param parentHeaderName the name of the parent header
 * @param childName the name of the child
 * @param childValue the value for the child
 *
 * @throws NullPointerException if no header exists named parentHeaderName
 */
public void setHeaderChild(Stub soapClient, String parentHeaderName, String childName, Object childValue) {
    SOAPHeaderElement headerElement = (SOAPHeaderElement) getHeader(soapClient, parentHeaderName);
    Object headerObject = Preconditions.checkNotNull(headerElement, "Parent header named %s does not exist", parentHeaderName).getObjectValue();
    try {
        BeanUtils.setProperty(headerObject, childName, childValue);
    } catch (IllegalAccessException e) {
        throw new ServiceException("Failed to set header child " + childName, e);
    } catch (InvocationTargetException e) {
        throw new ServiceException("Failed to set header child " + childName, e);
    }
}
Also used : SOAPHeaderElement(org.apache.axis.message.SOAPHeaderElement) ServiceException(com.google.api.ads.common.lib.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with ServiceException

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

the class AxisHandler method createSoapClient.

/**
 * Creates a SOAP client using a SOAP service descriptor.
 *
 * @param soapServiceDescriptor the descriptor to use for creating a client
 * @return the SOAP client for this descriptor
 * @throws ServiceException thrown if the SOAP client cannot be created
 */
@Override
public Stub createSoapClient(SoapServiceDescriptor soapServiceDescriptor) throws ServiceException {
    try {
        if (soapServiceDescriptor instanceof AxisCompatible) {
            AxisCompatible axisCompatibleService = (AxisCompatible) soapServiceDescriptor;
            EngineConfiguration engineConfiguration = engineConfigurationFactory.getClientEngineConfig();
            Service locator = (Service) axisCompatibleService.getLocatorClass().getConstructor(new Class[] { EngineConfiguration.class }).newInstance(new Object[] { engineConfiguration });
            return (Stub) locator.getClass().getMethod("getPort", Class.class).invoke(locator, soapServiceDescriptor.getInterfaceClass());
        }
        throw new ServiceException("Service [" + soapServiceDescriptor + "] not compatible with Axis", null);
    } catch (SecurityException e) {
        throw new ServiceException("Unexpected Exception.", e);
    } catch (NoSuchMethodException e) {
        throw new ServiceException("Unexpected Exception.", e);
    } catch (IllegalArgumentException e) {
        throw new ServiceException("Unexpected Exception.", e);
    } catch (IllegalAccessException e) {
        throw new ServiceException("Unexpected Exception.", e);
    } catch (InvocationTargetException e) {
        throw new ServiceException("Unexpected Exception.", e);
    } catch (ClassNotFoundException e) {
        throw new ServiceException("Unexpected Exception.", e);
    } catch (InstantiationException e) {
        throw new ServiceException("Unexpected Exception.", e);
    }
}
Also used : Service(org.apache.axis.client.Service) Stub(org.apache.axis.client.Stub) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServiceException(com.google.api.ads.common.lib.exception.ServiceException) EngineConfiguration(org.apache.axis.EngineConfiguration) AxisCompatible(com.google.api.ads.common.lib.soap.compatability.AxisCompatible)

Example 10 with ServiceException

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

the class AdsServiceClientFactoryTest method testGetServiceClient_failPreconditions.

/**
 * Test to verify that a ServiceException is thrown if the preconditions check by the underlying
 * AdsServiceClientFactoryHelper fails.
 */
@Test
public void testGetServiceClient_failPreconditions() {
    String version = "v1.1";
    ServiceException serviceException = new ServiceException("A service exception");
    doThrow(serviceException).when(adsServiceClientFactoryHelper).checkServiceClientPreconditions(adsSession, MockSoapClientInterface.class);
    when(adsServiceClientFactoryHelper.determineVersion(MockSoapClientInterface.class)).thenReturn(version);
    when(adsServiceClientFactoryHelper.createServiceDescriptor(MockSoapClientInterface.class, version)).thenReturn(adsServiceDescriptor);
    when(adsServiceClientFactoryHelper.createAdsServiceClient(adsServiceDescriptor, adsSession)).thenReturn(adsServiceClient);
    Mockito.<Class<?>>when(adsServiceDescriptor.getInterfaceClass()).thenReturn(MockSoapClientInterface.class);
    when(adsServiceClient.getSoapClient()).thenReturn(soapServiceClient);
    thrown.expect(Matchers.<Exception>is(serviceException));
    adsServiceClientFactory.getServiceClient(adsSession, MockSoapClientInterface.class);
}
Also used : ServiceException(com.google.api.ads.common.lib.exception.ServiceException) Test(org.junit.Test)

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