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

Action.java (2553B)


      1 package de.openrat.client.action;
      2 
      3 import de.openrat.client.util.CMSConnection;
      4 import de.openrat.client.util.CMSException;
      5 import de.openrat.client.util.CMSRequest;
      6 import de.openrat.client.util.CMSResponse;
      7 import de.openrat.client.util.HttpRequest.HttpMethod;
      8 import de.openrat.client.util.Id;
      9 import de.openrat.client.util.ParameterMap;
     10 
     11 /**
     12  * Action.
     13  *
     14  * This class is NOT threadsafe and should be used by one thread simultaneously.
     15  */
     16 public abstract class Action
     17 {
     18 
     19 	/**
     20 	 * Parameter map.
     21 	 */
     22 	private ParameterMap parameter = new ParameterMap();
     23 	private Id id;
     24 
     25 	private CMSConnection connection;
     26 
     27 	/**
     28 	 * Clear parameter values.
     29 	 */
     30 	public void clear()
     31 	{
     32 		parameter.clear();
     33 	}
     34 
     35 	protected Action(CMSConnection connection)
     36 	{
     37 		this.connection = connection;
     38 	}
     39 
     40 	/**
     41 	 * Setting a parameter value.
     42 	 * 
     43 	 * @param paramName
     44 	 *            name
     45 	 * @param paramValue
     46 	 *            value
     47 	 */
     48 	protected void setParameter(String paramName, String paramValue)
     49 	{
     50 
     51 		parameter.put(paramName, paramValue);
     52 	}
     53 
     54 	/**
     55 	 * Setting a parameter value. <strong>DO NOT url-encode your values</strong>
     56 	 * as this is done automatically inside this method!
     57 	 * 
     58 	 * @param paramName
     59 	 *            name
     60 	 * @param paramValue
     61 	 *            value
     62 	 */
     63 	public void addParameter(String paramName, String paramValue)
     64 	{
     65 
     66 		if (paramName == null || paramValue == null || "" == paramName)
     67 			throw new IllegalArgumentException("parameter name and value must have values");
     68 
     69 		parameter.put(paramName, paramValue);
     70 	}
     71 
     72 	public void setId(Id id)
     73 	{
     74 		this.id = id;
     75 	}
     76 
     77 	public CMSConnection getConnection()
     78 	{
     79 		return connection;
     80 	}
     81 
     82 	protected CMSResponse executeView(String action, String method) {
     83 		return execute(action,method,HttpMethod.GET);
     84 	}
     85 
     86 	protected CMSResponse executePost(String action, String method) {
     87 		return execute(action,method,HttpMethod.POST);
     88 	}
     89 
     90 	private CMSResponse execute(String action, String method, HttpMethod httpMethod) throws CMSException
     91 	{
     92 
     93 		final ParameterMap parameter = new ParameterMap();
     94 
     95 		parameter.put(connection.getParamActionName(), action);
     96 		parameter.put(connection.getParamMethodName(), method);
     97 		parameter.put("id", String.valueOf(id));
     98 		parameter.putAll(this.parameter);
     99 
    100 		CMSRequest request = new CMSRequest(this.connection);
    101 		request.setMethod(httpMethod);
    102 		request.getParameter().putAll(parameter);
    103 
    104 		try
    105 		{
    106 			CMSResponse response = request.execute();
    107 			return response;
    108 		}
    109 		catch (CMSException e)
    110 		{
    111 			throw e;
    112 		}
    113 		catch (Exception e)
    114 		{
    115 			throw new CMSException(e);
    116 		}
    117 	}
    118 }