예제 #1
0
 def __init__(self, reactor, conversion=None, **kwargs):
     Observable.__init__(self, **kwargs)
     self._conversion = Conversion() if conversion is None else conversion
     self._dynamicHtml = DynamicHtml(
         [dynamicPath],
         reactor=reactor,
         notFoundPage='notFound',
         additionalGlobals={
             'VERSION': version,
             'allCoreInfo': self._allCoreInfo,
             'parseCql': parseString,
             'cqlToExpression': cqlToExpression,
             'cql2string': cql2string,
             'dumps': dumps,
         })
     self._internalTree = be((
         Observable(),
         (PathFilter('/', excluding=['/static',
                                     '/version']), (self._dynamicHtml, )),
         (PathFilter('/static'),
          (PathRename(lambda path: path[len('/static'):]),
           (FileServer(staticPath), ))),
         (PathFilter('/version'),
          (StringServer("Meresco Lucene version %s" % version,
                        ContentTypePlainText), )),
     ))
예제 #2
0
 def __init__(self,
              host=None,
              port=None,
              path=None,
              name=None,
              conversion=None):
     Observable.__init__(self, name=name)
     self._host = host
     self._port = port
     self._path = '' if path is None else path
     self._path += '/__lucene_remote__'
     self._conversion = Conversion() if conversion is None else conversion
예제 #3
0
 def __init__(self, host=None, port=None, path=None, name=None, conversion=None):
     Observable.__init__(self, name=name)
     self._host = host
     self._port = port
     self._path = '' if path is None else path
     self._path += '/__lucene_remote__'
     self._conversion = Conversion() if conversion is None else conversion
예제 #4
0
class LuceneRemote(Observable):
    def __init__(self, host=None, port=None, path=None, name=None, conversion=None):
        Observable.__init__(self, name=name)
        self._host = host
        self._port = port
        self._path = '' if path is None else path
        self._path += '/__lucene_remote__'
        self._conversion = Conversion() if conversion is None else conversion

    def any_unknown(self, message, **kwargs):
        if message in self._ALLOWED_METHODS:
            result = yield self._send(message=message, **kwargs)
            raise StopIteration(result)
        raise DeclineMessage()

    def _luceneRemoteServer(self):
        return (self._host, self._port) if self._host else self.call.luceneRemoteServer()

    def _send(self, message, **kwargs):
        body = self._conversion.jsonDumpMessage(message, **kwargs)
        headers={'Content-Type': 'application/json', 'Content-Length': len(body)}
        host, port = self._luceneRemoteServer() # WARNING: can return a different server each time.
        response = yield self._httppost(host=host, port=port, request=self._path, body=body, headers=headers)
        header, responseBody = response.split("\r\n\r\n", 1)
        self._verify200(header, response)
        raise StopIteration(LuceneResponse.fromJson(responseBody))

    def _httppost(self, **kwargs):
        return httppost(**kwargs)

    def _verify200(self, header, response):
        if not header.startswith('HTTP/1.0 200'):
            raise IOError("Expected status '200' from LuceneRemoteService, but got: " + response)

    _ALLOWED_METHODS = ['executeQuery', 'prefixSearch', 'fieldnames', 'drilldownFieldnames', 'executeComposedQuery', 'similarDocuments']
예제 #5
0
 def __init__(self, reactor, conversion=None, **kwargs):
     Observable.__init__(self, **kwargs)
     self._conversion = Conversion() if conversion is None else conversion
     self._dynamicHtml = DynamicHtml([dynamicPath],
             reactor=reactor,
             notFoundPage='notFound',
             additionalGlobals={
                 'VERSION': version,
                 'allCoreInfo': self._allCoreInfo,
                 'parseCql': parseString,
                 'cqlToExpression': cqlToExpression,
                 'cql2string': cql2string,
                 'dumps': dumps,
             }
         )
     self._internalTree = be((Observable(),
         (PathFilter('/', excluding=['/static', '/version']),
             (self._dynamicHtml,)
         ),
         (PathFilter('/static'),
             (PathRename(lambda path: path[len('/static'):]),
                 (FileServer(staticPath),)
             )
         ),
         (PathFilter('/version'),
             (StringServer("Meresco Lucene version %s" % version, ContentTypePlainText),)
         ),
     ))
예제 #6
0
class LuceneRemote(Observable):
    def __init__(self,
                 host=None,
                 port=None,
                 path=None,
                 name=None,
                 conversion=None):
        Observable.__init__(self, name=name)
        self._host = host
        self._port = port
        self._path = '' if path is None else path
        self._path += '/__lucene_remote__'
        self._conversion = Conversion() if conversion is None else conversion

    def any_unknown(self, message, **kwargs):
        if message in self._ALLOWED_METHODS:
            result = yield self._send(message=message, **kwargs)
            raise StopIteration(result)
        raise DeclineMessage()

    def _luceneRemoteServer(self):
        return (self._host,
                self._port) if self._host else self.call.luceneRemoteServer()

    def _send(self, message, **kwargs):
        body = self._conversion.jsonDumpMessage(message, **kwargs)
        headers = {
            'Content-Type': 'application/json',
            'Content-Length': len(body)
        }
        host, port = self._luceneRemoteServer(
        )  # WARNING: can return a different server each time.
        response = yield self._httppost(host=host,
                                        port=port,
                                        request=self._path,
                                        body=body,
                                        headers=headers)
        header, responseBody = response.split("\r\n\r\n", 1)
        self._verify200(header, response)
        raise StopIteration(LuceneResponse.fromJson(responseBody))

    def _httppost(self, **kwargs):
        return httppost(**kwargs)

    def _verify200(self, header, response):
        if not header.startswith('HTTP/1.0 200'):
            raise IOError(
                "Expected status '200' from LuceneRemoteService, but got: " +
                response)

    _ALLOWED_METHODS = [
        'executeQuery', 'prefixSearch', 'fieldnames', 'drilldownFieldnames',
        'executeComposedQuery', 'similarDocuments'
    ]
예제 #7
0
class LuceneRemoteService(Observable):
    def __init__(self, reactor, conversion=None, **kwargs):
        Observable.__init__(self, **kwargs)
        self._conversion = Conversion() if conversion is None else conversion
        self._dynamicHtml = DynamicHtml([dynamicPath],
                reactor=reactor,
                notFoundPage='notFound',
                additionalGlobals={
                    'VERSION': version,
                    'allCoreInfo': self._allCoreInfo,
                    'parseCql': parseString,
                    'cqlToExpression': cqlToExpression,
                    'cql2string': cql2string,
                    'dumps': dumps,
                }
            )
        self._internalTree = be((Observable(),
            (PathFilter('/', excluding=['/static', '/version']),
                (self._dynamicHtml,)
            ),
            (PathFilter('/static'),
                (PathRename(lambda path: path[len('/static'):]),
                    (FileServer(staticPath),)
                )
            ),
            (PathFilter('/version'),
                (StringServer("Meresco Lucene version %s" % version, ContentTypePlainText),)
            ),
        ))

    def addObserver(self, *args, **kwargs):
        Observable.addObserver(self, *args, **kwargs)
        self._dynamicHtml.addObserver(*args, **kwargs)

    def addStrand(self, *args, **kwargs):
        Observable.addStrand(self, *args, **kwargs)
        self._dynamicHtml.addStrand(*args, **kwargs)

    def handleRequest(self, path, Method, Body=None, **kwargs):
        if Method == 'POST' and path.endswith('/__lucene_remote__'):
            yield self._handleQuery(Body)
        elif '/info' in path:
            originalPath = path
            _, _, path = path.partition('/info')
            yield self._internalTree.all.handleRequest(path=path or '/', originalPath=originalPath, Method=Method, Body=Body, **kwargs)

    def _handleQuery(self, Body):
        try:
            message, kwargs = self._conversion.jsonLoadMessage(Body)
            if message not in _ALLOWED_METHODS:
                raise ValueError('Expected %s' % (' or '.join('"%s"' % m for m in _ALLOWED_METHODS)))
            response = yield self.any.unknown(message=message, **kwargs)
        except Exception, e:
            print_exc()
            x = format_exc() # returns 'None' if e is a Java Error
            yield serverErrorPlainText
            yield x if x and x.strip() != 'None' else repr(e)
            return
        yield Ok
        yield ContentTypeHeader + 'application/json' + CRLF
        yield CRLF
        yield response.asJson()
예제 #8
0
class LuceneRemoteService(Observable):
    def __init__(self, reactor, conversion=None, **kwargs):
        Observable.__init__(self, **kwargs)
        self._conversion = Conversion() if conversion is None else conversion
        self._dynamicHtml = DynamicHtml(
            [dynamicPath],
            reactor=reactor,
            notFoundPage='notFound',
            additionalGlobals={
                'VERSION': version,
                'allCoreInfo': self._allCoreInfo,
                'parseCql': parseString,
                'cqlToExpression': cqlToExpression,
                'cql2string': cql2string,
                'dumps': dumps,
            })
        self._internalTree = be((
            Observable(),
            (PathFilter('/', excluding=['/static',
                                        '/version']), (self._dynamicHtml, )),
            (PathFilter('/static'),
             (PathRename(lambda path: path[len('/static'):]),
              (FileServer(staticPath), ))),
            (PathFilter('/version'),
             (StringServer("Meresco Lucene version %s" % version,
                           ContentTypePlainText), )),
        ))

    def addObserver(self, *args, **kwargs):
        Observable.addObserver(self, *args, **kwargs)
        self._dynamicHtml.addObserver(*args, **kwargs)

    def addStrand(self, *args, **kwargs):
        Observable.addStrand(self, *args, **kwargs)
        self._dynamicHtml.addStrand(*args, **kwargs)

    def handleRequest(self, path, Method, Body=None, **kwargs):
        if Method == 'POST' and path.endswith('/__lucene_remote__'):
            yield self._handleQuery(Body)
        elif '/info' in path:
            originalPath = path
            _, _, path = path.partition('/info')
            yield self._internalTree.all.handleRequest(
                path=path or '/',
                originalPath=originalPath,
                Method=Method,
                Body=Body,
                **kwargs)

    def _handleQuery(self, Body):
        try:
            message, kwargs = self._conversion.jsonLoadMessage(Body)
            if message not in _ALLOWED_METHODS:
                raise ValueError('Expected %s' %
                                 (' or '.join('"%s"' % m
                                              for m in _ALLOWED_METHODS)))
            response = yield self.any.unknown(message=message, **kwargs)
        except Exception, e:
            print_exc()
            x = format_exc()  # returns 'None' if e is a Java Error
            yield serverErrorPlainText
            yield x if x and x.strip() != 'None' else repr(e)
            return
        yield Ok
        yield ContentTypeHeader + 'application/json' + CRLF
        yield CRLF
        yield response.asJson()