use of org.apache.abdera.model.Entry in project cxf by apache.
the class AtomPojoProvider method readFromFeedOrEntry.
@SuppressWarnings("unchecked")
private Object readFromFeedOrEntry(Class<Object> cls, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException {
AtomFeedProvider p = new AtomFeedProvider();
p.setAutodetectCharset(autodetectCharset);
Object atomObject = p.readFrom(Feed.class, Feed.class, new Annotation[] {}, mt, headers, is);
if (atomObject instanceof Entry) {
return this.readFromEntry((Entry) atomObject, cls);
}
Feed feed = (Feed) atomObject;
AtomElementReader<?, ?> reader = getAtomReader(cls);
if (reader != null) {
return ((AtomElementReader<Feed, Object>) reader).readFrom(feed);
}
Object instance = null;
try {
String methodName = getCollectionMethod(cls, false);
Method m = cls.getMethod(methodName, new Class[] { List.class });
Class<Object> realCls = (Class<Object>) InjectionUtils.getActualType(m.getGenericParameterTypes()[0]);
List<Object> objects = new ArrayList<>();
for (Entry e : feed.getEntries()) {
objects.add(readFromEntry(e, realCls));
}
instance = cls.newInstance();
m.invoke(instance, new Object[] { objects });
} catch (Exception ex) {
reportError("Object of type " + cls.getName() + " can not be deserialized from Feed", ex, 400);
}
return instance;
}
use of org.apache.abdera.model.Entry in project cxf by apache.
the class AtomPojoProvider method readFrom.
public Object readFrom(Class<Object> cls, Type type, Annotation[] anns, MediaType mt, MultivaluedMap<String, String> headers, InputStream is) throws IOException, WebApplicationException {
boolean isFeed = isFeedRequested(mt);
if (isFeed) {
return readFromFeedOrEntry(cls, mt, headers, is);
}
AtomEntryProvider p = new AtomEntryProvider();
p.setAutodetectCharset(autodetectCharset);
Entry entry = p.readFrom(Entry.class, Entry.class, new Annotation[] {}, mt, headers, is);
return readFromEntry(entry, cls);
}
use of org.apache.abdera.model.Entry in project cxf by apache.
the class AtomPojoProviderTest method testWriteFeedWithBuilders.
@Test
public void testWriteFeedWithBuilders() throws Exception {
AtomPojoProvider provider = (AtomPojoProvider) ctx.getBean("atom");
assertNotNull(provider);
provider.setFormattedOutput(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Books books = new Books();
List<Book> bs = new ArrayList<>();
bs.add(new Book("a"));
bs.add(new Book("b"));
books.setBooks(bs);
provider.writeTo(books, Books.class, Books.class, new Annotation[] {}, MediaType.valueOf("application/atom+xml"), null, bos);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
Feed feed = new AtomFeedProvider().readFrom(Feed.class, null, null, null, null, bis);
assertEquals("Books", feed.getTitle());
List<Entry> entries = feed.getEntries();
assertEquals(2, entries.size());
verifyEntry(getEntry(entries, "a"), "a");
verifyEntry(getEntry(entries, "b"), "b");
}
use of org.apache.abdera.model.Entry in project cxf by apache.
the class AtomPojoProviderTest method testWriteFeedWithBuildersNoJaxb.
@Test
public void testWriteFeedWithBuildersNoJaxb() throws Exception {
AtomPojoProvider provider = (AtomPojoProvider) ctx.getBean("atomNoJaxb");
assertNotNull(provider);
provider.setFormattedOutput(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Books books = new Books();
List<Book> bs = new ArrayList<>();
bs.add(new Book("a"));
bs.add(new Book("b"));
books.setBooks(bs);
provider.writeTo(books, Books.class, Books.class, new Annotation[] {}, MediaType.valueOf("application/atom+xml"), null, bos);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
Feed feed = new AtomFeedProvider().readFrom(Feed.class, null, null, null, null, bis);
assertEquals("Books", feed.getTitle());
List<Entry> entries = feed.getEntries();
assertEquals(2, entries.size());
Entry entryA = getEntry(entries, "a");
verifyEntry(entryA, "a");
String entryAContent = entryA.getContent();
assertTrue("<a/>".equals(entryAContent) || "<a><a/>".equals(entryAContent) || "<a xmlns=\"\"/>".equals(entryAContent));
Entry entryB = getEntry(entries, "b");
verifyEntry(entryB, "b");
String entryBContent = entryB.getContent();
assertTrue("<b/>".equals(entryBContent) || "<b><b/>".equals(entryBContent) || "<b xmlns=\"\"/>".equals(entryBContent));
}
use of org.apache.abdera.model.Entry in project cxf by apache.
the class AtomPojoProviderTest method testWriteEntryWithBuilders.
@Test
public void testWriteEntryWithBuilders() throws Exception {
AtomPojoProvider provider = (AtomPojoProvider) ctx.getBean("atom2");
assertNotNull(provider);
provider.setFormattedOutput(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(new Book("a"), Book.class, Book.class, new Annotation[] {}, MediaType.valueOf("application/atom+xml;type=entry"), null, bos);
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
Entry entry = new AtomEntryProvider().readFrom(Entry.class, null, null, null, null, bis);
verifyEntry(entry, "a");
}
Aggregations