aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/frontend/client/Client.java
blob: bbf0001ff29d2ca09ab9c9076760cf3ee00d4c51 (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
71
72
73
74
75
76
/*
 * Client.java
 * -----------
 * 
 * Part of the URY Frontend Platform
 * 
 * V0.00  2011/03/23
 * 
 * (C) 2011 URY Computing
 */

package uk.org.ury.frontend.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.common.protocol.ProtocolUtils;
import uk.org.ury.common.protocol.exceptions.DecodeFailureException;

/**
 * An implementation of a client to communicate with the URY Server.
 * 
 * @author Matt Windsor
 */
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<String, Object> 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);
    }
}