Search in sources :

Example 6 with RawType

use of org.eclipse.smarthome.core.library.types.RawType in project smarthome by eclipse.

the class HttpUtil method downloadData.

/**
 * Download the data from an URL.
 *
 * @param url the URL of the data to be downloaded
 * @param contentTypeRegex the REGEX the content type must match; null to ignore the content type
 * @param scanTypeInContent true to allow the scan of data to determine the content type if not found in the headers
 * @param maxContentLength the maximum data size in bytes to trigger the download; any negative value to ignore the
 *            data size
 * @param timeout the socket timeout in milliseconds to wait for data
 * @return a RawType object containing the downloaded data, null if the content type does not match the expected
 *         type or the data size is too big
 */
public static RawType downloadData(String url, String contentTypeRegex, boolean scanTypeInContent, long maxContentLength, int timeout) {
    final ProxyParams proxyParams = prepareProxyParams();
    RawType rawData = null;
    try {
        ContentResponse response = executeUrlAndGetReponse("GET", url, null, null, null, timeout, proxyParams.proxyHost, proxyParams.proxyPort, proxyParams.proxyUser, proxyParams.proxyPassword, proxyParams.nonProxyHosts);
        byte[] data = response.getContent();
        long length = (data == null) ? 0 : data.length;
        String mediaType = response.getMediaType();
        logger.debug("Media download response: status {} content length {} media type {} (URL {})", response.getStatus(), length, mediaType, url);
        if (response.getStatus() != HttpStatus.OK_200 || length == 0) {
            logger.debug("Media download failed: unexpected return code {} (URL {})", response.getStatus(), url);
            return null;
        }
        if (maxContentLength >= 0 && length > maxContentLength) {
            logger.debug("Media download aborted: content length {} too big (URL {})", length, url);
            return null;
        }
        String contentType = mediaType;
        if (contentTypeRegex != null) {
            if ((contentType == null || contentType.isEmpty()) && scanTypeInContent) {
                // We try to get the type from the data
                contentType = guessContentTypeFromData(data);
                logger.debug("Media download: content type from data: {} (URL {})", contentType, url);
            }
            if (contentType != null && contentType.isEmpty()) {
                contentType = null;
            }
            if (contentType == null) {
                logger.debug("Media download aborted: unknown content type (URL {})", url);
                return null;
            } else if (!contentType.matches(contentTypeRegex)) {
                logger.debug("Media download aborted: unexpected content type \"{}\" (URL {})", contentType, url);
                return null;
            }
        } else if (contentType == null || contentType.isEmpty()) {
            contentType = RawType.DEFAULT_MIME_TYPE;
        }
        rawData = new RawType(data, contentType);
        logger.debug("Media downloaded: size {} type {} (URL {})", rawData.getBytes().length, rawData.getMimeType(), url);
    } catch (IOException e) {
        logger.debug("Media download failed (URL {}) : {}", url, e.getMessage());
    }
    return rawData;
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) RawType(org.eclipse.smarthome.core.library.types.RawType) IOException(java.io.IOException)

Example 7 with RawType

use of org.eclipse.smarthome.core.library.types.RawType in project smarthome by eclipse.

the class ImageRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Image image = (Image) w;
    String snippet = (image.getChildren().size() > 0) ? getSnippet("image_link") : getSnippet("image");
    if (image.getRefresh() > 0) {
        snippet = StringUtils.replace(snippet, "%update_interval%", Integer.toString(image.getRefresh()));
    } else {
        snippet = StringUtils.replace(snippet, "%update_interval%", "0");
    }
    String widgetId = itemUIRegistry.getWidgetId(w);
    snippet = StringUtils.replace(snippet, "%id%", widgetId);
    snippet = preprocessSnippet(snippet, w);
    String sitemap = null;
    if (w.eResource() != null) {
        sitemap = w.eResource().getURI().path();
    }
    boolean validUrl = false;
    if (image.getUrl() != null && !image.getUrl().isEmpty()) {
        try {
            URI.create(image.getUrl());
            validUrl = true;
        } catch (IllegalArgumentException ex) {
        }
    }
    String proxiedUrl = "../proxy?sitemap=" + sitemap + "&amp;widgetId=" + widgetId;
    State state = itemUIRegistry.getState(w);
    String url;
    boolean ignoreRefresh;
    if (!itemUIRegistry.getVisiblity(w)) {
        url = URL_NONE_ICON;
        ignoreRefresh = true;
    } else if (state instanceof RawType) {
        url = state.toFullString();
        ignoreRefresh = true;
    } else if ((sitemap != null) && ((state instanceof StringType) || validUrl)) {
        url = proxiedUrl + "&amp;t=" + (new Date()).getTime();
        ignoreRefresh = false;
    } else {
        url = URL_NONE_ICON;
        ignoreRefresh = true;
    }
    snippet = StringUtils.replace(snippet, "%valid_url%", validUrl ? "true" : "false");
    snippet = StringUtils.replace(snippet, "%proxied_url%", proxiedUrl);
    snippet = StringUtils.replace(snippet, "%ignore_refresh%", ignoreRefresh ? "true" : "false");
    snippet = StringUtils.replace(snippet, "%url%", url);
    sb.append(snippet);
    return null;
}
Also used : StringType(org.eclipse.smarthome.core.library.types.StringType) State(org.eclipse.smarthome.core.types.State) RawType(org.eclipse.smarthome.core.library.types.RawType) Image(org.eclipse.smarthome.model.sitemap.Image) Date(java.util.Date)

Aggregations

RawType (org.eclipse.smarthome.core.library.types.RawType)7 State (org.eclipse.smarthome.core.types.State)5 StringType (org.eclipse.smarthome.core.library.types.StringType)3 Date (java.util.Date)2 Image (org.eclipse.smarthome.model.sitemap.Image)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Temperature (javax.measure.quantity.Temperature)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 DateTimeType (org.eclipse.smarthome.core.library.types.DateTimeType)1 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)1 HSBType (org.eclipse.smarthome.core.library.types.HSBType)1 PercentType (org.eclipse.smarthome.core.library.types.PercentType)1 PointType (org.eclipse.smarthome.core.library.types.PointType)1 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)1 StringListType (org.eclipse.smarthome.core.library.types.StringListType)1 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)1