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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
Aggregations