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
20 import org.apache.commons.httpclient.HttpException;
21 import org.apache.commons.httpclient.methods.PostMethod;
22 import org.apache.commons.httpclient.util.URIUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import de.smartics.ci.comm.CiSystem;
27 import de.smartics.ci.comm.LogHelper;
28 import de.smartics.ci.comm.command.CommandException;
29 import de.smartics.ci.comm.command.InvalidRequestException;
30
31
32
33
34 public final class HudsonGenericJobCommand extends
35 AbstractHudsonApiJob<HudsonGenericJobCommand>
36 {
37
38
39
40
41
42
43
44 static final Logger LOG = LoggerFactory
45 .getLogger(HudsonGenericJobCommand.class);
46
47
48
49
50 private String ciXmlJobApiUrl;
51
52
53
54
55 private String regexp;
56
57
58
59
60
61
62
63
64
65
66
67
68 public HudsonGenericJobCommand(final String jobName)
69 {
70 super(jobName);
71 }
72
73
74
75
76
77 public HudsonGenericJobCommand()
78 {
79 this(null);
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public void setCiXmlJobApiUrl(final String ciXmlJobApiUrl)
96 {
97 this.ciXmlJobApiUrl = ciXmlJobApiUrl;
98 }
99
100
101
102
103
104
105
106
107 public void setResponseCheckingRegexp(final String regexp)
108 {
109 this.regexp = regexp;
110 }
111
112
113
114 @Override
115 public void execute(final CiSystem target) throws CommandException
116 {
117 try
118 {
119 executeGenericJob(target);
120 }
121 catch (final Exception e)
122 {
123 throw new CommandException("Checking the status of job " + jobName
124 + " failed.", e);
125
126 }
127 }
128
129 private void executeGenericJob(final CiSystem target) throws HttpException,
130 IOException, InvalidRequestException, CommandException
131 {
132 final String resolvedCiXmlJobApiUrl = resolve(ciXmlJobApiUrl);
133 final String resolvedXmlQueryString = resolve(xmlQueryString);
134
135 final PostMethod method = new PostMethod(resolvedCiXmlJobApiUrl);
136
137 method.setQueryString(URIUtil.encodeQuery(resolvedXmlQueryString));
138 addRequestHeaders(method);
139
140 LogHelper.logInfo(LOG, logLevel,
141 "Hudson: XML-API-URL called with queryString: \n\r"
142 + resolvedCiXmlJobApiUrl + " :: " + resolvedXmlQueryString);
143
144 final int statusCode = target.execute(method);
145
146 createResult(statusCode, method);
147 logResponse(getResult().getPageContent());
148 }
149
150
151
152
153
154
155
156 protected void checkResponseString(final String response)
157 throws CommandException
158 {
159 final String resolvedRegexp = resolve(regexp);
160 if (!response.matches(resolvedRegexp))
161 {
162 LogHelper.logError(LOG, logLevel,
163 "Regexp did not match response! Regexp: " + resolvedRegexp);
164 throw new CommandException("Regexp did not match response! Regexp: "
165 + resolvedRegexp);
166 }
167 }
168
169
170
171 }