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.maven.issue.command; 17 18 import java.io.IOException; 19 20 import org.apache.commons.httpclient.HttpClient; 21 import org.apache.commons.httpclient.HttpException; 22 import org.apache.commons.httpclient.HttpMethod; 23 24 /** 25 * The target of commands. 26 */ 27 public final class CommandTarget 28 { 29 // ********************************* Fields ********************************* 30 31 // --- constants ------------------------------------------------------------ 32 33 // --- members -------------------------------------------------------------- 34 35 /** 36 * The HTTP client to use. 37 */ 38 private final HttpClient client; 39 40 /** 41 * The URL to the target. 42 * 43 * @serial 44 */ 45 private final String url; 46 47 // ****************************** Initializer ******************************* 48 49 // ****************************** Constructors ****************************** 50 51 /** 52 * Convenience constructor that creates the HTTP client on-the-fly. 53 * 54 * @param url the URL to the target. 55 */ 56 public CommandTarget(final String url) 57 { 58 this(new HttpClient(), url); 59 } 60 61 /** 62 * Default constructor. 63 * 64 * @param client the HTTP client to use. 65 * @param url the URL to the target. 66 */ 67 public CommandTarget(final HttpClient client, final String url) 68 { 69 this.client = client; 70 this.url = url; 71 } 72 73 // ****************************** Inner Classes ***************************** 74 75 // ********************************* Methods ******************************** 76 77 // --- init ----------------------------------------------------------------- 78 79 // --- get&set -------------------------------------------------------------- 80 81 /** 82 * Returns the URL to the target. 83 * 84 * @return the URL to the target. 85 */ 86 public String getUrl() 87 { 88 return url; 89 } 90 91 // --- business ------------------------------------------------------------- 92 93 // --- object basics -------------------------------------------------------- 94 95 /** 96 * Returns the string representation of the object. 97 * 98 * @return the string representation of the object. 99 */ 100 @Override 101 public String toString() 102 { 103 return url; 104 } 105 106 /** 107 * Executes the given method on the target. 108 * 109 * @param method the method to execute. 110 * @return the return code of the method. 111 * @throws CommandException on any problem executing the command on the 112 * target. 113 */ 114 public int execute(final HttpMethod method) throws CommandException 115 { 116 try 117 { 118 method.getParams().setContentCharset("UTF-8"); 119 final int code = client.executeMethod(method); 120 return code; 121 } 122 catch (final HttpException e) 123 { 124 throw new CommandException("Cannot execute command '" + method 125 + "' on target '" + url + "'.", e); 126 } 127 catch (final IOException e) 128 { 129 throw new CommandException("Cannot execute command '" + method 130 + "' on target '" + url + "'.", e); 131 } 132 } 133 }