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.server;
30  
31  import java.net.URL;
32  import java.net.MalformedURLException;
33  
34  import org.apache.mina.common.ByteBuffer;
35  
36  import uk.ac.rdg.resc.jstyx.StyxException;
37  import uk.ac.rdg.resc.jstyx.StyxUtils;
38  import uk.ac.rdg.resc.jstyx.types.ULong;
39  
40  /***
41   * StyxFile that contains a URL.
42   *
43   * @author Jon Blower
44   * $Revision: 609 $
45   * $Date: 2006-03-31 18:09:42 +0100 (Fri, 31 Mar 2006) $
46   * $Log$
47   * Revision 1.3  2005/11/07 21:14:44  jonblower
48   * Fixed null pointer bug in getLength()
49   *
50   * Revision 1.2  2005/11/07 12:22:25  jonblower
51   * Added getLength() method
52   *
53   * Revision 1.1  2005/11/04 17:32:11  jonblower
54   * Initial import
55   *
56   */
57  
58  public class URLFile extends StyxFile
59  {
60      private URL url;
61      
62      /***
63       * Creates a new instance of URLFile with read-write permissions for everyone
64       * (0666).
65       */
66      public URLFile(String name) throws StyxException
67      {
68          super(name, 0666);
69          this.url = null;
70      }
71      
72      public synchronized void read(StyxFileClient client, long offset, int count,
73          int tag) throws StyxException
74      {
75          String urlStr = "";
76          if (this.url != null)
77          {
78              urlStr = this.url.toString();
79          }
80          this.processAndReplyRead(urlStr, client, offset, count, tag);
81      }
82      
83      /***
84       * The new value for the URL must come in a single message (i.e.
85       * the offset must be zero and the incoming ByteBuffer must contain the
86       * entire URL).  Must also write with truncation.
87       */
88      public synchronized void write(StyxFileClient client, long offset,
89          int count, ByteBuffer data, boolean truncate, int tag)
90          throws StyxException
91      {
92          if (!truncate)
93          {
94              throw new StyxException("Must write to the URL file with truncation");
95          }
96          if (count == 0)
97          {
98              if (offset != this.getLength().asLong())
99              {
100                 throw new StyxException("Can only write EOF to the end of this file");
101             }
102             // We can ignore EOF messages
103             this.replyWrite(client, 0, tag);
104             return;
105         }
106         if (offset != 0)
107         {
108             throw new StyxException("Must write data to the start of the URL file");
109         }
110         // Set the limit of the input data buffer correctly
111         data.limit(data.position() + count);
112         
113         String urlStr = StyxUtils.dataToString(data);
114         if (urlStr.trim().equals(""))
115         {
116             this.url = null;
117         }
118         else
119         {
120             try
121             {
122                 this.url = new URL(urlStr);
123             }
124             catch (MalformedURLException mue)
125             {
126                 throw new StyxException(urlStr + " is not recognized as a valid URL");
127             }
128         }
129         this.replyWrite(client, count, tag);
130     } 
131     
132     /***
133      * @return the size of this file in bytes
134      */
135     public ULong getLength()
136     {
137         if (this.url == null)
138         {
139             return ULong.ZERO;
140         }
141         else
142         {
143             String str = this.url.toString();
144             int len = StyxUtils.strToUTF8(str).length;
145             return new ULong(len);
146         }
147     }
148     
149     /***
150      * @return the URL contained in this file or null if it hasn't been set
151      */
152     public URL getURL()
153     {
154         return this.url;
155     }
156 }