in modules/binding-feed/src/main/java/org/apache/tuscany/sca/binding/feed/provider/FeedBindingListenerServlet.java [152:364]
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// No authentication required for a get request
// Get the request path
String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8");
// The feedType parameter is used to override what type of feed is going
// to be produced
String requestFeedType = request.getParameter("feedType");
if (requestFeedType == null)
requestFeedType = feedType;
logger.info(">>> FeedEndPointServlet (" + requestFeedType + ") " + request.getRequestURI());
// Handle an Atom request
if (requestFeedType.startsWith("atom_")) {
if (path != null && path.equals("/atomsvc")) {
// Return the Atom service document
response.setContentType("application/atomsvc+xml; charset=utf-8");
Document document = new Document();
Element service = new Element("service", APP_NS);
document.setRootElement(service);
Element workspace = new Element("workspace", APP_NS);
Element title = new Element("title", ATOM_NS);
title.setText("resource");
workspace.addContent(title);
service.addContent(workspace);
Element collection = new Element("collection", APP_NS);
String href = request.getRequestURL().toString();
href = href.substring(0, href.length() - "/atomsvc".length());
collection.setAttribute("href", href);
Element collectionTitle = new Element("title", ATOM_NS);
collectionTitle.setText("entries");
collection.addContent(collectionTitle);
workspace.addContent(collection);
XMLOutputter outputter = new XMLOutputter();
Format format = Format.getPrettyFormat();
format.setEncoding("UTF-8");
outputter.setFormat(format);
outputter.output(document, getWriter(response));
} else if (path == null || path.length() == 0 || path.equals("/")) {
// Return a feed containing the entries in the collection
Feed feed = null;
if (supportsFeedEntries) {
// The service implementation supports feed entries, invoke its getFeed operation
Message requestMessage = messageFactory.createMessage();
Message responseMessage = getFeedInvoker.invoke(requestMessage);
if (responseMessage.isFault()) {
throw new ServletException((Throwable)responseMessage.getBody());
}
feed = (Feed)responseMessage.getBody();
} else {
// The service implementation does not support feed entries,
// invoke its getAll operation to get the data item collection, then create
// feed entries from the items
Message requestMessage = messageFactory.createMessage();
Message responseMessage;
if (request.getQueryString() != null) {
requestMessage.setBody(new Object[] {request.getQueryString()});
responseMessage = queryInvoker.invoke(requestMessage);
} else {
responseMessage = getAllInvoker.invoke(requestMessage);
}
if (responseMessage.isFault()) {
throw new ServletException((Throwable)responseMessage.getBody());
}
org.apache.tuscany.sca.data.collection.Entry<Object, Object>[] collection =
(org.apache.tuscany.sca.data.collection.Entry<Object, Object>[])responseMessage.getBody();
if (collection != null) {
// Create the feed
feed = new Feed();
feed.setTitle("Feed");
for (org.apache.tuscany.sca.data.collection.Entry<Object, Object> entry: collection) {
Entry feedEntry = createFeedEntry(entry);
feed.getEntries().add(feedEntry);
}
}
}
if (feed != null) {
// Write the Atom feed
response.setContentType("application/atom+xml; charset=utf-8");
feed.setFeedType(requestFeedType);
WireFeedOutput feedOutput = new WireFeedOutput();
try {
feedOutput.output(feed, getWriter(response));
} catch (FeedException e) {
throw new ServletException(e);
}
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} else if (path.startsWith("/")) {
// Return a specific entry in the collection
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()) {
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
feedEntry = createFeedEntry(new org.apache.tuscany.sca.data.collection.Entry<Object, Object>(id, responseMessage.getBody()));
}
// Write the Atom entry
if (feedEntry != null) {
response.setContentType("application/atom+xml; charset=utf-8");
try {
AtomFeedEntryUtil.writeFeedEntry(feedEntry, feedType, getWriter(response));
} catch (FeedException e) {
throw new ServletException(e);
}
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} else {
// Path doesn't match any known pattern
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} else {
// Handle an RSS request
if (path == null || path.length() == 0 || path.equals("/")) {
// Return an RSS feed containing the entries in the collection
Feed feed = null;
if (supportsFeedEntries) {
// The service implementation supports feed entries, invoke its getFeed operation
Message requestMessage = messageFactory.createMessage();
Message responseMessage = getFeedInvoker.invoke(requestMessage);
if (responseMessage.isFault()) {
throw new ServletException((Throwable)responseMessage.getBody());
}
feed = (Feed)responseMessage.getBody();
} else {
// The service implementation does not support feed entries, invoke its
// getAll operation to get the data item collection. then create feed entries
// from the data items
Message requestMessage = messageFactory.createMessage();
Message responseMessage;
if (request.getQueryString() != null) {
requestMessage.setBody(new Object[] {request.getQueryString()});
responseMessage = queryInvoker.invoke(requestMessage);
} else {
responseMessage = getAllInvoker.invoke(requestMessage);
}
if (responseMessage.isFault()) {
throw new ServletException((Throwable)responseMessage.getBody());
}
org.apache.tuscany.sca.data.collection.Entry<Object, Object>[] collection =
(org.apache.tuscany.sca.data.collection.Entry<Object, Object>[])responseMessage.getBody();
if (collection != null) {
// Create the feed
feed = new Feed();
feed.setTitle("Feed");
for (org.apache.tuscany.sca.data.collection.Entry<Object, Object> entry: collection) {
Entry feedEntry = createFeedEntry(entry);
feed.getEntries().add(feedEntry);
}
}
}
// Convert to an RSS feed
if (feed != null) {
response.setContentType("application/rss+xml; charset=utf-8");
feed.setFeedType("atom_1.0");
SyndFeed syndFeed = new SyndFeedImpl(feed);
syndFeed.setFeedType(requestFeedType);
syndFeed.setLink(path);
SyndFeedOutput syndOutput = new SyndFeedOutput();
try {
syndOutput.output(syndFeed, getWriter(response));
} catch (FeedException e) {
throw new ServletException(e);
}
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
}