Beispiel #1
0
   def queryApi(self, remoteId):
      """
      Query Ext.Direct API by remote ID.
      """
      remote = self.remotesById.get(remoteId, None)
      if remote is None:
         return None

      if not remote.usePersistentConnections:
         ## Do HTTP/POST as individual request
         ##
         d = getPage(url = remote.apiUrl,
                     method = 'GET',
                     headers = {'User-Agent': ExtDirectRemoter.USER_AGENT},
                     timeout = remote.requestTimeout,
                     connectionTimeout = remote.connectionTimeout,
                     followRedirect = remote.redirectLimit > 0)

      else:
         ## Do HTTP/POST via HTTP connection pool
         ##

         ## avoid module level reactor import
         from twisted.web.client import Agent, RedirectAgent

         agent = Agent(self.reactor,
                       pool = self.httppools[remote.id],
                       connectTimeout = remote.connectionTimeout)

         if remote.redirectLimit > 0:
            agent = RedirectAgent(agent, redirectLimit = remote.redirectLimit)

         ## FIXME: honor requestTimeout
         d = agent.request('GET',
                           remote.apiUrl,
                           Headers({'User-Agent': [ExtDirectRemoter.USER_AGENT]}))

         def onResponse(response):
            if response.code == 200:
               finished = Deferred()
               response.deliverBody(StringReceiver(finished))
               return finished
            else:
               return defer.fail("%s [%s]" % (response.code, response.phrase))

         d.addCallback(onResponse)

      apiRequest = {'provider': 'extdirect',
                    'api-url': remote.apiUrl,
                    'use-persistent-connections': remote.usePersistentConnections,
                    'request-timeout': remote.requestTimeout,
                    'connection-timeout': remote.connectionTimeout}

      d.addCallbacks(self._onQueryApiResult,
                     self._onQueryApiError,
                     callbackArgs = [remote, apiRequest],
                     errbackArgs = [apiRequest])

      return d
   def remoteCall(self, call):
      """
      RPC handler remoting to REST servers. This method is usually
      registered via registerHandlerMethodForRpc on a WAMP protocol.
      """
      proto = call.proto
      uri = call.uri
      args = call.args

      ## extract extra information from RPC call handler argument
      (id, method) = call.extra

      ## get the REST remote onto which we will forward the call
      remote = self.remotesById[id]

      body = None

      if method in ['GET', 'DELETE']:
         if len(args) != 1:
            raise Exception(URI_ERROR_REMOTING,
                            "Invalid number of arguments (expected 1, was %d)" % len(args))
      elif method in ['PUT', 'POST']:
         if len(args) != 2:
            raise Exception(URI_ERROR_REMOTING,
                            "Invalid number of arguments (expected 2, was %d)" % len(args))
         body = json_dumps(args[1])
      else:
         ## should not arrive here!
         raise Exception("logic error")

      if remote.forwardCookies and \
         proto.cookies and \
         proto.cookies.has_key(remote.restDomain) and \
         proto.cookies[remote.restDomain] != "":

         cookie = str(proto.cookies[remote.restDomain])
      else:
         cookie = None

      if type(args[0]) not in [str, unicode]:
         raise Exception(URI_ERROR_REMOTING,
                         "Invalid type for argument 1 (expected str, was %s)" % type(args[0]))

      url = urlparse.urljoin(str(remote.restBaseUrl), str(args[0]))

      if not remote.usePersistentConnections:
         ## Do HTTP/POST as individual request
         ##

         headers = {'Content-Type': 'application/json',
                    'User-Agent': RestRemoter.USER_AGENT}

         if cookie:
            headers['Cookie'] = cookie

         d = getPage(url = url,
                     method = method,
                     postdata = body,
                     headers = headers,
                     timeout = remote.requestTimeout,
                     connectionTimeout = remote.connectionTimeout,
                     followRedirect = remote.redirectLimit > 0)

      else:
         ## Do HTTP/POST via HTTP connection pool
         ##
         ## http://twistedmatrix.com/documents/12.1.0/web/howto/client.html
         ##

         ## avoid module level reactor import
         from twisted.web.client import Agent, RedirectAgent

         headers = {'Content-Type': ['application/json'],
                    'User-Agent': [RestRemoter.USER_AGENT]}

         if cookie:
            headers['Cookie'] = [cookie]

         agent = Agent(self.reactor,
                       pool = self.httppools[remote.id],
                       connectTimeout = remote.connectionTimeout)

         if remote.redirectLimit > 0:
            agent = RedirectAgent(agent, redirectLimit = remote.redirectLimit)

         ## FIXME: honor requestTimeout
         if body:
            d = agent.request(method,
                              url,
                              Headers(headers),
                              StringProducer(body))
         else:
            d = agent.request(method,
                              url,
                              Headers(headers))

         def onResponse(response):
            if response.code == 200:
               finished = Deferred()
               response.deliverBody(StringReceiver(finished))
               return finished
            else:
               return defer.fail("%s [%s]" % (response.code, response.phrase))

         d.addCallback(onResponse)

      ## request information provided as error detail in case of call fails
      remotingRequest = {'provider': 'rest',
                         'rest-base-url': remote.restBaseUrl,
                         'use-persistent-connections': remote.usePersistentConnections,
                         'request-timeout': remote.requestTimeout,
                         'connection-timeout': remote.connectionTimeout,
                         'method': method}

      d.addCallbacks(self._onRemoteCallResult,
                     self._onRemoteCallError,
                     callbackArgs = [remotingRequest],
                     errbackArgs = [remotingRequest])

      ## FIXME!
      d.addCallback(self.onAfterRemoteCallSuccess, id)
      d.addErrback(self.onAfterRemoteCallError, id)

      return d
Beispiel #3
0
    def remoteCall(self, call):
        """
      RPC handler remoting to REST servers. This method is usually
      registered via registerHandlerMethodForRpc on a WAMP protocol.
      """
        proto = call.proto
        uri = call.uri
        args = call.args

        ## extract extra information from RPC call handler argument
        (id, method) = call.extra

        ## get the REST remote onto which we will forward the call
        remote = self.remotesById[id]

        body = None

        if method in ['GET', 'DELETE']:
            if len(args) != 1:
                raise Exception(
                    URI_ERROR_REMOTING,
                    "Invalid number of arguments (expected 1, was %d)" %
                    len(args))
        elif method in ['PUT', 'POST']:
            if len(args) != 2:
                raise Exception(
                    URI_ERROR_REMOTING,
                    "Invalid number of arguments (expected 2, was %d)" %
                    len(args))
            body = json_dumps(args[1])
        else:
            ## should not arrive here!
            raise Exception("logic error")

        if remote.forwardCookies and \
           proto.cookies and \
           proto.cookies.has_key(remote.restDomain) and \
           proto.cookies[remote.restDomain] != "":

            cookie = str(proto.cookies[remote.restDomain])
        else:
            cookie = None

        if type(args[0]) not in [str, unicode]:
            raise Exception(
                URI_ERROR_REMOTING,
                "Invalid type for argument 1 (expected str, was %s)" %
                type(args[0]))

        url = urlparse.urljoin(str(remote.restBaseUrl), str(args[0]))

        if not remote.usePersistentConnections:
            ## Do HTTP/POST as individual request
            ##

            headers = {
                'Content-Type': 'application/json',
                'User-Agent': RestRemoter.USER_AGENT
            }

            if cookie:
                headers['Cookie'] = cookie

            d = getPage(url=url,
                        method=method,
                        postdata=body,
                        headers=headers,
                        timeout=remote.requestTimeout,
                        connectionTimeout=remote.connectionTimeout,
                        followRedirect=remote.redirectLimit > 0)

        else:
            ## Do HTTP/POST via HTTP connection pool
            ##
            ## http://twistedmatrix.com/documents/12.1.0/web/howto/client.html
            ##

            ## avoid module level reactor import
            from twisted.web.client import Agent, RedirectAgent

            headers = {
                'Content-Type': ['application/json'],
                'User-Agent': [RestRemoter.USER_AGENT]
            }

            if cookie:
                headers['Cookie'] = [cookie]

            agent = Agent(self.reactor,
                          pool=self.httppools[remote.id],
                          connectTimeout=remote.connectionTimeout)

            if remote.redirectLimit > 0:
                agent = RedirectAgent(agent,
                                      redirectLimit=remote.redirectLimit)

            ## FIXME: honor requestTimeout
            if body:
                d = agent.request(method, url, Headers(headers),
                                  StringProducer(body))
            else:
                d = agent.request(method, url, Headers(headers))

            def onResponse(response):
                if response.code == 200:
                    finished = Deferred()
                    response.deliverBody(StringReceiver(finished))
                    return finished
                else:
                    return defer.fail("%s [%s]" %
                                      (response.code, response.phrase))

            d.addCallback(onResponse)

        ## request information provided as error detail in case of call fails
        remotingRequest = {
            'provider': 'rest',
            'rest-base-url': remote.restBaseUrl,
            'use-persistent-connections': remote.usePersistentConnections,
            'request-timeout': remote.requestTimeout,
            'connection-timeout': remote.connectionTimeout,
            'method': method
        }

        d.addCallbacks(self._onRemoteCallResult,
                       self._onRemoteCallError,
                       callbackArgs=[remotingRequest],
                       errbackArgs=[remotingRequest])

        ## FIXME!
        d.addCallback(self.onAfterRemoteCallSuccess, id)
        d.addErrback(self.onAfterRemoteCallError, id)

        return d
Beispiel #4
0
   def remoteCall(self, call):
      """
      RPC handler remoting to Ext.Direct servers. This method is usually
      registered via registerHandlerMethodForRpc on a WAMP protocol.
      """
      proto = call.proto
      uri = call.uri
      args = call.args

      ## extract extra information from RPC call handler argument
      (id, action, method, _) = call.extra

      ## get the Ext.Direct remote onto which we will forward the call
      remote = self.remotesById[id]

      ## construct the POST body
      d = {'action': action,
           'method': method,
           'data': args,
           'type': 'rpc',
           'tid': 1}
      body = json_dumps(d)

      if remote.forwardCookies and \
         proto.cookies and \
         proto.cookies.has_key(remote.routerDomain) and \
         proto.cookies[remote.routerDomain] != "":

         cookie = str(proto.cookies[remote.routerDomain])
      else:
         cookie = None

      if not remote.usePersistentConnections:
         ## Do HTTP/POST as individual request
         ##

         headers = {'Content-Type': 'application/json',
                    'User-Agent': ExtDirectRemoter.USER_AGENT}

         if cookie:
            headers['Cookie'] = cookie

         d = getPage(url = remote.routerUrl,
                     method = 'POST',
                     postdata = body,
                     headers = headers,
                     timeout = remote.requestTimeout,
                     connectionTimeout = remote.connectionTimeout,
                     followRedirect = remote.redirectLimit > 0)

      else:
         ## Do HTTP/POST via HTTP connection pool
         ##

         headers = {'Content-Type': ['application/json'],
                    'User-Agent': [ExtDirectRemoter.USER_AGENT]}

         if cookie:
            headers['Cookie'] = [cookie]

         agent = Agent(reactor,
                       pool = self.httppools[remote.id],
                       connectTimeout = remote.connectionTimeout)

         if remote.redirectLimit > 0:
            agent = RedirectAgent(agent, redirectLimit = remote.redirectLimit)

         ## FIXME: honor requestTimeout
         d = agent.request('POST',
                           remote.routerUrl,
                           Headers(headers),
                           StringProducer(body))

         def onResponse(response):
            if response.code == 200:
               finished = Deferred()
               response.deliverBody(StringReceiver(finished))
               return finished
            else:
               return defer.fail("%s [%s]" % (response.code, response.phrase))

         d.addCallback(onResponse)

      ## request information provided as error detail in case of call fails
      remotingRequest = {'provider': 'extdirect',
                         'router-url': remote.routerUrl,
                         'use-persistent-connections': remote.usePersistentConnections,
                         'request-timeout': remote.requestTimeout,
                         'connection-timeout': remote.connectionTimeout,
                         'action': action,
                         'method': method}

      d.addCallbacks(self._onRemoteCallResult,
                     self._onRemoteCallError,
                     callbackArgs = [remotingRequest],
                     errbackArgs = [remotingRequest])

      ## FIXME!
      d.addCallback(self.onAfterRemoteCallSuccess, id)
      d.addErrback(self.onAfterRemoteCallError, id)

      return d
Beispiel #5
0
   def remoteCall(self, call):
      """
      RPC handler remoting to Ext.Direct servers. This method is usually
      registered via registerHandlerMethodForRpc on a WAMP protocol.
      """
      proto = call.proto
      uri = call.uri
      args = call.args

      ## extract extra information from RPC call handler argument
      (id, action, method, _) = call.extra

      ## get the Ext.Direct remote onto which we will forward the call
      remote = self.remotesById[id]

      ## construct the POST body
      d = {'action': action,
           'method': method,
           'data': args,
           'type': 'rpc',
           'tid': 1}
      body = json_dumps(d)

      if remote.forwardCookies and \
         proto.cookies and \
         proto.cookies.has_key(remote.routerDomain) and \
         proto.cookies[remote.routerDomain] != "":

         cookie = str(proto.cookies[remote.routerDomain])
      else:
         cookie = None

      if not remote.usePersistentConnections:
         ## Do HTTP/POST as individual request
         ##

         headers = {'Content-Type': 'application/json',
                    'User-Agent': ExtDirectRemoter.USER_AGENT}

         if cookie:
            headers['Cookie'] = cookie

         d = getPage(url = remote.routerUrl,
                     method = 'POST',
                     postdata = body,
                     headers = headers,
                     timeout = remote.requestTimeout,
                     connectionTimeout = remote.connectionTimeout,
                     followRedirect = remote.redirectLimit > 0)

      else:
         ## Do HTTP/POST via HTTP connection pool
         ##

         headers = {'Content-Type': ['application/json'],
                    'User-Agent': [ExtDirectRemoter.USER_AGENT]}

         if cookie:
            headers['Cookie'] = [cookie]

         agent = Agent(self.reactor,
                       pool = self.httppools[remote.id],
                       connectTimeout = remote.connectionTimeout)

         if remote.redirectLimit > 0:
            agent = RedirectAgent(agent, redirectLimit = remote.redirectLimit)

         ## FIXME: honor requestTimeout
         d = agent.request('POST',
                           remote.routerUrl,
                           Headers(headers),
                           StringProducer(body))

         def onResponse(response):
            if response.code == 200:
               finished = Deferred()
               response.deliverBody(StringReceiver(finished))
               return finished
            else:
               return defer.fail("%s [%s]" % (response.code, response.phrase))

         d.addCallback(onResponse)

      ## request information provided as error detail in case of call fails
      remotingRequest = {'provider': 'extdirect',
                         'router-url': remote.routerUrl,
                         'use-persistent-connections': remote.usePersistentConnections,
                         'request-timeout': remote.requestTimeout,
                         'connection-timeout': remote.connectionTimeout,
                         'action': action,
                         'method': method}

      d.addCallbacks(self._onRemoteCallResult,
                     self._onRemoteCallError,
                     callbackArgs = [remotingRequest],
                     errbackArgs = [remotingRequest])

      ## FIXME!
      d.addCallback(self.onAfterRemoteCallSuccess, id)
      d.addErrback(self.onAfterRemoteCallError, id)

      return d