001/*
002 * (C) Copyright 2006-2015 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     dmetzler
016 */
017package org.nuxeo.datadog.reporter;
018
019import java.net.URI;
020import java.net.URISyntaxException;
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.Collections;
024import java.util.List;
025
026import org.apache.commons.lang.StringUtils;
027import org.nuxeo.common.xmap.annotation.XNode;
028import org.nuxeo.common.xmap.annotation.XObject;
029import org.nuxeo.runtime.api.Framework;
030
031@XObject("configuration")
032public class DatadogReporterConfDescriptor {
033
034    @XNode("apiKey")
035    String apiKey;
036
037    @XNode("pollInterval")
038    int pollInterval;
039
040    @XNode("host")
041    String host;
042
043    @XNode("tags")
044    String tags;
045
046    public long getPollInterval() {
047        return pollInterval;
048    }
049
050    public String getApiKey() {
051        return apiKey;
052    }
053
054    public String getHost() {
055        if (StringUtils.isNotBlank(host)) {
056            return host;
057        } else {
058            return computeHostFromNuxeoUrl();
059        }
060    }
061
062    private String computeHostFromNuxeoUrl() {
063        try {
064            String url = Framework.getProperty("nuxeo.url");
065            if (StringUtils.isBlank(url)) {
066                return "";
067            }
068
069            URI uri = new URI(url);
070
071            String domain = uri.getHost();
072            if (StringUtils.isBlank(domain)) {
073                return "";
074            }
075
076            return domain.startsWith("www.") ? domain.substring(4) : domain;
077
078        } catch (URISyntaxException e) {
079            return "";
080        }
081    }
082
083    public List<String> getTags() {
084        if(StringUtils.isBlank(tags)) {
085            return Collections.emptyList();
086        } else {
087            List<String> result = new ArrayList<>();
088
089            for(String tag : Arrays.asList(tags.split(","))){
090                result.add(tag.trim());
091            }
092            return result;
093        }
094    }
095}