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.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   * Check status job command.
33   */
34  public final class HudsonGenericJobCommand extends
35      AbstractHudsonApiJob<HudsonGenericJobCommand>
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    /**
42     * Reference to the logger for this class.
43     */
44    static final Logger LOG = LoggerFactory
45        .getLogger(HudsonGenericJobCommand.class);
46  
47    /**
48     * The ci xml job api URL.
49     */
50    private String ciXmlJobApiUrl;
51  
52    /**
53     * The regexp to check the response.
54     */
55    private String regexp;
56  
57    // --- members --------------------------------------------------------------
58  
59    // ****************************** Initializer *******************************
60  
61    // ****************************** Constructors ******************************
62  
63    /**
64     * Default constructor.
65     *
66     * @param jobName the name of the job that shall be checked.
67     */
68    public HudsonGenericJobCommand(final String jobName)
69    {
70      super(jobName);
71    }
72  
73    /**
74     * Default constructor. Sets jobName to null. Must be set before using this
75     * command.
76     */
77    public HudsonGenericJobCommand()
78    {
79      this(null);
80    }
81  
82    // ****************************** Inner Classes *****************************
83  
84    // ********************************* Methods ********************************
85  
86    // --- init -----------------------------------------------------------------
87  
88    // --- get&set --------------------------------------------------------------
89  
90    /**
91     * Sets the ci xml job api URL.
92     *
93     * @param ciXmlJobApiUrl the ci xml job api URL.
94     */
95    public void setCiXmlJobApiUrl(final String ciXmlJobApiUrl)
96    {
97      this.ciXmlJobApiUrl = ciXmlJobApiUrl;
98    }
99  
100   /**
101    * Sets the regexp used to check if the response is valid. If the regexp
102    * matches the response is treated as OK (true) but only if the response code
103    * (HTTP response code) is 2xx or 3xx.
104    *
105    * @param regexp the regexp used to check the response.
106    */
107   public void setResponseCheckingRegexp(final String regexp)
108   {
109     this.regexp = regexp;
110   }
111 
112   // --- business -------------------------------------------------------------
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    * Check the response.
152    *
153    * @param response the response.
154    * @throws CommandException when command failed.
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   // --- object basics --------------------------------------------------------
170 
171 }