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.apache.commons.lang.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import de.smartics.ci.comm.CiSystem;
28 import de.smartics.ci.comm.LogHelper;
29 import de.smartics.ci.comm.command.CommandException;
30 import de.smartics.ci.comm.command.InvalidRequestException;
31
32
33
34
35 public final class HudsonCheckStatusJobCommand extends
36 AbstractHudsonApiJob<HudsonCheckStatusJobCommand>
37 {
38
39
40
41
42
43
44
45 private static final Logger LOG = LoggerFactory
46 .getLogger(HudsonCheckStatusJobCommand.class);
47
48
49
50
51
52 private static final String DEFAULT_XML_QUERY_STRING =
53 "tree=jobs[name]&xpath=hudson/job[name=\"${jobName}\"]/name/text()";
54
55
56
57
58
59
60
61
62
63
64
65
66 public HudsonCheckStatusJobCommand(final String jobName)
67 {
68 super(jobName);
69 }
70
71
72
73
74
75 public HudsonCheckStatusJobCommand()
76 {
77 this(null);
78 }
79
80
81
82
83
84
85
86
87
88
89
90 @Override
91 public void execute(final CiSystem target) throws CommandException
92 {
93 try
94 {
95 checkJobStatus(target);
96 }
97 catch (final Exception e)
98 {
99 throw new CommandException("Checking the status of job " + jobName
100 + " failed.", e);
101
102 }
103 }
104
105 private void checkJobStatus(final CiSystem target) throws HttpException,
106 IOException, InvalidRequestException, CommandException
107 {
108 final String apiUrl = buildCiXmlJobApiUrl(target.getUrl(), "api/xml");
109 final PostMethod method = new PostMethod(apiUrl);
110 final String resolvedXmlQueryString;
111 if (StringUtils.isBlank(xmlQueryString))
112 {
113 resolvedXmlQueryString = resolve(DEFAULT_XML_QUERY_STRING);
114 }
115 else
116 {
117 resolvedXmlQueryString = resolve(xmlQueryString);
118 }
119
120 method.setQueryString(URIUtil.encodeQuery(resolvedXmlQueryString));
121 addRequestHeaders(method);
122
123 LogHelper.logInfo(LOG, logLevel,
124 "Hudson: XML-API-Url with queryString: \n\r" + apiUrl + " :: "
125 + resolvedXmlQueryString);
126
127 final int statusCode = target.execute(method);
128
129 createResult(statusCode, method);
130 logResponse(getResult().getPageContent());
131
132 }
133
134 private String buildCiXmlJobApiUrl(final String url, final String postPart)
135 {
136 return url + "/" + postPart;
137 }
138
139 }