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  import java.io.UnsupportedEncodingException;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.apache.commons.httpclient.HttpException;
24  import org.apache.commons.httpclient.methods.PostMethod;
25  import org.apache.commons.httpclient.methods.StringRequestEntity;
26  
27  import de.smartics.ci.comm.CiSystem;
28  import de.smartics.ci.comm.command.AbstractCreateJobCommand;
29  import de.smartics.ci.comm.command.CommandException;
30  
31  /**
32   * Create job command.
33   */
34  public final class HudsonCreateJobCommand extends
35      AbstractCreateJobCommand<HudsonCreateJobCommand>
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    // --- members --------------------------------------------------------------
42  
43    /**
44     * The configuration for the job / the config_xml as String.
45     */
46    private String configString;
47  
48    // ****************************** Initializer *******************************
49  
50    // ****************************** Constructors ******************************
51  
52    /**
53     * Default constructor.
54     *
55     * @param jobName the name of the job that shall be created.
56     * @param configString the configuration for the job / the config_xml as
57     *          String.
58     */
59    public HudsonCreateJobCommand(final String jobName, final String configString)
60    {
61      super(jobName);
62      this.configString = configString;
63    }
64  
65    /**
66     * Default constructor. jobName and configString are set to null and must be
67     * set later before command will be executed.
68     */
69    public HudsonCreateJobCommand()
70    {
71      this(null, null);
72    }
73  
74    // ****************************** Inner Classes *****************************
75  
76    // ********************************* Methods ********************************
77  
78    // --- init -----------------------------------------------------------------
79  
80    // --- get&set --------------------------------------------------------------
81  
82    /**
83     * Sets the configuration for the job / the config_xml as String.
84     *
85     * @param configString the configuration for the job / the config_xml as
86     *          String.
87     */
88    public void setConfigString(final String configString)
89    {
90      this.configString = configString;
91    }
92  
93    // --- business -------------------------------------------------------------
94  
95    @Override
96    public void execute(final CiSystem target) throws CommandException
97    {
98      try
99      {
100       checkParameterNotBlank(jobName, configString);
101       createTheJob(target, jobName, configString);
102     }
103     catch (final Exception e)
104     {
105       throw new CommandException("Creating of new job " + jobName
106                                  + " failed with config: \n\r" + configString,
107           e);
108     }
109   }
110 
111   private void createTheJob(final CiSystem target, final String jobName,
112       final String configString) throws UnsupportedEncodingException,
113     IOException, HttpException, CommandException
114   {
115     String apiUrl = buildCiCreateJobApiUrl(target.getUrl(), "createItem");
116 
117     final Map<String, String> parameter = new HashMap<String, String>();
118     parameter.put("name", jobName);
119     apiUrl = apiUrl + "?" + buildQueryString(parameter); // NOPMD
120 
121     final PostMethod method = new PostMethod(apiUrl);
122     method.addParameter("name", jobName);
123     addRequestHeaders(method);
124 
125     final StringRequestEntity req =
126         new StringRequestEntity(configString, "text/plain", "UTF-8");
127     method.setRequestEntity(req);
128     final int statusCode = target.execute(method);
129     checkResult(statusCode);
130   }
131 
132   private String buildCiCreateJobApiUrl(final String url, final String apiCall)
133   {
134     return url + "/" + apiCall;
135   }
136 
137   // --- object basics --------------------------------------------------------
138 
139 }