Informatica ActiveVOS
- Informatica ActiveVOS 9.2.6
- All Products
public class SimpleXmlServiceRequest { /** * Class to hold service response data. */ static class ServiceResponse { static final int SUCCESS = 0; static final int FAULTED = 1; static final int ERROR = 2; /** Code indicating if the service invoke was a success, fault or other error.*/ int responseCode; /** Response xml data */ String responseData; } /** * Invokes XML service using POST method returns service response. */ public static ServiceResponse invokeService(URL aXmlServiceUrl, String aXmlPayload, String aUsername, String aPassword) throws IOException { HttpURLConnection httpConnection = null; BufferedReader reader = null; OutputStreamWriter writer = null; try { // create connection URLConnection c = aXmlServiceUrl.openConnection(); httpConnection = (HttpURLConnection)c; httpConnection.setRequestProperty("Content-Type", "text/xml"); httpConnection.setRequestProperty("Content-Length", Integer.toString(aXmlPayload.length())); // Set credentials (if secured using BASIC auth). if (aUsername != null && aPassword != null) { // code to set the authorization header (e.g. BASIC) } httpConnection.setDoOutput(true); httpConnection.setInstanceFollowRedirects(true); // send the payload writer = new OutputStreamWriter(httpConnection.getOutputStream()); writer.write(aXmlPayload); writer.flush(); // read response if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK || httpConnection.getResponseCode() == HttpURLConnection.HTTP_ACCEPTED ) { reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream())); } else { reader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream())); } // read response StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } ServiceResponse response = new ServiceResponse(); response.responseData = sb.toString(); if ( httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK || httpConnection.getResponseCode() == HttpURLConnection.HTTP_ACCEPTED ) { // Success! response.responseCode = ServiceResponse.SUCCESS; } else if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR && httpConnection.getContentType().toLowerCase().startsWith("text/xml") ) { // Faulted! (response code is 500 and content-type is text/xml response.responseCode = ServiceResponse.FAULTED; } else { // http/transport or other error response.responseCode = ServiceResponse.ERROR; } return response; } finally { // // clean up code goes here. E.g.: close writer, reader and disconnect http connection. } } public static void main(String[] args) { // Create sample request. In this example, the sample request is created using // a string. Ideally, the XML element should be built using DOM i.e. with DocumentBuilderFactory, // and DocumentBuilder. String xmlRequest = "<loan:loanProcessRequest xmlns:loan=\"http://schemas.active-endpoints.com/sample/LoanRequest/2008/02/loanRequest.xsd\">\n" + " <loan:loanType>Automobile</loan:loanType>\n" + " <loan:firstName>John</loan:firstName>\n" + " <loan:lastName>Smith</loan:lastName>\n" + " <loan:dayPhone>2039299400</loan:dayPhone>\n" + " <loan:nightPhone>2035551212</loan:nightPhone>\n" + " <loan:socialSecurityNumber>123-45-6789</loan:socialSecurityNumber>\n" + " <loan:amountRequested>15000</loan:amountRequested>\n" + " <loan:loanDescription>Application to finance the purchase of a Toyota Prius</loan:loanDescription>\n" + " <loan:otherInfo>Down payment is US$7500</loan:otherInfo>\n" + " <loan:responseEmail>john.smith@example.com</loan:responseEmail>\n" + "</loan:loanProcessRequest>"; try { URL loanRequestUrl = new URL( "http://localhost:8080/active-bpel/services/XML/humantaskProcessDemoService"); System.out.println("Invoking service..."); ServiceResponse response = invokeService(loanRequestUrl, xmlRequest, "username", "password"); if ( response.responseCode == ServiceResponse.SUCCESS ) { System.out.println("Success:"); } else if ( response.responseCode == ServiceResponse.FAULTED ) { System.out.println("Faulted:"); } else { System.out.println("Error:"); } System.out.println(response.responseData); } catch (Exception e) { e.printStackTrace(); } } }