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