### Question:
ResourceBundleMessageBodyWriter implements MessageBodyWriter<ResourceBundle> { @Override public void writeTo( ResourceBundle resourceBundle, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream ) throws IOException, WebApplicationException { if ( MediaType.APPLICATION_JSON_TYPE.equals( mediaType ) ) { JSONObject resourceBundleJsonObject = new JSONObject(); for ( String key : Collections.list( resourceBundle.getKeys() ) ) { resourceBundleJsonObject.put( key, resourceBundle.getString( key ) ); } OutputStreamWriter outputStreamWriter = null; try { outputStreamWriter = new OutputStreamWriter( entityStream, StandardCharsets.UTF_8 ); resourceBundleJsonObject.writeJSONString( outputStreamWriter ); } finally { if ( outputStreamWriter != null ) { outputStreamWriter.flush(); } } } else if ( MediaType.APPLICATION_XML_TYPE.equals( mediaType ) ) { try { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Node propertiesNode = document.createElement( "properties" ); document.appendChild( propertiesNode ); for ( String key : Collections.list( resourceBundle.getKeys() ) ) { Node propertyNode = document.createElement( "property" ); propertiesNode.appendChild( propertyNode ); Node keyNode = document.createElement( "key" ); keyNode.setTextContent( key ); propertyNode.appendChild( keyNode ); Node valueNode = document.createElement( "value" ); valueNode.setTextContent( resourceBundle.getString( key ) ); propertyNode.appendChild( valueNode ); } Result output = new StreamResult( entityStream ); Source input = new DOMSource( document ); try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); transformer.setOutputProperty( "{http: transformer.transform( input, output ); } catch ( TransformerException e ) { throw new IOException( e ); } } catch ( ParserConfigurationException e ) { throw new WebApplicationException( e ); } } }  @Override boolean isWriteable( Class<?> type, Type genericType, Annotation[] annotations,
                                        MediaType mediaType ); @Override long getSize( ResourceBundle resourceBundle, Class<?> type, Type genericType,
                                 Annotation[] annotations, MediaType mediaType ); @Override void writeTo( ResourceBundle resourceBundle, Class<?> type, Type genericType,
                                 Annotation[] annotations, MediaType mediaType,
                                 MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream );  }### Answer:
@Test public void testJsonWrite() throws IOException { Map<String, String> props = new HashMap<String, String>(); props.put( "key1", "value1" ); props.put( "key2", "value2" ); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); new ResourceBundleMessageBodyWriter() .writeTo( makeResourceBundle( props ), ResourceBundle.class, null, null, MediaType.APPLICATION_JSON_TYPE, null, byteArrayOutputStream ); JSONObject result = (JSONObject) JSONValue.parse( byteArrayOutputStream.toString() ); assertEquals( 2, result.size() ); assertEquals( "value1", result.get( "key1" ) ); assertEquals( "value2", result.get( "key2" ) ); }
@Test public void testXmlWrite() throws IOException, ParserConfigurationException, SAXException { Map<String, String> props = new HashMap<String, String>(); props.put( "key1", "value1" ); props.put( "key2", "value2" ); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); new ResourceBundleMessageBodyWriter() .writeTo( makeResourceBundle( props ), ResourceBundle.class, null, null, MediaType.APPLICATION_XML_TYPE, null, byteArrayOutputStream ); Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse( new ByteArrayInputStream( byteArrayOutputStream.toByteArray() ) ); Map<String, String> result = documentToPropMap( doc ); assertEquals( 2, result.size() ); assertEquals( "value1", result.get( "key1" ) ); assertEquals( "value2", result.get( "key2" ) ); }