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")
})
Solved! Go to Solution.
Views
Replies
Total Likes
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
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.
yes,
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
copy and past your code (that is the same of mine), i received the same result (no UTF 8):
it's very strange.
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.
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?
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.
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 / @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
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:
The xml is cutted in proximity of the special char "€".
Sling issue in open since 2019 :|..
Views
Replies
Total Likes
Views
Likes
Replies