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