package org.jenkinsci.plugins.p4.review;

import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Logger;

import hudson.EnvVars;
import hudson.Extension;
import hudson.matrix.MatrixRun;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import jenkins.model.Jenkins;

@Extension
public class ReviewNotifier extends RunListener<Run> {

	private static Logger logger = Logger.getLogger(ReviewNotifier.class.getName());

	@Override
	public void onCompleted(Run run, TaskListener listener) {

		if (run instanceof MatrixRun) {
			return;
		}

		try {
			EnvVars env = run.getEnvironment(listener);
			String fail = env.get(ReviewProp.FAIL.getProp());
			String pass = env.get(ReviewProp.PASS.getProp());

			Result result = run.getResult();
			if (result == null) {
				logger.warning("Result is null!");
				return;
			}

			Jenkins j = Jenkins.getInstance();
			if (j == null) {
				logger.warning("Jenkins instance is null!");
				return;
			}
			
			// only process valid URLs (this gets triggered for non Reviews too)
			String url = (result.equals(Result.SUCCESS)) ? pass : fail;
			if (url != null && !url.isEmpty()) {
				String rootUrl = j.getRootUrl();
				if (rootUrl == null) {
					postURL(url, null);
				} else {
					String path = run.getUrl();
					postURL(url, rootUrl + path);
				}
			} 
			
		} catch (Exception e) {
			logger.warning("Unable to Notify Review");
			e.printStackTrace();
		}
		return;
	}

	private void postURL(String postUrl, String buildUrl) throws Exception {
		logger.info("ReviewNotifier: " + postUrl + " url=" + buildUrl);

		URL url = new URL(postUrl);

		HttpURLConnection http = (HttpURLConnection) url.openConnection();
		http.setDoInput(true);
		http.setDoOutput(true);
		http.setUseCaches(false);
		http.setRequestMethod("POST");
		http.connect();

		OutputStreamWriter writer = new OutputStreamWriter(http.getOutputStream(), "UTF-8");
		writer.write("url=" + buildUrl);
		writer.flush();
		writer.close();

		int response = http.getResponseCode();
		logger.info("Response code: " + response);
	}
}

@@ -39,133 +39,8 @@
 w;%0A%0A
-import java.io.OutputStreamWriter;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.URL;%0Aimport java.util.logging.Logger;%0A%0A
 impo
@@ -274,16 +274,192 @@
 Jenkins;
+%0Aimport jenkins.model.JenkinsLocationConfiguration;%0A%0Aimport java.io.OutputStreamWriter;%0Aimport java.net.HttpURLConnection;%0Aimport java.net.URL;%0Aimport java.util.logging.Logger;
 %0A%0A@Exten
@@ -1379,42 +1379,176 @@
 %09%09%09%09
-postURL(url, null);%0A%09%09%09%09%7D else %7B%0A%09
+JenkinsLocationConfiguration globalConfig = new JenkinsLocationConfiguration();%0A%09%09%09%09%09rootUrl = (globalConfig.getUrl() == null) ? %22unset%22 : globalConfig.getUrl();%0A%09%09%09%09%7D%0A
 %09%09%09%09
@@ -1571,25 +1571,24 @@
 n.getUrl();%0A
-%09
 %09%09%09%09postURL(
@@ -1609,22 +1609,16 @@
  path);%0A
-%09%09%09%09%7D%0A
 %09%09%09%7D %0A%09%09

Use JenkinsLocationConfiguration to fetch the URL