/**
 *
 * Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); You may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.speedment.stat;

import com.speedment.gui.Settings;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Optional;
import static java.util.stream.Collectors.joining;
import java.util.stream.Stream;

/**
 *
 * @author Emil Forslund
 */
public class Statistics {
    
    private final static String PING_URL = "http://speedment.com:8081/Beacon";
    private final static Optional<String> VERSION = Optional.ofNullable(Settings.class.getPackage().getImplementationVersion());
    private final static Settings SETTINGS = Settings.inst();
    
    public static void onGuiStarted() {
        notifyEvent("gui-started", 
            new Param("mail", SETTINGS.get("user_mail", "no-mail-specified"))
        );
    }

    public static void onGenerate() {
        notifyEvent("generate");
    }

    private static void notifyEvent(String event, Param... params) {
        final Param[] all = Arrays.copyOf(params, params.length + 3);
        all[all.length - 3] = new Param("project-key", Hash.md5(System.getProperty("user.dir")));
        all[all.length - 2] = new Param("version", VERSION.orElse("unknown"));
        all[all.length - 1] = new Param("event", event);
        sendPostRequest(all);
    }

    private static void sendPostRequest(Param... params) {
        new Thread(() -> {
            final URL url = createRequestURL(params);

            try {
                final HttpURLConnection con = (HttpURLConnection) url.openConnection();

                con.setDoOutput(true);
                con.setRequestMethod("GET");

                try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
                    wr.writeBytes("ping");
                    wr.flush();
                }

                con.getResponseCode();
            } catch (IOException ex) {}
        }).start();
    }
    
    private static URL createRequestURL(Param... params) {
        try {
            return new URL(PING_URL + "?" +
                Stream.of(params)
                    .map(Param::encode)
                    .collect(joining("&"))
            );
        } catch (MalformedURLException ex) {
            throw new RuntimeException("Could not parse statistics url.", ex);
        }
    }

    private static class Param {
        
        private final String key, value;
        
        public Param(String key, String value) {
            this.key   = key;
            this.value = value;
        }
        
        public String getKey() {
            return key;
        }
        
        public String getValue() {
            return value;
        }
        
        public String encode() {
            try {
                return URLEncoder.encode(getKey(), "UTF-8") + "=" + 
                       URLEncoder.encode(getValue(), "UTF-8");
            } catch (UnsupportedEncodingException ex) {
                throw new RuntimeException("Encoding 'UTF-8' is not supported.");
            }
        }
    }
}
@@ -1635,32 +1635,121 @@
 enerate%22);%0A    %7D
+%0A    %0A    public static void onNodeStarted() %7B%0A        notifyEvent(%22node-started%22);%0A    %7D
 %0A%0A    private st
@@ -2738,16 +2738,56 @@
 on ex) %7B
+%0A                // Silent.%0A            
 %7D%0A      

Make errors silent in statistics