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.methods.StringRequestEntity;
23  
24  import de.smartics.ci.comm.CiSystem;
25  import de.smartics.ci.comm.command.CommandException;
26  import de.smartics.ci.comm.command.InvalidRequestException;
27  import de.smartics.ci.comm.command.AbstractUpdateJobCommand;
28  
29  /**
30   * Enable job command.
31   */
32  public final class HudsonUpdateJobCommand extends
33      AbstractUpdateJobCommand<HudsonUpdateJobCommand>
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    /**
40     * The configuration for the job / the config_xml as String.
41     */
42    private String configString;
43  
44    // --- members --------------------------------------------------------------
45  
46    // ****************************** Initializer *******************************
47  
48    // ****************************** Constructors ******************************
49  
50    /**
51     * The default constructor.
52     *
53     * @param jobName the name of the job that shall be created.
54     * @param configString the configuration for the job / the config_xml as
55     *          String.
56     */
57    public HudsonUpdateJobCommand(final String jobName, final String configString)
58    {
59      super(jobName);
60      this.configString = configString;
61    }
62  
63    /**
64     * Constructor setting jobName to null. Must be set later.
65     */
66    public HudsonUpdateJobCommand()
67    {
68      this(null, null);
69    }
70  
71    // ****************************** Inner Classes *****************************
72  
73    // ********************************* Methods ********************************
74  
75    // --- init -----------------------------------------------------------------
76  
77    // --- get&set --------------------------------------------------------------
78  
79    /**
80     * Sets the configuration for the job / the config_xml as String.
81     *
82     * @param configString the configuration for the job / the config_xml as
83     *          String.
84     */
85    public void setConfigString(final String configString)
86    {
87      this.configString = configString;
88    }
89  
90    // --- business -------------------------------------------------------------
91  
92    @Override
93    public void execute(final CiSystem target) throws CommandException
94    {
95      try
96      {
97        updateTheJob(target, jobName, configString);
98      }
99      catch (final Exception e)
100     {
101       throw new CommandException("Updating of job " + jobName + " failed.", e);
102     }
103   }
104 
105   private void updateTheJob(final CiSystem target, final String jobName,
106       final String configString) throws HttpException, IOException,
107     InvalidRequestException, CommandException
108   {
109     final String apiUrl =
110         buildCiJobApiUrl(target.getUrl(), jobName, "config.xml");
111     final PostMethod method = new PostMethod(apiUrl);
112     final StringRequestEntity req =
113         new StringRequestEntity(configString, "text/plain", "UTF-8");
114     method.setRequestEntity(req);
115     addRequestHeaders(method);
116     final int statusCode = target.execute(method);
117     checkResult(statusCode);
118   }
119 
120   // --- object basics --------------------------------------------------------
121 
122 }