|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.net.www; |
|
|
|
import java.io.IOException; |
|
import java.net.URL; |
|
import java.util.*; |
|
|
|
/** |
|
* A class to represent an active connection to an object |
|
* represented by a URL. |
|
* @author James Gosling |
|
*/ |
|
|
|
public abstract class URLConnection extends java.net.URLConnection { |
|
|
|
/** The URL that it is connected to */ |
|
|
|
private String contentType; |
|
private int contentLength = -1; |
|
|
|
protected MessageHeader properties; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public URLConnection (URL u) { |
|
super(u); |
|
properties = new MessageHeader(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public MessageHeader getProperties() { |
|
return properties; |
|
} |
|
|
|
|
|
public void setProperties(MessageHeader properties) { |
|
this.properties = properties; |
|
} |
|
|
|
public void setRequestProperty(String key, String value) { |
|
if(connected) |
|
throw new IllegalStateException("Already connected"); |
|
if (key == null) |
|
throw new NullPointerException ("key cannot be null"); |
|
properties.set(key, value); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void addRequestProperty(String key, String value) { |
|
if (connected) |
|
throw new IllegalStateException("Already connected"); |
|
if (key == null) |
|
throw new NullPointerException ("key is null"); |
|
} |
|
|
|
public String getRequestProperty(String key) { |
|
if (connected) |
|
throw new IllegalStateException("Already connected"); |
|
return null; |
|
} |
|
|
|
public Map<String,List<String>> getRequestProperties() { |
|
if (connected) |
|
throw new IllegalStateException("Already connected"); |
|
return Collections.emptyMap(); |
|
} |
|
|
|
public String getHeaderField(String name) { |
|
try { |
|
getInputStream(); |
|
} catch (Exception e) { |
|
return null; |
|
} |
|
return properties == null ? null : properties.findValue(name); |
|
} |
|
|
|
|
|
Map<String, List<String>> headerFields; |
|
|
|
@Override |
|
public Map<String, List<String>> getHeaderFields() { |
|
if (headerFields == null) { |
|
try { |
|
getInputStream(); |
|
if (properties == null) { |
|
headerFields = super.getHeaderFields(); |
|
} else { |
|
headerFields = properties.getHeaders(); |
|
} |
|
} catch (IOException e) { |
|
return super.getHeaderFields(); |
|
} |
|
} |
|
return headerFields; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getHeaderFieldKey(int n) { |
|
try { |
|
getInputStream(); |
|
} catch (Exception e) { |
|
return null; |
|
} |
|
MessageHeader props = properties; |
|
return props == null ? null : props.getKey(n); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getHeaderField(int n) { |
|
try { |
|
getInputStream(); |
|
} catch (Exception e) { |
|
return null; |
|
} |
|
MessageHeader props = properties; |
|
return props == null ? null : props.getValue(n); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public String getContentType() { |
|
if (contentType == null) |
|
contentType = getHeaderField("content-type"); |
|
if (contentType == null) { |
|
String ct = null; |
|
try { |
|
ct = guessContentTypeFromStream(getInputStream()); |
|
} catch(java.io.IOException e) { |
|
} |
|
String ce = properties.findValue("content-encoding"); |
|
if (ct == null) { |
|
ct = properties.findValue("content-type"); |
|
|
|
if (ct == null) |
|
if (url.getFile().endsWith("/")) |
|
ct = "text/html"; |
|
else |
|
ct = guessContentTypeFromName(url.getFile()); |
|
} |
|
|
|
/* |
|
* If the Mime header had a Content-encoding field and its value |
|
* was not one of the values that essentially indicate no |
|
* encoding, we force the content type to be unknown. This will |
|
* cause a save dialog to be presented to the user. It is not |
|
* ideal but is better than what we were previously doing, namely |
|
* bringing up an image tool for compressed tar files. |
|
*/ |
|
|
|
if (ct == null || ce != null && |
|
!(ce.equalsIgnoreCase("7bit") |
|
|| ce.equalsIgnoreCase("8bit") |
|
|| ce.equalsIgnoreCase("binary"))) |
|
ct = "content/unknown"; |
|
setContentType(ct); |
|
} |
|
return contentType; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setContentType(String type) { |
|
contentType = type; |
|
properties.set("content-type", type); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public int getContentLength() { |
|
try { |
|
getInputStream(); |
|
} catch (Exception e) { |
|
return -1; |
|
} |
|
int l = contentLength; |
|
if (l < 0) { |
|
try { |
|
l = Integer.parseInt(properties.findValue("content-length")); |
|
setContentLength(l); |
|
} catch(Exception e) { |
|
} |
|
} |
|
return l; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
protected void setContentLength(int length) { |
|
contentLength = length; |
|
properties.set("content-length", String.valueOf(length)); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public boolean canCache() { |
|
return url.getFile().indexOf('?') < 0 /* && url.postData == null |
|
REMIND */ ; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public void close() { |
|
url = null; |
|
} |
|
|
|
private static HashMap<String,Void> proxiedHosts = new HashMap<>(); |
|
|
|
public static synchronized void setProxiedHost(String host) { |
|
proxiedHosts.put(host.toLowerCase(), null); |
|
} |
|
|
|
public static synchronized boolean isProxiedHost(String host) { |
|
return proxiedHosts.containsKey(host.toLowerCase()); |
|
} |
|
} |