All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.wicket.util.io.Connections Maven / Gradle / Ivy

Go to download

A module that creates a .jar from the classes in wicket, wicket-util and wicket-request modules in order to create a valid OSGi bundle of the wicket framework.

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.util.io;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.wicket.util.file.Files;
import org.apache.wicket.util.time.Time;

/**
 * {@link URLConnection} related utilities
 * 
 * @author igor.vaynberg
 */
public class Connections
{
	private Connections()
	{
	}

	/**
	 * Gets last modified date of the given {@link URL}
	 * 
	 * @param url
	 * @return last modified timestamp or null if not available
	 * @throws IOException
	 */
	public static Time getLastModified(final URL url) throws IOException
	{
		// check if url points to a local file
		final File file = Files.getLocalFileFromUrl(url);
		
		if(file != null)
		{
			// in that case we can get the timestamp faster
			return Files.getLastModified(file);
		}

		// otherwise open the url and proceed
		URLConnection connection = url.openConnection();

		final long milliseconds;
		
		try
		{
			if (connection instanceof JarURLConnection)
			{
				JarURLConnection jarUrlConnection = (JarURLConnection)connection;
				URL jarFileUrl = jarUrlConnection.getJarFileURL();
				URLConnection jarFileConnection = jarFileUrl.openConnection();
				
				try
				{
					// get timestamp from JAR
					milliseconds = jarFileConnection.getLastModified();
				}
				finally
				{
					close(jarFileConnection);
				}
			}
			else
			{
				// get timestamp from URL
				milliseconds = connection.getLastModified();
			}
			
			// return null if timestamp is unavailable
			if (milliseconds == 0)
			{
				return null;
			}
			
			// return UNIX timestamp
			return Time.millis(milliseconds);
			
		}
		finally
		{
			close(connection);
		}
	}

	/**
	 * Closes a connection, ignoring any exceptions if they occur
	 * 
	 * @param connection
	 */
	public static void closeQuietly(final URLConnection connection)
	{
		try
		{
			close(connection);
		}
		catch (Exception e)
		{
			// ignore
		}
	}

	/**
	 * Closes a connection
	 * 
	 * @param connection
	 * @throws IOException
	 */
	public static void close(final URLConnection connection) throws IOException
	{
		if (connection == null)
		{
			return;
		}

		if (connection instanceof HttpURLConnection)
		{
			((HttpURLConnection)connection).disconnect();
		}
		else
		{
			connection.getInputStream().close();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy