Exemplo n.º 1
0
    def GET(self, name=None):

        try:
                logging.info("[LayMan][GET] %s" % name)
                params = repr(web.input())
                logging.info("[LayMan][GET] Parameters: %s ... %s" %
                    (str(params)[:500], str(params)[-500:]))

                if not self.auth.authorised:
                    logging.error("[LayMan][GET] Unauthorised")
                    raise AuthError(401, "Authorisation failed. Are you logged-in?")

                code = None    # 200, 404...
                retval = None  # success: returned value. failure: error message.
                origName = name
                path = [d for d in name.split(os.path.sep) if d]

                # GET "http://localhost:8080/layman/fileman/"
                if path[0] == "fileman":

                    from fileman import FileMan
                    fm = FileMan()

                    # /fileman
                    if len(path) == 1:
                        logging.info("[LayMan][GET /fileman]")
                        (code, retval) = fm.getFiles(self.auth.getFSUserDir())

                    # /fileman/<file>
                    elif len(path) == 2:
                        (code, retval) = fm.getFile(self._getTargetFile(path[1]))

                    # /fileman/detail/<file>
                    elif len(path) == 3 and\
                        path[1] == "detail":
                        (code, retval) = fm.getFileDetails(self._getTargetFile(path[2]))

                    else:
                        (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                elif path[0] == 'publish':
                    
                    from layed import LayEd
                    le = LayEd()

                    roles = self.auth.getRoles()
                    (code,retval) = le.getLayers(roles)

                # Get the list of tables in db (or other data)
                elif path[0] == 'data':
                
                    # /data    
                    if len(path) == 1:
                        from layed import LayEd
                        le = LayEd()

                        roles = self.auth.getRoles()
                        (code,retval) = le.getData(roles)

                    elif len(path) == 2:

                        # /layed/groups
                        if path[1] == "groups":
                            (code, retval) = self.auth.getRolesStr()

                        # /layed/allgroups
                        elif path[1] == "allgroups":
                            (code, retval) = self.auth.getAllRolesStr()

                        else:
                            (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                    else:
                        (code, retval) = self._callNotSupported(restMethod="GET", call=origName)


                elif path[0] == 'layed':

                    from layed import LayEd
                    le = LayEd()

                    # /layed[?usergroup=FireBrigade]
                    if len(path) == 1:
                        """ Get the json of the layers.
                        If usergroup parameter is specified, only the layers
                        of the corresponding workspace are returned.
                        Otherwise, the layers of all groups allowed are returned
                        in proprietary json, ordered by group (i.e. workspace)
                        """
                        logging.info("[LayMan][GET /layed]")
                        inpt = web.input(usergroup=None)
                        if inpt.usergroup == None: # workspace not given, go for all
                            roles = self.auth.getRoles()
                        else: # workspace given, go for one
                            role = self.auth.getRole(inpt.usergroup)
                            roles = [role]
                        (code,retval) = le.getLayers(roles)

                    elif len(path) == 2:

                        # /layed/workspaces
                        if path[1] == "workspaces":
                            (code, retval) = le.getWorkspaces()

                        # /layed/groups
                        elif path[1] == "groups":
                            (code, retval) = self.auth.getRolesStr()

                        # /layed/allgroups
                        elif path[1] == "allgroups":
                            (code, retval) = self.auth.getAllRolesStr()

                        else:
                            (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                    elif len(path) == 3:

                        # /layed/config/<layer>?usergroup=FireBrigade
                        if path[1] == "config":
                            layerName = path[2]
                            inpt = web.input(usergroup=None)
                            gsWorkspace = self.auth.getGSWorkspace(inpt.usergroup)
                            (code, retval) = le.getLayerConfig(gsWorkspace, layerName)

                        # /layed/workspaces/<ws>
                        elif path[1] == "workspaces":
                            (code, retval) = le.getWorkspace(path[2])

                        else:
                            (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                    else:
                        (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                elif path[0] == "geoserver":
                    ws = None
                    g = None

                    from layed.gsconfig import GsConfig
                    code = 200

                    if path[1] == "style" and len(path) >= 3:
                        #if len(path) > 3:
                        #    ws = path[-2]
                        g = GsConfig()
                        retval = g.getStyle(path[-1])
                        web.header("Content-type", "text/xml")

                else:
                    (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                success = self._setReturnCode(code)
                if not success:
                    retval  = self._jsonReply(code, message=retval, success=success)
                # else: we return the returned value directly
                return retval

        except LaymanError as le:
            return self._handleLaymanError(le)

        except Exception as e:
            return self._handleException(e)
Exemplo n.º 2
0
    def GET(self, name=None):

        try:
                logging.info("[LayMan][GET] %s" % name)
                params = repr(web.input())
                logging.info("[LayMan][GET] Parameters: %s ... %s" %
                    (str(params)[:500], str(params)[-500:]))

                if not self.auth.authorised:
                    logging.error("[LayMan][GET] Unauthorised")
                    raise AuthError(401, "Authorisation failed. Are you logged-in?")

                code = None    # 200, 404...
                retval = None  # success: returned value. failure: error message.
                origName = name
                path = [d for d in name.split(os.path.sep) if d]

                # GET "http://localhost:8080/layman/files/<user>"
                if path[0] == "files" and len(path) >= 2:

                    from fileman import FileMan
                    fm = FileMan()

                    # Everyone can get only his/her own files 
                    userName = self.auth.getUserName()
                    if userName != path[1]:
                        logging.error("[LayMan][GET] %s is not authorized to get files of %s"% (userName, path[1]))
                        raise AuthError(401, "Sorry, you are not authorized to get files of %s"% path[1])

                    #     /files/<user>
                    # was /fileman
                    if len(path) == 2:
                        (code, retval) = fm.getFiles(self.auth.getFSUserDir())

                    #     /files/<user>/<file>
                    # was /fileman/<file> 
                    elif len(path) == 3:
                        (code, retval) = fm.getFile(self._getTargetFile(path[2]))

                    #     /files/<user>/<file>/details
                    # was /fileman/detail/<file>
                    elif len(path) == 4 and\
                        path[3] == "details":
                        (code, retval) = fm.getFileDetails(self._getTargetFile(path[2]))

                    else:
                        (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                # Get the list of tables in db (or other data)
                elif path[0] == 'data':
                
                    # /data    
                    if len(path) == 1:
                        from layed import LayEd
                        le = LayEd()

                        userName = self.auth.getUserName()
                        roles = self.auth.getRoles()
                        (code,retval) = le.getData(roles, userName)

                    else:
                        (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                # /sync
                elif path[0] == "sync" and len(path) == 2:

                    # /sync/data
                    if path[1] == "data":
                        from layed import LayEd
                        le = LayEd()    
                        roles = self.auth.getRoles()
                        (code, retval) = le.syncDataPad(roles)

                    # /sync/layers
                    elif path[1] == "layers":
                        from layed import LayEd
                        le = LayEd()
                        roles = self.auth.getRoles()
                        (code, retval) = le.syncLayerPad(roles)

                    else:
                        (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                # /groups
                elif path[0] == "groups" and len(path) == 2:
                        # /groups/mine
                        if path[1] == "mine":
                            (code, retval) = self.auth.getRolesStr()

                        # /groups/all
                        elif path[1] == "all":
                            (code, retval) = self.auth.getAllRolesStr()

                        else:
                            (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                # Get the list of ckan packages
                elif path[0] == 'ckan':

                    # /ckan
                    if len(path) == 1:
                        from layed import LayEd
                        le = LayEd()

                        inpt = web.input(limit="20", start="0", ckanUrl=None)
                        userName = self.auth.getUserName()
                        roles = self.auth.getRoles()

                        (code,retval) = le.getCkanResources(inpt.limit, inpt.start, inpt.ckanUrl) 
                        #(code,retval) = le.getCkanPackages(roles, userName, inpt.limit, inpt.start, inpt.ckanUrl) 

                    else:
                        (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                elif path[0] == 'layers':

                    from layed import LayEd
                    le = LayEd()

                    #     /layers
                    # was /layed[?usergroup=FireBrigade]
                    if len(path) == 1:
                        """ Get the json of the layers.
                        The layers of all groups allowed are returned
                        in json, ordered by group (i.e. workspace)
                        """
                        roles = self.auth.getRoles()
                        userName = self.auth.getUserName()
                        (code,retval) = le.getLayers(roles, userName)

                    #     /layers/<group>
                    # was /layed[?usergroup=FireBrigade]
                    elif len(path) == 2:
                        role = self.auth.getRole(path[1])
                        if role != path[1]:
                            logging.error("[LayMan][GET] Not authorized to get layers of %s group"% path[1])
                            raise AuthError(401, "Sorry, you are not authorized to get the layers of %s group"% path[1])
                        roles = [role]
                        userName = self.auth.getUserName()
                        (code,retval) = le.getLayers(roles, userName)

                    #     /layers/<group>/<layer>
                    # was /layed/config/<layer>?usergroup=FireBrigade
                    elif len(path) == 3:
                        gsWorkspace = self.auth.getGSWorkspace(path[1])
                        if gsWorkspace != path[1]:
                            logging.error("[LayMan][GET] Not authorized to get layer of %s group"% path[1])
                            raise AuthError(401, "Sorry, you are not authorized to get the details of a layer from the %s group"% path[1])
                        layerName = path[2]
                        (code, retval) = le.getLayerDetails(gsWorkspace, layerName)

                    else:
                        (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                elif path[0] == "geoserver":
                    ws = None
                    g = None

                    from layed.gsconfig import GsConfig
                    code = 200

                    if path[1] == "style" and len(path) >= 3:
                        #if len(path) > 3:
                        #    ws = path[-2]
                        g = GsConfig()
                        retval = g.getStyle(path[-1])
                        web.header("Content-type", "text/xml")

                else:
                    (code, retval) = self._callNotSupported(restMethod="GET", call=origName)

                success = self._setReturnCode(code)
                if not success:
                    retval  = self._jsonReply(code, message=retval, success=success)
                # else: we return the returned value directly
                return retval

        except LaymanError as le:
            return self._handleLaymanError(le)

        except Exception as e:
            return self._handleException(e)