Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Model exporter xml - how customize response content-type

Avatar

Level 3

Hi community,

 

i developed a custom XML exporter:

"

/**
* XML export used to generate XML render on servlets.
*/
@Component(service = ModelExporter.class)
public class CustomXmlExporter implements ModelExporter {
private static final Logger LOG = LoggerFactory.getLogger(CustomXmlExporter.class);
@Override
public boolean isSupported(Class<?> aClass) {
return true;
}

@Override
public <T> T export(Object model, Class<T> aClass, Map<String, String> options) throws ExportException {
StringWriter stringWriter = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(model.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(model, stringWriter);
} catch (JAXBException e) {
LOG.error("\n Marshell Error : {} ",e);
}
return (T) stringWriter.toString();
}

@Override
public String getName() {
return "custom-exporter";
}
}

"

and it work fine! 

now, during the integration test, i catched the necessary to customize the response content-type from:application/xml;charset=iso-8859-1 to:application/xml; charset=utf-8.

 

How can i reach the goal? i don't understand where set the content-type override.

 

Here my model:

@Exporters({
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, selector = "list", extensions = ExporterConstants.SLING_MODEL_EXTENSION, options = {
@ExporterOption(name = "MapperFeature.SORT_PROPERTIES_ALPHABETICALLY", value = "true"),
@ExporterOption(name = "SerializationFeature.WRITE_DATES_AS_TIMESTAMPS", value = "false")
}),
@Exporter(name = "custom-exporter", extensions = "xml", selector = "list")
})

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @davidef34326447  / @Jörg_Hoh ,

 

I have tried to check this issue. When i was investigating found that, earlier for application/json also it defaulted to charset=iso-8859-1 and later it was fixed.

 

https://issues.apache.org/jira/browse/SLING-7344 

 

https://github.com/apache/sling-org-apache-sling-models-impl/blob/master/src/main/java/org/apache/sl... 

 

kishorekumar14_0-1630817620933.png

And similarly we have also ticket for XML which is still Open.

 

https://issues.apache.org/jira/browse/SLING-8923 

 

Hope it gives some clarity. 

View solution in original post

14 Replies

Avatar

Community Advisor

Hi @davidef34326447 ,

 

If you hit your model in browser, whether output is coming fine in XML ?

Avatar

Level 3

yes,

 

  1. Content-Type:
    application/xml;charset=iso-8859-1
  2. Date:
    Wed, 01 Sep 2021 07:08:34 GMT
  3. Transfer-Encoding:
    chunked
  4. X-Content-Type-Options:
    nosniff

Avatar

Level 3

copy and past your code (that is the same of mine), i received the same result (no UTF 8):

davidef34326447_0-1630480907499.png

 

it's very strange.

 

Avatar

Community Advisor

Hi @davidef34326447 ,

 

you mean encoding format is incorrect ? can u try using it in your custom xml exporter.

marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

yes.. my issue is related to the response content-type charset wrong. 

also adding the "

marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

"

i receiving the same result on response content-type.. reading this article "https://howtodoinjava.com/jaxb/marshaller-example/",seems that utf-8 is the default option and it regard the xml first row output "<?xml version="1.0" encoding="UTF-8" standalone="yes"?" and not the response header... so, my current issue is regarding the response header, and in particular content-type to transform from: application/xml;charset=iso-8859-1 to application/xml;charset=utf-8.

Anyway thanks for your support.

davidef34326447_0-1630483325567.png

 

Avatar

Community Advisor

Hi @davidef34326447 ,

 

Ok. Not sure then, Is it causing any issue due to this charset ? If you still need it but not able to achieve via code, force charset at the dispatcher level. Something like below but more specific to your XMLs.

 

<LocationMatch "\.(?i:xml)$">
  ForceType application/xml
  Header set Content-Type application/xml;charset=utf-8
</LocationMatch>

 

" Is it causing any issue due to this charset ? " yeah i think, in particular, in the JCR i have saved the char , and in the XML a receive the char ?.

I think that the issue is related to the response content type.

 

Any other suggest?

 

 

 

Avatar

Community Advisor

hi @davidef34326447 ,

 

Have you checked the dispatcher level fix for content type which I mentioned above ? Is not working ?  

Not yet. Before to ask at client the dispatcher change, i would better understand possible other solution. As per now, in localhost (with author only), i'm receiving the issue above.

Avatar

Employee Advisor

I would ask on the sling list, as the Sling Model Exporter is pure Sling. Maybe the developers there can you give you a hint into the right direction.

Avatar

Correct answer by
Community Advisor

Hi @davidef34326447  / @Jörg_Hoh ,

 

I have tried to check this issue. When i was investigating found that, earlier for application/json also it defaulted to charset=iso-8859-1 and later it was fixed.

 

https://issues.apache.org/jira/browse/SLING-7344 

 

https://github.com/apache/sling-org-apache-sling-models-impl/blob/master/src/main/java/org/apache/sl... 

 

kishorekumar14_0-1630817620933.png

And similarly we have also ticket for XML which is still Open.

 

https://issues.apache.org/jira/browse/SLING-8923 

 

Hope it gives some clarity. 

tks @Kishore_Kumar_ , so the solutions are:

1) try to apply a similar workaround waiting the sling fix

2) try with dispatcher rule

 

thanks again!

@Kishore_Kumar_ meanwhile i tried with dispatcher workaround.. and the result is like the follow: 

davidef34326447_0-1631197384598.png

 

The xml is cutted in proximity of the special char "€". 

Sling issue in open since 2019 :|..