<commit_before>package org.drools.io.impl;

import java.io.Externalizable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.drools.io.InternalResource;
import org.drools.io.Resource;
import org.drools.util.StringUtils;

import com.sun.net.ssl.HttpsURLConnection;

/**
 * Borrowed gratuitously from Spring under ASL2.0.
 *
 */
public class UrlResource extends BaseResource
    implements
    InternalResource,
    Externalizable {
    private URL  url;
    private long lastRead = -1;

    public UrlResource() {

    }

    public UrlResource(URL url) {
        this.url = getCleanedUrl( url,
                                  url.toString() );
    }

    public UrlResource(String path) {
        try {
            this.url = getCleanedUrl( new URL( path ),
                                      path );
        } catch ( MalformedURLException e ) {
            throw new IllegalArgumentException( "'" + path + "' path is malformed",
                                                e );
        }
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject( this.url );
    }

    public void readExternal(ObjectInput in) throws IOException,
                                            ClassNotFoundException {
        this.url = (URL) in.readObject();
    }

    /**
     * This implementation opens an InputStream for the given URL.
     * It sets the "UseCaches" flag to <code>false</code>,
     * mainly to avoid jar file locking on Windows.
     * @see java.net.URL#openConnection()
     * @see java.net.URLConnection#setUseCaches(boolean)
     * @see java.net.URLConnection#getInputStream()
     */
    public InputStream getInputStream() throws IOException {
        this.lastRead = getLastModified();
        URLConnection con = this.url.openConnection();
        con.setUseCaches( false );
        return con.getInputStream();
    }

    public Reader getReader() throws IOException {
        return new InputStreamReader( getInputStream() );
    }

    /**
     * Determine a cleaned URL for the given original URL.
     * @param originalUrl the original URL
     * @param originalPath the original URL path
     * @return the cleaned URL
     * @see org.springframework.util.StringUtils#cleanPath
     */
    private URL getCleanedUrl(URL originalUrl,
                              String originalPath) {
        try {
            return new URL( StringUtils.cleanPath( originalPath ) );
        } catch ( MalformedURLException ex ) {
            // Cleaned URL path cannot be converted to URL
            // -> take original URL.
            return originalUrl;
        }
    }

    public URL getURL() throws IOException {
        return this.url;
    }

    public boolean hasURL() {
        return true;
    }

    public File getFile() throws IOException {
        try {
            return new File( StringUtils.toURI( url.toString() ).getSchemeSpecificPart() );
        } catch ( Exception e ) {
            throw new RuntimeException( "Unable to get File for url " + this.url, e);
        }
    }

    public long getLastModified() {
        try {
            // use File, as http rounds milliseconds on some machines, this fine level of granularity is only really an issue for testing
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4504473
            if ( "file".equals( url.getProtocol() ) ) {
                File file = getFile();
                return file.lastModified();
            } else {
                URLConnection conn = getURL().openConnection();
                if ( conn instanceof HttpURLConnection ) {
                    ((HttpURLConnection) conn).setRequestMethod( "HEAD" );
                }
                long date =  conn.getHeaderFieldDate("Last-Modified", 0); //conn.getLastModified(); to keep mic happy :)
                return date;
            }
        } catch ( IOException e ) {
            throw new RuntimeException( "Unable to get LastMofified for ClasspathResource",
                                        e );<commit_msg>JBRULES-2083 have more tests in guvnor codebase as well<commit_after>package org.drools.io.impl;

import java.io.Externalizable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.drools.io.InternalResource;
import org.drools.io.Resource;
import org.drools.util.StringUtils;

import com.sun.net.ssl.HttpsURLConnection;

/**
 * Borrowed gratuitously from Spring under ASL2.0.
 *
 */
public class UrlResource extends BaseResource
    implements
    InternalResource,
    Externalizable {
    private URL  url;
    private long lastRead = -1;

    public UrlResource() {

    }

    public UrlResource(URL url) {
        this.url = getCleanedUrl( url,
                                  url.toString() );
    }

    public UrlResource(String path) {
        try {
            this.url = getCleanedUrl( new URL( path ),
                                      path );
        } catch ( MalformedURLException e ) {
            throw new IllegalArgumentException( "'" + path + "' path is malformed",
                                                e );
        }
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject( this.url );
    }

    public void readExternal(ObjectInput in) throws IOException,
                                            ClassNotFoundException {
        this.url = (URL) in.readObject();
    }

    /**
     * This implementation opens an InputStream for the given URL.
     * It sets the "UseCaches" flag to <code>false</code>,
     * mainly to avoid jar file locking on Windows.
     * @see java.net.URL#openConnection()
     * @see java.net.URLConnection#setUseCaches(boolean)
     * @see java.net.URLConnection#getInputStream()
     */
    public InputStream getInputStream() throws IOException {
        this.lastRead = getLastModified();
        URLConnection con = this.url.openConnection();
        con.setUseCaches( false );
        return con.getInputStream();
    }

    public Reader getReader() throws IOException {
        return new InputStreamReader( getInputStream() );
    }

    /**
     * Determine a cleaned URL for the given original URL.
     * @param originalUrl the original URL
     * @param originalPath the original URL path
     * @return the cleaned URL
     * @see org.springframework.util.StringUtils#cleanPath
     */
    private URL getCleanedUrl(URL originalUrl,
                              String originalPath) {
        try {
            return new URL( StringUtils.cleanPath( originalPath ) );
        } catch ( MalformedURLException ex ) {
            // Cleaned URL path cannot be converted to URL
            // -> take original URL.
            return originalUrl;
        }
    }

    public URL getURL() throws IOException {
        return this.url;
    }

    public boolean hasURL() {
        return true;
    }

    public File getFile() throws IOException {
        try {
            return new File( StringUtils.toURI( url.toString() ).getSchemeSpecificPart() );
        } catch ( Exception e ) {
            throw new RuntimeException( "Unable to get File for url " + this.url, e);
        }
    }

    public long getLastModified() {
        try {
            // use File, as http rounds milliseconds on some machines, this fine level of granularity is only really an issue for testing
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4504473
            if ( "file".equals( url.getProtocol() ) ) {
                File file = getFile();
                return file.lastModified();
            } else {
                URLConnection conn = getURL().openConnection();
                if ( conn instanceof HttpURLConnection ) {
                    ((HttpURLConnection) conn).setRequestMethod( "HEAD" );
                }
                long date =  conn.getLastModified();
                if (date == 0) {
                     try {
                         date = Long.parseLong(conn.getHeaderField("lastModified"));
                     } catch (Exception e) { /* well, we tried ... */ }
                }
                return date;
            }
        } catch ( IOException e ) {
            throw new RuntimeException( "Unable to get LastMofified for ClasspathResource",
                                        e );
{'fraction_non_alphanumeric': 0.0908485856905158, 'fraction_numerical': 0.0028840820854132004, 'mean_word_length': 1.9696969696969697, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2767430', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2271, 'n_words': 662}