Created
December 13, 2015 01:08
-
-
Save venkyvb/4f5a9586c4f493a51352 to your computer and use it in GitHub Desktop.
SAP Cloud for customer CustomerQueryByElements Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:glob="http://sap.com/xi/SAPGlobal20/Global"> | |
<soapenv:Header/> | |
<soapenv:Body> | |
<glob:CustomerByElementsQuery_sync> | |
<CustomerSelectionByElements> | |
<SelectionByCategoryCode> | |
<InclusionExclusionCode>I</InclusionExclusionCode> | |
<IntervalBoundaryTypeCode>1</IntervalBoundaryTypeCode> | |
<LowerBoundaryCategoryCode>1</LowerBoundaryCategoryCode> | |
</SelectionByCategoryCode> | |
<PagingActiveIndicator>true</PagingActiveIndicator> | |
</CustomerSelectionByElements> | |
<ProcessingConditions> | |
<QueryHitsMaximumNumberValue>1000</QueryHitsMaximumNumberValue> | |
<QueryHitsUnlimitedIndicator>false</QueryHitsUnlimitedIndicator> | |
</ProcessingConditions> | |
</glob:CustomerByElementsQuery_sync> | |
</soapenv:Body> | |
</soapenv:Envelope> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:glob="http://sap.com/xi/SAPGlobal20/Global"> | |
<soapenv:Header/> | |
<soapenv:Body> | |
<glob:CustomerByElementsQuery_sync> | |
<CustomerSelectionByElements> | |
<SelectionByCategoryCode> | |
<InclusionExclusionCode>I</InclusionExclusionCode> | |
<IntervalBoundaryTypeCode>1</IntervalBoundaryTypeCode> | |
<LowerBoundaryCategoryCode>1</LowerBoundaryCategoryCode> | |
</SelectionByCategoryCode> | |
<PagingActiveIndicator>true</PagingActiveIndicator> | |
</CustomerSelectionByElements> | |
<ProcessingConditions> | |
<QueryHitsMaximumNumberValue>1000</QueryHitsMaximumNumberValue> | |
<QueryHitsUnlimitedIndicator>false</QueryHitsUnlimitedIndicator> | |
<LastReturnedObjectID>@LAST_RETURNED_OBJECT_ID@</LastReturnedObjectID> | |
</ProcessingConditions> | |
</glob:CustomerByElementsQuery_sync> | |
</soapenv:Body> | |
</soapenv:Envelope> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main.java.venkyvb.soap; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.xml.stream.XMLEventReader; | |
import javax.xml.stream.XMLInputFactory; | |
import javax.xml.stream.XMLStreamException; | |
import javax.xml.stream.events.EndElement; | |
import javax.xml.stream.events.StartElement; | |
import javax.xml.stream.events.XMLEvent; | |
import org.apache.commons.codec.binary.Base64; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.HttpClientBuilder; | |
public class SOAPTest { | |
boolean firstCallDone = false; | |
private String lastReturnedObjectId; | |
private boolean moreHitsAvailable = true; | |
private static int totalCount = 0; | |
private static String SERVICE_URL = "https://myNNNNNN.crm.ondemand.com/sap/bc/srt/scs/sap/querycustomerin1"; | |
public static void main(String[] args) throws ClientProtocolException, IOException, XMLStreamException { | |
SOAPTest runner = new SOAPTest(); | |
while (runner.hasNext()) { | |
totalCount = totalCount + runner.next().size(); | |
System.out.println(runner.hasNext()); | |
System.out.println(totalCount); | |
} | |
} | |
private boolean hasNext() { | |
return moreHitsAvailable; | |
} | |
private List<CustomerEntry> next() throws ClientProtocolException, IOException, XMLStreamException { | |
final HttpPost post = new HttpPost(SERVICE_URL); | |
String body = null; | |
if (!firstCallDone) { | |
body = IOUtils.toString(SOAPTest.class.getResourceAsStream("first_page_request.xml")); | |
firstCallDone = true; | |
} else { | |
body = IOUtils.toString(SOAPTest.class.getResourceAsStream("next_page_request.xml")); | |
body = body.replaceAll("@LAST_RETURNED_OBJECT_ID@", lastReturnedObjectId); | |
} | |
post.setHeader("Content-Type", "text/xml"); | |
post.setHeader("SOAPAction", "http://sap.com/xi/A1S/Global/QueryCustomerIn/FindByElementsRequest"); | |
post.setHeader("Authorization", getAuthorizationHeader()); | |
HttpEntity entity = new StringEntity(body); | |
post.setEntity(entity); | |
HttpResponse response = getHttpClient().execute(post); | |
InputStream is = response.getEntity().getContent(); | |
List<CustomerEntry> result = new ArrayList<CustomerEntry>(); | |
if (moreHitsAvailable == false) { | |
return result; | |
} | |
XMLInputFactory inputFactory = XMLInputFactory.newInstance(); | |
inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true); | |
XMLEventReader eventReader; | |
eventReader = inputFactory.createXMLEventReader(is); | |
CustomerEntry entry = null; | |
while (eventReader.hasNext()) { | |
XMLEvent event = eventReader.nextEvent(); | |
if (event.isStartElement()) { | |
StartElement startElement = event.asStartElement(); | |
if (startElement.getName().getLocalPart().toString().equals("Customer")) { | |
entry = new CustomerEntry(); | |
continue; | |
} | |
if (startElement.getName().getLocalPart().toString().equals("UUID")) { | |
entry.setUuid(eventReader.nextEvent().asCharacters().getData()); | |
continue; | |
} | |
if (startElement.getName().getLocalPart().toString().equals("InternalID")) { | |
entry.setInternalId(eventReader.nextEvent().asCharacters().getData()); | |
continue; | |
} | |
if (startElement.getName().getLocalPart().toString().equals("GivenName")) { | |
entry.setFirstName(eventReader.nextEvent().asCharacters().getData()); | |
continue; | |
} | |
if (startElement.getName().getLocalPart().toString().equals("FamilyName")) { | |
entry.setLastName(eventReader.nextEvent().asCharacters().getData()); | |
continue; | |
} | |
if (startElement.getName().getLocalPart().toString().equals("FamilyName")) { | |
entry.setLastName(eventReader.nextEvent().asCharacters().getData()); | |
continue; | |
} | |
if (startElement.getName().getLocalPart().toString().equals("MoreHitsAvailableIndicator")) { | |
moreHitsAvailable = Boolean.parseBoolean(eventReader.nextEvent().asCharacters().getData()); | |
continue; | |
} | |
if (startElement.getName().getLocalPart().toString().equals("LastReturnedObjectID")) { | |
lastReturnedObjectId = eventReader.nextEvent().asCharacters().getData(); | |
continue; | |
} | |
} | |
if (event.isEndElement()) { | |
EndElement endElement = event.asEndElement(); | |
if (endElement.getName().getLocalPart().toString().equals("Customer")) { | |
result.add(entry); | |
} | |
} | |
} | |
return result; | |
} | |
private HttpClient getHttpClient() { | |
return HttpClientBuilder.create().build(); | |
} | |
private String getAuthorizationHeader() { | |
String temp = new StringBuilder("USER_NAME").append(":").append("PASSWORD").toString(); | |
String result = "Basic " + new String(Base64.encodeBase64(temp.getBytes())); | |
return result; | |
} | |
private class CustomerEntry { | |
private String uuid; | |
private String internalId; | |
private String firstName; | |
private String lastName; | |
public void setUuid(String uuid) { | |
this.uuid = uuid; | |
} | |
public void setInternalId(String internalId) { | |
this.internalId = internalId; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
@Override | |
public String toString() { | |
return "CustomerEntry [uuid=" + uuid + ", internalId=" + internalId + ", firstName=" + firstName | |
+ ", lastName=" + lastName + "]"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment