下面列出了java.net.HttpURLConnection#getLastModified ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public CacheEntry call() throws Exception {
LOGGER.log(Level.FINE, "Attempting to fetch remote avatar: {0}", url);
long start = System.nanoTime();
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
try {
connection.setConnectTimeout(10000);
connection.setReadTimeout(30000);
if (!connection.getContentType().startsWith("image/")) {
return new CacheEntry(url);
}
int length = connection.getContentLength();
// buffered stream should be no more than 16k if we know the length
// if we don't know the length then 8k is what we will use
length = length > 0 ? Math.min(16384, length) : 8192;
try (InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, length)) {
BufferedImage image = ImageIO.read(bis);
if (image == null) {
return new CacheEntry(url);
}
return new CacheEntry(url, image, connection.getLastModified());
}
} finally {
connection.disconnect();
}
} catch (IOException e) {
LOGGER.log(Level.INFO, e.getMessage(), e);
return new CacheEntry(url);
} finally {
long end = System.nanoTime();
long duration = TimeUnit.NANOSECONDS.toMillis(end - start);
LOGGER.log(duration > 250 ? Level.INFO : Level.FINE, "Avatar lookup of {0} took {1}ms",
new Object[]{url, duration}
);
}
}
/**
* isValid
*
* @return boolean
*/
public boolean isValid() {
if (timestamp == -1) {
return false;
}
if (expires >= System.currentTimeMillis()) {
return true;
}
try {
URLConnection connection = getURI().toURL().openConnection();
if (connection instanceof HttpURLConnection) {
connection.setIfModifiedSince(timestamp);
((HttpURLConnection) connection).setRequestMethod("HEAD"); //$NON-NLS-1$
}
connection.connect();
if (connection instanceof HttpURLConnection) {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
long lastModified = httpConnection.getLastModified();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED || (lastModified != 0 && timestamp >= lastModified)) {
expires = System.currentTimeMillis();
long expiration = connection.getExpiration();
long date = connection.getDate();
if (expiration != 0 && date != 0 && expiration > date) {
expires += (expiration - date);
} else {
expires += 10 * 1000; // 10 sec
}
return true;
}
}
} catch (IOException e) {
IdeLog.logError(CorePlugin.getDefault(), e);
}
return false;
}
void updatePropertiesFromResponse(HttpURLConnection request) {
// ETag
this.getProperties().setEtag(request.getHeaderField(Constants.HeaderConstants.ETAG));
// Last Modified
if (0 != request.getLastModified()) {
final Calendar lastModifiedCalendar = Calendar.getInstance(Utility.LOCALE_US);
lastModifiedCalendar.setTimeZone(Utility.UTC_ZONE);
lastModifiedCalendar.setTime(new Date(request.getLastModified()));
this.getProperties().setLastModified(lastModifiedCalendar.getTime());
}
}
protected void updateEtagAndLastModifiedFromResponse(HttpURLConnection request) {
// ETag
this.getProperties().setEtag(request.getHeaderField(Constants.HeaderConstants.ETAG));
// Last Modified
if (0 != request.getLastModified()) {
final Calendar lastModifiedCalendar = Calendar.getInstance(Utility.LOCALE_US);
lastModifiedCalendar.setTimeZone(Utility.UTC_ZONE);
lastModifiedCalendar.setTime(new Date(request.getLastModified()));
this.getProperties().setLastModified(lastModifiedCalendar.getTime());
}
}
private void updatePropertiesFromResponse(HttpURLConnection request) {
// ETag
this.getProperties().setEtag(request.getHeaderField(Constants.HeaderConstants.ETAG));
// Last Modified
if (0 != request.getLastModified()) {
final Calendar lastModifiedCalendar = Calendar.getInstance(Utility.LOCALE_US);
lastModifiedCalendar.setTimeZone(Utility.UTC_ZONE);
lastModifiedCalendar.setTime(new Date(request.getLastModified()));
this.getProperties().setLastModified(lastModifiedCalendar.getTime());
}
}
private void updatePropertiesFromResponse(HttpURLConnection request) {
// ETag
this.getProperties().setEtag(request.getHeaderField(Constants.HeaderConstants.ETAG));
// Last Modified
if (0 != request.getLastModified()) {
final Calendar lastModifiedCalendar = Calendar.getInstance(Utility.LOCALE_US);
lastModifiedCalendar.setTimeZone(Utility.UTC_ZONE);
lastModifiedCalendar.setTime(new Date(request.getLastModified()));
this.getProperties().setLastModified(lastModifiedCalendar.getTime());
}
}
protected void updateEtagAndLastModifiedFromResponse(HttpURLConnection request) {
// ETag
this.getProperties().setEtag(request.getHeaderField(Constants.HeaderConstants.ETAG));
// Last Modified
if (0 != request.getLastModified()) {
final Calendar lastModifiedCalendar = Calendar.getInstance(Utility.LOCALE_US);
lastModifiedCalendar.setTimeZone(Utility.UTC_ZONE);
lastModifiedCalendar.setTime(new Date(request.getLastModified()));
this.getProperties().setLastModified(lastModifiedCalendar.getTime());
}
}
/**
* Check if there is a new version of the script engine
*
* @throws IOException
* if any error occur
*/
private void download() throws IOException {
boolean is32 = "32".equals( System.getProperty( "sun.arch.data.model" ) );
String fileName;
final String os = System.getProperty( "os.name", "" ).toLowerCase();
if( os.contains( "windows" ) ) {
fileName = is32 ? "win32" : "win64";
} else if( os.contains( "mac" ) ) {
fileName = is32 ? "mac" : "mac64";
} else if( os.contains( "linux" ) ) {
fileName = is32 ? "linux-i686" : "linux-x86_64";
} else {
throw new IllegalStateException( "Unknown OS: " + os );
}
File target = new File( System.getProperty( "java.io.tmpdir" ) + "/SpiderMonkey" );
URL url = new URL( "https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/jsshell-" + fileName + ".zip" );
System.out.println( "\tDownload: " + url );
command = target.getAbsolutePath() + "/js";
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout( 5000 );
if( target.exists() ) {
conn.setIfModifiedSince( target.lastModified() );
}
InputStream input;
try {
input = conn.getInputStream();
} catch( IOException ex ) {
if( target.exists() ) {
System.err.println( ex );
return;
}
throw ex;
}
if( conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED ) {
System.out.println( "\tUP-TP-DATE, use version from " + Instant.ofEpochMilli( target.lastModified() ) );
return;
}
long lastModfied = conn.getLastModified();
extractStream( input, false, target );
target.setLastModified( lastModfied );
System.out.println( "\tUse Version from " + Instant.ofEpochMilli( lastModfied ) );
}
private URLConnection openConnection(URL aSource) throws IOException {
// set up the URL connection
URLConnection connection = aSource.openConnection();
// modify the headers
// NB: things like user authentication could go in here too.
if (hasTimestamp) {
connection.setIfModifiedSince(timestamp);
}
// in case the plugin manager is its own project, this can become an authenticator
boolean isSecureProcotol = "https".equalsIgnoreCase(aSource.getProtocol());
boolean isAuthInfoSet = !Strings.isNullOrEmpty(aSource.getUserInfo());
if (isAuthInfoSet) {
if (!isSecureProcotol) {
throw new IOException("Basic auth is only supported for HTTPS!");
}
String basicAuth = Base64.encodeBytes(aSource.getUserInfo().getBytes(StandardCharsets.UTF_8));
connection.setRequestProperty("Authorization", "Basic " + basicAuth);
}
if (connection instanceof HttpURLConnection) {
((HttpURLConnection) connection).setInstanceFollowRedirects(false);
connection.setUseCaches(true);
connection.setConnectTimeout(5000);
}
connection.setRequestProperty("ES-Version", Version.CURRENT.toString());
connection.setRequestProperty("ES-Build-Hash", Build.CURRENT.hashShort());
connection.setRequestProperty("User-Agent", "elasticsearch-plugin-manager");
// connect to the remote site (may take some time)
connection.connect();
// First check on a 301 / 302 (moved) response (HTTP only)
if (connection instanceof HttpURLConnection) {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM ||
responseCode == HttpURLConnection.HTTP_MOVED_TEMP ||
responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
String newLocation = httpConnection.getHeaderField("Location");
URL newURL = new URL(newLocation);
if (!redirectionAllowed(aSource, newURL)) {
return null;
}
return openConnection(newURL);
}
// next test for a 304 result (HTTP only)
long lastModified = httpConnection.getLastModified();
if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED
|| (lastModified != 0 && hasTimestamp && timestamp >= lastModified)) {
// not modified so no file download. just return
// instead and trace out something so the user
// doesn't think that the download happened when it
// didn't
return null;
}
// test for 401 result (HTTP only)
if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
String message = "HTTP Authorization failure";
throw new IOException(message);
}
}
//REVISIT: at this point even non HTTP connections may
//support the if-modified-since behaviour -we just check
//the date of the content and skip the write if it is not
//newer. Some protocols (FTP) don't include dates, of
//course.
return connection;
}
@Override
protected Long getLastModifiedForScheme(URI uri) {
getLogger()
.warn(MessageFormat
.format("The URI [{0}] seems to represent an absolute HTTP or HTTPS URL. "
+ "Getting the last modification timestamp is only possible "
+ "if the URL is accessible "
+ "and if the server returns the [Last-Modified] header correctly. "
+ "This method is not reliable and is likely to fail. "
+ "In this case the last modification timestamp will be assumed to be unknown.",
uri));
try {
final URL url = uri.toURL();
try {
final URLConnection urlConnection = url.openConnection();
if (urlConnection instanceof HttpURLConnection) {
final HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
httpURLConnection.setInstanceFollowRedirects(true);
final long lastModified = httpURLConnection
.getLastModified();
if (lastModified == 0) {
getLogger()
.error(MessageFormat
.format("Could not retrieve the last modification timestamp for the URI [{0}] from the HTTP URL connection. "
+ "The [Last-Modified] header was probably not set correctly.",
uri));
} else {
getLogger()
.debug(MessageFormat
.format("HTTP connection to the URI [{0}] returned the last modification timestamp [{1,date,yyyy-MM-dd HH:mm:ss.SSS}].",
uri, lastModified));
return lastModified;
}
} else {
getLogger()
.error(MessageFormat
.format("URL connection for the URI [{0}] is not a HTTP or HTTPS connection, can't read the [Last-Modified] header.",
uri));
}
} catch (IOException ioex) {
getLogger()
.error(MessageFormat.format(
"Error opening the URL connection for the URI [{0}].",
uri), ioex);
}
} catch (MalformedURLException murlex) {
getLogger().error(MessageFormat.format("URI [{0}].", uri), murlex);
}
getLogger()
.warn(MessageFormat
.format("Last modification of the URI [{0}] is not known.",
uri));
return null;
}