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

commit cce661bd2c21b2ac6b1c55e5bae4e5e88f020773
parent 2b9f18ca46b5dcdd910a7c2bd8573461ec600b7a
Author: Jan Dankert <devnull@localhost>
Date:   Wed, 12 Dec 2018 18:07:08 +0100

better encapsulation for action classes, new common CMSAction for a more low-level api.

Diffstat:
src/de/openrat/client/CMSClient.java | 50++++++++++++++++++++++++++++++++++++++------------
src/de/openrat/client/action/Action.java | 17++++++++++-------
src/de/openrat/client/action/CMSAction.java | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/de/openrat/client/action/LoginAction.java | 11+++++------
src/de/openrat/client/action/ProjectAction.java | 8++++----
src/de/openrat/client/util/CMSNotice.java | 2+-
src/de/openrat/client/util/CMSRequest.java | 17+++++++----------
src/de/openrat/client/util/CMSResponse.java | 2+-
src/de/openrat/client/util/HttpClient.java | 14++++++++++----
test/de/openrat/client/test/TestConfiguration.java | 20++++++++++++++++++++
test/de/openrat/client/test/TestLogin.java | 64+++++++++++++++++++++++++++++-----------------------------------
test/de/openrat/client/test/TestLowLevelAPI.java | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12 files changed, 250 insertions(+), 80 deletions(-)

diff --git a/src/de/openrat/client/CMSClient.java b/src/de/openrat/client/CMSClient.java @@ -22,8 +22,12 @@ package de.openrat.client; import java.io.IOException; import java.io.PrintWriter; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.Locale; +import de.openrat.client.action.Action; +import de.openrat.client.action.CMSAction; import de.openrat.client.action.LoginAction; import de.openrat.client.util.CMSConnection; @@ -55,10 +59,15 @@ import de.openrat.client.util.CMSConnection; */ public class CMSClient { - // the api version which we are supporting + /** + * the api version which we are supporting. + */ public final static int SUPPORTED_API_VERSION = 2; - final CMSConnection connection; + /** + * The internal connection-object to the cms. + */ + private final CMSConnection connection; /** * Constructs a CMS-Connection to the specified server.<br> @@ -106,16 +115,6 @@ public class CMSClient } /** - * 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 @@ -241,4 +240,31 @@ public class CMSClient super.finalize(); } + + + /** + * Creates a more low-level common action. + * + * @return new action instance + */ + public CMSAction createAction() { + return createAction( CMSAction.class ); + } + + + /** + * Creates a custom action. + * + * @param actionClass the action class to instantiate + * @param <T> + * @return new action instance + */ + public <T extends Action> T createAction(Class<T> actionClass) { + try { + Constructor<T> c = actionClass.getConstructor(CMSConnection.class); + return c.newInstance( this.connection ); + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException("The action '" + actionClass.getSimpleName() + " could not be created: " + e.getMessage(), e); + } + } } diff --git a/src/de/openrat/client/action/Action.java b/src/de/openrat/client/action/Action.java @@ -1,7 +1,5 @@ package de.openrat.client.action; -import java.io.IOException; - import de.openrat.client.util.CMSConnection; import de.openrat.client.util.CMSException; import de.openrat.client.util.CMSRequest; @@ -20,21 +18,18 @@ public abstract class Action private Id id; private CMSConnection connection; - private String action; /** * Clear parameter values. */ public void clear() { - parameter.clear(); } - protected Action(CMSConnection connection, String action) + protected Action(CMSConnection connection) { this.connection = connection; - this.action = action; } /** @@ -79,7 +74,15 @@ public abstract class Action return connection; } - protected CMSResponse execute(String method, HttpMethod httpMethod) throws CMSException + protected CMSResponse executeView(String action, String method) { + return execute(action,method,HttpMethod.GET); + } + + protected CMSResponse executePost(String action, String method) { + return execute(action,method,HttpMethod.POST); + } + + private CMSResponse execute(String action, String method, HttpMethod httpMethod) throws CMSException { final ParameterMap parameter = new ParameterMap(); diff --git a/src/de/openrat/client/action/CMSAction.java b/src/de/openrat/client/action/CMSAction.java @@ -0,0 +1,67 @@ +package de.openrat.client.action; + +import de.openrat.client.util.CMSConnection; +import de.openrat.client.util.CMSException; +import de.openrat.client.util.CMSNode; +import de.openrat.client.util.CMSResponse; +import de.openrat.client.util.HttpRequest.HttpMethod; + +import javax.security.auth.login.LoginException; +import java.util.Map; + +/** + * This class is NOT threadsafe and should be used by one thread simultaneously. + * + * @author dankert + */ +public class CMSAction extends Action { + + public CMSAction(CMSConnection connection) { + + super(connection); + } + + /** + * Custom method call. + * + * @return CMSNode + */ + public CMSNode executeView(String action, String method, Map<String, String> parameter) { + + return execute(action,method,parameter,HttpMethod.GET); + } + /** + * Custom method call. + * + * @return CMSNode + */ + public CMSNode executePost(String action, String method, Map<String, String> parameter) { + + return execute(action,method,parameter,HttpMethod.POST); + } + + + /** + * Custom method call. + * + * @return CMSNode + */ + private CMSNode execute(String action, String method, Map<String, String> parameter,HttpMethod httpMethod) { + + clear(); + + for (Map.Entry<String, String> entry : parameter.entrySet()) + setParameter(entry.getKey(), entry.getValue()); + + try { + CMSResponse response; + if( httpMethod.equals(HttpMethod.GET)) + response = executeView(action, method); + else + response = executePost(action, method); + return response.getOutput(); + } catch (CMSException e) { + throw e; + } + } +} diff --git a/src/de/openrat/client/action/LoginAction.java b/src/de/openrat/client/action/LoginAction.java @@ -15,10 +15,10 @@ import de.openrat.client.util.HttpRequest.HttpMethod; */ public class LoginAction extends Action { - + private final static String ACTION = "login"; public LoginAction(CMSConnection connection) { - super(connection, "login"); + super(connection); } /** @@ -37,7 +37,7 @@ public class LoginAction extends Action public User login(String username, String password, String databaseId) throws LoginException { - execute("login", HttpMethod.GET); + executeView(ACTION, "login"); clear(); setParameter("login_name", username); @@ -45,7 +45,7 @@ public class LoginAction extends Action setParameter("dbid", databaseId); try { - CMSResponse response = execute("login", HttpMethod.POST); + CMSResponse response = executePost(ACTION, "login"); User user = new User(); return user; @@ -64,7 +64,6 @@ public class LoginAction extends Action public void logout() { - - execute("logout", HttpMethod.POST); + executePost(ACTION, "logout"); } } diff --git a/src/de/openrat/client/action/ProjectAction.java b/src/de/openrat/client/action/ProjectAction.java @@ -3,7 +3,6 @@ package de.openrat.client.action; import de.openrat.client.dto.Project; import de.openrat.client.util.CMSConnection; import de.openrat.client.util.CMSResponse; -import de.openrat.client.util.HttpRequest.HttpMethod; import de.openrat.client.util.Id; /** @@ -13,16 +12,17 @@ import de.openrat.client.util.Id; */ public class ProjectAction extends Action { + private final static String PROJECT = "project"; - public ProjectAction(CMSConnection request) + public ProjectAction(CMSConnection connection) { - super(request, "project"); + super(connection); } public Project getInfo(Id id) { - CMSResponse response = execute("info", HttpMethod.GET); + CMSResponse response = executeView(PROJECT, "info"); // TODO return new Project(); diff --git a/src/de/openrat/client/util/CMSNotice.java b/src/de/openrat/client/util/CMSNotice.java @@ -5,7 +5,7 @@ public class CMSNotice public static enum CMSErrorStatus { - NOTICE, WARN, ERROR; + NOTICE, WARN, ERROR, INFO; } private CMSErrorStatus status; diff --git a/src/de/openrat/client/util/CMSRequest.java b/src/de/openrat/client/util/CMSRequest.java @@ -100,12 +100,7 @@ public class CMSRequest /** * Constructs a CMS-Request to the specified server/path/port. * - * @param host - * hostname - * @param path - * path - * @param port - * port-number + * @param connection Connection to server */ public CMSRequest(CMSConnection connection) { @@ -212,7 +207,6 @@ public class CMSRequest if (rootNode.getName() == "server") { // Server reports an answer - CMSResponse cmsResponse = createCMSReponse(rootNode); return cmsResponse; @@ -240,10 +234,10 @@ public class CMSRequest if (apiVersion != CMSClient.SUPPORTED_API_VERSION) { - // oh no, the server api is older or newer than our client - // api. + // 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")); + throw new CMSException("Only API Version " + CMSClient.SUPPORTED_API_VERSION + + " is supported. The server is using API Version " + apiVersion); } cmsResponse.setApi(apiVersion); @@ -267,10 +261,13 @@ public class CMSRequest 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 if (status.equalsIgnoreCase("info")) + error.setStatus(CMSErrorStatus.INFO); else error.setStatus(CMSErrorStatus.ERROR); noticeList.add(error); diff --git a/src/de/openrat/client/util/CMSResponse.java b/src/de/openrat/client/util/CMSResponse.java @@ -99,7 +99,7 @@ public class CMSResponse } /** - * Inhalt des Feldes <code>api</code>. + * the API version. * * @return api */ diff --git a/src/de/openrat/client/util/HttpClient.java b/src/de/openrat/client/util/HttpClient.java @@ -250,11 +250,17 @@ public class HttpClient } } - private String addTrailingSlash(String httpUrl) { - if (httpUrl.endsWith("/")) - return httpUrl; + /** + * Add trailing slash (if it does not exist). + * + * @param path + * @return + */ + private String addTrailingSlash(String path) { + if (path.endsWith("/")) + return path; else - return httpUrl + "/"; + return path + "/"; } public Socket createSocket() throws IOException diff --git a/test/de/openrat/client/test/TestConfiguration.java b/test/de/openrat/client/test/TestConfiguration.java @@ -0,0 +1,20 @@ +package de.openrat.client.test; + +import java.io.PrintWriter; +import java.io.Writer; +import java.util.Locale; + +public class TestConfiguration { + + public static final String HOST = "demo.openrat.de"; + public static final String PATH = "/"; + public static final int PORT = 80; + public static final String USER = "admin"; + public static final String PASS = "admin"; + public static final String DB = "db1"; + public static final String PROXY_HOST = null; + public static final int PROXY_PORT = 8080; + + public static final PrintWriter WRITER = new PrintWriter(System.out, true); + public static final Locale LOCALE = Locale.ENGLISH; +} diff --git a/test/de/openrat/client/test/TestLogin.java b/test/de/openrat/client/test/TestLogin.java @@ -1,43 +1,37 @@ package de.openrat.client.test; -import static org.junit.Assert.fail; - -import java.io.PrintWriter; -import java.util.Locale; +import de.openrat.client.CMSClient; +import de.openrat.client.action.LoginAction; +import org.junit.Test; import javax.security.auth.login.LoginException; -import org.junit.Test; - -import de.openrat.client.CMSClient; -import de.openrat.client.action.LoginAction; +import static de.openrat.client.test.TestConfiguration.*; +import static org.junit.Assert.fail; -public class TestLogin -{ - - /** - * simple example for using the client. - */ - @Test - public void test() - { - - 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); - client.setKeepAlive(false); - client.setTimeout(15000); - LoginAction loginAction = client.getLoginAction(); - - try - { - loginAction.login("admin", "admin", "db1"); - } - catch (LoginException e) - { - fail("Login failed" + e.getLocalizedMessage()); - } - } +public class TestLogin { + + /** + * simple example for using the client. + */ + @Test + public void test() { + final CMSClient client = new CMSClient(TestConfiguration.HOST, PATH, PORT); + client.setLogWriter(WRITER); + client.setProxy(PROXY_HOST, PROXY_PORT); + client.setLocale(LOCALE); + client.setKeepAlive(false); + client.setTimeout(15000); + + LoginAction loginAction = client.createAction(LoginAction.class); + + try { + loginAction.login(USER, PASS, DB); + } catch (LoginException e) { + fail("Login failed" + e.getLocalizedMessage()); + } + + client.close(); + } } diff --git a/test/de/openrat/client/test/TestLowLevelAPI.java b/test/de/openrat/client/test/TestLowLevelAPI.java @@ -0,0 +1,58 @@ +package de.openrat.client.test; + +import de.openrat.client.CMSClient; +import de.openrat.client.action.CMSAction; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.PrintWriter; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import static de.openrat.client.test.TestConfiguration.*; + +public class TestLowLevelAPI { + + private CMSClient client; + + /** + * + */ + @Before + public void connect() { + + client = new CMSClient(TestConfiguration.HOST, PATH, PORT); + client.setLogWriter(WRITER); + client.setProxy(PROXY_HOST, PROXY_PORT); + client.setLocale(LOCALE); + client.setKeepAlive(false); + client.setTimeout(15000); + } + + + /** + * simple example for using the client. + */ + @Test + public void test() { + final CMSAction action = client.createAction(); + + action.executeView("login", "login", new HashMap<>()); + + final Map<String, String> logindata = new HashMap<>(); + logindata.put("login_username", USER); + logindata.put("login_password", PASS); + logindata.put("dbid", DB); + + action.executePost("login", "login", logindata); + } + + @After + public void close() { + + client.close(); + } + +}