01: package org.drools.agent;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05: import java.net.HttpURLConnection;
06: import java.net.URL;
07: import java.net.URLConnection;
08:
09: import org.drools.common.DroolsObjectInputStream;
10: import org.drools.rule.Package;
11:
12: public class HttpClientImpl implements IHttpClient {
13:
14: public LastUpdatedPing checkLastUpdated(URL url) throws IOException {
15: URLConnection con = url.openConnection();
16: HttpURLConnection httpCon = (HttpURLConnection) con;
17: try {
18: httpCon.setRequestMethod("HEAD");
19:
20: String lm = httpCon.getHeaderField("lastModified");
21: LastUpdatedPing ping = new LastUpdatedPing();
22:
23: ping.responseMessage = httpCon.getHeaderFields().toString();
24:
25: if (lm != null) {
26: ping.lastUpdated = Long.parseLong(lm);
27: }
28:
29: return ping;
30: } finally {
31: httpCon.disconnect();
32: }
33:
34: }
35:
36: public Package fetchPackage(URL url) throws IOException,
37: ClassNotFoundException {
38: URLConnection con = url.openConnection();
39: HttpURLConnection httpCon = (HttpURLConnection) con;
40: try {
41: httpCon.setRequestMethod("GET");
42: InputStream in = httpCon.getInputStream();
43:
44: DroolsObjectInputStream oin = new DroolsObjectInputStream(
45: in);
46: return (Package) oin.readObject();
47:
48: } finally {
49: httpCon.disconnect();
50: }
51: }
52:
53: public static void main(String[] args) throws Exception {
54: HttpClientImpl cl = new HttpClientImpl();
55: URL url = new URL(
56: "http://localhost:8888/org.drools.brms.JBRMS/package/com.billasurf.manufacturing.plant/SNAP");
57:
58: LastUpdatedPing ping = cl.checkLastUpdated(url);
59:
60: Package p = cl.fetchPackage(url);
61:
62: System.err.println(ping);
63: System.err.println(ping.isError());
64: }
65:
66: }
|