/**
 * Copyright 2008-2009 DRIVER PROJECT (Bielefeld University)
 * Original author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * Licensed 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 eu.dnetlib.data.utility.download.utils;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;

/**
 * The URL utils class.
 * 
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 */
public class UrlUtils {


    /**
     * Verify url.
     * 
     * @param url the url
     * 
     * @return the uRL
     */
    public static URL verifyUrl(String url) {
        
        if (!url.toLowerCase().startsWith("http"))
            return null;
      
        URL verifiedUrl = null;
        try {
            verifiedUrl = new URL(url);
        } catch (Exception e) {
   
        	return null;
        }
        
        return verifiedUrl;
    }
    
    /**
     * Gets the file name.
     * 
     * @param url the url
     * 
     * @return the file name
     * */
	public static String getFileName(URL url) {
	
		if (url == null)
			return "notRecognizedFileName";
		
		if (url.getFile() == null || ("".equals(url.getFile()))
				|| ("/".equals(url.getFile()))) {
			return "notRecognizedFileName";
		} else {
			String fileName = url.getFile();
			return fileName.substring(fileName.lastIndexOf('/') + 1);
		}
		
	}
    
	/**
	 * Writes the contents of the url to string.
	 * 
	 * @param url the url
	 * 
	 * @return the string
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static String writeURLtoString(URL url) 
		throws IOException {
		
		StringWriter sw = new StringWriter();
		
		BufferedInputStream in = new BufferedInputStream(url.openStream());
		for (int c = in.read(); c != -1; c = in.read()) {
			sw.write(c);
		}
		
		return sw.toString();
	}

	/**
	 * Writes contents of the url to a new file.
	 * 
	 * @param url the url
	 * @param filename the filename
	 * 
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static void writeURLtoFile(URL url, String filename)
		throws IOException {
		
		FileOutputStream os = new FileOutputStream(filename);
		InputStream is = url.openStream();
		byte[] buf = new byte[1048576];
		int n = is.read(buf);
		while (n != -1) {
			os.write(buf, 0, n);
			n = is.read(buf);
		}
		os.close();
	}
}
