CTC++ Coverage Report - Execution Profile    #32/32

Directory Summary | Files Summary | Functions Summary | Execution Profile
To files: First | Previous | Next | Last | Index | No Index


File: net/library/jiga/XmlRequest.java
Instrumentation mode: function-decision-multicondition
TER: 0 % ( 0/ 66)

Start/ End/    
True False - Line Source

  1 package net.library.jiga; 
  2  
  3 import java.io.BufferedInputStream; 
  4 import java.net.HttpURLConnection; 
  5 import java.net.URL; 
  6  
  7 /** 
  8  *  Provide simple layout to make an HTTP request to the server 
  9  */ 
  10 public class XmlRequest implements Runnable { 
  11     
  12    /** POST Method */ 
  13    public final static String METHOD_POST = "POST"; 
  14    /** GET Method */ 
  15    public final static String METHOD_GET = "GET"; 
  16     
  17    /** Successfully completed */ 
  18    public final static int STATE_OK = 0; 
  19    /** Not yet completed */ 
  20    public final static int STATE_INCOMPLETE = 1;    
  21    /** Request is invalid, can't execute */  
  22    public final static int STATE_INVALID_REQUEST = 3; 
  23    /** Timeout */ 
  24    public final static int STATE_TIMEOUT = 4; 
  25    /** Completed with errors */ 
  26    public final static int STATE_ERROR = 5; 
  27     
  28    /** Timeout */ 
  29    private final static int REQUEST_TIMEOUT = 30000; // 30 secondes  
  30     
  31    /** CodeBase URL */ 
  32    private URL codeBase; 
  33    /** Method (POST / GET) */ 
  34    private String method; 
  35    /** URL to call */ 
  36    private String url; 
  37    /** Data */ 
  38    private String data; 
  39    /** Should this call be synchronous */ 
  40    private boolean async; 
  41    /** Complete state */ 
  42    private int state; 
  43    /** Answer */ 
  44    private String answer; 
  45     
 
- 46    public XmlRequest(GameApplet applet) { 
  47       this(applet.getCodeBase()); 
  48    } 
  49     
 
- 50    public XmlRequest(URL url) { 
  51       codeBase = url; 
  52    } 
  53        
  54    /** 
  55     * Defines what to call 
  56     * @param method Must be POST or GET. Set to POST if not understood 
  57     * @param url url to call (Will be completed with the codebase value) 
  58     * @param async defines asynchronous call 
  59     */ 
 
- 60    public void open(String method, String url, boolean async) { 
  61  
- 62       if (METHOD_POST.equals(method) || METHOD_GET.equals(method)) { 
 - 62   T || _
 - 62   F || T
 - 62   F || F
  63          this.method = method; 
  64       } 
    65       else { 
  66          this.method = METHOD_POST; 
  67       } 
  68        
- 69       if (url == null) { 
  70          url = "/"; 
  71       } 
    72       else { 
  73          this.url = url; 
  74       } 
  75       this.async = async; 
  76        
  77       this.state = STATE_INCOMPLETE; 
  78    } 
  79     
  80    /** 
  81     * Calls server.   
  82     * @param data form parameters, concatened to url (GET) or added to body (POST) 
  83     */ 
 
- 84    public void send(String data) { 
  85        
  86       this.data = data; 
  87        
- 88       if (method == null) { 
  89          // Do nothing if open() was not called 
  90          state = STATE_INVALID_REQUEST; 
  91           
 - 92          return;  
  93       } 
  94        
  95       state = STATE_INCOMPLETE; 
  96        
- 97       if (async) { 
  98          new Thread(this).start(); 
  99       } 
    100       else { 
  101          run(); 
  102       } 
  103    } 
  104     
 
- 105    public void run() { 
- 106       if (method != null && "http".equalsIgnoreCase(codeBase.getProtocol())) { // Avoid direct invalid call 
 - 106   T && T
 - 106   T && F
 - 106   F && _
- 107          try { 
  108             state = STATE_INCOMPLETE; 
  109             answer = null; 
  110              
  111             long timeLimit = System.currentTimeMillis() + REQUEST_TIMEOUT; 
  112  
  113             // Build URL 
  114             String urlString = "http://"; 
  115             urlString += codeBase.getHost(); 
  116              
- 117             if (codeBase.getPort() != -1 && codeBase.getPort() != 80) { 
 - 117       T && T
 - 117       T && F
 - 117       F && _
  118                urlString += ":" + String.valueOf(codeBase.getPort()); 
  119             } 
  120  
- 121             if (!url.startsWith("/")) { 
  122                urlString += "/"; 
  123             } 
  124             urlString += url; 
  125  
  126             // Add GET data 
  127             if (METHOD_GET.equals(method)  
  128                   && data != null  
- 129                   && data.trim().length() > 0) { 
 - 129       T && T && T
 - 129       T && T && F
 - 129       T && F && _
 - 129       F && _ && _
  130                // Add parameters 
- 131                if (url.indexOf("?") != -1) { 
  132                   urlString += "&"; 
  133                } 
    134                else { 
  135                   urlString += "?"; 
  136                } 
  137                 
  138                urlString += data; 
  139                 
  140                // Reset data 
  141                data = new String(); 
  142             } 
  143              
- 144             if (data == null) { 
  145                data = new String(); 
  146             } 
  147              
  148             // Init HttpURLConnection 
  149             HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection(); 
  150  
  151             connection.setRequestMethod(method); 
  152              
- 153             if (METHOD_POST.equals(method)) { 
  154                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");                
  155             } 
  156             connection.setRequestProperty("Content-Length", String.valueOf(data.length())); 
  157              
  158             connection.setInstanceFollowRedirects(true); 
  159             connection.setUseCaches(false); 
  160              
  161             connection.setDoOutput(true); 
  162             connection.setDoInput(true); 
  163              
  164             // Connect 
  165             connection.connect(); 
  166              
  167             // Send request 
  168             connection.getOutputStream().write(data.getBytes("ISO-8859-1")); 
  169             connection.getOutputStream().flush(); 
  170  
  171             // Read answer             
  172             BufferedInputStream bis = new BufferedInputStream(connection.getInputStream()); 
  173             int available = bis.available(); 
  174              
  175             boolean dataReceived = false; 
  176             String content = new String(); 
  177             int dataSize = connection.getContentLength(); 
  178              
- 179             while (!dataReceived) { 
- 180                if (available != 0) { 
  181                    
  182                   byte[] inData = new byte[available]; 
  183                   bis.read(inData); 
  184                    
  185                   content += new String(inData, "ISO-8859-1"); 
  186                    
  187                   dataReceived = (content.length() >= dataSize); 
  188                } 
  189                 
- 190                if (System.currentTimeMillis() > timeLimit && !dataReceived) { 
 - 190         T && T
 - 190         T && F
 - 190         F && _
  191                   state = STATE_TIMEOUT; 
  192                   dataReceived = true; // Stop while 
  193                }                
  194                 
- 195                if (!dataReceived) { 
- 196                   try { 
  197                      Thread.sleep(500); 
  198                   } 
 - 199                   catch(Exception e) {}  
  200                    
  201                   available = bis.available(); 
  202                } 
    203                else { 
  204                   state = STATE_OK; 
  205                   answer = content; 
  206                }                
  207             } 
  208          } 
 - 209          catch (Exception e) { 
  210             e.printStackTrace(); 
  211             state = STATE_ERROR; 
  212          } 
  213       } 
  214    } 
  215     
 
- 216    public int getState() { 
 - 217       return state; 
  218    } 
  219     
 
- 220    public boolean completed() { 
 - 221       return state != STATE_INCOMPLETE; 
  222    } 
  223     
 
- 224    public String getResponseText() { 
 - 225       return answer; 
  226    } 
  227     
 
- 228    public MiniDOM getResponseXML() { 
  229       MiniDOM output = null; 
  230        
- 231       try { 
  232          output = MiniDOM.getTree(answer); 
  233       } 
 - 234       catch(Exception e) {} 
  235        
 - 236       return output; 
  237    } 
  238 
***TER 0% (0/66) of SOURCE FILE XmlRequest.java

Directory Summary | Files Summary | Functions Summary | Execution Profile
To files: First | Previous | Next | Last | Top | Index | No Index