View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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; // The underlying Styx Connection
76      private CStyxFile file; // the Styx file we are connecting to.
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                      // No user info: connect anonymously
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                 // TODO: check that the file exists here?
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 }