1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.ci.comm.hudson.command;
17
18 import java.io.IOException;
19 import java.io.UnsupportedEncodingException;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.commons.httpclient.HttpException;
24 import org.apache.commons.httpclient.methods.PostMethod;
25 import org.apache.commons.httpclient.methods.StringRequestEntity;
26
27 import de.smartics.ci.comm.CiSystem;
28 import de.smartics.ci.comm.command.AbstractCreateJobCommand;
29 import de.smartics.ci.comm.command.CommandException;
30
31
32
33
34 public final class HudsonCreateJobCommand extends
35 AbstractCreateJobCommand<HudsonCreateJobCommand>
36 {
37
38
39
40
41
42
43
44
45
46 private String configString;
47
48
49
50
51
52
53
54
55
56
57
58
59 public HudsonCreateJobCommand(final String jobName, final String configString)
60 {
61 super(jobName);
62 this.configString = configString;
63 }
64
65
66
67
68
69 public HudsonCreateJobCommand()
70 {
71 this(null, null);
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public void setConfigString(final String configString)
89 {
90 this.configString = configString;
91 }
92
93
94
95 @Override
96 public void execute(final CiSystem target) throws CommandException
97 {
98 try
99 {
100 checkParameterNotBlank(jobName, configString);
101 createTheJob(target, jobName, configString);
102 }
103 catch (final Exception e)
104 {
105 throw new CommandException("Creating of new job " + jobName
106 + " failed with config: \n\r" + configString,
107 e);
108 }
109 }
110
111 private void createTheJob(final CiSystem target, final String jobName,
112 final String configString) throws UnsupportedEncodingException,
113 IOException, HttpException, CommandException
114 {
115 String apiUrl = buildCiCreateJobApiUrl(target.getUrl(), "createItem");
116
117 final Map<String, String> parameter = new HashMap<String, String>();
118 parameter.put("name", jobName);
119 apiUrl = apiUrl + "?" + buildQueryString(parameter);
120
121 final PostMethod method = new PostMethod(apiUrl);
122 method.addParameter("name", jobName);
123 addRequestHeaders(method);
124
125 final StringRequestEntity req =
126 new StringRequestEntity(configString, "text/plain", "UTF-8");
127 method.setRequestEntity(req);
128 final int statusCode = target.execute(method);
129 checkResult(statusCode);
130 }
131
132 private String buildCiCreateJobApiUrl(final String url, final String apiCall)
133 {
134 return url + "/" + apiCall;
135 }
136
137
138
139 }