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 package uk.ac.rdg.resc.jstyx.client.protocol.styx;
30
31 import java.net.URLConnection;
32 import java.net.URL;
33 import java.net.InetSocketAddress;
34
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.OutputStream;
38
39 import uk.ac.rdg.resc.jstyx.client.StyxConnection;
40 import uk.ac.rdg.resc.jstyx.client.CStyxFileInputStream;
41 import uk.ac.rdg.resc.jstyx.client.CStyxFileOutputStream;
42 import uk.ac.rdg.resc.jstyx.client.CStyxFile;
43
44 import uk.ac.rdg.resc.jstyx.StyxUtils;
45
46 /***
47 * Represents a connection to a Styx server that's been created with a URL
48 *
49 * @author Jon Blower
50 * $Revision: 602 $
51 * $Date: 2006-03-21 09:06:15 +0000 (Tue, 21 Mar 2006) $
52 * $Log$
53 * Revision 1.6 2006/03/21 09:06:15 jonblower
54 * Still implementing authentication
55 *
56 * Revision 1.5 2005/09/01 17:12:11 jonblower
57 * Changes to Input and Output stream code
58 *
59 * Revision 1.4 2005/08/31 17:03:18 jonblower
60 * Renamed "StyxFile*putStream*" to "CStyxFile*putStream*" for consistency with CStyxFile class
61 *
62 * Revision 1.3 2005/05/23 16:48:23 jonblower
63 * Overhauled CStyxFile (esp. asynchronous methods) and StyxConnection (added cache of CStyxFiles)
64 *
65 * Revision 1.2 2005/02/24 07:47:06 jonblower
66 * Added connect() in response to change to StyxConnection
67 *
68 * Revision 1.1.1.1 2005/02/16 18:58:25 jonblower
69 * Initial import
70 *
71 */
72 public class StyxURLConnection extends URLConnection
73 {
74
75 private StyxConnection conn;
76 private CStyxFile file;
77
78 /*** Creates a new instance of StyxURLConnection */
79 public StyxURLConnection(URL url)
80 {
81 super(url);
82 }
83
84 public void connect() throws IOException
85 {
86 if (!this.connected)
87 {
88 try
89 {
90 if (this.url.getUserInfo() == null)
91 {
92
93 conn = new StyxConnection(this.url.getHost(), this.url.getPort());
94 }
95 else
96 {
97 String[] els = this.url.getUserInfo().split(":");
98 String user = els[0];
99 String password = els.length > 1 ? els[1] : "";
100 conn = new StyxConnection(this.url.getHost(),
101 this.url.getPort(), user, password);
102 }
103 conn.connect();
104 this.connected = true;
105 this.file = conn.getFile(this.url.getPath());
106
107 }
108 catch (Exception e)
109 {
110 throw new IOException(e.getClass().getName() + ": " + e.getMessage());
111 }
112 }
113 }
114
115 /***
116 * This is empty because we don't need to set any header information
117 * to find a resource on a Styx server
118 */
119 public void setRequestHeader(String name, String value)
120 {
121 }
122
123 public InputStream getInputStream() throws IOException
124 {
125 try
126 {
127 this.file.open(StyxUtils.OREAD);
128 CStyxFileInputStream in = new CStyxFileInputStream(this.file, true);
129 return in;
130 }
131 catch(Exception e)
132 {
133 throw new IOException(e.getClass().getName() + ": " + e.getMessage());
134 }
135 }
136
137 public OutputStream getOutputStream() throws IOException
138 {
139 try
140 {
141 this.file.open(StyxUtils.OWRITE);
142 CStyxFileOutputStream out = new CStyxFileOutputStream(this.file, true);
143 return out;
144 }
145 catch(Exception e)
146 {
147 throw new IOException(e.getClass().getName() + ": " + e.getMessage());
148 }
149 }
150 }