Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
Replaced org.apache.commons.httpclient with HttpUtil (#5294)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp authored and 9037568 committed Sep 27, 2017
1 parent a990bde commit bc94d08
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ Bundle-Version: 1.11.0.qualifier
Bundle-Activator: org.openhab.binding.weather.internal.bus.WeatherActivator
Bundle-ManifestVersion: 2
Bundle-License: http://www.eclipse.org/legal/epl-v10.html
Bundle-Description: This is the Weather binding of the open Home Aut
omation Bus (openHAB)
Bundle-Description: This is the Weather binding of the open Home Automation Bus (openHAB)
Import-Package: javax.servlet,
javax.servlet.http,
javax.xml.bind,
javax.xml.bind.annotation,
javax.xml.bind.annotation.adapters,
org.apache.commons.httpclient,
org.apache.commons.httpclient.cookie,
org.apache.commons.httpclient.methods,
org.apache.commons.httpclient.params,
org.apache.commons.io,
org.apache.commons.lang,
org.apache.commons.lang.builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
package org.openhab.binding.weather.internal.parser;

import java.io.InputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.openhab.binding.weather.internal.converter.Converter;
Expand All @@ -33,6 +31,7 @@
* Common base class for all weather parsers.
*
* @author Gerhard Riegler
* @author Christoph Weitkamp - Replaced org.apache.commons.httpclient with HttpUtil
* @since 1.6.0
*/
public abstract class AbstractWeatherParser implements WeatherParser {
Expand All @@ -41,11 +40,8 @@ public abstract class AbstractWeatherParser implements WeatherParser {

private MetadataHandler metadataHandler = MetadataHandler.getInstance();

/**
* {@inheritDoc}
*/
@Override
public void parseInto(InputStream is, Weather weather) throws Exception {
public void parseInto(String r, Weather weather) throws Exception {
postProcessEach(weather);
for (Forecast forecast : weather.getForecast()) {
postProcessEach(forecast);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
package org.openhab.binding.weather.internal.parser;

import java.io.InputStream;

import org.openhab.binding.weather.internal.model.Weather;
import org.openhab.binding.weather.internal.utils.PropertyResolver;
import org.slf4j.Logger;
Expand All @@ -23,24 +21,22 @@
* Weather parser with JSON data in the InputStream.
*
* @author Gerhard Riegler
* @author Christoph Weitkamp - Replaced org.apache.commons.httpclient with HttpUtil
* @since 1.6.0
*/
public class JsonWeatherParser extends AbstractWeatherParser {
private static final Logger logger = LoggerFactory.getLogger(JsonWeatherParser.class);

/**
* {@inheritDoc}
*/
@Override
public void parseInto(InputStream is, Weather weather) throws Exception {
public void parseInto(String r, Weather weather) throws Exception {
JsonFactory jsonFactory = new JsonFactory();
JsonParser jp = jsonFactory.createParser(is);
JsonParser jp = jsonFactory.createParser(r);

jp.nextValue();
handleToken(jp, null, weather);
jp.close();

super.parseInto(is, weather);
super.parseInto(r, weather);
}

/**
Expand All @@ -60,7 +56,7 @@ private void handleToken(JsonParser jp, String property, Weather weather) throws
if (isObject) {
endIfForecast(weather, forecast, prop);
}
} else if (token != null) {
} else {
try {
setValue(weather, prop, jp.getValueAsString());
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@
*/
package org.openhab.binding.weather.internal.parser;

import java.io.InputStream;

import org.openhab.binding.weather.internal.model.Weather;

/**
* Weather parser definition.
*
* @author Gerhard Riegler
* @author Christoph Weitkamp - Replaced org.apache.commons.httpclient with HttpUtil
* @since 1.6.0
*/
public interface WeatherParser {

/**
* Parses the InputStream into the weather object.
* Parses the response String into the weather object.
*/
public void parseInto(InputStream is, Weather weather) throws Exception;
public void parseInto(String r, Weather weather) throws Exception;

/**
* Sets the property of the weather object with the value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,8 @@
*/
package org.openhab.binding.weather.internal.provider;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Calendar;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.HttpVersion;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.lang.StringUtils;
import org.openhab.binding.weather.internal.common.LocationConfig;
import org.openhab.binding.weather.internal.common.ProviderConfig;
Expand All @@ -28,6 +19,7 @@
import org.openhab.binding.weather.internal.model.ProviderName;
import org.openhab.binding.weather.internal.model.Weather;
import org.openhab.binding.weather.internal.parser.WeatherParser;
import org.openhab.io.net.http.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -36,25 +28,15 @@
* weather data.
*
* @author Gerhard Riegler
* @author Christoph Weitkamp - Replaced org.apache.commons.httpclient with HttpUtil
* @since 1.6.0
*/
public abstract class AbstractWeatherProvider implements WeatherProvider {
private static final Logger logger = LoggerFactory.getLogger(AbstractWeatherProvider.class);
private static HttpClient httpClient = null;

private WeatherConfig config = WeatherContext.getInstance().getConfig();
private WeatherParser parser;

static {
httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
HttpClientParams params = httpClient.getParams();
params.setConnectionManagerTimeout(15000);
params.setSoTimeout(30000);
params.setContentCharset("UTF-8");
params.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
params.setVersion(HttpVersion.HTTP_1_0);
}

public AbstractWeatherProvider(WeatherParser parser) {
this.parser = parser;
}
Expand Down Expand Up @@ -108,35 +90,28 @@ private String prepareUrl(String url, LocationConfig locationConfig) {
* Executes the http request and parses the returned stream.
*/
private void executeRequest(Weather weather, String url, LocationConfig locationConfig) throws Exception {
GetMethod get = null;
try {
logger.trace("{}[{}]: request : {}", getProviderName(), locationConfig.getLocationId(), url);

get = new GetMethod(url);
httpClient.executeMethod(get);
String response = HttpUtil.executeUrl("GET", url, 15000);

InputStream is = null;
if (logger.isTraceEnabled()) {
String response = get.getResponseBodyAsString(100000);
response = StringUtils.remove(response, "\n");
response = StringUtils.trim(response);
logger.trace("{}[{}]: response: {}", getProviderName(), locationConfig.getLocationId(), response);
is = new ByteArrayInputStream(response.getBytes(get.getResponseCharSet()));
} else {
is = get.getResponseBodyAsStream();
}

if (get.getStatusCode() == HttpStatus.SC_OK) {
parser.parseInto(is, weather);
if (!response.isEmpty()) {
parser.parseInto(response, weather);
}
// special handling because of bad OpenWeatherMap json structure
if (weather.getProvider() == ProviderName.OPENWEATHERMAP && weather.getResponseCode() != null
&& weather.getResponseCode() == 200) {
weather.setError(null);
}

if (!weather.hasError() && get.getStatusCode() != HttpStatus.SC_OK) {
weather.setError(get.getStatusLine().toString());
if (!weather.hasError() && response.isEmpty()) {
weather.setError("Error: response is empty!");
}

if (weather.hasError()) {
Expand All @@ -149,10 +124,6 @@ private void executeRequest(Weather weather, String url, LocationConfig location
logger.error(getProviderName() + ": " + ex.getMessage());
weather.setError(ex.getClass().getSimpleName() + ": " + ex.getMessage());
throw ex;
} finally {
if (get != null) {
get.releaseConnection();
}
}
}

Expand Down

0 comments on commit bc94d08

Please sign in to comment.