Hi,
Is it possible to get the value of "output:media-type" through QueryProcessor?
I am currently trying to use BaseX as a view renderer for a Spring MVC application. The view is rendering fine using embedded BaseX. However, I can't figure out how to get the "output:media-type" value from the XQuery so that I can set the response header during runtime. I can set the content-type through the controller in Spring. But I would rather not have to do it in two separate places. Below is the relevant code snippet from the view renderer.
try(QueryProcessor proc = new QueryProcessor(query, context)) { Iter iter = proc.iter();
//response is HTTPServletResponse response.setContentType(????);
try(Serializer ser = proc.getSerializer(response.getOutputStream())) { for(Item item; (item = iter.next()) != null;) { ser.serialize(item); } } }
Any suggestion?
Thanks Andrew
Hi Andrew,
This is how you can request the value of output:media-type:
import org.basex.io.serial.SerializerOptions; .... SerializerOptions sopts = proc.qc.serParams(); String type = sopts.get(SerializerOptions.MEDIA_TYPE);
As the media type can be empty, you can additionally take advantage of the HTTPConnection.mediaType() function [1], which will always returns a media type (provided that you have the basex-api classes embedded):
import org.basex.http.HTTPConnection; import org.basex.io.serial.SerializerOptions; .... SerializerOptions sopts = proc.qc.serParams(); response.setContentType(HTTPConnection.mediaType(sopts).toString());
Hope this helps, Christian
[1] https://github.com/BaseXdb/basex/blob/master/basex-api/src/main/java/org/bas...
On Wed, Jul 26, 2017 at 10:55 PM, Andrew Tsui a.tpw25@googlemail.com wrote:
Hi,
Is it possible to get the value of "output:media-type" through QueryProcessor?
I am currently trying to use BaseX as a view renderer for a Spring MVC application. The view is rendering fine using embedded BaseX. However, I can't figure out how to get the "output:media-type" value from the XQuery so that I can set the response header during runtime. I can set the content-type through the controller in Spring. But I would rather not have to do it in two separate places. Below is the relevant code snippet from the view renderer.
try(QueryProcessor proc = new QueryProcessor(query, context)) { Iter iter = proc.iter();
//response is HTTPServletResponse response.setContentType(????); try(Serializer ser = proc.getSerializer(response.getOutputStream())) { for(Item item; (item = iter.next()) != null;) { ser.serialize(item); } }
}
Any suggestion?
Thanks Andrew
Hi Christian,
It worked after I made modification to my code based on HTTPConnection.mediaType(). Many thanks for pointing me to the right direction.
Andrew
On 26 July 2017 at 22:08, Christian Grün christian.gruen@gmail.com wrote:
Hi Andrew,
This is how you can request the value of output:media-type:
import org.basex.io.serial.SerializerOptions; .... SerializerOptions sopts = proc.qc.serParams(); String type = sopts.get(SerializerOptions.MEDIA_TYPE);
As the media type can be empty, you can additionally take advantage of the HTTPConnection.mediaType() function [1], which will always returns a media type (provided that you have the basex-api classes embedded):
import org.basex.http.HTTPConnection; import org.basex.io.serial.SerializerOptions; .... SerializerOptions sopts = proc.qc.serParams(); response.setContentType(HTTPConnection.mediaType(sopts).toString());
Hope this helps, Christian
[1] https://github.com/BaseXdb/basex/blob/master/basex-api/ src/main/java/org/basex/http/HTTPConnection.java#L293
On Wed, Jul 26, 2017 at 10:55 PM, Andrew Tsui a.tpw25@googlemail.com wrote:
Hi,
Is it possible to get the value of "output:media-type" through QueryProcessor?
I am currently trying to use BaseX as a view renderer for a Spring MVC application. The view is rendering fine using embedded BaseX. However, I can't figure out how to get the "output:media-type" value from the
XQuery so
that I can set the response header during runtime. I can set the content-type through the controller in Spring. But I would rather not
have
to do it in two separate places. Below is the relevant code snippet from
the
view renderer.
try(QueryProcessor proc = new QueryProcessor(query, context)) { Iter iter = proc.iter();
//response is HTTPServletResponse response.setContentType(????); try(Serializer ser = proc.getSerializer(response.getOutputStream()))
{
for(Item item; (item = iter.next()) != null;) { ser.serialize(item); } }
}
Any suggestion?
Thanks Andrew
basex-talk@mailman.uni-konstanz.de