openrat-java-client

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/openrat-java-client.git
Log | Files | Refs

CMSClient.java (5373B)


      1 /*
      2 OpenRat Java-Client
      3 Copyright (C) 2009 Jan Dankert
      4  
      5 This library is free software; you can redistribute it and/or
      6 modify it under the terms of the GNU Library General Public
      7 License as published by the Free Software Foundation; either
      8 version 2 of the License, or (at your option) any later version.
      9 
     10 This library is distributed in the hope that it will be useful,
     11 but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13 Library General Public License for more details.
     14 
     15 You should have received a copy of the GNU Library General Public
     16 License along with this library; if not, write to the
     17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
     18 Boston, MA  02110-1301, USA.
     19 
     20  */
     21 package de.openrat.client;
     22 
     23 import java.io.IOException;
     24 import java.io.PrintWriter;
     25 import java.util.Locale;
     26 
     27 import de.openrat.client.util.CMSConnection;
     28 import de.openrat.client.util.CMSRequest;
     29 
     30 /**
     31  * Client for the OpenRat Content Management System. <br>
     32  * <br>
     33  * The call to the CMS server is done via a HTTP connection.<br>
     34  * <br>
     35  * Example <br>
     36  * 
     37  * <pre>
     38  * CMSClient client = new CMSClient(&quot;demo.openrat.de&quot;, &quot;/latest-snapshot/openrat/dispatcher.php&quot;, 80);
     39  * client.setLogWriter(new PrintWriter(System.out, true));
     40  * // client.setProxy(&quot;proxy.mycompany.exmaple&quot;, 8080, &quot;user&quot;, &quot;pass&quot;);
     41  * client.setLocale(Locale.GERMAN);
     42  * LoginAction loginAction = client.getLoginAction();
     43  * 
     44  * try
     45  * {
     46  * 	loginAction.login(&quot;admin&quot;, &quot;admin&quot;, &quot;db1&quot;);
     47  * }
     48  * catch (LoginException e)
     49  * {
     50  * 	fail(&quot;Login failed&quot; + e.getLocalizedMessage());
     51  * }
     52  * </pre>
     53  * 
     54  * @author Jan Dankert
     55  */
     56 public class CMSClient
     57 {
     58 	/**
     59 	 * the api version which we are supporting.
     60 	 */
     61 	public final static Version SUPPORTED_API_VERSION = new Version("2");
     62 
     63 	/**
     64 	 * The internal connection-object to the cms.
     65 	 */
     66 	private final CMSConnection connection;
     67 
     68 	/**
     69 	 * Constructs a CMS-Connection to the specified server.<br>
     70 	 * Server-Path is "/", Server-Port is 80.
     71 	 * 
     72 	 * @param host
     73 	 *            hostname
     74 	 */
     75 	public CMSClient(String host)
     76 	{
     77 
     78 		this.connection = new CMSConnection(host, 80, "/");
     79 	}
     80 
     81 	/**
     82 	 * Constructs a CMS-Connection to the specified server/path.<br>
     83 	 * Server-Port is 80.
     84 	 * 
     85 	 * @param host
     86 	 *            hostname
     87 	 * @param path
     88 	 *            path
     89 	 */
     90 	public CMSClient(String host, String path)
     91 	{
     92 
     93 		this.connection = new CMSConnection(host, 80, path);
     94 
     95 	}
     96 
     97 	/**
     98 	 * Constructs a CMS-Connection to the specified server/path/port.
     99 	 * 
    100 	 * @param host
    101 	 *            hostname
    102 	 * @param path
    103 	 *            path
    104 	 * @param port
    105 	 *            port-number
    106 	 */
    107 	public CMSClient(String host, String path, int port)
    108 	{
    109 
    110 		this.connection = new CMSConnection(host, port, path);
    111 	}
    112 
    113 	/**
    114 	 * you may want to set a LogWriter, in which log messages are written.
    115 	 * 
    116 	 * @param logWriter
    117 	 */
    118 	public void setLogWriter(PrintWriter logWriter)
    119 	{
    120 		this.connection.setLogWriter(logWriter);
    121 	}
    122 
    123 	/**
    124 	 * CMS Connection
    125 	 * 
    126 	 * @return
    127 	 */
    128 	public CMSConnection getConnection()
    129 	{
    130 		return connection;
    131 	}
    132 
    133 	/**
    134 	 * Setting a HTTP-Proxy.
    135 	 * 
    136 	 * @param host
    137 	 *            hostname
    138 	 * @param port
    139 	 *            port
    140 	 */
    141 	public void setProxy(String host, int port)
    142 	{
    143 
    144 		setProxy(host, port, null, null);
    145 	}
    146 
    147 	/**
    148 	 * Setting a HTTP-Proxy.
    149 	 * 
    150 	 * @param host
    151 	 *            hostname
    152 	 * @param port
    153 	 *            port
    154 	 * @param user
    155 	 *            proxy username
    156 	 * @param password
    157 	 *            password
    158 	 */
    159 	public void setProxy(String host, int port, String user, String password)
    160 	{
    161 
    162 		this.connection.setProxyHostname(host);
    163 		this.connection.setProxyPort(port);
    164 		this.connection.setProxyUser(user);
    165 		this.connection.setProxyPassword(password);
    166 	}
    167 
    168 	/**
    169 	 * changing the {@link Locale}. Default is the Default-Locale.
    170 	 * 
    171 	 * @param locale
    172 	 */
    173 	public void setLocale(Locale locale)
    174 	{
    175 		connection.setLocale(locale);
    176 	}
    177 
    178 	/**
    179 	 * Socket-Timeout in milliseconds. Default: 5000.
    180 	 * 
    181 	 * @param timeout
    182 	 */
    183 	public void setTimeout(int timeout)
    184 	{
    185 		connection.setTimeout(timeout);
    186 	}
    187 
    188 	/**
    189 	 * Experimental Feature, DO <b>NOT</b> SET THIS TO TRUE. HTTP persistent
    190 	 * connections are not supported!
    191 	 * 
    192 	 * @param useKeepAlive
    193 	 */
    194 	public void setKeepAlive(boolean useKeepAlive)
    195 	{
    196 		connection.setKeepAlive(useKeepAlive);
    197 	}
    198 
    199 	@Override
    200 	public String toString()
    201 	{
    202 		return super.toString() + ": " + String.valueOf(this.connection);
    203 	}
    204 
    205 	/**
    206 	 * closing all resources. Normally, you do not need to call this, because
    207 	 * the sockets are closed after each call. this is only for future versions
    208 	 * wenn keep-alive is implemented.
    209 	 *
    210 	 * thank you for calling this method.
    211 	 * 
    212 	 */
    213 	public void close()
    214 	{
    215 		if (connection.getSocket() != null)
    216 		{
    217 			try
    218 			{
    219 				connection.getSocket().close();
    220 			}
    221 			catch (IOException e)
    222 			{
    223 				;
    224 			}
    225 		}
    226 	}
    227 
    228 	/**
    229 	 * Closes all resources before finalizing an instance of this class.
    230 	 * {@inheritDoc}
    231 	 * 
    232 	 * @see java.lang.Object#finalize()
    233 	 */
    234 	@Override
    235 	protected void finalize() throws Throwable
    236 	{
    237 		close();
    238 		super.finalize();
    239 	}
    240 
    241 
    242 
    243 	/**
    244 	 * Creating a new CMS request.
    245 	 *
    246 	 * @return CMS request
    247 	 */
    248 	public CMSRequest request(String action, String method ) {
    249 		return new CMSRequest(this.connection, action,method);
    250 	}
    251 
    252 
    253 }