001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.weather;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.plugin.weather.beans.WeatherInformation;
035: import org.blojsom.util.BlojsomConstants;
036: import org.w3c.dom.Document;
037: import org.xml.sax.InputSource;
038: import org.xml.sax.SAXException;
039:
040: import javax.xml.parsers.DocumentBuilder;
041: import javax.xml.parsers.DocumentBuilderFactory;
042: import javax.xml.parsers.ParserConfigurationException;
043: import java.io.*;
044: import java.net.HttpURLConnection;
045: import java.net.URL;
046: import java.net.URLConnection;
047: import java.util.zip.GZIPInputStream;
048:
049: /**
050: * WeatherFetcher
051: *
052: * @author David Czarnecki
053: * @author Mark Lussier
054: * @version $Id: WeatherFetcher.java,v 1.3 2007/01/17 02:35:15 czarneckid Exp $
055: * @since blojsom 3.0
056: */
057: public class WeatherFetcher {
058:
059: private Log _logger = LogFactory.getLog(WeatherFetcher.class);
060:
061: private DocumentBuilder _documentBuilder;
062:
063: /**
064: * Construct a new instance of the <code>WeatherFetcher</code> class
065: */
066: public WeatherFetcher() {
067: DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
068: .newInstance();
069: documentBuilderFactory.setValidating(false);
070: documentBuilderFactory
071: .setIgnoringElementContentWhitespace(true);
072: documentBuilderFactory.setIgnoringComments(true);
073: documentBuilderFactory.setCoalescing(true);
074: documentBuilderFactory.setNamespaceAware(false);
075:
076: try {
077: _documentBuilder = documentBuilderFactory
078: .newDocumentBuilder();
079: } catch (ParserConfigurationException e) {
080: if (_logger.isErrorEnabled()) {
081: _logger.error(e);
082: }
083: }
084: }
085:
086: /**
087: * Retrieve the {@link WeatherInformation} from a site containing an XML feed of weather information
088: *
089: * @param provider {@link WeatherInformation}
090: * @return {@link WeatherInformation} populated with data
091: * @throws IllegalArgumentException If there is an error parsing weather information
092: * @throws IOException If there is an error parsing weather information
093: */
094: public WeatherInformation retrieveForecast(
095: WeatherInformation provider)
096: throws IllegalArgumentException, IOException {
097: URL forecastUrl = new URL(provider.getProviderUrl());
098: URLConnection connection = forecastUrl.openConnection();
099:
100: if (!(connection instanceof HttpURLConnection)) {
101: throw new IllegalArgumentException(forecastUrl
102: .toExternalForm()
103: + " is not a valid HTTP Url");
104: }
105:
106: HttpURLConnection httpConnection = (HttpURLConnection) connection;
107: httpConnection.setRequestProperty("Accept-Encoding",
108: WeatherConstants.GZIP);
109: httpConnection.setRequestProperty("User-Agent",
110: WeatherConstants.BLOJSOM_WEATHER_USER_AGENT);
111:
112: httpConnection.connect();
113: InputStream is = connection.getInputStream();
114: BufferedInputStream bis;
115:
116: if (WeatherConstants.GZIP.equalsIgnoreCase(httpConnection
117: .getContentEncoding())) {
118: bis = new BufferedInputStream(new GZIPInputStream(is));
119: } else {
120: bis = new BufferedInputStream(is);
121: }
122:
123: StringBuffer buffer = new StringBuffer();
124: BufferedReader br = new BufferedReader(new InputStreamReader(
125: bis));
126:
127: String input;
128: while ((input = br.readLine()) != null) {
129: buffer.append(input)
130: .append(BlojsomConstants.LINE_SEPARATOR);
131: }
132:
133: try {
134: Document document = _documentBuilder.parse(new InputSource(
135: new StringReader(buffer.toString())));
136: provider.parseDocument(document);
137: } catch (SAXException e) {
138: if (_logger.isErrorEnabled()) {
139: _logger.error(e);
140: }
141: }
142:
143: bis.close();
144:
145: return provider;
146: }
147: }
|