Model exporter xml - how customize response content-type | Community
Skip to main content
Level 3
August 31, 2021
Solved

Model exporter xml - how customize response content-type

  • August 31, 2021
  • 2 replies
  • 4097 views

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")
})

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kishore_Kumar_

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.


Hi @davidef34326447  / @joerghoh ,

 

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/sling/models/impl/ExportServlet.java 

 

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. 

2 replies

Kishore_Kumar_
Level 9
August 31, 2021

Hi @davidef34326447 ,

 

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

Level 3
September 1, 2021

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
Umesh_Thakur
Community Advisor
Community Advisor
September 1, 2021

Can you please have a look on the below article:

http://www.sgaemsolutions.com/2018/01/custom-sling-model-exporter-in-aem-63.html

hope this will help.

Umesh Thakur

Level 3
September 1, 2021

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

 

it's very strange.

 

Kishore_Kumar_
Level 9
September 1, 2021

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");