summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/net/MyJsonRpcHttpClient.java
blob: d5e30d76eceb66a88108ae2e908e79927ae7dd06 (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
package com.orbekk.net;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.googlecode.jsonrpc4j.JsonRpcClient;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;

/**
 * This class is horrible. :S
 * 
 * We extend JsonRpcHttpClient but try to override everything it does.
 */
public class MyJsonRpcHttpClient extends JsonRpcHttpClient {
    Logger logger = LoggerFactory.getLogger(getClass());
    private URL serviceUrl;
    private JsonRpcClient rpcClient;
    private HttpClient httpClient;

    public MyJsonRpcHttpClient(URL serviceUrl, int connectionTimeout,
            int readTimeout) {
        super(null);
        httpClient = new DefaultHttpClient();
        HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, connectionTimeout);
        HttpConnectionParams.setSoTimeout(params, readTimeout);
        rpcClient = new JsonRpcClient();
        this.serviceUrl = serviceUrl;
    }

    @Override
    public synchronized Object invoke(
            final String methodName, final Object[] arguments, Type returnType, 
            Map<String, String> extraHeaders)
                    throws Exception {
        EntityTemplate entity = new EntityTemplate(new ContentProducer() {
            @Override
            public void writeTo(OutputStream out) throws IOException {
                try {
                    rpcClient.invoke(methodName, arguments, out);
                } catch (Exception e) {
                    throw new IOException("RPC Failed: " + e.getMessage());
                }
            }
        });
        entity.setContentType("application/json-rpc");

        HttpPost post = new HttpPost(serviceUrl.toString());

        for (Map.Entry<String, String> entry : extraHeaders.entrySet()) {
            post.addHeader(entry.getKey(), entry.getValue());
        }

        post.setEntity(entity);

        HttpResponse response = httpClient.execute(post);
        HttpEntity responseEntity = response.getEntity();

        return super.readResponse(returnType, responseEntity.getContent());
    }
}