KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > HttpFile


1 /*
2  * HttpFile.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: HttpFile.java,v 1.1.1.1 2002/09/24 16:08:24 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public final class HttpFile extends File
25 {
26     private File cache;
27     private String JavaDoc headers;
28     private String JavaDoc contentType;
29
30     private HttpFile()
31     {
32         isRemote = true;
33     }
34
35     private HttpFile(String JavaDoc hostName, String JavaDoc path, int protocol, int port)
36     {
37         this();
38         this.hostName = hostName;
39         this.canonicalPath = path;
40         this.protocol = protocol;
41         this.port = port;
42     }
43
44     public static HttpFile getHttpFile(String JavaDoc name)
45     {
46         HttpFile file = new HttpFile();
47         if (name.startsWith(PREFIX_HTTP)) {
48             name = name.substring(PREFIX_HTTP.length());
49             file.protocol = PROTOCOL_HTTP;
50         } else if (name.startsWith(PREFIX_HTTPS)) {
51             name = name.substring(PREFIX_HTTPS.length());
52             file.protocol = PROTOCOL_HTTPS;
53         }
54         int index = name.indexOf('/');
55         if (index < 0) {
56             file.hostName = name;
57             file.canonicalPath = "/";
58         } else {
59             file.hostName = name.substring(0, index);
60             file.canonicalPath = name.substring(index);
61         }
62         index = file.hostName.indexOf(':');
63         if (index >= 0) {
64             try {
65                 file.port = Integer.parseInt(file.hostName.substring(index + 1));
66                 file.hostName = file.hostName.substring(0, index);
67             }
68             catch (NumberFormatException JavaDoc e) {
69                 Log.error(e);
70                 return null;
71             }
72         }
73         if (file.port == 0)
74             file.port = file.protocol == PROTOCOL_HTTPS ? 443 : 80;
75         return file;
76     }
77
78     public static HttpFile getHttpFile(HttpFile directory, String JavaDoc name)
79     {
80         if (name.startsWith("http://") || name.startsWith("https://")) {
81             // Ignore directory.
82
return getHttpFile(name);
83         } else if (name.startsWith("//")) {
84             switch (directory.protocol) {
85                 case PROTOCOL_HTTP:
86                     return getHttpFile("http:" + name);
87                 case PROTOCOL_HTTPS:
88                     return getHttpFile("https:" + name);
89                 default:
90                     Debug.assertTrue(false);
91                     return null;
92             }
93         } else if (name.startsWith("/")) {
94             return new HttpFile(directory.hostName, name, directory.protocol, directory.port);
95         } else {
96             return new HttpFile(directory.hostName,
97                 canonicalize(appendNameToPath(directory.canonicalPath(), name, '/'), "/"),
98                 directory.protocol, directory.port);
99         }
100     }
101
102     public final File getCache()
103     {
104         return cache;
105     }
106
107     public final void setCache(File cache)
108     {
109         this.cache = cache;
110     }
111
112     public final String JavaDoc getHeaders()
113     {
114         return headers;
115     }
116
117     public final void setHeaders(String JavaDoc s)
118     {
119         headers = s;
120     }
121
122     public final String JavaDoc getContentType()
123     {
124         return contentType;
125     }
126
127     public final void setContentType(String JavaDoc s)
128     {
129         contentType = s;
130     }
131
132     public final File getRoot()
133     {
134         return new HttpFile(hostName, "/", protocol, port);
135     }
136
137     public final String JavaDoc getSeparator()
138     {
139         return "/";
140     }
141
142     public final char getSeparatorChar()
143     {
144         return '/';
145     }
146
147     public File getParentFile()
148     {
149         if (canonicalPath() == null || canonicalPath.equals("/")) {
150             // The file might really be "http://www.cnn.com/index.html", but
151
// it might appear to be "http://www.cnn.com/".
152
return new HttpFile(hostName, "/", protocol, port);
153         }
154         // Strip query (if any).
155
int index = canonicalPath.indexOf('?');
156         String JavaDoc stripped = index >= 0 ? canonicalPath.substring(0, index) : canonicalPath;
157         index = stripped.lastIndexOf('/');
158         if (index < 0) {
159             // No parent.
160
return null;
161         } else if (index == 0) {
162             // "/index.html"
163
return new HttpFile(hostName, "/", protocol, port);
164         } else {
165             return new HttpFile(hostName, stripped.substring(0, index),
166                 protocol, port);
167         }
168     }
169
170     public String JavaDoc netPath()
171     {
172         FastStringBuffer sb = new FastStringBuffer(256);
173         if (protocol == PROTOCOL_HTTP) {
174             sb.append(PREFIX_HTTP);
175         } else if (protocol == PROTOCOL_HTTPS) {
176             sb.append(PREFIX_HTTPS);
177         } else {
178             Debug.assertTrue(false);
179             return null;
180         }
181         sb.append(hostName);
182         int defaultPort = protocol == PROTOCOL_HTTPS ? 443 : 80;
183         if (port != defaultPort) {
184             sb.append(':');
185             sb.append(port);
186         }
187         sb.append(canonicalPath);
188         return sb.toString();
189     }
190
191     public String JavaDoc getName()
192     {
193         int index = canonicalPath.lastIndexOf('/');
194         String JavaDoc name = index >= 0 ? canonicalPath.substring(index + 1) : canonicalPath;
195         index = name.indexOf('?');
196         if (index >= 0)
197             return name.substring(0, index);
198         else
199             return name;
200     }
201 }
202
Popular Tags