01: package vicazh.hyperpool.stream.net.http;
02:
03: import java.io.*;
04: import java.net.*;
05: import vicazh.hyperpool.*;
06: import vicazh.hyperpool.Start;
07: import vicazh.hyperpool.stream.*;
08: import vicazh.hyperpool.stream.Selector;
09: import vicazh.hyperpool.stream.net.Stream;
10:
11: class ContentClientStream extends ClientStream {
12:
13: ContentClientStream(Session session, OutputStream outputstream) {
14: super (session, outputstream);
15: }
16:
17: public void head(String method, String file, String version)
18: throws IOException {
19: int index;
20: URL url;
21: try {
22: url = new URL(file);
23: } catch (MalformedURLException e) {
24: url = new URL("https://" + file);
25: }
26: if (url.getProtocol().equalsIgnoreCase("http")
27: && (method.equalsIgnoreCase("get") || method
28: .equalsIgnoreCase("post"))) {
29: if (((ContentConnection) connection).c == null) {
30: Service service = new ContentHeadService();
31: service
32: .setElement((ElementMBean) ((Selector) connection.element)
33: .getGroup().getObjects().get(
34: ((Selector) connection.element)
35: .getDef()).getObject());
36: ((ContentConnection) connection).c = (Connection) ((Stream) service
37: .get(connection.getServer())).connection;
38: }
39: ClientStream dst = (ClientStream) ((ContentConnection) connection).c
40: .getClient().outputstream;
41: try {
42: dst.head("HEAD", file, version);
43: dst.field("Host", url.getAuthority());
44: dst.field("User-Agent", Start.NAME + "/"
45: + Start.VERSION);
46: dst.header();
47: } catch (NullPointerException e) {
48: throw new BreakException();
49: }
50: ((ContentConnection) connection).c.getClient().flush();
51: synchronized (((ContentConnection) connection).c) {
52: if (((ContentConnection) connection).c.getSessions()
53: .size() > 1)
54: try {
55: ((ContentConnection) connection).c.wait();
56: } catch (InterruptedException e) {
57: }
58: }
59: String ct = dst.session.getServer().getFields().get(
60: "content-type");
61: index = ((ContentSelector) connection.element)
62: .getIndex(ct == null ? ct : ct.substring(0, ct
63: .indexOf('/')));
64: } else
65: index = ((Selector) connection.element).getDef();
66: if (outputstream == null
67: || ((ContentConnection) connection).index != index) {
68: if (outputstream != null) {
69: synchronized (connection) {
70: if (((Connection) connection).getSessions().size() > 1)
71: try {
72: connection.wait();
73: } catch (InterruptedException e) {
74: }
75: }
76: connection.getServer().outputstream = null;
77: connection.setServer(new Stream(
78: (Connection) connection, session.getServer()));
79: outputstream.flush();
80: outputstream.close();
81: }
82: ((ContentConnection) connection).index = index;
83: outputstream = ((Selector) connection.element).get(
84: connection.getServer(), index);
85: ((ContentConnection) connection).priority = (Priority) ((ContentSelector) connection.element)
86: .getList().get(index);
87: }
88: super .head(method, file, version);
89: }
90:
91: public void close() throws IOException {
92: super .close();
93: ((ContentConnection) connection).c.getClient().close();
94: }
95: }
|