Example #1
0
class Main(HTTPServlet):
    registerShutdown = 1
    
    def __init__(self):
        HTTPServlet.__init__(self)
        
        self.blogDirectory = WeblogDirectory("../blogs.xml")    
        self.mutex = RLock()
             
    def awake(self, transaction):
        # Register our shutdown handler if it hasn't already been done. This is to
        # make sure the databases are properly closed when the system is shutdown.
        self.mutex.acquire()
        try:        
            if (Main.registerShutdown == 1):
                transaction.application().addShutDownHandler(self.blogDirectory.shutdown)
                Main.registerShutdown = 0
        finally:
            self.mutex.release()
            
    def respondToGet(self, transaction):
        request = transaction.request()
        response = transaction.response()
        
        pathInfo = request.extraURLPath() 

        try:
            (blog, pathInfo) = self._parsePathInfo(pathInfo)
            weblog = self.blogDirectory.getBlog(blog)
        
            try:
                stylesheet = request.field('t', "")
                # Extra optional argument that can be passed to the stylesheet
                arg = request.field('a', "")
            
                # Content query that can be applied as a final step to extract
                # something from the rendered content
                contentQuery = request.field('c', "")
            
                result = weblog.handleRequest(pathInfo, stylesheet, arg, contentQuery)
            
                # Determine the content-type for the result
                if (result.startswith("<?xml")):                             
                    contentType = "text/xml"         
                elif (result.startswith("<html")):
                    contentType = "text/html"
                else:
                    contentType = "text/plain"
                #print result
                
                response.setStatus(200, 'OK')
                response.setHeader('Content-type', contentType)
                response.setHeader('Content-length', str(len(result)))
                response.write(result)
            except NotFoundError:
                response.setStatus(404, 'Not Found')
        except KeyError, IndexError:
            response.setStatus(404, 'Weblog Not Found')
Example #2
0
 def __init__(self):
     HTTPServlet.__init__(self)
     
     self.blogDirectory = WeblogDirectory("../blogs.xml")    
     self.mutex = RLock()