Author: amichalec
Date: Sat Nov 28 00:39:45 2009
New Revision: 885056
URL: http://svn.apache.org/viewvc?rev=885056&view=rev
Log:
Mandatory properties of Atom feeds/entries set by customizable postprocessor.
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/logging/atom/converter/StandardConverter.java
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/logging/atom/converter/StandardConverter.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/logging/atom/converter/StandardConverter.java?rev=885056&r1=885055&r2=885056&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/logging/atom/converter/StandardConverter.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/logging/atom/converter/StandardConverter.java
Sat Nov 28 00:39:45 2009
@@ -23,7 +23,10 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
import java.util.List;
+import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
@@ -45,7 +48,7 @@
* Converter producing ATOM Feeds on standalone Entries with LogRecords or
LogRecordsLists embedded as content
* or extension. For configuration details see constructor documentation.
*/
-public class StandardConverter implements Converter {
+public final class StandardConverter implements Converter {
/** Conversion output */
public enum Output {
@@ -65,14 +68,27 @@
EXTENSION
}
+ /**
+ * Post-processing for feeds/entries properties customization eg setup of
dates, titles, author etc.
+ */
+ public interface Postprocessor {
+
+ /** Called after entry creation for given log records. */
+ void afterEntry(Entry entry, List<LogRecord> records);
+
+ /** Called after feed creation; at this stage feed has associated
entries. */
+ void afterFeed(Feed feed);
+ }
+
private Factory factory;
private Marshaller marsh;
private DateFormat df;
private Converter worker;
+ private Postprocessor postprocessor;
/**
- * Creates configured converter. Regardless of "format", combination of
"output" and "multiplicity" flags
- * can be interpreted as follow:
+ * Creates configured converter with custom feeds/entries post-processor.
Regardless of "format",
+ * combination of "output" and "multiplicity" flags can be interpreted as
follow:
* <ul>
* <li>ENTRY ONE - for each log record one entry is produced, converter
returns list of entries</li>
* <li>ENTRY MANY - list of log records is packed in one entry, converter
return one entry.</li>
@@ -86,12 +102,16 @@
* @param multiplicity for output==FEED it is multiplicity of entities in
feed for output==ENTITY it is
* multiplicity of log records in entity.
* @param format log records embedded as entry content or extension.
+ * @param postprocessor custom feeds/entries post-processor.
*/
- public StandardConverter(Output output, Multiplicity multiplicity, Format
format) {
+ public StandardConverter(Output output, Multiplicity multiplicity, Format
format,
+ Postprocessor postprocessor) {
Validate.notNull(output, "output is null");
Validate.notNull(multiplicity, "multiplicity is null");
Validate.notNull(format, "format is null");
+ Validate.notNull(postprocessor, "interceptor is null");
configure(output, multiplicity, format);
+ this.postprocessor = postprocessor;
df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
factory = Abdera.getNewFactory();
try {
@@ -101,6 +121,14 @@
}
}
+ /**
+ * Creates configured converter with default post-processing of
feeds/entries mandatory properties. See
+ * {...@link #StandardConverter(Output, Multiplicity, Format,
Postprocessor)} for description.
+ */
+ public StandardConverter(Output output, Multiplicity multiplicity, Format
format) {
+ this(output, multiplicity, format, new DefaultPostprocessor());
+ }
+
public List<? extends Element> convert(List<LogRecord> records) {
return worker.convert(records);
}
@@ -112,11 +140,14 @@
// produces many entries, each entry with one log record
List<Element> ret = new ArrayList<Element>();
for (LogRecord record : records) {
+ Entry e;
if (format == Format.CONTENT) {
- ret.add(createEntry(createContent(record)));
+ e = createEntry(createContent(record));
} else {
- ret.add(createEntry(createExtension(record)));
+ e = createEntry(createExtension(record));
}
+ ret.add(e);
+ postprocessor.afterEntry(e,
Collections.singletonList(record));
}
return ret;
}
@@ -126,11 +157,14 @@
worker = new Converter() {
public List<? extends Element> convert(List<LogRecord>
records) {
// produces one entry with list of all log records
+ Entry e;
if (format == Format.CONTENT) {
- return
Arrays.asList(createEntry(createContent(records)));
+ e = createEntry(createContent(records));
} else {
- return
Arrays.asList(createEntry(createExtension(records)));
+ e = createEntry(createExtension(records));
}
+ postprocessor.afterEntry(e, records);
+ return Arrays.asList(e);
}
};
}
@@ -138,11 +172,16 @@
worker = new Converter() {
public List<? extends Element> convert(List<LogRecord>
records) {
// produces one feed with one entry with list of all log
records
+ Entry e;
if (format == Format.CONTENT) {
- return
Arrays.asList(createFeed(createEntry(createContent(records))));
+ e = createEntry(createContent(records));
} else {
- return
Arrays.asList(createFeed(createEntry(createExtension(records))));
+ e = createEntry(createExtension(records));
}
+ postprocessor.afterEntry(e, records);
+ Feed f = createFeed(e);
+ postprocessor.afterFeed(f);
+ return Arrays.asList(f);
}
};
}
@@ -152,13 +191,18 @@
// produces one feed with many entries, each entry with
one log record
List<Entry> entries = new ArrayList<Entry>();
for (LogRecord record : records) {
+ Entry e;
if (format == Format.CONTENT) {
- entries.add(createEntry(createContent(record)));
+ e = createEntry(createContent(record));
} else {
- entries.add(createEntry(createExtension(record)));
+ e = createEntry(createExtension(record));
}
+ entries.add(e);
+ postprocessor.afterEntry(e,
Collections.singletonList(record));
}
- return Arrays.asList(createFeed(entries));
+ Feed f = createFeed(entries);
+ postprocessor.afterFeed(f);
+ return Arrays.asList(f);
}
};
}
@@ -250,4 +294,22 @@
}
return feed;
}
+
+ private static class DefaultPostprocessor implements Postprocessor {
+ public void afterEntry(Entry entry, List<LogRecord> records) {
+ // required fields (see RFC 4287)
+ entry.setId("uuid:" + UUID.randomUUID().toString());
+ entry.addAuthor("CXF");
+ entry.setTitle(String.format("Entry with %d log record(s)",
records.size()));
+ entry.setUpdated(new Date());
+ }
+
+ public void afterFeed(Feed feed) {
+ // required fields (see RFC 4287)
+ feed.setId("uuid:" + UUID.randomUUID().toString());
+ feed.addAuthor("CXF");
+ feed.setTitle(String.format("Feed with %d entry(ies)",
feed.getEntries().size()));
+ feed.setUpdated(new Date());
+ }
+ }
}