commit 4d18a3f33ad100caf97bb41ca7b90fef52f1742f
parent b96bfd688cd1cd603a1dccdee3cc3cb7d2c219e7
Author: dankert <devnull@localhost>
Date: Fri, 30 Sep 2016 23:38:24 +0200
Parsen der Server-Notices und füllen der CMSResponse. Weitere Doku mit JavaDoc.
Diffstat:
12 files changed, 274 insertions(+), 226 deletions(-)
diff --git a/src/de/openrat/client/CMSClient.java b/src/de/openrat/client/CMSClient.java
@@ -28,30 +28,26 @@ import de.openrat.client.action.LoginAction;
import de.openrat.client.util.CMSConnection;
/**
- * API-Request to the OpenRat Content Management System. <br>
+ * Client for the OpenRat Content Management System. <br>
* <br>
- * The call to the CMS server is done via a (non-SSL) HTTP connection.<br>
+ * The call to the CMS server is done via a HTTP connection.<br>
* <br>
- * Before a call you are able to set some key/value-pairs as parameters. After
- * calling the CMS a DOM-document is returned, which contains the server
- * response.<br>
* Example <br>
*
* <pre>
- * CMSRequest request = new CMSRequest("your.openrat.example.com");
- * // prints tracing information to stdout.
- * request.trace = true;
+ * CMSClient client = new CMSClient("demo.openrat.de", "/latest-snapshot/openrat/dispatcher.php", 80);
+ * client.setLogWriter(new PrintWriter(System.out, true));
+ * // client.setProxy("proxy.mycompany.exmaple", 8080, "user", "pass");
+ * client.setLocale(Locale.GERMAN);
+ * LoginAction loginAction = client.getLoginAction();
+ *
* try
* {
- * request.parameter.put("action", "index");
- * request.parameter.put("subaction", "showlogin"); // login page
- * request.parameter.put("...", "...");
- * Document response = request.call();
- * // now traverse through the dom tree and get your information.
+ * loginAction.login("admin", "admin", "db1");
* }
- * catch (IOException e)
+ * catch (LoginException e)
* {
- * // your error handling.
+ * fail("Login failed" + e.getLocalizedMessage());
* }
* </pre>
*
@@ -59,7 +55,7 @@ import de.openrat.client.util.CMSConnection;
*/
public class CMSClient
{
-
+ // the api version which we are supporting
public final static int SUPPORTED_API_VERSION = 2;
final CMSConnection connection;
@@ -78,7 +74,7 @@ public class CMSClient
}
/**
- * Constructs a CMS-Request to the specified server/path.<br>
+ * Constructs a CMS-Connection to the specified server/path.<br>
* Server-Port is 80.
*
* @param host
@@ -94,7 +90,7 @@ public class CMSClient
}
/**
- * Constructs a CMS-Request to the specified server/path/port.
+ * Constructs a CMS-Connection to the specified server/path/port.
*
* @param host
* hostname
@@ -109,16 +105,31 @@ public class CMSClient
this.connection = new CMSConnection(host, port, path);
}
+ /**
+ * Action class for login methods.
+ *
+ * @return
+ */
public LoginAction getLoginAction()
{
return new LoginAction(connection);
}
+ /**
+ * you may want to set a LogWriter, in which log messages are written.
+ *
+ * @param logWriter
+ */
public void setLogWriter(PrintWriter logWriter)
{
this.connection.setLogWriter(logWriter);
}
+ /**
+ * CMS Connection
+ *
+ * @return
+ */
public CMSConnection getConnection()
{
return connection;
@@ -145,6 +156,10 @@ public class CMSClient
* hostname
* @param port
* port
+ * @param user
+ * proxy username
+ * @param password
+ * password
*/
public void setProxy(String host, int port, String user, String password)
{
@@ -155,16 +170,32 @@ public class CMSClient
this.connection.setProxyPassword(password);
}
+ /**
+ * changing the {@link Locale}. Default is the Default-Locale.
+ *
+ * @param locale
+ */
public void setLocale(Locale locale)
{
connection.setLocale(locale);
}
+ /**
+ * Socket-Timeout in milliseconds. Default: 5000.
+ *
+ * @param timeout
+ */
public void setTimeout(int timeout)
{
connection.setTimeout(timeout);
}
+ /**
+ * Experimental Feature, DO <b>NOT</b> SET THIS TO TRUE. HTTP persistent
+ * connections are not supported!
+ *
+ * @param useKeepAlive
+ */
public void setKeepAlive(boolean useKeepAlive)
{
connection.setKeepAlive(useKeepAlive);
@@ -176,6 +207,12 @@ public class CMSClient
return super.toString() + ": " + String.valueOf(this.connection);
}
+ /**
+ * closing all resources. Normally, you do not need to call this, because
+ * the sockets are closed after each call. this is only for future vesions
+ * wenn keep-alive is implemented.
+ *
+ */
public void close()
{
if (connection.getSocket() != null)
@@ -192,6 +229,7 @@ public class CMSClient
}
/**
+ * Closes all resources before finalizing an instance of this class.
* {@inheritDoc}
*
* @see java.lang.Object#finalize()
diff --git a/src/de/openrat/client/action/Action.java b/src/de/openrat/client/action/Action.java
@@ -98,7 +98,11 @@ public abstract class Action
CMSResponse response = request.execute();
return response;
}
- catch (IOException e)
+ catch (CMSException e)
+ {
+ throw e;
+ }
+ catch (Exception e)
{
throw new CMSException(e);
}
diff --git a/src/de/openrat/client/action/LoginAction.java b/src/de/openrat/client/action/LoginAction.java
@@ -21,6 +21,19 @@ public class LoginAction extends Action
super(connection, "login");
}
+ /**
+ * Login.
+ *
+ * @param username
+ * username
+ * @param password
+ * password
+ * @param databaseId
+ * database-id
+ * @return {@link User}
+ * @throws LoginException
+ * if credentials are wrong
+ */
public User login(String username, String password, String databaseId) throws LoginException
{
diff --git a/src/de/openrat/client/util/CMSConnection.java b/src/de/openrat/client/util/CMSConnection.java
@@ -439,7 +439,8 @@ public class CMSConnection
}
/**
- * Setzt das Feld <code>keepAlive</code>.
+ * Using Keep-alive-sockets and HTTP-persistent-connections. we do NOT
+ * support this! DO NOT set this to true!
*
* @param keepAlive
* keepAlive
diff --git a/src/de/openrat/client/util/CMSControl.java b/src/de/openrat/client/util/CMSControl.java
@@ -1,6 +0,0 @@
-package de.openrat.client.util;
-
-public class CMSControl
-{
-
-}
diff --git a/src/de/openrat/client/util/CMSError.java b/src/de/openrat/client/util/CMSError.java
@@ -1,100 +0,0 @@
-package de.openrat.client.util;
-
-public class CMSError
-{
-
- public static enum CMSErrorStatus
- {
- NOTICE, WARN, ERROR;
- }
-
- private CMSErrorStatus status;
- private String type;
- private String message;
- private String reason;
-
- /**
- * Inhalt des Feldes <code>status</code>.
- *
- * @return status
- */
- public CMSErrorStatus getStatus()
- {
- return status;
- }
-
- /**
- * Setzt das Feld <code>status</code>.
- *
- * @param status
- * status
- */
- public void setStatus(CMSErrorStatus status)
- {
- this.status = status;
- }
-
- /**
- * Inhalt des Feldes <code>type</code>.
- *
- * @return type
- */
- public String getType()
- {
- return type;
- }
-
- /**
- * Setzt das Feld <code>type</code>.
- *
- * @param type
- * type
- */
- public void setType(String type)
- {
- this.type = type;
- }
-
- /**
- * Inhalt des Feldes <code>message</code>.
- *
- * @return message
- */
- public String getMessage()
- {
- return message;
- }
-
- /**
- * Setzt das Feld <code>message</code>.
- *
- * @param message
- * message
- */
- public void setMessage(String message)
- {
- this.message = message;
- }
-
- /**
- * Inhalt des Feldes <code>reason</code>.
- *
- * @return reason
- */
- public String getReason()
- {
- return reason;
- }
-
- /**
- * Setzt das Feld <code>reason</code>.
- *
- * @param reason
- * reason
- */
- public void setReason(String reason)
- {
- this.reason = reason;
- }
-
-}
diff --git a/src/de/openrat/client/util/CMSNode.java b/src/de/openrat/client/util/CMSNode.java
@@ -48,6 +48,22 @@ public class CMSNode
return value;
}
+ public boolean isTrue()
+ {
+ return Boolean.valueOf(value).booleanValue();
+ }
+
+ public int toInt()
+ {
+ return NumberUtils.toInt(value);
+ }
+
+ public long toLong()
+ {
+ return NumberUtils.toLong(value);
+ }
+
+
public String getName()
{
return name;
diff --git a/src/de/openrat/client/util/CMSNotice.java b/src/de/openrat/client/util/CMSNotice.java
@@ -0,0 +1,67 @@
+package de.openrat.client.util;
+
+public class CMSNotice
+{
+
+ public static enum CMSErrorStatus
+ {
+ NOTICE, WARN, ERROR;
+ }
+
+ private CMSErrorStatus status;
+ private String type;
+ private String name;
+ private String text;
+ private String key;
+
+ public CMSErrorStatus getStatus()
+ {
+ return status;
+ }
+
+ public void setStatus(CMSErrorStatus status)
+ {
+ this.status = status;
+ }
+
+ public String getType()
+ {
+ return type;
+ }
+
+ public void setType(String type)
+ {
+ this.type = type;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public String getText()
+ {
+ return text;
+ }
+
+ public void setText(String text)
+ {
+ this.text = text;
+ }
+
+ public String getKey()
+ {
+ return key;
+ }
+
+ public void setKey(String key)
+ {
+ this.key = key;
+ }
+
+}
diff --git a/src/de/openrat/client/util/CMSRequest.java b/src/de/openrat/client/util/CMSRequest.java
@@ -41,6 +41,7 @@ import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import de.openrat.client.CMSClient;
+import de.openrat.client.util.CMSNotice.CMSErrorStatus;
import de.openrat.client.util.HttpRequest.HttpMethod;
/**
@@ -212,41 +213,7 @@ public class CMSRequest
{
// Server reports an answer
- // Do we support the server api version?
- int apiVersion = Integer.parseInt(rootNode.getChild("api").getValue());
-
- if (apiVersion != CMSClient.SUPPORTED_API_VERSION)
- {
- // oh no, the server api is older or newer than our client
- // api.
- // there is nothing we can do.
- throw new CMSException("Only API Version 2 is supported. The server is using API Version " + rootNode.getChild("api"));
- }
-
- CMSResponse cmsResponse = new CMSResponse();
-
- cmsResponse.setApi(apiVersion);
- cmsResponse.setVersion(rootNode.getChild("version").getValue());
-
- List<CMSError> errorList = new ArrayList<CMSError>();
- for (CMSNode errorNode : rootNode.getChild("errors").getChildren())
- {
- CMSError error = new CMSError();
- errorList.add(error);
- }
- cmsResponse.setError(errorList.toArray(new CMSError[] {}));
-
- CMSControl control = new CMSControl();
- cmsResponse.setControl(control);
-
- CMSNode sessionNode = rootNode.getChild("session");
- CMSSession session = new CMSSession();
- session.setName(sessionNode.getChild("name").getValue());
- session.setId(sessionNode.getChild("id").getValue());
- session.setToken(sessionNode.getChild("token").getValue());
- cmsResponse.setSession(session);
-
- cmsResponse.setOutput(rootNode.getChild("outpout"));
+ CMSResponse cmsResponse = createCMSReponse(rootNode);
return cmsResponse;
}
@@ -263,6 +230,64 @@ public class CMSRequest
}
}
+ private CMSResponse createCMSReponse(final CMSNode rootNode)
+ {
+
+ CMSResponse cmsResponse = new CMSResponse();
+
+ // Do we support the server api version?
+ int apiVersion = Integer.parseInt(rootNode.getChild("api").getValue());
+
+ if (apiVersion != CMSClient.SUPPORTED_API_VERSION)
+ {
+ // oh no, the server api is older or newer than our client
+ // api.
+ // there is nothing we can do.
+ throw new CMSException("Only API Version 2 is supported. The server is using API Version " + rootNode.getChild("api"));
+ }
+
+ cmsResponse.setApi(apiVersion);
+ cmsResponse.setVersion(rootNode.getChild("version").getValue());
+
+ List<String> errorList = new ArrayList<String>();
+ for (CMSNode errorNode : rootNode.getChild("errors").getChildren())
+ {
+ errorList.add(errorNode.getValue());
+ }
+ cmsResponse.setValidationErrors(errorList);
+
+ List<CMSNotice> noticeList = new ArrayList<CMSNotice>();
+
+ for (CMSNode noticeNode : rootNode.getChild("notices").getChildren())
+ {
+ CMSNotice error = new CMSNotice();
+ error.setKey(noticeNode.getChild("key").getValue());
+ error.setType(noticeNode.getChild("type").getValue());
+ error.setName(noticeNode.getChild("name").getValue());
+ error.setText(noticeNode.getChild("text").getValue());
+
+ String status = noticeNode.getChild("status").getValue();
+ if (status.equalsIgnoreCase("ok"))
+ error.setStatus(CMSErrorStatus.NOTICE);
+ else if (status.equalsIgnoreCase("warning"))
+ error.setStatus(CMSErrorStatus.WARN);
+ else
+ error.setStatus(CMSErrorStatus.ERROR);
+ noticeList.add(error);
+ }
+ cmsResponse.setNotices(noticeList);
+
+ CMSNode sessionNode = rootNode.getChild("session");
+ CMSSession session = new CMSSession();
+ session.setName(sessionNode.getChild("name").getValue());
+ session.setId(sessionNode.getChild("id").getValue());
+ session.setToken(sessionNode.getChild("token").getValue());
+ cmsResponse.setSession(session);
+
+ cmsResponse.setOutput(rootNode.getChild("outpout"));
+ return cmsResponse;
+ }
+
public void setLogWriter(PrintWriter logWriter)
{
this.logWriter = logWriter;
diff --git a/src/de/openrat/client/util/CMSResponse.java b/src/de/openrat/client/util/CMSResponse.java
@@ -20,6 +20,8 @@ Boston, MA 02110-1301, USA.
*/
package de.openrat.client.util;
+import java.util.List;
+
/**
* @author Jan Dankert
@@ -27,9 +29,9 @@ package de.openrat.client.util;
public class CMSResponse
{
- private CMSError[] error;
+ private List<CMSNotice> notices;
+ private List<String> validationErrors;
private CMSNode output;
- private CMSControl control;
private CMSSession session;
private int httpStatus;
@@ -41,9 +43,9 @@ public class CMSResponse
*
* @return error
*/
- public CMSError[] getError()
+ public List<CMSNotice> getNotices()
{
- return error;
+ return notices;
}
/**
@@ -52,51 +54,48 @@ public class CMSResponse
* @param error
* error
*/
- public void setError(CMSError[] error)
+ public void setNotices(List<CMSNotice> error)
{
- this.error = error;
+ this.notices = error;
}
+
/**
- * Inhalt des Feldes <code>output</code>.
- *
- * @return output
+ * List of input fields with validation errors.
+ * @return
*/
- public CMSNode getOutput()
+ public List<String> getValidationErrors()
{
- return output;
+ return validationErrors;
}
/**
- * Setzt das Feld <code>output</code>.
- *
- * @param cmsNode
- * output
+ * @param validationErrors
*/
- public void setOutput(CMSNode cmsNode)
+ public void setValidationErrors(List<String> validationErrors)
{
- this.output = cmsNode;
+ this.validationErrors = validationErrors;
}
/**
- * Inhalt des Feldes <code>control</code>.
+ * Inhalt des Feldes <code>output</code>.
*
- * @return control
+ * @return output
*/
- public CMSControl getControl()
+ public CMSNode getOutput()
{
- return control;
+ return output;
}
/**
- * Setzt das Feld <code>control</code>.
+ * Setzt das Feld <code>output</code>.
*
- * @param control
- * control
+ * @param cmsNode
+ * output
*/
- public void setControl(CMSControl control)
+ public void setOutput(CMSNode cmsNode)
{
- this.control = control;
+ this.output = cmsNode;
}
/**
diff --git a/src/de/openrat/client/util/HttpClient.java b/src/de/openrat/client/util/HttpClient.java
@@ -36,37 +36,18 @@ import javax.xml.bind.DatatypeConverter;
import de.openrat.client.util.HttpRequest.HttpMethod;
/**
- * API-Request to the OpenRat Content Management System. <br>
- * <br>
- * The call to the CMS server is done via a (non-SSL) HTTP connection.<br>
- * <br>
- * Before a call you are able to set some key/value-pairs as parameters. After
- * calling the CMS a DOM-document is returned, which contains the server
- * response.<br>
- * Example <br>
- *
- * <pre>
- * CMSRequest request = new CMSRequest("your.openrat.example.com");
- * // prints tracing information to stdout.
- * request.trace = true;
- * try
- * {
- * request.parameter.put("action", "index");
- * request.parameter.put("subaction", "showlogin"); // login page
- * request.parameter.put("...", "...");
- * Document response = request.call();
- * // now traverse through the dom tree and get your information.
- * }
- * catch (IOException e)
- * {
- * // your error handling.
- * }
- * </pre>
+ * HTTP-Client.
*
* @author Jan Dankert
*/
public class HttpClient
{
+ /**
+ * at the moment we are only supporting HTTP/1.0, because for HTTP/1.1 we
+ * have to implement transfer chunked encoding, gzip-encoding, persistent
+ * connections and much more. that is non-trivial. but HTTP/1.0 does the job
+ * for us.
+ */
private static final String HTTP_VERSION = "HTTP/1.0";
private PrintWriter logWriter;
@@ -75,16 +56,6 @@ public class HttpClient
private ParameterMap parameter = new ParameterMap();
- /**
- * Constructs a CMS-Request to the specified server/path/port.
- *
- * @param host
- * hostname
- * @param path
- * path
- * @param port
- * port-number
- */
public HttpClient(CMSConnection connection)
{
@@ -94,8 +65,7 @@ public class HttpClient
}
/**
- * Sends a request to the openrat-server and parses the response into a DOM
- * tree document.
+ * Sends a HTTP request to the server and parses the response.
*
* @return server response as a DOM tree
* @throws IOException
@@ -107,7 +77,6 @@ public class HttpClient
try
{
-
String httpUrl = this.connection.getServerPath();
final PrintWriter socketWriter = new PrintWriter(socket.getOutputStream(), true);
@@ -137,6 +106,7 @@ public class HttpClient
requestHeader.putAll(request.getRequestHeader());
+ // keep-alive is only for future use. for now we MUST close the connection after the call.
String connectionStatus = connection.isKeepAlive() ? "keep-alive" : "close";
requestHeader.put("Connection", connectionStatus);
diff --git a/src/de/openrat/client/util/NumberUtils.java b/src/de/openrat/client/util/NumberUtils.java
@@ -21,4 +21,25 @@ public class NumberUtils
return 0;
}
}
+
+ /**
+ * Null-safe and Exception-safe conversion from {@link String} to
+ * {@link Long}.
+ *
+ * @param number
+ * Number
+ * @return int (0, if number is not a number)
+ */
+ public static long toLong(String number)
+ {
+ try
+ {
+ return Long.parseLong(number);
+ }
+ catch (NumberFormatException e)
+ {
+ return 0;
+ }
+ }
+
}