From 1930bb886e7dafbaaea9003cd2938f14c28d2dfc Mon Sep 17 00:00:00 2001 From: huguenindo Date: Tue, 4 Jan 2022 17:14:50 +0100 Subject: [PATCH] --- .../datasource/rest/ArticleMapperImpl.java | 56 +- .../datasource/rest/ClientMapperImpl.java | 56 +- .../datasource/rest/CommandeMapperImpl.java | 52 +- .../datasource/rest/RestMapperManager.java | 39 +- .../rest/RestTransactionManager.java | 27 +- .../rest/TransactionManagerFactory.java | 15 +- .../rest/json/ArticleMapperImpl.java | 69 +++ .../rest/json/ClientMapperImpl.java | 68 +++ .../rest/json/CommandeMapperImpl.java | 68 +++ .../rest/json/RestMapperManager.java | 43 ++ .../rest/json/RestTransactionManager.java | 28 + .../rest/json/TransactionManagerFactory.java | 18 + .../rest/xml/ArticleMapperImpl.java | 51 +- .../datasource/rest/xml/ClientMapperImpl.java | 54 +- .../rest/xml/CommandeMapperImpl.java | 135 +---- .../rest/xml/DatabaseManagerImpl.java | 105 ---- .../datasource/rest/xml/EntiteMapperImpl.java | 495 ------------------ .../rest/xml/RestMapperManager.java | 43 ++ .../rest/xml/RestTransactionManager.java | 19 +- .../rest/xml/TransactionManagerFactory.java | 15 +- .../rest/xml/XmlRestMapperManager.java | 62 --- .../campanule/datasource/rest/CONSTANTES.java | 14 + .../{ => json}/ArticleMapperImplTest.java | 2 +- .../rest/{ => json}/ClientMapperImplTest.java | 2 +- .../{ => json}/CommandeMapperImplTest.java | 2 +- .../{ => json}/DatabaseManagerImplTest.java | 2 +- .../TestTransactionManagerFactory.java | 8 +- .../xml/TestTransactionManagerFactory.java | 9 +- 28 files changed, 424 insertions(+), 1133 deletions(-) create mode 100644 src/main/java/campanule/datasource/rest/json/ArticleMapperImpl.java create mode 100644 src/main/java/campanule/datasource/rest/json/ClientMapperImpl.java create mode 100644 src/main/java/campanule/datasource/rest/json/CommandeMapperImpl.java create mode 100644 src/main/java/campanule/datasource/rest/json/RestMapperManager.java create mode 100644 src/main/java/campanule/datasource/rest/json/RestTransactionManager.java create mode 100644 src/main/java/campanule/datasource/rest/json/TransactionManagerFactory.java delete mode 100644 src/main/java/campanule/datasource/rest/xml/DatabaseManagerImpl.java delete mode 100644 src/main/java/campanule/datasource/rest/xml/EntiteMapperImpl.java create mode 100644 src/main/java/campanule/datasource/rest/xml/RestMapperManager.java delete mode 100644 src/main/java/campanule/datasource/rest/xml/XmlRestMapperManager.java create mode 100644 src/test/java/campanule/datasource/rest/CONSTANTES.java rename src/test/java/campanule/datasource/rest/{ => json}/ArticleMapperImplTest.java (96%) rename src/test/java/campanule/datasource/rest/{ => json}/ClientMapperImplTest.java (96%) rename src/test/java/campanule/datasource/rest/{ => json}/CommandeMapperImplTest.java (96%) rename src/test/java/campanule/datasource/rest/{ => json}/DatabaseManagerImplTest.java (98%) rename src/test/java/campanule/datasource/rest/{ => json}/TestTransactionManagerFactory.java (52%) diff --git a/src/main/java/campanule/datasource/rest/ArticleMapperImpl.java b/src/main/java/campanule/datasource/rest/ArticleMapperImpl.java index e7917c8..8346251 100644 --- a/src/main/java/campanule/datasource/rest/ArticleMapperImpl.java +++ b/src/main/java/campanule/datasource/rest/ArticleMapperImpl.java @@ -3,25 +3,14 @@ package campanule.datasource.rest; import campanule.datasource.ArticleMapper; import campanule.domain.Article; import campanule.domain.ArticleBase; -import campanule.domain.serialisation.json.ArticleJson; import core.datasource.PersistenceException; import core.domain.Identifiant; -import core.serialisation.SerialisationException; -import core.serialisation.json.JsonSerialiseur; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; -import org.apache.http.Header; -import org.apache.http.HttpHeaders; -import org.apache.http.entity.ContentType; -import org.apache.http.message.BasicHeader; /** * * @author dominique huguenin (dominique.huguenin at rpn.ch) */ -public class ArticleMapperImpl extends EntiteMapperImpl
+public abstract class ArticleMapperImpl extends EntiteMapperImpl
implements ArticleMapper { public static final String MESSAGE_BUNDLE @@ -68,47 +57,4 @@ public class ArticleMapperImpl extends EntiteMapperImpl
return targetURL; } - @Override - protected ByteArrayOutputStream serialiserEntite(final Article e) - throws SerialisationException { - final JsonSerialiseur serialiseur = new JsonSerialiseur(); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - serialiseur.serialiser(os, new ArticleJson(e)); - return os; - } - - @Override - protected Article deserialiserEntite(final InputStream is) - throws UnsupportedOperationException, - SerialisationException, IOException { - Article entite; - JsonSerialiseur serialiseur = new JsonSerialiseur(); - entite = (Article) serialiseur.deserialiser(is, ArticleJson.class); - return entite; - } - - @Override - protected List
deserialiserEntiteList(final InputStream is) - throws SerialisationException, IOException, - UnsupportedOperationException { - List
entites; - JsonSerialiseur serialiseur = new JsonSerialiseur(); - entites - = (List
) serialiseur.deserialiser(is, - ArticleJson.class, List.class - ); - return entites; - } - - @Override - protected ContentType getContentType() { - return ContentType.APPLICATION_JSON; - } - - @Override - protected Header getAcceptHeader() { - return new BasicHeader(HttpHeaders.ACCEPT, - ContentType.APPLICATION_JSON.getMimeType()); - } - } diff --git a/src/main/java/campanule/datasource/rest/ClientMapperImpl.java b/src/main/java/campanule/datasource/rest/ClientMapperImpl.java index 7392880..80e0ca5 100644 --- a/src/main/java/campanule/datasource/rest/ClientMapperImpl.java +++ b/src/main/java/campanule/datasource/rest/ClientMapperImpl.java @@ -3,25 +3,14 @@ package campanule.datasource.rest; import campanule.datasource.ClientMapper; import campanule.domain.Client; import campanule.domain.ClientBase; -import campanule.domain.serialisation.json.ClientJson; import core.datasource.PersistenceException; import core.domain.Identifiant; -import core.serialisation.SerialisationException; -import core.serialisation.json.JsonSerialiseur; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; -import org.apache.http.Header; -import org.apache.http.HttpHeaders; -import org.apache.http.entity.ContentType; -import org.apache.http.message.BasicHeader; /** * * @author dominique huguenin (dominique.huguenin at rpn.ch) */ -public class ClientMapperImpl extends EntiteMapperImpl +public abstract class ClientMapperImpl extends EntiteMapperImpl implements ClientMapper { public static final String MESSAGE_BUNDLE @@ -64,49 +53,6 @@ public class ClientMapperImpl extends EntiteMapperImpl return targetURL; } - @Override - protected ByteArrayOutputStream serialiserEntite(final Client e) - throws SerialisationException { - final JsonSerialiseur serialiseur = new JsonSerialiseur(); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - serialiseur.serialiser(os, new ClientJson(e)); - return os; - } - - @Override - protected Client deserialiserEntite(final InputStream is) - throws UnsupportedOperationException, - SerialisationException, IOException { - Client entite; - JsonSerialiseur serialiseur = new JsonSerialiseur(); - entite = (Client) serialiseur.deserialiser(is, ClientJson.class); - return entite; - } - - @Override - protected List deserialiserEntiteList(final InputStream is) - throws SerialisationException, IOException, - UnsupportedOperationException { - List entites; - JsonSerialiseur serialiseur = new JsonSerialiseur(); - entites - = (List) serialiseur.deserialiser(is, - ClientJson.class, List.class - ); - return entites; - } - - @Override - protected ContentType getContentType() { - return ContentType.APPLICATION_JSON; - } - - @Override - protected Header getAcceptHeader() { - return new BasicHeader(HttpHeaders.ACCEPT, - ContentType.APPLICATION_JSON.getMimeType()); - } - @Override protected Client buildNullEntite() { return ClientBase.builder().build(); diff --git a/src/main/java/campanule/datasource/rest/CommandeMapperImpl.java b/src/main/java/campanule/datasource/rest/CommandeMapperImpl.java index c9c1e33..6fa11ae 100644 --- a/src/main/java/campanule/datasource/rest/CommandeMapperImpl.java +++ b/src/main/java/campanule/datasource/rest/CommandeMapperImpl.java @@ -3,35 +3,28 @@ package campanule.datasource.rest; import campanule.datasource.CommandeMapper; import campanule.domain.Commande; import campanule.domain.CommandeBase; -import campanule.domain.serialisation.json.CommandeJson; import core.datasource.EntiteInconnuePersistenceException; import core.datasource.PersistenceException; import core.domain.Identifiant; import core.serialisation.SerialisationException; -import core.serialisation.json.JsonSerialiseur; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -import org.apache.http.Header; import org.apache.http.HttpEntity; -import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.fluent.Request; -import org.apache.http.entity.ContentType; -import org.apache.http.message.BasicHeader; /** * * @author dominique huguenin (dominique.huguenin at rpn.ch) */ -public class CommandeMapperImpl +public abstract class CommandeMapperImpl extends EntiteMapperImpl implements CommandeMapper { @@ -146,49 +139,6 @@ public class CommandeMapperImpl return entites; } - @Override - protected ByteArrayOutputStream serialiserEntite(final Commande e) - throws SerialisationException { - final JsonSerialiseur serialiseur = new JsonSerialiseur(); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - serialiseur.serialiser(os, new CommandeJson(e)); - return os; - } - - @Override - protected Commande deserialiserEntite(final InputStream is) - throws UnsupportedOperationException, - SerialisationException, IOException { - Commande entite; - JsonSerialiseur serialiseur = new JsonSerialiseur(); - entite = (Commande) serialiseur.deserialiser(is, CommandeJson.class); - return entite; - } - - @Override - protected List deserialiserEntiteList(final InputStream is) - throws SerialisationException, IOException, - UnsupportedOperationException { - List entites; - JsonSerialiseur serialiseur = new JsonSerialiseur(); - entites - = (List) serialiseur.deserialiser(is, - CommandeJson.class, List.class - ); - return entites; - } - - @Override - protected ContentType getContentType() { - return ContentType.APPLICATION_JSON; - } - - @Override - protected Header getAcceptHeader() { - return new BasicHeader(HttpHeaders.ACCEPT, - ContentType.APPLICATION_JSON.getMimeType()); - } - @Override protected Commande buildNullEntite() { return CommandeBase.builder().build(); diff --git a/src/main/java/campanule/datasource/rest/RestMapperManager.java b/src/main/java/campanule/datasource/rest/RestMapperManager.java index 3e7efe2..6963d29 100644 --- a/src/main/java/campanule/datasource/rest/RestMapperManager.java +++ b/src/main/java/campanule/datasource/rest/RestMapperManager.java @@ -11,7 +11,7 @@ import core.datasource.PersistenceException; * * @author dominique huguenin (dominique.huguenin@rpn.ch) */ -public class RestMapperManager implements MapperManager { +public abstract class RestMapperManager implements MapperManager { private ArticleMapper articleMapper; private ClientMapper clientMapper; @@ -19,7 +19,7 @@ public class RestMapperManager implements MapperManager { private DatabaseManager databaseSetup; private final String context; - public RestMapperManager(final String context) { + protected RestMapperManager(final String context) { this.context = context; } @@ -27,14 +27,6 @@ public class RestMapperManager implements MapperManager { return context; } - @Override - public ArticleMapper getArticleMapper() { - if (this.articleMapper == null) { - this.articleMapper = new ArticleMapperImpl(this); - } - return this.articleMapper; - } - @Override public DatabaseManager getDatabaseManager() throws PersistenceException { if (this.databaseSetup == null) { @@ -43,20 +35,31 @@ public class RestMapperManager implements MapperManager { return this.databaseSetup; } + @Override + public ArticleMapper getArticleMapper() throws PersistenceException { + return articleMapper; + } + + protected void setArticleMapper(final ArticleMapper articleMapper) { + this.articleMapper = articleMapper; + } + @Override public ClientMapper getClientMapper() throws PersistenceException { - if (this.clientMapper == null) { - this.clientMapper = new ClientMapperImpl(this); - } - return this.clientMapper; + return clientMapper; + } + + protected void setClientMapper(final ClientMapper clientMapper) { + this.clientMapper = clientMapper; } @Override public CommandeMapper getCommandeMapper() throws PersistenceException { - if (this.commandeMapper == null) { - this.commandeMapper = new CommandeMapperImpl(this); - } - return this.commandeMapper; + return commandeMapper; + } + + protected void setCommandeMapper(final CommandeMapper commandeMapper) { + this.commandeMapper = commandeMapper; } } diff --git a/src/main/java/campanule/datasource/rest/RestTransactionManager.java b/src/main/java/campanule/datasource/rest/RestTransactionManager.java index b1765ce..c239c5f 100644 --- a/src/main/java/campanule/datasource/rest/RestTransactionManager.java +++ b/src/main/java/campanule/datasource/rest/RestTransactionManager.java @@ -6,28 +6,29 @@ import core.datasource.TransactionManager.Operation; /** * - * @author dominique huguenin (dominique.huguenin at rpn.ch) + * @author dominique huguenin (dominique.huguenin@rpn.ch) */ -public final class RestTransactionManager +public abstract class RestTransactionManager implements TransactionManager> { private static RestTransactionManager transactionManager; - public static TransactionManager getInstance(final String context) { - if (transactionManager == null) { - transactionManager = new RestTransactionManager(context); - } - return transactionManager; - } private final String context; - private RestTransactionManager(final String context) { + protected RestTransactionManager(final String context) { this.context = context; } - @Override - public Object executeTransaction(final Operation operation) - throws core.datasource.PersistenceException { - return operation.execute(new RestMapperManager(this.context)); + public String getContext() { + return context; + } + + protected static RestTransactionManager setInstance( + final RestTransactionManager tm) { + if (transactionManager == null) { + transactionManager = tm; + } + return transactionManager; } + } diff --git a/src/main/java/campanule/datasource/rest/TransactionManagerFactory.java b/src/main/java/campanule/datasource/rest/TransactionManagerFactory.java index dfa75d5..d2b344a 100644 --- a/src/main/java/campanule/datasource/rest/TransactionManagerFactory.java +++ b/src/main/java/campanule/datasource/rest/TransactionManagerFactory.java @@ -1,27 +1,20 @@ package campanule.datasource.rest; -import core.datasource.TransactionManager; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; /** * - * @author dominique huguenin (dominique.huguenin at rpn.ch) + * @author dominique huguenin (dominique.huguenin@rpn.ch) */ -public final class TransactionManagerFactory { +public abstract class TransactionManagerFactory { - private TransactionManagerFactory() { - } - - public static TransactionManager getInstance() throws NamingException { + protected static String getRestServiceBaseUrl() throws NamingException { InitialContext initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); - String restServiceBaseUrl = (String) envCtx + return (String) envCtx .lookup("restServiceBaseUrl"); - return RestTransactionManager.getInstance(restServiceBaseUrl); - } - } diff --git a/src/main/java/campanule/datasource/rest/json/ArticleMapperImpl.java b/src/main/java/campanule/datasource/rest/json/ArticleMapperImpl.java new file mode 100644 index 0000000..81a8984 --- /dev/null +++ b/src/main/java/campanule/datasource/rest/json/ArticleMapperImpl.java @@ -0,0 +1,69 @@ +package campanule.datasource.rest.json; + +import campanule.domain.Article; +import campanule.domain.serialisation.json.ArticleJson; +import core.serialisation.SerialisationException; +import core.serialisation.json.JsonSerialiseur; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import org.apache.http.Header; +import org.apache.http.HttpHeaders; +import org.apache.http.entity.ContentType; +import org.apache.http.message.BasicHeader; + +/** + * + * @author dominique huguenin (dominique.huguenin at rpn.ch) + */ +public class ArticleMapperImpl extends campanule.datasource.rest.ArticleMapperImpl { + + public ArticleMapperImpl(final RestMapperManager mm) { + super(mm); + } + + @Override + protected ByteArrayOutputStream serialiserEntite(final Article e) + throws SerialisationException { + final JsonSerialiseur serialiseur = new JsonSerialiseur(); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + serialiseur.serialiser(os, new ArticleJson(e)); + return os; + } + + @Override + protected Article deserialiserEntite(final InputStream is) + throws UnsupportedOperationException, + SerialisationException, IOException { + Article entite; + JsonSerialiseur serialiseur = new JsonSerialiseur(); + entite = (Article) serialiseur.deserialiser(is, ArticleJson.class); + return entite; + } + + @Override + protected List
deserialiserEntiteList(final InputStream is) + throws SerialisationException, IOException, + UnsupportedOperationException { + List
entites; + JsonSerialiseur serialiseur = new JsonSerialiseur(); + entites + = (List
) serialiseur.deserialiser(is, + ArticleJson.class, List.class + ); + return entites; + } + + @Override + protected ContentType getContentType() { + return ContentType.APPLICATION_JSON; + } + + @Override + protected Header getAcceptHeader() { + return new BasicHeader(HttpHeaders.ACCEPT, + ContentType.APPLICATION_JSON.getMimeType()); + } + +} diff --git a/src/main/java/campanule/datasource/rest/json/ClientMapperImpl.java b/src/main/java/campanule/datasource/rest/json/ClientMapperImpl.java new file mode 100644 index 0000000..e6f813c --- /dev/null +++ b/src/main/java/campanule/datasource/rest/json/ClientMapperImpl.java @@ -0,0 +1,68 @@ +package campanule.datasource.rest.json; + +import campanule.domain.Client; +import campanule.domain.serialisation.json.ClientJson; +import core.serialisation.SerialisationException; +import core.serialisation.json.JsonSerialiseur; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import org.apache.http.Header; +import org.apache.http.HttpHeaders; +import org.apache.http.entity.ContentType; +import org.apache.http.message.BasicHeader; + +/** + * + * @author dominique huguenin (dominique.huguenin at rpn.ch) + */ +public class ClientMapperImpl extends campanule.datasource.rest.ClientMapperImpl { + + public ClientMapperImpl(final RestMapperManager mm) { + super(mm); + } + + @Override + protected ByteArrayOutputStream serialiserEntite(final Client e) + throws SerialisationException { + final JsonSerialiseur serialiseur = new JsonSerialiseur(); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + serialiseur.serialiser(os, new ClientJson(e)); + return os; + } + + @Override + protected Client deserialiserEntite(final InputStream is) + throws UnsupportedOperationException, + SerialisationException, IOException { + Client entite; + JsonSerialiseur serialiseur = new JsonSerialiseur(); + entite = (Client) serialiseur.deserialiser(is, ClientJson.class); + return entite; + } + + @Override + protected List deserialiserEntiteList(final InputStream is) + throws SerialisationException, IOException, + UnsupportedOperationException { + List entites; + JsonSerialiseur serialiseur = new JsonSerialiseur(); + entites + = (List) serialiseur.deserialiser(is, + ClientJson.class, List.class + ); + return entites; + } + + @Override + protected ContentType getContentType() { + return ContentType.APPLICATION_JSON; + } + + @Override + protected Header getAcceptHeader() { + return new BasicHeader(HttpHeaders.ACCEPT, + ContentType.APPLICATION_JSON.getMimeType()); + } +} diff --git a/src/main/java/campanule/datasource/rest/json/CommandeMapperImpl.java b/src/main/java/campanule/datasource/rest/json/CommandeMapperImpl.java new file mode 100644 index 0000000..183618f --- /dev/null +++ b/src/main/java/campanule/datasource/rest/json/CommandeMapperImpl.java @@ -0,0 +1,68 @@ +package campanule.datasource.rest.json; + +import campanule.domain.Commande; +import campanule.domain.serialisation.json.CommandeJson; +import core.serialisation.SerialisationException; +import core.serialisation.json.JsonSerialiseur; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import org.apache.http.Header; +import org.apache.http.HttpHeaders; +import org.apache.http.entity.ContentType; +import org.apache.http.message.BasicHeader; + +/** + * + * @author dominique huguenin (dominique.huguenin at rpn.ch) + */ +public class CommandeMapperImpl extends campanule.datasource.rest.CommandeMapperImpl { + + public CommandeMapperImpl(final RestMapperManager mm) { + super(mm); + } + + @Override + protected ByteArrayOutputStream serialiserEntite(final Commande e) + throws SerialisationException { + final JsonSerialiseur serialiseur = new JsonSerialiseur(); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + serialiseur.serialiser(os, new CommandeJson(e)); + return os; + } + + @Override + protected Commande deserialiserEntite(final InputStream is) + throws UnsupportedOperationException, + SerialisationException, IOException { + Commande entite; + JsonSerialiseur serialiseur = new JsonSerialiseur(); + entite = (Commande) serialiseur.deserialiser(is, CommandeJson.class); + return entite; + } + + @Override + protected List deserialiserEntiteList(final InputStream is) + throws SerialisationException, IOException, + UnsupportedOperationException { + List entites; + JsonSerialiseur serialiseur = new JsonSerialiseur(); + entites + = (List) serialiseur.deserialiser(is, + CommandeJson.class, List.class + ); + return entites; + } + + @Override + protected ContentType getContentType() { + return ContentType.APPLICATION_JSON; + } + + @Override + protected Header getAcceptHeader() { + return new BasicHeader(HttpHeaders.ACCEPT, + ContentType.APPLICATION_JSON.getMimeType()); + } +} diff --git a/src/main/java/campanule/datasource/rest/json/RestMapperManager.java b/src/main/java/campanule/datasource/rest/json/RestMapperManager.java new file mode 100644 index 0000000..b1bd433 --- /dev/null +++ b/src/main/java/campanule/datasource/rest/json/RestMapperManager.java @@ -0,0 +1,43 @@ +package campanule.datasource.rest.json; + +import campanule.datasource.ArticleMapper; +import campanule.datasource.ClientMapper; +import campanule.datasource.CommandeMapper; +import core.datasource.PersistenceException; + +/** + * + * @author dominique huguenin (dominique.huguenin@rpn.ch) + */ +public class RestMapperManager + extends campanule.datasource.rest.RestMapperManager { + + RestMapperManager(final String context) { + super(context); + } + + @Override + public ArticleMapper getArticleMapper() throws PersistenceException { + if (super.getArticleMapper() == null) { + super.setArticleMapper(new ArticleMapperImpl(this)); + } + return super.getArticleMapper(); + } + + @Override + public ClientMapper getClientMapper() throws PersistenceException { + if (super.getClientMapper() == null) { + super.setClientMapper(new ClientMapperImpl(this)); + } + return super.getClientMapper(); + } + + @Override + public CommandeMapper getCommandeMapper() throws PersistenceException { + if (super.getCommandeMapper() == null) { + super.setCommandeMapper(new CommandeMapperImpl(this)); + } + return super.getCommandeMapper(); + } + +} diff --git a/src/main/java/campanule/datasource/rest/json/RestTransactionManager.java b/src/main/java/campanule/datasource/rest/json/RestTransactionManager.java new file mode 100644 index 0000000..2b4eb82 --- /dev/null +++ b/src/main/java/campanule/datasource/rest/json/RestTransactionManager.java @@ -0,0 +1,28 @@ +package campanule.datasource.rest.json; + +import campanule.datasource.MapperManager; +import core.datasource.TransactionManager; +import core.datasource.TransactionManager.Operation; + +/** + * + * @author dominique huguenin (dominique.huguenin at rpn.ch) + */ +public final class RestTransactionManager + extends campanule.datasource.rest.RestTransactionManager { + + public RestTransactionManager(final String context) { + super(context); + } + + public static TransactionManager getInstance(final String context) { + return setInstance(new RestTransactionManager(context)); + } + + @Override + public Object executeTransaction(final Operation operation) + throws core.datasource.PersistenceException { + return operation.execute( + new RestMapperManager(this.getContext())); + } +} diff --git a/src/main/java/campanule/datasource/rest/json/TransactionManagerFactory.java b/src/main/java/campanule/datasource/rest/json/TransactionManagerFactory.java new file mode 100644 index 0000000..e61a2ba --- /dev/null +++ b/src/main/java/campanule/datasource/rest/json/TransactionManagerFactory.java @@ -0,0 +1,18 @@ +package campanule.datasource.rest.json; + +import core.datasource.TransactionManager; +import javax.naming.NamingException; + +/** + * + * @author dominique huguenin (dominique.huguenin@rpn.ch) + */ +public final class TransactionManagerFactory + extends campanule.datasource.rest.TransactionManagerFactory { + + public static TransactionManager getInstance() throws NamingException { + return RestTransactionManager.getInstance(getRestServiceBaseUrl()); + + } + +} diff --git a/src/main/java/campanule/datasource/rest/xml/ArticleMapperImpl.java b/src/main/java/campanule/datasource/rest/xml/ArticleMapperImpl.java index 40224e3..5d7cb80 100644 --- a/src/main/java/campanule/datasource/rest/xml/ArticleMapperImpl.java +++ b/src/main/java/campanule/datasource/rest/xml/ArticleMapperImpl.java @@ -1,13 +1,10 @@ package campanule.datasource.rest.xml; import campanule.datasource.ArticleMapper; -import campanule.datasource.rest.REST; +import campanule.datasource.rest.RestMapperManager; import campanule.domain.Article; -import campanule.domain.ArticleBase; import campanule.domain.serialisation.xml.ArticleXml; import campanule.domain.serialisation.xml.ArticlesXml; -import core.datasource.PersistenceException; -import core.domain.Identifiant; import core.serialisation.SerialisationException; import core.serialisation.Serialiseur; import core.serialisation.xml.XmlSerialiseur; @@ -25,46 +22,11 @@ import org.apache.http.message.BasicHeader; * * @author dominique huguenin (dominique.huguenin@rpn.ch) */ -public class ArticleMapperImpl extends EntiteMapperImpl
+public class ArticleMapperImpl extends campanule.datasource.rest.ArticleMapperImpl implements ArticleMapper { - public static final String MESSAGE_BUNDLE - = "campanule.datasource.rest.MessagesBundle"; - private final XmlRestMapperManager mapperManager; - - public ArticleMapperImpl(final XmlRestMapperManager mm) { - super(MESSAGE_BUNDLE); - this.mapperManager = mm; - - } - - @Override - protected String getRetrieveUrl(final Identifiant identifiant) { - return String.format(REST.ARTICLES.DETAIL_URL_TEMPLATE, - this.mapperManager.getContext(), identifiant.getUUID()); - } - - @Override - protected String getRetrieveUrl(final String filtre) { - String targetURL = String.format(REST.ARTICLES.FILTRE_URL_TEMPLATE, - this.mapperManager.getContext(), filtre); - return targetURL; - } - - @Override - protected String getCreateUrl(final Article e) throws PersistenceException { - String targetURL = String.format(REST.ARTICLES.LIST_URL_TEMPLATE, - this.mapperManager.getContext()); - return targetURL; - } - - @Override - protected String getDeleteUrl(final Article e) throws PersistenceException { - String targetURL = String.format(REST.ARTICLES.DELETE_URL_TEMPLATE, - this.mapperManager.getContext(), - e.getIdentifiant().getUUID(), - e.getIdentifiant().getVersion()); - return targetURL; + ArticleMapperImpl(final RestMapperManager mm) { + super(mm); } @Override @@ -108,9 +70,4 @@ public class ArticleMapperImpl extends EntiteMapperImpl
ContentType.APPLICATION_XML.getMimeType()); } - @Override - protected Article buildNullEntite() { - return ArticleBase.builder().build(); - } - } diff --git a/src/main/java/campanule/datasource/rest/xml/ClientMapperImpl.java b/src/main/java/campanule/datasource/rest/xml/ClientMapperImpl.java index a847a66..c35b3a9 100644 --- a/src/main/java/campanule/datasource/rest/xml/ClientMapperImpl.java +++ b/src/main/java/campanule/datasource/rest/xml/ClientMapperImpl.java @@ -1,13 +1,8 @@ package campanule.datasource.rest.xml; -import campanule.datasource.ClientMapper; -import campanule.datasource.rest.REST; import campanule.domain.Client; -import campanule.domain.ClientBase; import campanule.domain.serialisation.xml.ClientXml; import campanule.domain.serialisation.xml.ClientsXml; -import core.datasource.PersistenceException; -import core.domain.Identifiant; import core.serialisation.SerialisationException; import core.serialisation.Serialiseur; import core.serialisation.xml.XmlSerialiseur; @@ -25,47 +20,10 @@ import org.apache.http.message.BasicHeader; * * @author dominique huguenin (dominique.huguenin@rpn.ch) */ -public class ClientMapperImpl extends EntiteMapperImpl - implements ClientMapper { +public class ClientMapperImpl extends campanule.datasource.rest.ClientMapperImpl { - public static final String MESSAGE_BUNDLE - = "campanule.datasource.rest.MessagesBundle"; - private final XmlRestMapperManager mapperManager; - - public ClientMapperImpl(final XmlRestMapperManager mm) { - super(MESSAGE_BUNDLE); - this.mapperManager = mm; - - } - - @Override - protected String getRetrieveUrl(final Identifiant identifiant) { - return String.format(REST.CLIENTS.DETAIL_URL_TEMPLATE, - this.mapperManager.getContext(), identifiant.getUUID()); - } - - @Override - protected String getRetrieveUrl(final String filtre) { - String targetURL = String.format(REST.CLIENTS.FILTRE_URL_TEMPLATE, - this.mapperManager.getContext(), filtre); - return targetURL; - } - - @Override - protected String getCreateUrl(final Client e) throws PersistenceException { - String targetURL = String.format(REST.CLIENTS.LIST_URL_TEMPLATE, - this.mapperManager.getContext()); - return targetURL; - } - - @Override - protected String getDeleteUrl(final Client e) throws PersistenceException { - - String targetURL = String.format(REST.CLIENTS.DELETE_URL_TEMPLATE, - this.mapperManager.getContext(), - e.getIdentifiant().getUUID(), - e.getIdentifiant().getVersion()); - return targetURL; + ClientMapperImpl(final RestMapperManager mm) { + super(mm); } @Override @@ -108,10 +66,4 @@ public class ClientMapperImpl extends EntiteMapperImpl return new BasicHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_XML.getMimeType()); } - - @Override - protected Client buildNullEntite() { - return ClientBase.builder().build(); - } - } diff --git a/src/main/java/campanule/datasource/rest/xml/CommandeMapperImpl.java b/src/main/java/campanule/datasource/rest/xml/CommandeMapperImpl.java index 64fa218..36e825d 100644 --- a/src/main/java/campanule/datasource/rest/xml/CommandeMapperImpl.java +++ b/src/main/java/campanule/datasource/rest/xml/CommandeMapperImpl.java @@ -1,14 +1,8 @@ package campanule.datasource.rest.xml; -import campanule.datasource.CommandeMapper; -import campanule.datasource.rest.REST; import campanule.domain.Commande; -import campanule.domain.CommandeBase; import campanule.domain.serialisation.xml.CommandeXml; import campanule.domain.serialisation.xml.CommandesXml; -import core.datasource.EntiteInconnuePersistenceException; -import core.datasource.PersistenceException; -import core.domain.Identifiant; import core.serialisation.SerialisationException; import core.serialisation.Serialiseur; import core.serialisation.xml.XmlSerialiseur; @@ -17,16 +11,8 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; import org.apache.http.Header; -import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.fluent.Request; import org.apache.http.entity.ContentType; import org.apache.http.message.BasicHeader; @@ -34,120 +20,10 @@ import org.apache.http.message.BasicHeader; * * @author dominique huguenin (dominique.huguenin@rpn.ch) */ -public class CommandeMapperImpl - extends EntiteMapperImpl - implements CommandeMapper { +public class CommandeMapperImpl extends campanule.datasource.rest.CommandeMapperImpl { - private static final Logger LOG - = Logger.getLogger(CommandeMapperImpl.class.getName()); - - public static final String MESSAGE_BUNDLE - = "campanule.datasource.rest.MessagesBundle"; - private final XmlRestMapperManager mapperManager; - - public CommandeMapperImpl(final XmlRestMapperManager mm) { - super(MESSAGE_BUNDLE); - this.mapperManager = mm; - - } - - @Override - protected String getRetrieveUrl(final Identifiant identifiant) { - return String.format(REST.COMMANDES.DETAIL_URL_TEMPLATE, - this.mapperManager.getContext(), identifiant.getUUID()); - } - - @Override - protected String getRetrieveUrl(final String filtre) { - String targetURL = String.format(REST.COMMANDES.FILTRE_URL_TEMPLATE, - this.mapperManager.getContext(), filtre); - return targetURL; - } - - @Override - protected String getCreateUrl(final Commande e) throws PersistenceException { - if (e.getClient() == null) { - throw new EntiteInconnuePersistenceException(); - } - - String targetURL = String.format(REST.CLIENTS.COMMANDES_URL_TEMPLATE, - this.mapperManager.getContext(), - e.getClient().getIdentifiant().getUUID()); - return targetURL; - } - - @Override - protected String getDeleteUrl(final Commande e) throws PersistenceException { - String targetURL = String.format(REST.COMMANDES.DELETE_URL_TEMPLATE, - this.mapperManager.getContext(), - e.getIdentifiant().getUUID(), - e.getIdentifiant().getVersion()); - return targetURL; - } - - @Override - public List retrieveByClient(final Identifiant identifiant) - throws PersistenceException { - List entites = new ArrayList<>(); - if (identifiant != null) { - String targetURL = String.format(REST.CLIENTS.COMMANDES_URL_TEMPLATE, - this.mapperManager.getContext(), - identifiant.getUUID()); - try { - return (List) Request.Get(targetURL) - .execute() - .handleResponse(new ResponseHandler>() { - @Override - public List handleResponse( - final HttpResponse response) - throws ClientProtocolException, IOException { - try { - List entites = new ArrayList<>(); - switch (response.getStatusLine().getStatusCode()) { - case HttpStatus.SC_OK: - HttpEntity responseEntity = response.getEntity(); - - if (!responseEntity.getContentType().getValue() - .contains(ContentType.APPLICATION_JSON - .getMimeType())) { - String msg = String.format( - getMessages().getString("ERREUR_VALEUR_NON_ATTENDU"), - responseEntity.getContentType().getName(), - responseEntity.getContentType().getValue()); - LOG.info(msg); - throw new ClientProtocolException(msg); - } - - InputStream is = responseEntity.getContent(); - entites = deserialiserEntiteList(is); - break; - - case HttpStatus.SC_NOT_FOUND: - entites = null; - break; - - default: - String msg = String.format( - getMessages().getString("ERREUR_STATUS_NON_ATTENDU"), - response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase()); - LOG.info(msg); - throw new ClientProtocolException(msg); - } - return entites; - } catch (SerialisationException ex) { - LOG.log(Level.SEVERE, ex.getMessage(), ex); - throw new ClientProtocolException(ex.getMessage(), ex); - } - - } - }); - } catch (IOException ex) { - LOG.log(Level.SEVERE, ex.getMessage(), ex); - throw new PersistenceException(ex.getMessage(), ex); - } - } - return entites; + CommandeMapperImpl(final RestMapperManager mm) { + super(mm); } @Override @@ -191,9 +67,4 @@ public class CommandeMapperImpl ContentType.APPLICATION_XML.getMimeType()); } - @Override - protected Commande buildNullEntite() { - return CommandeBase.builder().build(); - } - } diff --git a/src/main/java/campanule/datasource/rest/xml/DatabaseManagerImpl.java b/src/main/java/campanule/datasource/rest/xml/DatabaseManagerImpl.java deleted file mode 100644 index e78ab16..0000000 --- a/src/main/java/campanule/datasource/rest/xml/DatabaseManagerImpl.java +++ /dev/null @@ -1,105 +0,0 @@ -package campanule.datasource.rest.xml; - -import campanule.datasource.DatabaseManager; -import campanule.datasource.rest.REST; -import core.datasource.PersistenceException; -import java.io.IOException; -import java.util.ResourceBundle; -import java.util.logging.Logger; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.fluent.Request; - -/** - * - * @author dominique huguenin (dominique.huguenin@rpn.ch) - */ -public class DatabaseManagerImpl implements DatabaseManager { - - static final Logger LOGGER - = Logger.getLogger(DatabaseManagerImpl.class.getCanonicalName()); - - private final XmlRestMapperManager mapperManager; - private final ResourceBundle messages; - public static final String MESSAGE_BUNDLE - = "campanule.datasource.rest.MessagesBundle"; - - public DatabaseManagerImpl(final XmlRestMapperManager mapperManager) { - this.mapperManager = mapperManager; - this.messages = ResourceBundle.getBundle(MESSAGE_BUNDLE); - } - - @Override - public void createTables() throws PersistenceException { - String targetURL = String.format(REST.DATABASE.URL_TEMPLATE, - mapperManager.getContext()); - - try { - HttpResponse response = Request.Post(targetURL) - .execute() - .returnResponse(); - if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { - LOGGER.info(String.format("status - code: %1$d, message: %2$s", - response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase())); - throw new PersistenceException( - String.format(messages.getString("ERREUR_CREATION_DATABASE"), - response.getStatusLine().getStatusCode())); - } - } catch (IOException ex) { - LOGGER.severe(ex.getMessage()); - throw new PersistenceException(ex); - - } - } - - @Override - public void dropTables() throws PersistenceException { - String targetURL = String.format(REST.DATABASE.URL_TEMPLATE, - mapperManager.getContext()); - - try { - HttpResponse response = Request.Delete(targetURL) - .execute() - .returnResponse(); - if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NO_CONTENT) { - LOGGER.info(String.format("status - code: %1$d, message: %2$s", - response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase())); - throw new PersistenceException( - String.format(messages.getString("ERREUR_SUPPRESSION_DATABASE"), - response.getStatusLine().getStatusCode())); - } - } catch (IOException ex) { - LOGGER.severe(ex.getMessage()); - throw new PersistenceException(ex); - - } - } - - @Override - public void insertDatas() throws PersistenceException { - String targetURL = String.format(REST.DATABASE.DEMO.URL_TEMPLATE, - mapperManager.getContext()); - - try { - HttpResponse response = Request.Post(targetURL) - .execute() - .returnResponse(); - if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { - LOGGER.info(String.format("status - code: %1$d, message: %2$s", - response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase())); - throw new PersistenceException( - String.format(messages.getString("ERREUR_INSERTION_DEMO"), - response.getStatusLine().getStatusCode())); - } - } catch (IOException ex) { - LOGGER.severe(ex.getMessage()); - throw new PersistenceException(ex); - - } - - } - -} diff --git a/src/main/java/campanule/datasource/rest/xml/EntiteMapperImpl.java b/src/main/java/campanule/datasource/rest/xml/EntiteMapperImpl.java deleted file mode 100644 index 3194616..0000000 --- a/src/main/java/campanule/datasource/rest/xml/EntiteMapperImpl.java +++ /dev/null @@ -1,495 +0,0 @@ -package campanule.datasource.rest.xml; - -import core.datasource.EntiteInconnuePersistenceException; -import core.datasource.EntiteTropAnciennePersistenceException; -import core.datasource.EntiteUtiliseePersistenceException; -import core.datasource.Mapper; -import core.datasource.PersistenceException; -import core.domain.Entite; -import core.domain.Identifiant; -import core.serialisation.SerialisationException; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.StatusLine; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.fluent.Request; -import org.apache.http.entity.ContentType; -import org.apache.http.util.EntityUtils; - -/** - * - * @author dominique huguenin (dominique.huguenin at rpn.ch) - */ -public abstract class EntiteMapperImpl implements Mapper { - - private static final Logger LOGGER - = Logger.getLogger(EntiteMapperImpl.class.getCanonicalName()); - - private final ResourceBundle messages; - - public ResourceBundle getMessages() { - return messages; - } - - public EntiteMapperImpl(final String messageBundleFile) { - this.messages = ResourceBundle.getBundle(messageBundleFile); - - } - - protected abstract E buildNullEntite(); - - protected abstract ByteArrayOutputStream serialiserEntite(E e) - throws SerialisationException; - - protected abstract E deserialiserEntite(InputStream is) - throws UnsupportedOperationException, - SerialisationException, IOException; - - protected abstract List deserialiserEntiteList(InputStream is) - throws SerialisationException, IOException, - UnsupportedOperationException; - - protected abstract ContentType getContentType(); - - protected abstract Header getAcceptHeader(); - - protected abstract String getCreateUrl(E entite) throws PersistenceException; - - protected abstract String getRetrieveUrl(String filtre); - - protected abstract String getRetrieveUrl(Identifiant id); - - protected abstract String getDeleteUrl(E e) throws PersistenceException; - - @Override - public E create(final E e) throws PersistenceException { - String targetURL = getCreateUrl(e); - - E entite = e; - if (entite == null) { - entite = buildNullEntite(); - } - - E nouvelleEntite = null; - try { - ByteArrayOutputStream os = serialiserEntite(entite); - - nouvelleEntite = Request.Post(targetURL) - .bodyStream(new ByteArrayInputStream(os.toByteArray()), - getContentType()) - .execute() - .handleResponse(new ResponseHandler() { - @Override - public E handleResponse( - final HttpResponse response) - throws ClientProtocolException, IOException { - E entite = null; - - try { - StatusLine status = response.getStatusLine(); - - switch (status.getStatusCode()) { - case HttpStatus.SC_CREATED: - String location - = response.getFirstHeader("Location").getValue(); - entite = retrieveByUrl(location); - break; - case HttpStatus.SC_BAD_REQUEST: - String body = EntityUtils.toString(response.getEntity()); - - if (body.contains(EntiteInconnuePersistenceException.class - .getCanonicalName())) { - throw new ClientProtocolException( - new EntiteInconnuePersistenceException( - status.getReasonPhrase()) - ); - } - if (body.contains(EntiteInconnuePersistenceException.class - .getCanonicalName())) { - throw new ClientProtocolException( - new EntiteInconnuePersistenceException( - status.getReasonPhrase()) - ); - } - throw new ClientProtocolException( - new PersistenceException( - status.getReasonPhrase()) - ); - - default: - String msg = String.format( - messages.getString("ERREUR_STATUS_NON_ATTENDU"), - response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase()); - LOGGER.info(msg); - throw new ClientProtocolException(msg); - - } - } catch (PersistenceException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - throw new ClientProtocolException(ex.getMessage(), ex); - } - return entite; - - } - }); - - } catch (IOException - | SerialisationException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - throw new PersistenceException(ex.getMessage(), ex); - } - return nouvelleEntite; - - } - - private E retrieveByUrl(final String targetURL) - throws PersistenceException { - E entite = null; - - if (targetURL == null) { - return null; - } - - try { - entite = Request.Get(targetURL) - .addHeader(getAcceptHeader()) - .execute() - .handleResponse(new ResponseHandler() { - @Override - public E handleResponse( - final HttpResponse response) - throws ClientProtocolException, IOException { - try { - E entite = null; - switch (response.getStatusLine().getStatusCode()) { - case HttpStatus.SC_OK: - HttpEntity responseEntity = response.getEntity(); - - if (!responseEntity.getContentType().getValue() - .contains(getContentType() - .getMimeType())) { - String msg = String.format( - messages.getString("ERREUR_VALEUR_NON_ATTENDU"), - responseEntity.getContentType().getName(), - responseEntity.getContentType().getValue()); - LOGGER.info(msg); - throw new ClientProtocolException(msg); - } - - InputStream is = responseEntity.getContent(); - - entite = deserialiserEntite(is); - break; - - case HttpStatus.SC_NOT_FOUND: - entite = null; - break; - - default: - String msg = String.format( - messages.getString("ERREUR_STATUS_NON_ATTENDU"), - response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase()); - LOGGER.info(msg); - throw new ClientProtocolException(msg); - } - - return entite; - } catch (SerialisationException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - throw new ClientProtocolException(ex.getMessage(), ex); - } - } - }); - - } catch (IOException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - throw new PersistenceException(ex.getMessage(), ex); - } - return entite; - } - - @Override - public E retrieve(final Identifiant id) throws PersistenceException { - if (id == null) { - return null; - } - - String targetURL = getRetrieveUrl(id); - - return retrieveByUrl(targetURL); - } - - @Override - public List retrieve(final String filtre) throws PersistenceException { - List entites = new ArrayList<>(); - try { - String targetURL = getRetrieveUrl(filtre); - - entites = Request.Get(targetURL) - .addHeader(getAcceptHeader()) - .execute() - .handleResponse(new ResponseHandler>() { - @Override - public List handleResponse( - final HttpResponse response) - throws ClientProtocolException, IOException { - try { - List entites = new ArrayList<>(); - switch (response.getStatusLine().getStatusCode()) { - case HttpStatus.SC_OK: - HttpEntity responseEntity = response.getEntity(); - - if (!responseEntity.getContentType().getValue() - .contains(getContentType() - .getMimeType())) { - String msg = String.format( - messages.getString("ERREUR_VALEUR_NON_ATTENDU"), - responseEntity.getContentType().getName(), - responseEntity.getContentType().getValue()); - LOGGER.info(msg); - throw new ClientProtocolException(msg); - } - - InputStream is = responseEntity.getContent(); - entites = deserialiserEntiteList(is); - break; - - case HttpStatus.SC_NOT_FOUND: - entites = null; - break; - - default: - String msg = String.format( - messages.getString("ERREUR_STATUS_NON_ATTENDU"), - response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase()); - LOGGER.info(msg); - throw new ClientProtocolException(msg); - } - return entites; - } catch (SerialisationException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - throw new ClientProtocolException(ex.getMessage(), ex); - } - - } - }); - } catch (IOException ex) { - LOGGER.severe(ex.getMessage()); - throw new PersistenceException(ex); - } - - return entites; - - } - - @Override - public void update(final E e) throws PersistenceException { - if (e == null) { - return; - } - - try { - String targetURL = this.getRetrieveUrl(e.getIdentifiant()); - - ByteArrayOutputStream os = serialiserEntite(e); - - Request.Put(targetURL) - .bodyStream(new ByteArrayInputStream(os.toByteArray()), - this.getContentType()) - .execute().handleResponse(new ResponseHandler() { - @Override - public StatusLine handleResponse( - final HttpResponse response) - throws ClientProtocolException, IOException { - StatusLine status = response.getStatusLine(); - - switch (status.getStatusCode()) { - case HttpStatus.SC_OK: - case HttpStatus.SC_NO_CONTENT: - break; - - case HttpStatus.SC_NOT_FOUND: - throw new ClientProtocolException( - new EntiteInconnuePersistenceException( - status.getReasonPhrase()) - ); - - case HttpStatus.SC_BAD_REQUEST: - String body = EntityUtils.toString(response.getEntity()); - - if (body.contains(EntiteUtiliseePersistenceException.class - .getCanonicalName())) { - throw new ClientProtocolException( - new EntiteUtiliseePersistenceException( - status.getReasonPhrase()) - ); - } - if (body.contains(EntiteTropAnciennePersistenceException.class - .getCanonicalName())) { - throw new ClientProtocolException( - new EntiteTropAnciennePersistenceException( - status.getReasonPhrase()) - ); - } - if (body.contains(EntiteInconnuePersistenceException.class - .getCanonicalName())) { - throw new ClientProtocolException( - new EntiteInconnuePersistenceException( - status.getReasonPhrase()) - ); - } - throw new ClientProtocolException( - new PersistenceException( - status.getReasonPhrase()) - ); - - default: - String msg = String.format( - messages.getString("ERREUR_STATUS_NON_ATTENDU"), - response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase()); - LOGGER.info(msg); - throw new ClientProtocolException(msg); - - } - - return status; - } - - }); - - } catch (ClientProtocolException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - - if (ex.getCause() instanceof EntiteUtiliseePersistenceException) { - throw (EntiteUtiliseePersistenceException) ex.getCause(); - } - - if (ex.getCause() instanceof EntiteTropAnciennePersistenceException) { - throw (EntiteTropAnciennePersistenceException) ex.getCause(); - } - - if (ex.getCause() instanceof EntiteInconnuePersistenceException) { - throw (EntiteInconnuePersistenceException) ex.getCause(); - } - - throw (PersistenceException) ex.getCause(); - - } catch (IOException - | SerialisationException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - throw new PersistenceException(ex.getMessage(), ex); - } - - } - - @Override - public void delete(final E e) throws PersistenceException { - if (e == null) { - return; - } - try { - String targetURL = getDeleteUrl(e); - - Request.Delete(targetURL) - .execute() - .handleResponse(new ResponseHandler() { - @Override - public StatusLine handleResponse( - final HttpResponse response) - throws ClientProtocolException, IOException { - - StatusLine status = response.getStatusLine(); - - switch (status.getStatusCode()) { - case HttpStatus.SC_NO_CONTENT: - break; - - case HttpStatus.SC_NOT_FOUND: - throw new ClientProtocolException( - new EntiteInconnuePersistenceException( - status.getReasonPhrase()) - ); - - case HttpStatus.SC_BAD_REQUEST: - String body = EntityUtils.toString(response.getEntity()); - - if (body.contains(EntiteUtiliseePersistenceException.class - .getCanonicalName())) { - throw new ClientProtocolException( - new EntiteUtiliseePersistenceException( - status.getReasonPhrase()) - ); - } - if (body.contains(EntiteTropAnciennePersistenceException.class - .getCanonicalName())) { - throw new ClientProtocolException( - new EntiteTropAnciennePersistenceException( - status.getReasonPhrase()) - ); - } - if (body.contains(EntiteInconnuePersistenceException.class - .getCanonicalName())) { - throw new ClientProtocolException( - new EntiteInconnuePersistenceException( - status.getReasonPhrase()) - ); - } - throw new ClientProtocolException( - new PersistenceException( - status.getReasonPhrase()) - ); - - default: - String msg = String.format( - messages.getString("ERREUR_STATUS_NON_ATTENDU"), - response.getStatusLine().getStatusCode(), - response.getStatusLine().getReasonPhrase()); - LOGGER.info(msg); - throw new ClientProtocolException(msg); - - } - return status; - } - }); - - } catch (ClientProtocolException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - - if (ex.getCause() instanceof EntiteUtiliseePersistenceException) { - throw (EntiteUtiliseePersistenceException) ex.getCause(); - } - - if (ex.getCause() instanceof EntiteTropAnciennePersistenceException) { - throw (EntiteTropAnciennePersistenceException) ex.getCause(); - } - - if (ex.getCause() instanceof EntiteInconnuePersistenceException) { - throw (EntiteInconnuePersistenceException) ex.getCause(); - } - - throw (PersistenceException) ex.getCause(); - - } catch (IOException ex) { - LOGGER.log(Level.SEVERE, ex.getMessage(), ex); - throw new PersistenceException(ex.getMessage(), ex); - } - - } - -} diff --git a/src/main/java/campanule/datasource/rest/xml/RestMapperManager.java b/src/main/java/campanule/datasource/rest/xml/RestMapperManager.java new file mode 100644 index 0000000..6836b65 --- /dev/null +++ b/src/main/java/campanule/datasource/rest/xml/RestMapperManager.java @@ -0,0 +1,43 @@ +package campanule.datasource.rest.xml; + +import campanule.datasource.ArticleMapper; +import campanule.datasource.ClientMapper; +import campanule.datasource.CommandeMapper; +import core.datasource.PersistenceException; + +/** + * + * @author dominique huguenin (dominique.huguenin@rpn.ch) + */ +public class RestMapperManager + extends campanule.datasource.rest.RestMapperManager { + + RestMapperManager(final String context) { + super(context); + } + + @Override + public ArticleMapper getArticleMapper() throws PersistenceException { + if (super.getArticleMapper() == null) { + super.setArticleMapper(new ArticleMapperImpl(this)); + } + return super.getArticleMapper(); + } + + @Override + public ClientMapper getClientMapper() throws PersistenceException { + if (super.getClientMapper() == null) { + super.setClientMapper(new ClientMapperImpl(this)); + } + return super.getClientMapper(); + } + + @Override + public CommandeMapper getCommandeMapper() throws PersistenceException { + if (super.getCommandeMapper() == null) { + super.setCommandeMapper(new CommandeMapperImpl(this)); + } + return super.getCommandeMapper(); + } + +} diff --git a/src/main/java/campanule/datasource/rest/xml/RestTransactionManager.java b/src/main/java/campanule/datasource/rest/xml/RestTransactionManager.java index 393266e..11d40ca 100644 --- a/src/main/java/campanule/datasource/rest/xml/RestTransactionManager.java +++ b/src/main/java/campanule/datasource/rest/xml/RestTransactionManager.java @@ -9,25 +9,20 @@ import core.datasource.TransactionManager.Operation; * @author dominique huguenin (dominique.huguenin@rpn.ch) */ public final class RestTransactionManager - implements TransactionManager> { + extends campanule.datasource.rest.RestTransactionManager { - private static RestTransactionManager transactionManager; - - public static TransactionManager getInstance(final String context) { - if (transactionManager == null) { - transactionManager = new RestTransactionManager(context); - } - return transactionManager; + public RestTransactionManager(final String context) { + super(context); } - private final String context; - private RestTransactionManager(final String context) { - this.context = context; + public static TransactionManager getInstance(final String context) { + return setInstance(new RestTransactionManager(context)); } @Override public Object executeTransaction(final Operation operation) throws core.datasource.PersistenceException { - return operation.execute(new XmlRestMapperManager(this.context)); + return operation.execute( + new RestMapperManager(this.getContext())); } } diff --git a/src/main/java/campanule/datasource/rest/xml/TransactionManagerFactory.java b/src/main/java/campanule/datasource/rest/xml/TransactionManagerFactory.java index ed81177..70e8976 100644 --- a/src/main/java/campanule/datasource/rest/xml/TransactionManagerFactory.java +++ b/src/main/java/campanule/datasource/rest/xml/TransactionManagerFactory.java @@ -1,26 +1,17 @@ package campanule.datasource.rest.xml; import core.datasource.TransactionManager; -import javax.naming.Context; -import javax.naming.InitialContext; import javax.naming.NamingException; /** * * @author dominique huguenin (dominique.huguenin@rpn.ch) */ -public final class TransactionManagerFactory { - - private TransactionManagerFactory() { - } +public final class TransactionManagerFactory + extends campanule.datasource.rest.TransactionManagerFactory { public static TransactionManager getInstance() throws NamingException { - InitialContext initCtx = new InitialContext(); - Context envCtx = (Context) initCtx.lookup("java:comp/env"); - String restServiceBaseUrl = (String) envCtx - .lookup("restServiceBaseUrl"); - - return RestTransactionManager.getInstance(restServiceBaseUrl); + return RestTransactionManager.getInstance(getRestServiceBaseUrl()); } diff --git a/src/main/java/campanule/datasource/rest/xml/XmlRestMapperManager.java b/src/main/java/campanule/datasource/rest/xml/XmlRestMapperManager.java deleted file mode 100644 index 3ab5605..0000000 --- a/src/main/java/campanule/datasource/rest/xml/XmlRestMapperManager.java +++ /dev/null @@ -1,62 +0,0 @@ -package campanule.datasource.rest.xml; - -import campanule.datasource.ArticleMapper; -import campanule.datasource.ClientMapper; -import campanule.datasource.CommandeMapper; -import campanule.datasource.DatabaseManager; -import campanule.datasource.MapperManager; -import core.datasource.PersistenceException; - -/** - * - * @author dominique huguenin (dominique.huguenin@rpn.ch) - */ -public class XmlRestMapperManager implements MapperManager { - - private ArticleMapper articleMapper; - private ClientMapper clientMapper; - private CommandeMapper commandeMapper; - private DatabaseManager databaseSetup; - private final String context; - - public XmlRestMapperManager(final String context) { - this.context = context; - } - - public String getContext() { - return context; - } - - @Override - public ArticleMapper getArticleMapper() { - if (this.articleMapper == null) { - this.articleMapper = new ArticleMapperImpl(this); - } - return this.articleMapper; - } - - @Override - public DatabaseManager getDatabaseManager() throws PersistenceException { - if (this.databaseSetup == null) { - this.databaseSetup = new DatabaseManagerImpl(this); - } - return this.databaseSetup; - } - - @Override - public ClientMapper getClientMapper() throws PersistenceException { - if (this.clientMapper == null) { - this.clientMapper = new ClientMapperImpl(this); - } - return this.clientMapper; - } - - @Override - public CommandeMapper getCommandeMapper() throws PersistenceException { - if (this.commandeMapper == null) { - this.commandeMapper = new CommandeMapperImpl(this); - } - return this.commandeMapper; - } - -} diff --git a/src/test/java/campanule/datasource/rest/CONSTANTES.java b/src/test/java/campanule/datasource/rest/CONSTANTES.java new file mode 100644 index 0000000..72b4e2c --- /dev/null +++ b/src/test/java/campanule/datasource/rest/CONSTANTES.java @@ -0,0 +1,14 @@ +package campanule.datasource.rest; + +/** + * + * @author dominique huguenin (dominique.huguenin@rpn.ch) + */ +public class CONSTANTES { + + //Configuration pour les tests en local + //public static final String SERVICE_REST_URL = "http://localhost:8080/campanule.rest/resources"; + + //Configuration pour les test dans la CI/CD de gitlab + public static final String SERVICE_REST_URL = "http://campanule.rest:8080/resources"; +} diff --git a/src/test/java/campanule/datasource/rest/ArticleMapperImplTest.java b/src/test/java/campanule/datasource/rest/json/ArticleMapperImplTest.java similarity index 96% rename from src/test/java/campanule/datasource/rest/ArticleMapperImplTest.java rename to src/test/java/campanule/datasource/rest/json/ArticleMapperImplTest.java index 06a280c..f2b42ca 100644 --- a/src/test/java/campanule/datasource/rest/ArticleMapperImplTest.java +++ b/src/test/java/campanule/datasource/rest/json/ArticleMapperImplTest.java @@ -1,4 +1,4 @@ -package campanule.datasource.rest; +package campanule.datasource.rest.json; import campanule.datasource.ArticleMapperTest; import campanule.datasource.MapperManager; diff --git a/src/test/java/campanule/datasource/rest/ClientMapperImplTest.java b/src/test/java/campanule/datasource/rest/json/ClientMapperImplTest.java similarity index 96% rename from src/test/java/campanule/datasource/rest/ClientMapperImplTest.java rename to src/test/java/campanule/datasource/rest/json/ClientMapperImplTest.java index a381457..657aa99 100644 --- a/src/test/java/campanule/datasource/rest/ClientMapperImplTest.java +++ b/src/test/java/campanule/datasource/rest/json/ClientMapperImplTest.java @@ -1,4 +1,4 @@ -package campanule.datasource.rest; +package campanule.datasource.rest.json; import campanule.datasource.ClientMapperTest; import campanule.datasource.MapperManager; diff --git a/src/test/java/campanule/datasource/rest/CommandeMapperImplTest.java b/src/test/java/campanule/datasource/rest/json/CommandeMapperImplTest.java similarity index 96% rename from src/test/java/campanule/datasource/rest/CommandeMapperImplTest.java rename to src/test/java/campanule/datasource/rest/json/CommandeMapperImplTest.java index 9af823c..9bd6862 100644 --- a/src/test/java/campanule/datasource/rest/CommandeMapperImplTest.java +++ b/src/test/java/campanule/datasource/rest/json/CommandeMapperImplTest.java @@ -1,4 +1,4 @@ -package campanule.datasource.rest; +package campanule.datasource.rest.json; import campanule.datasource.CommandeMapperTest; import campanule.datasource.MapperManager; diff --git a/src/test/java/campanule/datasource/rest/DatabaseManagerImplTest.java b/src/test/java/campanule/datasource/rest/json/DatabaseManagerImplTest.java similarity index 98% rename from src/test/java/campanule/datasource/rest/DatabaseManagerImplTest.java rename to src/test/java/campanule/datasource/rest/json/DatabaseManagerImplTest.java index d2a0c16..b68e83b 100644 --- a/src/test/java/campanule/datasource/rest/DatabaseManagerImplTest.java +++ b/src/test/java/campanule/datasource/rest/json/DatabaseManagerImplTest.java @@ -1,4 +1,4 @@ -package campanule.datasource.rest; +package campanule.datasource.rest.json; import campanule.datasource.MapperManager; import campanule.domain.DemoData; diff --git a/src/test/java/campanule/datasource/rest/TestTransactionManagerFactory.java b/src/test/java/campanule/datasource/rest/json/TestTransactionManagerFactory.java similarity index 52% rename from src/test/java/campanule/datasource/rest/TestTransactionManagerFactory.java rename to src/test/java/campanule/datasource/rest/json/TestTransactionManagerFactory.java index eb9c467..e6cd069 100644 --- a/src/test/java/campanule/datasource/rest/TestTransactionManagerFactory.java +++ b/src/test/java/campanule/datasource/rest/json/TestTransactionManagerFactory.java @@ -1,6 +1,6 @@ -package campanule.datasource.rest; +package campanule.datasource.rest.json; -import campanule.datasource.rest.RestTransactionManager; +import campanule.datasource.rest.CONSTANTES; import core.datasource.TransactionManager; /** @@ -10,8 +10,8 @@ import core.datasource.TransactionManager; public class TestTransactionManagerFactory { public static TransactionManager createTransactionManager() { - return RestTransactionManager - .getInstance("http://campanule.rest:8080/resources"); + return campanule.datasource.rest.json.RestTransactionManager + .getInstance(CONSTANTES.SERVICE_REST_URL); } } diff --git a/src/test/java/campanule/datasource/rest/xml/TestTransactionManagerFactory.java b/src/test/java/campanule/datasource/rest/xml/TestTransactionManagerFactory.java index a841889..3132add 100644 --- a/src/test/java/campanule/datasource/rest/xml/TestTransactionManagerFactory.java +++ b/src/test/java/campanule/datasource/rest/xml/TestTransactionManagerFactory.java @@ -1,16 +1,13 @@ package campanule.datasource.rest.xml; +import campanule.datasource.rest.CONSTANTES; import core.datasource.TransactionManager; -/** - * - * @author dominique huguenin (dominique.huguenin@rpn.ch) - */ public class TestTransactionManagerFactory { public static TransactionManager createTransactionManager() { - return RestTransactionManager - .getInstance("http://campanule.rest:8080/resources"); + return campanule.datasource.rest.json.RestTransactionManager + .getInstance(CONSTANTES.SERVICE_REST_URL); } } -- GitLab