KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > model > BuildAuthorizationToken


1 package hudson.model;
2
3 import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter;
4 import org.kohsuke.stapler.StaplerRequest;
5 import org.kohsuke.stapler.StaplerResponse;
6
7 import javax.servlet.ServletException JavaDoc;
8 import java.io.IOException JavaDoc;
9
10 /**
11  * Authorization token to allow projects to trigger themselves under the secured environment.
12  *
13  * @author Kohsuke Kawaguchi
14  * @see BuildableItem
15  */

16 public final class BuildAuthorizationToken {
17     private final String JavaDoc token;
18
19     public BuildAuthorizationToken(String JavaDoc token) {
20         this.token = token;
21     }
22
23     public static BuildAuthorizationToken create(StaplerRequest req) {
24         if (req.getParameter("pseudoRemoteTrigger") != null)
25             return new BuildAuthorizationToken(req.getParameter("authToken"));
26         else
27             return null;
28     }
29
30     public static void startBuildIfAuthorized(BuildAuthorizationToken token, BuildableItem job, StaplerRequest req, StaplerResponse rsp) throws IOException JavaDoc, ServletException JavaDoc {
31         if(token==null || token.authorizedToStartBuild(req,rsp)) {
32             job.scheduleBuild();
33             rsp.forwardToPreviousPage(req);
34         }
35     }
36
37     public boolean authorizedToStartBuild(StaplerRequest req, StaplerResponse rsp) throws IOException JavaDoc {
38         if (!Hudson.getInstance().isUseSecurity())
39             // everyone is authorized
40
return true;
41
42         if(token != null) {
43             //check the provided token
44
String JavaDoc providedToken = req.getParameter("token");
45             if (providedToken != null && providedToken.equals(token))
46                 return true;
47         }
48
49         // otherwise it must be an admin
50
return Hudson.adminCheck(req, rsp);
51     }
52
53     public String JavaDoc getToken() {
54         return token;
55     }
56
57     public static final class ConverterImpl extends AbstractBasicConverter {
58         public boolean canConvert(Class JavaDoc type) {
59             return type== BuildAuthorizationToken.class;
60         }
61
62         protected Object JavaDoc fromString(String JavaDoc str) {
63             return new BuildAuthorizationToken(str);
64         }
65
66         protected String JavaDoc toString(Object JavaDoc obj) {
67             return ((BuildAuthorizationToken)obj).token;
68         }
69     }
70 }
71
Popular Tags