View Javadoc

1   /*
2    * Copyright 2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Check status job command.
34   */
35  public final class HudsonCheckStatusJobCommand extends
36      AbstractHudsonApiJob<HudsonCheckStatusJobCommand>
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    /**
43     * Reference to the logger for this class.
44     */
45    private static final Logger LOG = LoggerFactory
46        .getLogger(HudsonCheckStatusJobCommand.class);
47  
48    /**
49     * The default query string to use for the xml api. Uses one variable
50     * ${jobName}, that will be resolved.
51     */
52    private static final String DEFAULT_XML_QUERY_STRING =
53        "tree=jobs[name]&xpath=hudson/job[name=\"${jobName}\"]/name/text()";
54  
55    // --- members --------------------------------------------------------------
56  
57    // ****************************** Initializer *******************************
58  
59    // ****************************** Constructors ******************************
60  
61    /**
62     * Default constructor.
63     *
64     * @param jobName the name of the job that shall be checked.
65     */
66    public HudsonCheckStatusJobCommand(final String jobName)
67    {
68      super(jobName);
69    }
70  
71    /**
72     * Default constructor. Sets jobName to null. Must be set before using this
73     * command.
74     */
75    public HudsonCheckStatusJobCommand()
76    {
77      this(null);
78    }
79  
80    // ****************************** Inner Classes *****************************
81  
82    // ********************************* Methods ********************************
83  
84    // --- init -----------------------------------------------------------------
85  
86    // --- get&set --------------------------------------------------------------
87  
88    // --- business -------------------------------------------------------------
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 }