Skip to content

Commit

Permalink
Merge pull request #664 from i-Cell-Mobilsoft-Open-Source/feature/663…
Browse files Browse the repository at this point in the history
…-multiple-catalog-file-in-a-war

#663 multiple catalog file
  • Loading branch information
cserhatit authored May 16, 2024
2 parents 0150830 + 19547d3 commit 7cb60d6
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.net.URI;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javax.xml.catalog.Catalog;
Expand Down Expand Up @@ -73,17 +75,20 @@ public CatalogProducer() {
*/
@Produces
public Catalog publicCatalogResolver() throws BaseException {
Optional<String> xmlCatalogPath = applicationConfiguration.getOptionalString(IConfigKey.CATALOG_XML_PATH);
String path = xmlCatalogPath
Optional<String[]> configCatalogPaths = applicationConfiguration.getOptionalValue(IConfigKey.CATALOG_XML_PATH, String[].class);
String[] catalogPaths = configCatalogPaths
.orElseThrow(() -> new TechnicalException(MessageFormat.format("The config of [{0}] not found!", IConfigKey.CATALOG_XML_PATH)));
try {
URI catalogUri = Thread.currentThread().getContextClassLoader().getResource(path).toURI();
List<URI> catalogUris = new ArrayList<>();

return CatalogManager.catalog(CatalogFeatures.defaults(), catalogUri);
} catch (Exception e) {
String msg = MessageFormat.format("Can not resolve catalog:[{0}], [{1}]", path, e.getLocalizedMessage());
log.error(msg, e);
throw new XsdProcessingException(CoffeeFaultType.OPERATION_FAILED, msg, e);
for (String catalogPath : catalogPaths) {
try {
catalogUris.add(Thread.currentThread().getContextClassLoader().getResource(catalogPath).toURI());
} catch (Exception e) {
String msg = MessageFormat.format("Can not resolve catalog:[{0}], [{1}]", catalogPath, e.getLocalizedMessage());
log.error(msg, e);
throw new XsdProcessingException(CoffeeFaultType.OPERATION_FAILED, msg, e);
}
}
return CatalogManager.catalog(CatalogFeatures.defaults(), catalogUris.toArray(new URI[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
Expand Down Expand Up @@ -267,7 +270,7 @@ public <T> T unmarshalXML(Class<T> type, byte[] binary, String schemaPath) throw
* @param obj
* XML {@code Object}
* @param schemaPath
* path to XSD to validate on, if null, then validation is not executed
* path to XSD or catalog to validate on, if null, then validation is not executed
* @return XML String
* @throws BaseException
* if invalid input or cannot be marshalled
Expand All @@ -280,26 +283,83 @@ public String marshalXML(Object obj, String schemaPath) throws BaseException {
}

/**
* Marshals given XML {@link Object} to {@link String}. <br>
* Sets the following fix parameters for the conversion:
* <ul>
* <li>jaxb.formatted.output - TRUE ({@link Marshaller#JAXB_FORMATTED_OUTPUT})</li>
* <li>jaxb.fragment - TRUE ({@link Marshaller#JAXB_FRAGMENT})</li>
* </ul>
*
* Marshals given XML {@link Object} to {@link String} with given parameters.
* @param obj
* XML {@code Object}
* @param schemaPath
* path to XSD or catalog to validate on, if null, then validation is not executed
* @return XML String
* @param additionalClasses
* these classes will be added to the {@link JAXBContext}. Typically in case of 'Class not known to this context' errors.
* @throws BaseException
* if invalid input or cannot be marshalled
*/
public String marshalXML(Object obj, String schemaPath, Class<?>... additionalClasses) throws BaseException {
Map<String, Object> properties = new HashMap<>();
properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);
properties.put(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
return marshalXML(obj, schemaPath, properties, additionalClasses);
}

/**
* Marshals given XML {@link Object} to {@link String}. <br>
* Sets the following fix parameters for the conversion:
* <ul>
* <li>jaxb.formatted.output - TRUE ({@link Marshaller#JAXB_FORMATTED_OUTPUT})</li>
* <li>jaxb.fragment - TRUE ({@link Marshaller#JAXB_FRAGMENT})</li>
* </ul>
*
* @param obj
* XML {@code Object}
* @param schemaPath
* path to XSD to validate on, if null, then validation is not executed
* path to XSD or catalog file to validate on, if null, then validation is not executed
* @param marshallerProperties
* marshaller properties
* @return XML String
* @throws BaseException
* if invalid input or cannot be marshalled
*/
public String marshalXML(Object obj, String schemaPath, Map<String, Object> marshallerProperties) throws BaseException {
return marshalXML(obj, schemaPath, marshallerProperties, (Class<?>) null);
}

/**
*
* Marshals given XML {@link Object} to {@link String} with given parameters.
*
* @param obj
* XML {@code Object}
* @param schemaPath
* path to XSD or catalog to validate on, if null, then validation is not executed (possibly it should be an xml catalog file)
* @param marshallerProperties
* marshaller properties
* @param additionalClasses
* these classes will be added to the {@link JAXBContext}. Typically in case of 'Class not known to this context' errors.
* @return XML String
* @throws BaseException
* if invalid input or cannot be marshalled
*/
public String marshalXML(Object obj, String schemaPath, Map<String, Object> marshallerProperties, Class<?>... additionalClasses)
throws BaseException {
if (obj == null) {
throw new InvalidParameterException("obj is null!");
}
try {
IXsdHelper xsdHelper = createCDIInstance(IXsdHelper.class);
JAXBContext jaxbContext = xsdHelper.getJAXBContext(obj.getClass());
JAXBContext jaxbContext;
if (additionalClasses != null && additionalClasses.length != 0) {
List<Class<?>> contextClasses = new ArrayList<>(Arrays.asList(additionalClasses));
contextClasses.add(obj.getClass());
jaxbContext = xsdHelper.getJAXBContext(contextClasses.toArray(new Class<?>[0]));
} else {
jaxbContext = xsdHelper.getJAXBContext(obj.getClass());
}
Marshaller marshaller = jaxbContext.createMarshaller();
if (marshallerProperties != null) {
for (Entry<String, Object> entry : marshallerProperties.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public interface IXsdHelper {
*/
JAXBContext getJAXBContext(Class<?> forClass) throws JAXBException, BaseException;

/**
* Returns {@link JAXBContext} for given class.
*
* @param forClasses
* classes of desired {@code JAXBContext}
* @return {@code JAXBContext}
* @throws JAXBException
* if JAXB exception occurs
* @throws BaseException
* if XSD processing exception occurs
*/
JAXBContext getJAXBContext(Class<?> ...forClasses) throws JAXBException, BaseException;

/**
* Returns {@link Schema} for given XSD.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
package hu.icellmobilsoft.coffee.rest.validation.xml.utils;

import java.io.InputStream;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
Expand Down Expand Up @@ -69,7 +72,7 @@ public XsdHelper() {
/**
* {@inheritDoc}
*
* Létrehoz egy osztályhoz egy JAXBContext-et, cache-eli a választ.
* Creates and caches JAXBContext for the class
*/
@Override
public JAXBContext getJAXBContext(Class<?> forClass) throws JAXBException, BaseException {
Expand All @@ -89,7 +92,27 @@ public JAXBContext getJAXBContext(Class<?> forClass) throws JAXBException, BaseE
/**
* {@inheritDoc}
*
* Létrehoz a megadott XSD-hez egy Schema-t, cache-eli a választ.
* Creates and caches JAXBContext for the classes
*/
@Override
public JAXBContext getJAXBContext(Class<?>... forClasses) throws JAXBException, BaseException {
if (forClasses == null) {
throw new InvalidParameterException("forClasses is null!");
}
String className = Arrays.stream(forClasses).filter(Objects::nonNull).map(Class::getName).sorted().collect(Collectors.joining());
if (jaxbContextCache.containsKey(className)) {
return jaxbContextCache.get(className);
} else {
JAXBContext jaxbContext = JAXBContext.newInstance(forClasses);
jaxbContextCache.put(className, jaxbContext);
return jaxbContext;
}
}

/**
* {@inheritDoc}
*
* Creates and caches a {@link Schema} for the given XSD
*/
@Override
public Schema getSchema(String xsd, LSResourceResolver lsResourceResolver) throws BaseException, SAXException {
Expand Down
6 changes: 6 additions & 0 deletions docs/en/common/core/coffee-rest.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ coffee:
----
After that we are ready and the XSD Catalog will do the XSD schema reading.

[NOTE]
====
You can add multiple catalog file separated with `,`
====

== Json support

The framework supports JSON format messages in addition to XML for REST communication.
Expand Down
12 changes: 12 additions & 0 deletions docs/en/migration/migration270to280.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
= v2.7.0 → v2.8.0

coff:ee v2.7.0 -> v2.8.0 migration description, news, changes

=== coffee-rest

* Support of multiple xml catalog file separated with `,`
* For marshalling, you can give `JaxbTool` multiple classes to add to `JAXBContext`

==== Migration

Changes are backwards compatible doesn't need any migration.
6 changes: 6 additions & 0 deletions docs/hu/common/core/coffee-rest.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ coffee:
----
Ezekután készen is vagyunk és XSD Catalog szerint fog történni a XSD séma felolvasás.

[NOTE]
====
Több katalógus fájl is megadható `,` jellel elválasztva
====

== Json támogatás

A keretrendszer támogatja a REST kommunikáció során a JSON formátumú üzeneteket is az XML mellett.
Expand Down
12 changes: 12 additions & 0 deletions docs/hu/migration/migration270to280.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
= v2.7.0 → v2.8.0

coff:ee v2.7.0 -> v2.8.0 migrációs leírás, újdonságok, változások leírása

=== coffee-rest

* Több xml katalógus fájl támogatása `,` jellel elválasztva
* Marshalling esetén megadható a `JaxbTool`-nak több osztály, amit hozzá szeretnénk adni a `JAXBContext`-hez

==== Migration

A változtatások nem eredményeznek átállási munkálatokat, visszafelé kompatibilis.

0 comments on commit 7cb60d6

Please sign in to comment.