in modules/binding-atom-runtime/src/main/java/org/apache/tuscany/sca/binding/atom/provider/AtomBindingListenerServlet.java [159:473]
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// No authentication required for a get request
// Test for any cache info in the request
HTTPCacheContext cacheContext = null;
try {
cacheContext = HTTPCacheContext.createCacheContextFromRequest( request );
} catch ( java.text.ParseException e ) {
}
// System.out.println( "AtomBindingListener.doGet cache context=" + cacheContext );
// Get the request path
String path = URLDecoder.decode(HTTPUtils.getRequestPath(request), "UTF-8");
logger.fine("get " + request.getRequestURI());
// Handle an Atom request
if (path != null && path.equals("/atomsvc")) {
/*
<?xml version='1.0' encoding='UTF-8'?>
<service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom">
<workspace>
<atom:title type="text">resource</atom:title>
<collection href="http://luck.ibm.com:8084/customer">
<atom:title type="text">entries</atom:title>
<accept>application/atom+xml</accept>
<categories />
</collection>
</workspace>
</service>
*/
// Return the Atom service document
response.setDateHeader("Date", System.currentTimeMillis());
response.setContentType("application/atomsvc+xml");
String href = request.getRequestURL().toString();
href = href.substring(0, href.length() - "/atomsvc".length());
String workspaceURL = new String( href );
int pathIndex = workspaceURL.indexOf( request.getServletPath() );
if ( -1 != pathIndex ) {
workspaceURL = workspaceURL.substring( 0, pathIndex ) + "/";
}
Service service = abderaFactory.newService();
//service.setText("service");
Workspace workspace = abderaFactory.newWorkspace();
if ( title != null ) {
workspace.setTitle(title);
} else {
workspace.setTitle("workspace");
}
workspace.setBaseUri( new IRI( workspaceURL ));
Collection collection = workspace.addCollection("collection", href );
Feed feed = getFeed( request );
if ( feed != null ) {
String title = feed.getTitle();
if ( title != null ) {
collection.setTitle(title);
} else {
collection.setTitle("entries");
}
collection.addAccepts("application/atom+xml");
collection.addAccepts("application/json");
List<Category> categories = feed.getCategories();
if ( categories != null ) {
collection.addCategories(categories, false, null);
} else {
collection.addCategories().setFixed(false);
}
} else {
collection.setTitle("entries");
collection.addAccepts("application/atom+xml");
collection.addAccepts("application/json");
collection.addCategories().setFixed(false);
}
workspace.addCollection(collection);
service.addWorkspace(workspace);
//FIXME add prettyPrint support
try {
service.getDocument().writeTo(response.getOutputStream());
} catch (IOException ioe) {
throw new ServletException(ioe);
}
} else if (path == null || path.length() == 0 || path.equals("/")) {
// Return a feed containing the entries in the collection
Feed feed = getFeed( request );
if (feed != null) {
String feedETag = null;
feedETag = HTTPUtils.calculateHashETag(feed.toString().getBytes("utf-8"));
Date feedUpdated = feed.getUpdated();
// Test request for predicates.
String predicate = request.getHeader( "If-Match" );
if (( predicate != null ) && ( !predicate.equals(feedETag) )) {
// No match, should short circuit
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return;
}
predicate = request.getHeader( "If-None-Match" );
if (( predicate != null ) && ( predicate.equals(feedETag) )) {
// Match, should short circuit
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
if ( feedUpdated != null ) {
predicate = request.getHeader( "If-Unmodified-Since" );
if ( predicate != null ) {
try {
Date predicateDate = dateFormat.parse( predicate );
if ( predicateDate.compareTo( exactSeconds(feedUpdated) ) < 0 ) {
// Match, should short circuit
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
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( exactSeconds(feedUpdated) ) >= 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 ( feedETag != null ) {
response.addHeader(ETAG, feedETag);
}
if ( feedUpdated != null ) {
response.addHeader(LASTMODIFIED, dateFormat.format( feedUpdated ));
}
//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");
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 {
// XML response body
response.setContentType("application/atom+xml");
try {
feedEntry.writeTo(getWriter(response));
} catch (IOException ioe) {
throw new ServletException(ioe);
}
}
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
} else {
// Path doesn't match any known pattern
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}