Writes the payload of a body or part in the output stream of the connection. @param payload body/part payload @param atts payload attributes @param out output stream @throws IOException I/O exception
(final ItemList payload, final Map<String, String> atts,
final OutputStream out)
| 278 | * @throws IOException I/O exception |
| 279 | */ |
| 280 | private static void writePayload(final ItemList payload, final Map<String, String> atts, |
| 281 | final OutputStream out) throws IOException { |
| 282 | |
| 283 | // choose serialization parameters |
| 284 | final SerializerOptions sopts = new SerializerOptions(); |
| 285 | sopts.set(SerializerOptions.NEWLINE, Newline.NL); |
| 286 | |
| 287 | String method = null, type = null; |
| 288 | for(final Entry<String, String> entry : atts.entrySet()) { |
| 289 | final String key = entry.getKey(), value = entry.getValue(); |
| 290 | |
| 291 | // send specified source |
| 292 | if(key.equals(SRC)) { |
| 293 | out.write(IO.get(value).read()); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | // serialization parameters |
| 298 | if(key.equals(SerializerOptions.METHOD.name())) { |
| 299 | method = value.equals(BINARY) ? SerialMethod.BASEX.toString() : value; |
| 300 | } else { |
| 301 | sopts.assign(key, value); |
| 302 | if(key.equals(SerializerOptions.MEDIA_TYPE.name())) type = value; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // no method specified (yet): choose method based on media type |
| 307 | if(method == null && type != null) { |
| 308 | final MediaType mt = new MediaType(type); |
| 309 | if(mt.is(MediaType.APPLICATION_HTML_XML)) { |
| 310 | method = SerialMethod.XHTML.toString(); |
| 311 | } else if(mt.is(MediaType.TEXT_HTML)) { |
| 312 | method = SerialMethod.HTML.toString(); |
| 313 | } else if(mt.isXml()) { |
| 314 | method = SerialMethod.XML.toString(); |
| 315 | } else if(mt.isJSON()) { |
| 316 | method = SerialMethod.JSON.toString(); |
| 317 | } else if(mt.isCSV()) { |
| 318 | method = SerialMethod.CSV.toString(); |
| 319 | } else if(mt.isText()) { |
| 320 | method = SerialMethod.TEXT.toString(); |
| 321 | } |
| 322 | } |
| 323 | // no method, EXPath binary method: use default serialization, atomize nodes |
| 324 | final boolean atom = method == null || method.equals(BINARY); |
| 325 | if(atom) method = SerialMethod.BASEX.toString(); |
| 326 | sopts.assign(SerializerOptions.METHOD.name(), method); |
| 327 | |
| 328 | // serialize payload |
| 329 | try(Serializer ser = Serializer.get(out, sopts)) { |
| 330 | for(final Item item : payload) { |
| 331 | ser.serialize(atom && item instanceof final XNode xnode ? |
| 332 | xnode.atomItem(null, null) : item); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |