Code example for HttpContext

Methods: getAttribute

0
			// Send the post request async 
			StringEntity postRequestEntity = new StringEntity(postRequestMsg);
			httpPost.setEntity(postRequestEntity);
			 
			StatusLog.updateLog().updateStatusMsg("Sent Http Post  request"); 
			HttpRequestAsync sendReqAsync = new HttpRequestAsync(mMainActivity, this, mHttpContext, mErrorLogHandler);
			sendReqAsync.execute(httpPost);
	         
		} catch (IOException e) {
			StatusLog.updateLog().updateErrorMsg("IO exception"); 
		} catch (IllegalStateException e) {
			StatusLog.updateLog().updateErrorMsg("Illegal State Exception"); 
		} 
	} 
	 
	/** 
	 * Callback when you get response for the Http request 
	 * Validate the response and pass it for parsing the response. 
	 */ 
	public void receivedResponse(HttpResponse response) {
		 
		if(response != null) {
			 
			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				StatusLog.updateLog().updateErrorMsg(response.getStatusLine().toString());
				 
			} else {     	 
				 
				StatusLog.updateLog().updateStatusMsg("Got HTTP Response"); 
				try { 
					 
					InputStream inputStream = response.getEntity().getContent();
					Reader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
					 
					// Read the data from the InputStream 
					char[] buffer = new char[100];
					StringBuilder readData = new StringBuilder();
					int read;
					 
					do { 
					  read = inputStreamReader.read(buffer, 0, buffer.length);
					  if (read>0) {
						  readData.append(buffer, 0, read);
					  } 
					} while (read>=0);
															 
					// Generate the doc for xml parsing 
					DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			        DocumentBuilder docBuilder = factory.newDocumentBuilder();
			        InputStream is = new ByteArrayInputStream(readData.toString().getBytes());
					Document xmlDoc = docBuilder.parse(is);
			        															 
					if(xmlDoc == null) {
						StatusLog.updateLog().updateErrorMsg("Not a valid XML response"); 
						return; 
					} 
						 
					// Get the root node and validate it. 
					Node rootNode = xmlDoc.getFirstChild();
					if(rootNode == null) {
						StatusLog.updateLog().updateErrorMsg("Not a valid XML response"); 
						return; 
					} 
					 
					if( rootNode.getNodeType() != Node.ELEMENT_NODE) {
						StatusLog.updateLog().updateErrorMsg("Not a valid XML response"); 
						return; 
					} 
					 
					if(rootNode.getNodeName().compareTo("auth") == 0) {
						 
						//Generate the Dialog based on XML 
						DialogFromXml dialogFromXml = new DialogFromXml(mMainActivity, this);
						int retVal = dialogFromXml.parseXmlResponse(rootNode, response.getAllHeaders());
						if(retVal == -1) {
							StatusLog.updateLog().updateErrorMsg("Error in parsing the xml"); 
						} else if(retVal == 2) {
							//TODO: For success case 
						} 
						 
					} else if(rootNode.getNodeName().compareTo("AnyConnectProfile") == 0) {
												 
				        //Write the configuration xml into a file.  
						FileOutputStream fileOutputStream = mMainActivity.openFileOutput("config.xml", Context.MODE_PRIVATE);
						OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream); 
						// Write the string to the file 
						outputStreamWriter.write(readData.toString());
						outputStreamWriter.flush();
						fileOutputStream.close();
						 
					} else {	 
						StatusLog.updateLog().updateErrorMsg("XML response has no valid root node" ); 
					} 
					 
				} catch (SAXException e) {
					StatusLog.updateLog().updateErrorMsg("Failed to parse server response");		 
				} catch (IOException e) {
					StatusLog.updateLog().updateErrorMsg("IO exception"); 
				} catch (ParserConfigurationException e) {
					StatusLog.updateLog().updateErrorMsg("IllegalStateException"); 
				}  
				catch (IllegalStateException e) {
					StatusLog.updateLog().updateErrorMsg("IllegalStateException"); 
				}  
			} 
		} 
	} 
	 
	/** 
	 * Get the redirected url from the response 
	 */ 
	public String getRedirectedURL() {
		 
		if(mHttpContext != null) { 
			 
			HttpUriRequest currentReq = (HttpUriRequest) mHttpContext.getAttribute(
					ExecutionContext.HTTP_REQUEST);
			HttpHost currentHost = (HttpHost)  mHttpContext.getAttribute( 
	                ExecutionContext.HTTP_TARGET_HOST);
			 
			if(currentReq != null && currentHost != null)
				return currentHost.toURI() + currentReq.getURI();