Cisco Cisco Security Manager 4.11 User Guide

Page of 301
 
 
 
  
 
Cisco Security Manager 4.11 API Specification (Version 2.3) 
 
Page  232
 
            sslContext = SSLContext.getInstance(
"SSL"
); 
            sslContext.init(
null
new
 TrustManager[] { 
new
 X509TrustManager() { 
                
public
 X509Certificate[] getAcceptedIssuers() { 
                    System.
out
.println(
"getAcceptedIssuers ============="
); 
                    
return
 
null
                } 
                
public
 
void
 checkClientTrusted(X509Certificate[] certs, String authType) { 
                    System.
out
.println(
"checkClientTrusted ============="
); 
                } 
                
public
 
void
 checkServerTrusted(X509Certificate[] certs, String authType) { 
                    System.
out
.println(
"checkServerTrusted ============="
); 
                } 
            }}, 
new
 SecureRandom()); 
 
 
            SSLSocketFactory sf = 
new
 SSLSocketFactory(sslContext); 
 
            Scheme httpsScheme = 
new
 Scheme(
"https"
, 443, sf); 
            SchemeRegistry schemeRegistry = 
new
 SchemeRegistry(); 
            schemeRegistry.register(httpsScheme); 
            HttpParams params = 
new
 BasicHttpParams(); 
            ClientConnectionManager cm = 
new
 ThreadSafeClientConnManager(params, schemeRegistry); 
            
httpclient
 = 
new
 DefaultHttpClient(cm, params); 
        } 
catch
 (NoSuchAlgorithmException e) { 
            e.printStackTrace(); 
        } 
catch
 (KeyManagementException e) { 
            e.printStackTrace(); 
        } 
    } 
 
    
/**
 
     * This method will send the XML payload and return the XML response as a string.
 
     * 
@param
 uri
 
     * 
@param
 host
 
     * 
@param
 isCookieNeeded
 
     * 
@return
 
     * 
@throws
 IOException
 
     * 
@throws
 ClientProtocolException
 
     */
 
    
public
 
void
 doPost (URI uri, String payload, String host, 
boolean
 isCookieNeeded) 
throws
 
Exception { 
 
        HttpResponse httpresponse = 
null
        HttpPost httppost = 
new
 HttpPost (uri); 
 
        
if
 (isCookieNeeded) { 
            
httpclient
.setCookieStore(
ascookie
); 
        } 
        httppost.addHeader(
"Content-Type"
"text/xml"
); 
        StringEntity strEntity = 
new
 StringEntity (payload, 
"UTF-8"
); 
        httppost.setEntity(strEntity); 
        System.
out
.println(
"Calling : "
+uri.toString()); 
        httpresponse = 
httpclient
.execute(httppost); 
        
ascookie
 = 
httpclient
.getCookieStore(); 
        processResponse(httpresponse); 
 
    } 
 
    
public
 
void
 processResponse (HttpResponse httpresponse) 
throws
 Exception, IOException, 
SAXException { 
        HttpEntity ent = httpresponse.getEntity(); 
        String response = EntityUtils.toString(ent); 
        DocumentBuilder domp = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
        Document doc = domp.parse(
new
 ByteArrayInputStream(response.getBytes())); 
         
        NodeList errorNodes = doc.getDocumentElement().getElementsByTagName(
"code"
); 
        
for
 (
int
 i = 0; i < errorNodes.getLength(); i++) { 
            Element element = (Element) errorNodes.item(i); 
            
if
(element.getTextContent() != 
null
 && !element.getTextContent().equals(
""
)) {