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  
23  import de.smartics.ci.comm.CiSystem;
24  import de.smartics.ci.comm.command.CommandException;
25  import de.smartics.ci.comm.command.AbstractEnableJobCommand;
26  import de.smartics.ci.comm.command.InvalidRequestException;
27  
28  /**
29   * Enable job command.
30   */
31  public final class HudsonEnableJobCommand extends
32      AbstractEnableJobCommand<HudsonEnableJobCommand>
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    // --- members --------------------------------------------------------------
39  
40    // ****************************** Initializer *******************************
41  
42    // ****************************** Constructors ******************************
43  
44    /**
45     * Default constructor.
46     *
47     * @param jobName the name of the job that shall be enabled.
48     */
49    public HudsonEnableJobCommand(final String jobName)
50    {
51      super(jobName);
52    }
53  
54    /**
55     * Default constructor. Sets jobName to null. Must be set before using this
56     * command.
57     */
58    public HudsonEnableJobCommand()
59    {
60      this(null);
61    }
62  
63    // ****************************** Inner Classes *****************************
64  
65    // ********************************* Methods ********************************
66  
67    // --- init -----------------------------------------------------------------
68  
69    // --- get&set --------------------------------------------------------------
70  
71    // --- business -------------------------------------------------------------
72  
73    @Override
74    public void execute(final CiSystem target) throws CommandException
75    {
76      try
77      {
78        enableTheJob(target);
79      }
80      catch (final Exception e)
81      {
82        throw new CommandException("Enabling of job " + jobName + " failed.", e);
83  
84      }
85    }
86  
87    private void enableTheJob(final CiSystem target) throws HttpException,
88      IOException, InvalidRequestException, CommandException
89    {
90      final String apiUrl = buildCiJobApiUrl(target.getUrl(), jobName, "enable");
91      final PostMethod method = new PostMethod(apiUrl);
92      addRequestHeaders(method);
93      final int statusCode = target.execute(method);
94      checkResultRegardingRedirect(statusCode);
95    }
96  
97    // --- object basics --------------------------------------------------------
98  
99  }