aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/client/Client.java
blob: 1a3b53a7239c1e960435513445439dbc6b9a70dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package uk.org.ury.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import java.util.Map;

import uk.org.ury.protocol.ProtocolUtils;
import uk.org.ury.protocol.exceptions.DecodeFailureException;

public class Client
{
  /**
   * Get a raw response from the server.
   * 
   * @param file  The "file", including path and query string, to 
   *              fetch from the server.
   * 
   * @return      The response from the server, as a key-value map.
   * @throws      DecodeFailureException  if the decode failed.
   */
  
  public Map<?, ?>
  get (String file) throws DecodeFailureException
  {
    URL url = null;
    URLConnection uc = null;
    String result = "";
    
    try
      {
        url = new URL ("http://localhost:8000" + file);
      }
    catch (MalformedURLException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace ();
      }
    
    try
      {
        uc = url.openConnection ();
        
        BufferedReader in = new BufferedReader (new InputStreamReader 
                                               (uc.getInputStream ()));
        
        
        String inputLine;

        for (inputLine = in.readLine();
             inputLine != null;
             inputLine = in.readLine())
          {
            result += inputLine;
          }
      }
    catch (IOException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace ();
      }
    
    return ProtocolUtils.decode (result);
  }
}