001: // HttpURLConnection.java
002: // $Id: HttpURLConnection.java,v 1.20 2002/07/25 17:45:29 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.protocol.http;
007:
008: import java.net.MalformedURLException;
009: import java.net.URL;
010: import java.net.URLConnection;
011:
012: import java.io.ByteArrayInputStream;
013: import java.io.ByteArrayOutputStream;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.io.OutputStream;
017:
018: import java.util.Enumeration;
019:
020: import org.w3c.www.mime.MimeType;
021:
022: import org.w3c.www.http.HeaderDescription;
023: import org.w3c.www.http.HttpEntityMessage;
024: import org.w3c.www.http.HttpMessage;
025: import org.w3c.www.http.HttpReplyMessage;
026: import org.w3c.www.http.HttpRequestMessage;
027:
028: public class HttpURLConnection extends java.net.HttpURLConnection {
029: protected Request request = null;
030: protected Reply reply = null;
031: protected ByteArrayOutputStream output = null;
032:
033: protected final synchronized void checkRequest() {
034: if (request == null) {
035: request = HttpManager.getManager().createRequest();
036: request.setMethod(method);
037: if (ifModifiedSince > 0)
038: request.setIfModifiedSince(ifModifiedSince * 1000);
039: if (!useCaches) {
040: request.addPragma("no-cache");
041: request.setNoCache();
042: }
043: request.setURL(url);
044: }
045: }
046:
047: protected final void checkReply() {
048: if ((reply == null) && connected) {
049: error("Was connected but didn't get any reply !");
050: } else if (!connected) {
051: try {
052: connect();
053: } catch (IOException ex) {
054: ex.printStackTrace();
055: error("Error while silently connecting.");
056: }
057: }
058: }
059:
060: protected void error(String msg) {
061: debug("ERROR \"" + msg + "\"");
062: throw new RuntimeException(msg);
063: }
064:
065: protected final void debug(String m) {
066: // System.out.println("[http]: "
067: // + ((request != null)
068: // ? ":"+request.getURL().toExternalForm()+"]: "
069: // : "]: ")
070: // + m);
071: }
072:
073: public boolean usingProxy() {
074: return HttpManager.getManager().usingProxy();
075: }
076:
077: public synchronized void disconnect() {
078: if (request != null)
079: request.interruptRequest();
080: }
081:
082: public void connect() throws IOException {
083: // Check if we have already connected ourself:
084: debug("connect");
085: if (connected)
086: return;
087: // Check for a valid request, and update stream if needed:
088: checkRequest();
089: if (doOutput) {
090: byte data[] = output.toByteArray();
091: ByteArrayInputStream in = new ByteArrayInputStream(data);
092: request.setOutputStream(in);
093: if (!request.hasHeader(Request.H_CONTENT_LENGTH))
094: request.setContentLength(data.length);
095: if (!request.hasHeader(Request.H_CONTENT_TYPE)) {
096: MimeType t = (request.getMethod().equals("POST") ? MimeType.APPLICATION_X_WWW_FORM_URLENCODED
097: : MimeType.TEXT_PLAIN);
098: request.setContentType(t);
099: }
100: }
101: try {
102: // Run the reply, handling redirection:
103: int redirects = 0;
104: redir: while (redirects < 5) {
105: if (redirects == 1) {
106: // Reinit the request, cope with URLConnnection brokeness
107: request.setOutputStream(null);
108: request.setContentLength(-1);
109: request.setMethod("GET");
110: }
111: reply = request.getManager().runRequest(request);
112: // Take care of redirects:
113: if ((reply.getStatus() / 100) == 3) {
114: String reloc = reply.getLocation();
115: if (reloc == null)
116: break redir;
117: reply.getInputStream().close();
118: URL orig = request.getURL();
119: try {
120: url = new URL(orig, reloc);
121: request.setURL(url);
122: } catch (MalformedURLException ex) {
123: break redir;
124: }
125: } else {
126: break redir;
127: }
128: if (!getFollowRedirects())
129: break redir;
130: redirects++;
131: }
132: } catch (HttpException ex) {
133: ex.printStackTrace();
134: throw new IOException(ex.getMessage());
135: }
136: connected = true;
137: }
138:
139: public int getResponseCode() throws IOException {
140: checkReply();
141: return reply.getStatus();
142: }
143:
144: public int getContentLength() {
145: checkReply();
146: return reply.getContentLength();
147: }
148:
149: public String getContentType() {
150: checkReply();
151: MimeType t = reply.getContentType();
152: if (t == null) {
153: return null;
154: }
155: return t.toString();
156: }
157:
158: public long getExpiration() {
159: checkReply();
160: return reply.getExpires();
161: }
162:
163: public long getDate() {
164: checkReply();
165: return reply.getDate();
166: }
167:
168: public long getLastModified() {
169: checkReply();
170: return reply.getLastModified();
171: }
172:
173: public String getHeaderField(String name) {
174: checkReply();
175: return reply.getValue(name);
176: }
177:
178: public String getHeaderFieldKey(int n) {
179: Enumeration e = reply.enumerateHeaderDescriptions(true);
180: HeaderDescription d = null;
181: while (--n >= 0) {
182: if (e.hasMoreElements())
183: d = (HeaderDescription) e.nextElement();
184: return null;
185: }
186: if (d != null)
187: return d.getName();
188: return null;
189: }
190:
191: public String getHeaderField(int n) {
192: String key = getHeaderFieldKey(n);
193: if (key != null)
194: return reply.getValue(key);
195: return null;
196: }
197:
198: public InputStream getInputStream() throws IOException {
199: debug("getInputStream");
200: checkReply();
201: return reply.getInputStream();
202: }
203:
204: public OutputStream getOutputStream() throws IOException {
205: debug("getOutputStream");
206: // We can still catch some of the deficiencies here:
207: if ((!connected) && (!doOutput)) {
208: setDoOutput(true);
209: } else if (connected) {
210: error("Already connected, too late for getOutputStream.");
211: }
212: // Now return a fake output stream:
213: // I am really ashame of this (having to buffer the data before
214: // emiting it), but I cannot see any better way of using this
215: // broken API (?)
216: output = new ByteArrayOutputStream();
217: return output;
218: }
219:
220: public InputStream getErrorStream() {
221: debug("getErrorStream");
222: try {
223: int errorCat = reply.getStatus() / 100;
224: if (connected && (errorCat == 4 || errorCat == 5)) {
225: return reply.getInputStream();
226: }
227: // the next catch the case where there is no reply
228: // as well as errors while handling the reply (no useful info...)
229: } catch (Exception ex) {
230: return null;
231: }
232: return null;
233: }
234:
235: public void setRequestProperty(String key, String value) {
236: checkRequest();
237: request.setValue(key, value);
238: }
239:
240: public String getRequestProperty(String key) {
241: checkRequest();
242: return request.getValue(key);
243: }
244:
245: public static void setGlobalRequestProperty(String key, String value) {
246: HttpManager.getManager().setGlobalHeader(key, value);
247: }
248:
249: public static String getDefaultRequestProperty(String key) {
250: return HttpManager.getManager().getGlobalHeader(key);
251: }
252:
253: public void setDoOutput(boolean doinput) {
254: debug("DoOutput !!");
255: super .setDoOutput(doinput);
256: checkRequest();
257: if (request.getContentType() == null)
258: request
259: .setContentType(MimeType.APPLICATION_X_WWW_FORM_URLENCODED);
260: }
261:
262: HttpURLConnection(URL u) {
263: super(u);
264: }
265:
266: }
|