Cisco Cisco Security Manager 4.0 仕様ガイド

ページ / 217
 
 
 
  
 
Cisco Security Manager 4.4 API Specification (Version 1.1) 
 
OL- 29074-01 
Page  188
 
            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(
""
)) { 
                NodeList nodes = doc.getDocumentElement().getElementsByTagName(
"description"
); 
                
for
 (
int
 j = 0; j < nodes.getLength(); j++) { 
                    Element element2 = (Element) nodes.item(j); 
                    
throw
 
new
 Exception(element2.getTextContent()); 
                } 
            } 
        } 
        
if
(response != 
null
 && response.trim().length() != 0){ 
            StatusLine sl; 
            
if
 ((sl = httpresponse.getStatusLine()) != 
null
) { 
                
if
 (sl.getStatusCode() == HttpStatus.
SC_UNAUTHORIZED
) { 
                    System.
out
.println(
"Hit Authorization exception"
); 
                    System.
out
.println(response); 
                    
//Do something...
 
                }
else
 
if
 (sl.getStatusCode() == HttpStatus.
SC_OK
) { 
                    System.
out
.println(
"The request is a success..."
); 
                    System.
out
.println(response);