Access output:media-type value from QueryProcessor
Wednesday, 26 July 2017 · 3 messages · last reply 27 Jul 2017
AT
Andrew Tsui26 Jul 2017 22:55 #
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
CG
Christian Grün26 Jul 2017 23:08 #
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
Quoted text · Andrew Tsui · 32 lines
On Wed, Jul 26, 2017 at 10:55 PM, Andrew Tsui <a.tpw25 at 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
AT
Andrew Tsui27 Jul 2017 00:13 #
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
Quoted text · Christian Grün · 66 lines
On 26 July 2017 at 22:08, Christian Grün <christian.gruen at 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 at 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 >
Written for BaseX 8.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery