// JSON response body
response.setContentType("application/json");
try {
Abdera abdera = new Abdera();
WriterFactory wf = abdera.getWriterFactory();
org.apache.abdera.writer.Writer json = wf.getWriter("json");
feed.writeTo(json, response.getWriter());
} catch (Exception e) {
throw new ServletException(e);
}
} else {
// Write the Atom feed
response.setContentType("application/atom+xml");
try {
feed.getDocument().writeTo(response.getOutputStream());
} catch (IOException ioe) {
throw new ServletException(ioe);
}
}
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} else if (path.startsWith("/")) {
// Return a specific entry in the collection
org.apache.abdera.model.Entry feedEntry;
// Invoke the get operation on the service implementation
Message requestMessage = messageFactory.createMessage();
String id = path.substring(1);
requestMessage.setBody(new Object[] {id});
Message responseMessage = getInvoker.invoke(requestMessage);
if (responseMessage.isFault()) {
Object body = responseMessage.getBody();
if (body.getClass().getName().endsWith(".NotFoundException")) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} else {
throw new ServletException((Throwable)responseMessage.getBody());
}
}
if (supportsFeedEntries) {
// The service implementation returns a feed entry
feedEntry = responseMessage.getBody();
} else {
// The service implementation only returns a data item, create an entry
// from it
Entry<Object, Object> entry = new Entry<Object, Object>(id, responseMessage.getBody());
feedEntry = feedEntry(entry, itemClassType, itemXMLType, mediator, abderaFactory);
}
// Write the Atom entry
if (feedEntry != null) {
String entryETag = null;
entryETag = HTTPUtils.calculateHashETag(feedEntry.toString().getBytes("utf-8"));
Date entryUpdated = feedEntry.getUpdated();
if ( entryUpdated != null )
response.addHeader(LASTMODIFIED, dateFormat.format( entryUpdated ));
// TODO Check If-Modified-Since If-Unmodified-Since predicates against LASTMODIFIED.
// If true return 304 and null body.
Link link = feedEntry.getSelfLink();
if (link != null) {
response.addHeader(LOCATION, link.getHref().toString());
} else {
link = feedEntry.getLink( "Edit" );
if (link != null) {
response.addHeader(LOCATION, link.getHref().toString());
}
}
// Test request for predicates.
String predicate = request.getHeader( "If-Match" );
if (( predicate != null ) && ( !predicate.equals(entryETag) )) {
// No match, should short circuit
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return;
}
predicate = request.getHeader( "If-None-Match" );
if (( predicate != null ) && ( predicate.equals(entryETag) )) {
// Match, should short circuit
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
if ( entryUpdated != null ) {
predicate = request.getHeader( "If-Unmodified-Since" );
if ( predicate != null ) {
try {
Date predicateDate = dateFormat.parse( predicate );
if ( predicateDate.compareTo( entryUpdated ) < 0 ) {
// Match, should short circuit
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
} catch ( java.text.ParseException e ) {
// Ignore and move on
}
}
predicate = request.getHeader( "If-Modified-Since" );
if ( predicate != null ) {
try {
Date predicateDate = dateFormat.parse( predicate );
if ( predicateDate.compareTo( entryUpdated ) > 0 ) {
// Match, should short circuit
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
} catch ( java.text.ParseException e ) {
// Ignore and move on
}
}
}
// Provide Etag based on Id and time if given.
// Ignore if not given. (Browser may cache if trivial ETag is given.)
if (entryETag != null) {
response.addHeader(ETAG, entryETag );
}
if ( entryUpdated != null ) {
response.addHeader(LASTMODIFIED, dateFormat.format( entryUpdated ));
}
//default http header for response
AtomBindingHttpUtils.prepareHTTPResponse(request, response);
// Content negotiation
String acceptType = request.getHeader( "Accept" );
String preferredType = getContentPreference( acceptType );
if (( preferredType != null ) && ((preferredType.indexOf( "json") > -1) || (preferredType.indexOf( "JSON") > -1 ))) {
// JSON response body
response.setContentType("application/json");
try {
Abdera abdera = new Abdera();
WriterFactory wf = abdera.getWriterFactory();
org.apache.abdera.writer.Writer json = wf.getWriter("json");
feedEntry.writeTo(json, response.getWriter());
} catch (Exception e) {
throw new ServletException(e);
}
} else {