Advertisement
Guest User

Untitled

a guest
Mar 5th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.90 KB | None | 0 0
  1. public class HTTPRequest {
  2.     public enum HTTPStates {
  3.         CONNECTED,
  4.         CONNECTION_FAILED,
  5.         GETTING,
  6.         TIMEOUT,
  7.         CANCELLED,
  8.         OK,
  9.         NO_CONTENT,
  10.         REDIRECTION,
  11.         BAD_REQUEST,
  12.         FORBIDDEN,
  13.         NOT_FOUND,
  14.         SERVER_ERROR,
  15.         BAD_GATEWAY,
  16.         SERVICE_UNAVAILABLE,
  17.         UNKNOWN
  18.     }
  19.     Object m_lock = new Object();
  20.     URL m_url;
  21.     Thread m_thread;
  22.     String m_urlString;
  23.     boolean m_complete;
  24.     boolean m_cancelled;
  25.     boolean m_success;
  26.     String m_result;
  27.     HTTPStates m_state;
  28.    
  29.     public HTTPRequest(String _url) throws HTTPRequestException {
  30.         m_urlString = _url;
  31.         try {
  32.             m_url = new URL(_url);
  33.             if (!m_url.getProtocol().toLowerCase().equals("http"))
  34.                 throw new HTTPRequestException("Not an HTTP URL");
  35.         } catch (MalformedURLException e) {
  36.             throw new HTTPRequestException("Invalid URL");
  37.         }
  38.        
  39.         m_cancelled = false;
  40.         m_complete = false;
  41.         m_success = false;
  42.         m_result = null;
  43.        
  44.         m_thread = new Thread(new HTTPRequestThread(this));
  45.        
  46.         m_thread.start();
  47.     }
  48.    
  49.     String getURL() {
  50.         return this.m_urlString;
  51.     }
  52.    
  53.     void cancel() {
  54.         synchronized (m_lock) {
  55.             m_cancelled = true;
  56.         }
  57.     }
  58.    
  59.     public boolean isComplete() {
  60.         synchronized (m_lock) {
  61.             return m_complete;
  62.         }
  63.     }
  64.    
  65.     public boolean wasSuccessful() {
  66.         synchronized (m_lock) {
  67.             return m_success;
  68.         }
  69.     }
  70.    
  71.     public HTTPStates getState() {
  72.         synchronized (m_lock) {
  73.             return m_state;
  74.         }
  75.     }
  76.    
  77.     public BufferedReader getContents() {
  78.         String result = null;
  79.         synchronized (result) {
  80.             result = m_result;
  81.         }
  82.        
  83.         if (result != null)
  84.             return new BufferedReader(new StringReader(result));
  85.         return null;
  86.     }
  87.    
  88.     private class HTTPRequestThread implements Runnable {
  89.         private HTTPRequest request;
  90.         private HttpURLConnection conn;
  91.        
  92.         public HTTPRequestThread(HTTPRequest request) {
  93.             this.request = request;
  94.         }
  95.        
  96.         public void run() {
  97.             try {
  98.                 conn = (HttpURLConnection) request.m_url.openConnection();
  99.                 conn.setReadTimeout(10000);
  100.                
  101.                 try {
  102.                     conn.connect();
  103.                     request.m_state = HTTPStates.CONNECTED;
  104.                 } catch (IOException ex) {
  105.                     synchronized (request.m_lock) {
  106.                         request.m_complete = true;
  107.                         request.m_success = false;
  108.                         request.m_result = null;
  109.                         request.m_state = HTTPStates.CONNECTION_FAILED;
  110.                     }
  111.                 }
  112.                
  113.                 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  114.                 StringBuilder result = new StringBuilder();
  115.                
  116.                 while (true) {
  117.                     synchronized (request.m_lock) {
  118.                         if (request.m_cancelled) {
  119.                             request.m_state = HTTPStates.CANCELLED;
  120.                             break;
  121.                         }
  122.                     }
  123.                    
  124.                     String line = reader.readLine();
  125.                     if (line == null)
  126.                         break;
  127.                     result.append(line);
  128.                     result.append('\n');
  129.                     request.m_state = HTTPStates.GETTING;
  130.                 }
  131.                 reader.close();
  132.                
  133.                 synchronized (request.m_lock) {
  134.                     if (request.m_cancelled) {
  135.                         request.m_complete = true;
  136.                         request.m_success = false;
  137.                         request.m_result = null;
  138.                         request.m_state = HTTPStates.CANCELLED;
  139.                     } else {
  140.                         request.m_complete = true;
  141.                         request.m_success = true;
  142.                         request.m_result = result.toString();
  143.                         setState(conn.getResponseCode());
  144.                     }
  145.                 }
  146.             } catch (ConnectException e) {
  147.                 synchronized (request.m_lock) {
  148.                     request.m_complete = true;
  149.                     request.m_success = false;
  150.                     request.m_result = null;
  151.                     request.m_state = HTTPStates.TIMEOUT;
  152.                 }
  153.             } catch (IOException e) {
  154.                 synchronized (request.m_lock) {
  155.                     request.m_complete = true;
  156.                     request.m_success = false;
  157.                     request.m_result = null;
  158.                     setState(0);
  159.                 }
  160.             } finally {
  161.                 conn.disconnect();
  162.                 conn = null;
  163.             }
  164.         }
  165.        
  166.         private void setState(int responseCode) {
  167.             switch (responseCode) {
  168.                 case HttpURLConnection.HTTP_OK:
  169.                     request.m_state = HTTPStates.OK;
  170.                     break;
  171.                 case HttpURLConnection.HTTP_NO_CONTENT:
  172.                     request.m_state = HTTPStates.NO_CONTENT;
  173.                     break;
  174.                 case HttpURLConnection.HTTP_MOVED_PERM:
  175.                 case HttpURLConnection.HTTP_MOVED_TEMP:
  176.                     request.m_state = HTTPStates.REDIRECTION;
  177.                     break;
  178.                 case HttpURLConnection.HTTP_BAD_REQUEST:
  179.                     request.m_state = HTTPStates.BAD_REQUEST;
  180.                     break;
  181.                 case HttpURLConnection.HTTP_FORBIDDEN:
  182.                     request.m_state = HTTPStates.FORBIDDEN;
  183.                     break;
  184.                 case HttpURLConnection.HTTP_NOT_FOUND:
  185.                     request.m_state = HTTPStates.NOT_FOUND;
  186.                     break;
  187.                 case HttpURLConnection.HTTP_INTERNAL_ERROR:
  188.                 case HttpURLConnection.HTTP_NOT_IMPLEMENTED:
  189.                 case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
  190.                 case HttpURLConnection.HTTP_VERSION:
  191.                     request.m_state = HTTPStates.SERVER_ERROR;
  192.                     break;
  193.                 case HttpURLConnection.HTTP_BAD_GATEWAY:
  194.                     request.m_state = HTTPStates.BAD_GATEWAY;
  195.                     break;
  196.                 case HttpURLConnection.HTTP_UNAVAILABLE:
  197.                     request.m_state = HTTPStates.SERVICE_UNAVAILABLE;
  198.                     break;
  199.                 default:
  200.                     request.m_state = HTTPStates.UNKNOWN;
  201.             }
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement