Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Model exporter xml - how customize response content-type

davidef34326447
Level 2
Level 2

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
kishorekumar14
Correct answer by
Level 8
Level 8

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
kishorekumar14
Level 8
Level 8

Hi @davidef34326447 ,

 

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

davidef34326447
Level 2
Level 2

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
davidef34326447
Level 2
Level 2

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.

 

kishorekumar14
Level 8
Level 8

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

davidef34326447
Level 2
Level 2

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

 

kishorekumar14
Level 8
Level 8

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>

 

davidef34326447
Level 2
Level 2

" 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?

 

 

 

kishorekumar14
Level 8
Level 8

hi @davidef34326447 ,

 

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

davidef34326447
Level 2
Level 2

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.

Jörg_Hoh
Employee
Employee

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.

kishorekumar14
Correct answer by
Level 8
Level 8

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

davidef34326447
Level 2
Level 2

tks @kishorekumar14 , so the solutions are:

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

2) try with dispatcher rule

 

thanks again!

davidef34326447
Level 2
Level 2

@kishorekumar14 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 :|..