Android Open Source - Swinedroid Web Transport Connection






From Project

Back to project page Swinedroid.

License

The source code is released under:

GNU General Public License

If you think the Android project Swinedroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.legind.web.WebTransport;
//  w w  w  .j  a v  a  2  s . c  o  m
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.KeyManagementException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;

import com.legind.ssl.SSLHandler.SSLHandler;

public class WebTransportConnection{
  private WebTransport parent;
  private SSLHandler sslhandler;
  private ArrayList<String> lastHeaders;
  private String lastDocument;
  private X509Certificate serverCertificate;
  
  public WebTransportConnection(WebTransport webtransport){
    lastDocument = new String();
    lastHeaders = new ArrayList<String>();
    parent = webtransport;
  }
  
  public void open() throws KeyManagementException, IOException{
    parent.setLocked(true);
    if(parent.getSsl()){
      sslhandler = new SSLHandler(parent.getHost(), parent.getPort());
      sslhandler.open();
      serverCertificate = sslhandler.getServerCertificate();
    }
  }
  
  public void close(){
    parent.setLocked(false);
    if(parent.getSsl()){
      sslhandler.close();
    }
  }
    
  /**
   * Send a request over the http(s) socket
   * @param requestLines an array of strings, each a line of the request 
   */
  public void sendRequest(String[] requestLines) throws IOException{
    try {
      if(parent.getSsl()){
        for(String requestLine : requestLines){
          sslhandler.writeLine(requestLine);
        }
        sslhandler.writeLine("");
      }
    } catch (IOException e){
      throw new IOException(e.toString());
    }
  }
  
  public void handleResponse() throws IOException, WebTransportException{
    handleHeaders();
    handleDocument();
  }
  
  /** Sort the response into headers, document */
  public void handleHeaders() throws IOException, WebTransportException{
    String line;
    lastHeaders.clear();
    if(parent.getSsl()){
      do{
        line = sslhandler.readLine();
        if(line.trim().contentEquals("HTTP/1.1 408 Request Timeout"))
          throw new WebTransportException("Request Timeout");
        if(line.trim() != "")
          lastHeaders.add(line.trim());
      } while(line.trim() != "");
    }
  }
  
  public void handleDocument() throws IOException, WebTransportException{
    if(!lastHeaders.isEmpty()){
      for(String header : lastHeaders){
        if(header.contains("Content-Length: ")){
          // Pass the Content-Length so readBuffer knows when to stop reading 
          lastDocument = sslhandler.readBuffer(Integer.parseInt(header.replace("Content-Length: ", "")));
          break;
        }
      }
    } else {
      throw new WebTransportException("Empty Header");
    }
  }
  
  public ArrayList<String> getLastHeaders(){
    return lastHeaders;
  }
  
  public String getLastDocument(){
    return lastDocument;
  }
  
  public BufferedInputStream getInputStream(){
    if(parent.getSsl()){
      return sslhandler.getInputStream();
    }
    return null;
  }
  
  public OutputStream getOutputStream(){
    if(parent.getSsl()){
      return sslhandler.getOutputStream();
    }
    return null;
  }
  
  public X509Certificate getServerCertificate(){
    return serverCertificate;
  }
  
}




Java Source Code List

com.legind.Dialogs.ErrorMessageHandler.java
com.legind.Dialogs.MessageHandler.java
com.legind.sqlite.AlertDbAdapter.java
com.legind.sqlite.DbAdapter.java
com.legind.sqlite.ServerDbAdapter.java
com.legind.ssl.CertificateInspect.CertificateInspect.java
com.legind.ssl.SSLHandler.SSLHandler.java
com.legind.ssl.TrustManagerFactory.TrustManagerFactory.java
com.legind.swinedroid.AlertList.java
com.legind.swinedroid.AlertSearch.java
com.legind.swinedroid.AlertView.java
com.legind.swinedroid.ServerEdit.java
com.legind.swinedroid.ServerHashDialog.java
com.legind.swinedroid.ServerView.java
com.legind.swinedroid.Swinedroid.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnableBindRequires.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnableManager.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnableRequires.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnableUniqueRequires.java
com.legind.swinedroid.NetworkRunnable.NetworkRunnable.java
com.legind.swinedroid.RequestService.RequestElement.java
com.legind.swinedroid.RequestService.Request.java
com.legind.swinedroid.xml.AlertListXMLElement.java
com.legind.swinedroid.xml.AlertListXMLHandler.java
com.legind.swinedroid.xml.AlertXMLElement.java
com.legind.swinedroid.xml.AlertXMLHandler.java
com.legind.swinedroid.xml.OverviewXMLElement.java
com.legind.swinedroid.xml.OverviewXMLHandler.java
com.legind.swinedroid.xml.XMLHandlerException.java
com.legind.swinedroid.xml.XMLHandler.java
com.legind.web.WebTransport.WebTransportConnection.java
com.legind.web.WebTransport.WebTransportException.java
com.legind.web.WebTransport.WebTransport.java
org.achartengine.chartlib.AbstractChart.java
org.achartengine.chartlib.AlertChart.java