Get URLConnection Expiration : URLConnection « Network Protocol « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Network Protocol » URLConnectionScreenshots 
Get URLConnection Expiration
  
/*
    GNU LESSER GENERAL PUBLIC LICENSE
    Copyright (C) 2006 The Lobo Project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Contact info: lobochief@users.sourceforge.net
*/
/*
 * Created on Jun 12, 2005
 */

import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.*;

public class Urls {
  private static final Logger logger = Logger.getLogger(Urls.class.getName());
  public static final DateFormat PATTERN_RFC1123 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);

  static {
    DateFormat df = PATTERN_RFC1123;
    df.setTimeZone(TimeZone.getTimeZone("GMT"));
  }
  
  private Urls() {
    super();
  }

  /** Whether the URL refers to a resource in the local file system. */
  public static boolean isLocal(java.net.URL url) {
    if(isLocalFile(url)) {
      return true;
    }
    String protocol = url.getProtocol();
    if("jar".equalsIgnoreCase(protocol)) {
      String path = url.getPath();
      int emIdx = path.lastIndexOf('!');
      String subUrlString = emIdx == -? path : path.substring(0, emIdx);
      try {
        URL subUrl = new URL(subUrlString);
        return isLocal(subUrl);
      catch(java.net.MalformedURLException mfu) {
        return false;
      }
    }
    else {
      return false;
    }
  }
  
  /** Whether the URL is a file in the local file system. */
  public static boolean isLocalFile(java.net.URL url) {
    String scheme = url.getProtocol();
    return "file".equalsIgnoreCase(scheme&& !hasHost(url);
  }

  public static boolean hasHost(java.net.URL url) {
    String host = url.getHost();
    return host != null && !"".equals(host);
  }

  /**
   * Creates an absolute URL in a manner equivalent to
   * major browsers. 
   */
  public static URL createURL(URL baseUrl, String relativeUrlthrows java.net.MalformedURLException {
    return new URL(baseUrl, relativeUrl);
  
  
  /**
   * Returns the time when the document should be considered expired.
   * The time will be zero if the document always needs to be revalidated.
   * It will be <code>null</code> if no expiration time is specified.
   */
  public static Long getExpiration(URLConnection connection, long baseTime) {
    String cacheControl = connection.getHeaderField("Cache-Control");
    if(cacheControl != null) {
      StringTokenizer tok = new StringTokenizer(cacheControl, ",");
      while(tok.hasMoreTokens()) {
        String token = tok.nextToken().trim().toLowerCase();
        if("must-revalidate".equals(token)) {
          return new Long(0);
        }
        else if(token.startsWith("max-age")) {
          int eqIdx = token.indexOf('=');
          if(eqIdx != -1) {
            String value = token.substring(eqIdx+1).trim();
            int seconds;
            try {
              seconds = Integer.parseInt(value);
              return new Long(baseTime + seconds * 1000);
            catch(NumberFormatException nfe) {
              logger.warning("getExpiration(): Bad Cache-Control max-age value: " + value);
              // ignore
            }
          }
        }
      }
    }
    String expires = connection.getHeaderField("Expires");
    if(expires != null) {
      try {
        synchronized(PATTERN_RFC1123) {
          Date expDate = PATTERN_RFC1123.parse(expires);
          return new Long(expDate.getTime());
        }
      catch(java.text.ParseException pe) {
        int seconds;
        try {
          seconds = Integer.parseInt(expires);
          return new Long(baseTime + seconds * 1000);
        catch(NumberFormatException nfe) {
          logger.warning("getExpiration(): Bad Expires header value: " + expires);
        }
      }
    }
    return null;
  }

  
    private static String getDefaultCharset(URLConnection connection) {
      URL url = connection.getURL();
      if(Urls.isLocalFile(url)) {
        String charset = System.getProperty("file.encoding");
        return charset == null "ISO-8859-1" : charset;
      }
      else {
        return "ISO-8859-1";
      }
    }
    
    public static String getNoRefForm(URL url) {
      String host = url.getHost();
      int port = url.getPort();
      String portText = port == -"" ":" + port;
      String userInfo = url.getUserInfo();
      String userInfoText = userInfo == null || userInfo.length() == "" : userInfo + "@";
      String hostPort = host == null || host.length() == "" "//" + userInfoText + host + portText;
      return url.getProtocol() ":" + hostPort + url.getFile();
    }


}

   
    
  
Related examples in the same category
1. Demonstrate URLConnection.
2. Call a servlet from a Java command line application
3. A CGI POST Example
4. Http authentication header
5. URLConnection.setRequestProperty
6. File size from URL
7. Sending a POST Request Using a URL
8. Check if a file was modified on the server
9. Getting Text from a URL
10. Getting an Image from a URL
11. Sending a POST Request with Parameters From a Java Class
12. Downloading a web page using URL and URLConnection classes
13. Get response header from HTTP request
14. Read / download webpage content
15. Read a GIF or CLASS from an URL save it locally
16. Zip URLConnection
17. Zip URLStream Handler
18. Http Parser
19. Http Constants
20. Locating files by path or URL
21. Download from URL
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.