Add authentication support in hudson.Main (if username/password are included in

HUDSON_HOME URL, then include Authorization header in Hudson http requests).

Also moved Functions.isUnitTest to Main.isUnitTest, to remove dependency on
winstone.jar for running hudson.Main (Main uses EnvVars which checks isUnitTest
flag.. in Functions which uses servlet classes).

Updated http://wiki.hudson-ci.org/display/HUDSON/Monitoring+external+jobs
with info about required jars and authentication.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@21479 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
mindless committed 2009-09-05 04:49:03 +00:00
1 parent 5eaec18e1b
commit 22b729ff81
4 files changed
+20 -4

No files matched your search

+13
View File
@@ -25,6 +25,7 @@ package hudson;
import hudson.util.DualOutputStream;
import hudson.util.EncodingStream;
import com.thoughtworks.xstream.core.util.Base64Encoder;
import java.io.File;
import java.io.FileInputStream;
@@ -80,8 +81,13 @@ public class Main {
String home = getHudsonHome();
if(!home.endsWith("/")) home = home + '/'; // make sure it ends with '/'
// check for authentication info
String auth = new URL(home).getUserInfo();
if(auth != null) auth = "Basic " + new Base64Encoder().encode(auth.getBytes("UTF-8"));
{// check if the home is set correctly
HttpURLConnection con = (HttpURLConnection)new URL(home).openConnection();
if (auth != null) con.setRequestProperty("Authorization", auth);
con.connect();
if(con.getResponseCode()!=200
|| con.getHeaderField("X-Hudson")==null) {
@@ -94,6 +100,7 @@ public class Main {
{// check if the job name is correct
HttpURLConnection con = (HttpURLConnection)new URL(home+"job/"+projectNameEnc+"/acceptBuildResult").openConnection();
if (auth != null) con.setRequestProperty("Authorization", auth);
con.connect();
if(con.getResponseCode()!=200) {
System.err.println(projectName+" is not a valid job name on "+home+" ("+con.getResponseMessage()+")");
@@ -130,6 +137,7 @@ public class Main {
try {
// start a remote connection
HttpURLConnection con = (HttpURLConnection) new URL(location).openConnection();
if (auth != null) con.setRequestProperty("Authorization", auth);
con.setDoOutput(true);
// this tells HttpURLConnection not to buffer the whole thing
con.setFixedLengthStreamingMode((int)tmpFile.length());
@@ -155,4 +163,9 @@ public class Main {
}
}
}
/**
* Set to true if we are running unit tests.
*/
public static boolean isUnitTest = false;
}