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; 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 import org.apache.commons.lang.StringUtils; 24 25 import de.smartics.ci.comm.command.CommandException; 26 27 /** 28 * The ci system, the target of commands. 29 */ 30 public final class CiSystem 31 { 32 // ********************************* Fields ********************************* 33 34 // --- constants ------------------------------------------------------------ 35 36 // --- members -------------------------------------------------------------- 37 38 /** 39 * The HTTP client to use. 40 */ 41 private final HttpClient client; 42 43 /** 44 * The URL to the target. 45 * 46 * @serial 47 */ 48 private final String url; 49 50 // ****************************** Initializer ******************************* 51 52 // ****************************** Constructors ****************************** 53 54 /** 55 * Convenience constructor that creates the HTTP client on-the-fly. 56 * 57 * @param url the URL to the target. 58 */ 59 public CiSystem(final String url) 60 { 61 this(new HttpClient(), url); 62 } 63 64 /** 65 * Default constructor. 66 * 67 * @param client the HTTP client to use. 68 * @param url the URL to the target. 69 */ 70 public CiSystem(final HttpClient client, final String url) 71 { 72 this.client = client; 73 this.url = normalizeUrl(url); 74 } 75 76 // ****************************** Inner Classes ***************************** 77 78 // ********************************* Methods ******************************** 79 80 // --- init ----------------------------------------------------------------- 81 82 // --- get&set -------------------------------------------------------------- 83 84 private String normalizeUrl(final String url) 85 { 86 if (StringUtils.isBlank(url)) 87 { 88 return url; 89 } 90 else 91 { 92 final char lastChar = url.charAt(url.length() - 1); 93 if (checkForTrailingSlash(lastChar)) 94 { 95 return removeLastChar(url); 96 } 97 else 98 { 99 return url; 100 } 101 } 102 103 } 104 105 private String removeLastChar(final String url) 106 { 107 return url.substring(0, url.length() - 1); 108 } 109 110 private boolean checkForTrailingSlash(final char lastChar) 111 { 112 return lastChar == '/' || lastChar == '\\'; 113 } 114 115 /** 116 * Returns the URL to the target. 117 * 118 * @return the URL to the target. 119 */ 120 public String getUrl() 121 { 122 return url; 123 } 124 125 /** 126 * Returns the HTTP client to use. 127 * 128 * @return the HTTP client to use. 129 */ 130 public HttpClient getClient() 131 { 132 return client; 133 } 134 135 // --- business ------------------------------------------------------------- 136 137 // --- object basics -------------------------------------------------------- 138 139 /** 140 * Returns the string representation of the object. 141 * 142 * @return the string representation of the object. 143 */ 144 @Override 145 public String toString() 146 { 147 return url; 148 } 149 150 /** 151 * Executes the given method on the target. 152 * 153 * @param method the method to execute. 154 * @return the return code of the method. 155 * @throws CommandException on any problem executing the command on the 156 * target. 157 */ 158 public int execute(final HttpMethod method) throws CommandException 159 { 160 try 161 { 162 final int code = client.executeMethod(method); 163 return code; 164 } 165 catch (final HttpException e) 166 { 167 throw new CommandException("Cannot execute command '" + method 168 + "' on target '" + url + "'.", e); 169 } 170 catch (final IOException e) 171 { 172 throw new CommandException("Cannot execute command '" + method 173 + "' on target '" + url + "'.", e); 174 } 175 } 176 }