Question

I have a properties file that contains a property specifying the URL of a NOAA web site containing a temperature data set. The property contains a [DATE_REPLACE] token because the URL changes daily when NOAA generates a new forecast.

In my properties file, I am specifying:

WEATHER_DATA_URL="http://weather.noaa.gov/pub/SL.us008001/DF.anf/DC.mos/DS.mex/RD.[DATE_REPLACE]/cy.00.txt"

I have declared a method withing a PropertyHelper class (a wrapper for java.util.Properties) to generate the URL String for the current day using WEATHER_DATA_URL as the name, "yyyyMMdd" as the date format, a today's Date.

public String getPropertyWithDateReplaceToken(String name, String dateFormat, Date dateToFormat)
{
    String value = this.properties.getProperty(name);

    if (StringHelper.isNullOrWhitespace(value) || !value.contains("[DATE_REPLACE]"))
    {
        throw new UnsupportedOperationException("The property value should specify the [DATE_REPLACE] token");
    }

    StringBuilder sb = new StringBuilder(value);
    int index = sb.indexOf("[DATE_REPLACE]");
    while (index != -1)
    {
        String replacement = StringHelper.getTodayAsDateString(dateFormat, dateToFormat);
        sb.replace(index, index + "[DATE_REPLACE]".length(), replacement);
        index += replacement.length();
        index = sb.indexOf(value, index);
    }

    return sb.toString();
}

I then call another helper class with the following method to read the text from the web page:

public static List<String> readLinesFromWebPage(String urlText) throws Exception
{
    List<String> lines = new ArrayList<String>();
    if (StringHelper.isNullOrWhitespace(urlText))
    {
        throw new NullPointerException("URL text cannot be null or empty");
    }

    BufferedReader dataReader = null;
    try
    {
        System.out.println("URL = " + urlText);
        String trimmedUrlText = urlText.replaceAll("\\s", "");

        URL url = new URL(trimmedUrlText);
        dataReader = new BufferedReader(new InputStreamReader(url.openStream()));

        String inputLine;
        while((inputLine = dataReader.readLine()) != null)
        {
            lines.add(inputLine);
        }

        return lines; 
    }
    catch(Exception e)
    {
        logger.logThrow(Level.SEVERE, e, "Exception (" + e.getMessage() + ") attempting to " +
                "read data from URL (" + urlText + ")");
        throw e;
    }
}

As you can see I have tried to trim spaces from the generated URL String in the hopes that that was causing the issue. The URL string is generated properly but I am getting the following exception:

java.net.MalformedURLException: no protocol: "http://weather.noaa.gov/pub/SL.us008001/DF.anf/DC.mos/DS.mex/RD.20121219/cy.00.txt"

If I set the string manually, everything works ... what am I missing?

Was it helpful?

Solution

Your property file has double quotes around the value of the URL. Remove these.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top