@Test public void readsEntityStreamAsJAXBElement() throws Exception { Class<JAXBElement> type = JAXBElement.class; Type genericType = newParameterizedType(JAXBElement.class, Book.class); InputStream in = new ByteArrayInputStream(TEST_XML_CONTENT_BYTES); JAXBElement<Book> bookJAXBElement = (JAXBElement<Book>)reader.readFrom(type, genericType, null, APPLICATION_XML_TYPE, new MultivaluedMapImpl(), in); Book book = bookJAXBElement.getValue(); assertEquals("Java and XML Data Binding", book.getTitle()); assertTrue(book.isSendByPost()); assertEquals("Brett McLaughlin", book.getAuthor()); assertEquals("USD", book.getPrice().getCurrency()); assertEquals(new BigDecimal("34.95"), book.getPrice().getValue()); assertEquals("USD", book.getMemberPrice().getCurrency()); assertEquals(new BigDecimal("26.56"), book.getMemberPrice().getValue()); }
@Override public JAXBElement<?> readFrom(Class<JAXBElement<?>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { ParameterizedType parameterizedType = (ParameterizedType)genericType; Class<?> aClass = (Class<?>)parameterizedType.getActualTypeArguments()[0]; try { JAXBContext jaxbContext = getJAXBContext(aClass, mediaType); return jaxbContext.createUnmarshaller().unmarshal(new StreamSource(entityStream), aClass); } catch (UnmarshalException e) { LOG.debug(e.getMessage(), e); return null; } catch (JAXBException e) { throw new IOException(String.format("Can't read from input stream, %s", e)); } }
JAXBElementEntityProvider implements EntityProvider<JAXBElement<?>> { @Override public JAXBElement<?> readFrom(Class<JAXBElement<?>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { ParameterizedType parameterizedType = (ParameterizedType)genericType; Class<?> aClass = (Class<?>)parameterizedType.getActualTypeArguments()[0]; try { JAXBContext jaxbContext = getJAXBContext(aClass, mediaType); return jaxbContext.createUnmarshaller().unmarshal(new StreamSource(entityStream), aClass); } catch (UnmarshalException e) { LOG.debug(e.getMessage(), e); return null; } catch (JAXBException e) { throw new IOException(String.format("Can't read from input stream, %s", e)); } } }
JAXBElementEntityProvider implements EntityProvider<JAXBElement<?>> { @Override public JAXBElement<?> readFrom(Class<JAXBElement<?>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { ParameterizedType parameterizedType = (ParameterizedType)genericType; Class<?> aClass = (Class<?>)parameterizedType.getActualTypeArguments()[0]; try { JAXBContext jaxbContext = getJAXBContext(aClass, mediaType); return jaxbContext.createUnmarshaller().unmarshal(new StreamSource(entityStream), aClass); } catch (UnmarshalException e) { LOG.debug(e.getMessage(), e); return null; } catch (JAXBException e) { throw new IOException(String.format("Can't read from input stream, %s", e)); } } JAXBElementEntityProvider(@Context Providers providers); }
JAXBElementEntityProvider implements EntityProvider<JAXBElement<?>> { @Override public JAXBElement<?> readFrom(Class<JAXBElement<?>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { ParameterizedType parameterizedType = (ParameterizedType)genericType; Class<?> aClass = (Class<?>)parameterizedType.getActualTypeArguments()[0]; try { JAXBContext jaxbContext = getJAXBContext(aClass, mediaType); return jaxbContext.createUnmarshaller().unmarshal(new StreamSource(entityStream), aClass); } catch (UnmarshalException e) { LOG.debug(e.getMessage(), e); return null; } catch (JAXBException e) { throw new IOException(String.format("Can't read from input stream, %s", e)); } } JAXBElementEntityProvider(@Context Providers providers); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override JAXBElement<?> readFrom(Class<JAXBElement<?>> type,
                                   Type genericType,
                                   Annotation[] annotations,
                                   MediaType mediaType,
                                   MultivaluedMap<String, String> httpHeaders,
                                   InputStream entityStream); @Override long getSize(JAXBElement<?> t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(JAXBElement<?> t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream); }
JAXBElementEntityProvider implements EntityProvider<JAXBElement<?>> { @Override public JAXBElement<?> readFrom(Class<JAXBElement<?>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { ParameterizedType parameterizedType = (ParameterizedType)genericType; Class<?> aClass = (Class<?>)parameterizedType.getActualTypeArguments()[0]; try { JAXBContext jaxbContext = getJAXBContext(aClass, mediaType); return jaxbContext.createUnmarshaller().unmarshal(new StreamSource(entityStream), aClass); } catch (UnmarshalException e) { LOG.debug(e.getMessage(), e); return null; } catch (JAXBException e) { throw new IOException(String.format("Can't read from input stream, %s", e)); } } JAXBElementEntityProvider(@Context Providers providers); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override JAXBElement<?> readFrom(Class<JAXBElement<?>> type,
                                   Type genericType,
                                   Annotation[] annotations,
                                   MediaType mediaType,
                                   MultivaluedMap<String, String> httpHeaders,
                                   InputStream entityStream); @Override long getSize(JAXBElement<?> t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override void writeTo(JAXBElement<?> t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream);  }