001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     dmetzler
018 */
019package org.nuxeo.datadog.reporter;
020
021import java.net.URI;
022import java.net.URISyntaxException;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.Collections;
026import java.util.List;
027
028import org.apache.commons.lang.StringUtils;
029import org.nuxeo.common.xmap.annotation.XNode;
030import org.nuxeo.common.xmap.annotation.XObject;
031import org.nuxeo.runtime.api.Framework;
032
033@XObject("configuration")
034public class DatadogReporterConfDescriptor {
035
036    @XNode("apiKey")
037    String apiKey;
038
039    @XNode("pollInterval")
040    int pollInterval;
041
042    @XNode("host")
043    String host;
044
045    @XNode("tags")
046    String tags;
047
048    public long getPollInterval() {
049        return pollInterval;
050    }
051
052    public String getApiKey() {
053        return apiKey;
054    }
055
056    public String getHost() {
057        if (StringUtils.isNotBlank(host)) {
058            return host;
059        } else {
060            return computeHostFromNuxeoUrl();
061        }
062    }
063
064    private String computeHostFromNuxeoUrl() {
065        try {
066            String url = Framework.getProperty("nuxeo.url");
067            if (StringUtils.isBlank(url)) {
068                return "";
069            }
070
071            URI uri = new URI(url);
072
073            String domain = uri.getHost();
074            if (StringUtils.isBlank(domain)) {
075                return "";
076            }
077
078            return domain.startsWith("www.") ? domain.substring(4) : domain;
079
080        } catch (URISyntaxException e) {
081            return "";
082        }
083    }
084
085    public List<String> getTags() {
086        if(StringUtils.isBlank(tags)) {
087            return Collections.emptyList();
088        } else {
089            List<String> result = new ArrayList<>();
090
091            for(String tag : Arrays.asList(tags.split(","))){
092                result.add(tag.trim());
093            }
094            return result;
095        }
096    }
097}