Beispiel #1
0
    def POST(self, *args, **kwargs):
        cherrypy.response.status = 201

        if len(args) != 1:
            return rest_error(400, 'Invalid arguments. Found %d'
                                   ' there should be one.')
        self.debug('REST POST %s' % kwargs)
        name = args[0]
        metadata = kwargs.get('metadata')
        description = kwargs.get('description', '')
        visible = kwargs.get('visible', True)
        imagefile = kwargs.get('image', None)
        splink = kwargs.get('splink', '')

        obj = self._site[FACILITY].available[self.parent.plugin_name]
        try:
            spc = ServiceProviderCreator(obj)
            sp = spc.create_from_buffer(name, metadata, description,
                                        visible, imagefile, splink)
        except (InvalidProviderId, ServerAddProviderFailedError) as e:
            self.debug(repr(e))
            return rest_error(400, str(e))
        except Exception, e:  # pylint: disable=broad-except
            self.debug(repr(e))
            return rest_error(500, "Failed to create Service Provider")
Beispiel #2
0
    def POST(self, *args, **kwargs):
        cherrypy.response.status = 201

        if len(args) != 1:
            return rest_error(
                400, 'Invalid arguments. Found %d'
                ' there should be one.')
        self.debug('REST POST %s' % kwargs)
        name = args[0]
        metadata = kwargs.get('metadata')
        description = kwargs.get('description', '')
        visible = kwargs.get('visible', True)
        imagefile = kwargs.get('image', None)
        splink = kwargs.get('splink', '')

        obj = self._site[FACILITY].available[self.parent.plugin_name]
        try:
            spc = ServiceProviderCreator(obj)
            sp = spc.create_from_buffer(name, metadata, description, visible,
                                        imagefile, splink)
        except (InvalidProviderId, ServerAddProviderFailedError) as e:
            self.debug(repr(e))
            return rest_error(400, str(e))
        except Exception as e:  # pylint: disable=broad-except
            self.debug(repr(e))
            return rest_error(500, "Failed to create Service Provider")

        obj.admin.add_sp(name, sp)

        # Added. Now fetch and return the SP data
        return self._get_sp(name)
Beispiel #3
0
    def POST(self, *args, **kwargs):

        if self.user.is_admin:
            # TODO: allow authenticated user to create SPs on their own
            #       set the owner in that case
            name = None
            meta = None
            description = None
            splink = None
            visible = False
            imagefile = None
            if 'content-type' not in cherrypy.request.headers:
                self.debug("Invalid request, missing content-type")
                message = "Malformed request"
                message_type = ADMIN_STATUS_ERROR
                return self.form_new(message, message_type)
            ctype = cherrypy.request.headers['content-type'].split(';')[0]
            if ctype != 'multipart/form-data':
                self.debug("Invalid form type (%s), trying to cope" %
                           (cherrypy.request.content_type, ))
            for key, value in kwargs.iteritems():
                if key == 'name':
                    name = value
                elif key == 'description':
                    description = value
                elif key == 'splink':
                    # pylint: disable=unused-variable
                    (scheme, netloc, path, params, query,
                     frag) = urlparse(value)
                    # minimum URL validation
                    if (scheme not in ['http', 'https'] or not netloc):
                        message = "Invalid URL for Service Provider link"
                        message_type = ADMIN_STATUS_ERROR
                        return self.form_new(message, message_type)
                    splink = value
                elif key == 'portalvisible' and value.lower() == 'on':
                    visible = True
                elif key == 'imagefile':
                    if hasattr(value, 'content_type'):
                        imagefile = value.fullvalue()
                        if len(imagefile) == 0:
                            imagefile = None
                        else:
                            imagefile = base64.b64encode(imagefile)
                    else:
                        self.debug("Invalid format for 'imagefile'")
                elif key == 'metatext':
                    if len(value) > 0:
                        meta = value
                elif key == 'metafile':
                    if hasattr(value, 'content_type'):
                        meta = value.fullvalue()
                    else:
                        self.debug("Invalid format for 'meta'")
                elif key == 'metaurl':
                    if len(value) > 0:
                        try:
                            r = requests.get(value)
                            r.raise_for_status()
                            meta = r.content
                        except Exception as e:  # pylint: disable=broad-except
                            self.debug("Failed to fetch metadata: " + repr(e))
                            message = "Failed to fetch metadata: " + repr(e)
                            message_type = ADMIN_STATUS_ERROR
                            return self.form_new(message, message_type)

            if name and meta:
                try:
                    spc = ServiceProviderCreator(self.parent.cfg)
                    sp = spc.create_from_buffer(name, meta, description,
                                                visible, imagefile, splink)
                    sp_page = self.parent.add_sp(name, sp)
                    message = "SP Successfully added"
                    message_type = ADMIN_STATUS_OK
                    return sp_page.root_with_msg(message, message_type)
                except InvalidProviderId as e:
                    message = str(e)
                    message_type = ADMIN_STATUS_ERROR
                except Exception as e:  # pylint: disable=broad-except
                    self.debug(repr(e))
                    message = "Failed to create Service Provider!"
                    message_type = ADMIN_STATUS_ERROR
            else:
                message = "A name and a metadata file must be provided"
                message_type = ADMIN_STATUS_ERROR
        else:
            message = "Unauthorized"
            message_type = ADMIN_STATUS_ERROR

        return self.form_new(message, message_type)
Beispiel #4
0
    def POST(self, *args, **kwargs):

        if self.user.is_admin:
            # TODO: allow authenticated user to create SPs on their own
            #       set the owner in that case
            name = None
            meta = None
            description = None
            splink = None
            visible = False
            imagefile = None
            if 'content-type' not in cherrypy.request.headers:
                self.debug("Invalid request, missing content-type")
                message = "Malformed request"
                message_type = ADMIN_STATUS_ERROR
                return self.form_new(message, message_type)
            ctype = cherrypy.request.headers['content-type'].split(';')[0]
            if ctype != 'multipart/form-data':
                self.debug("Invalid form type (%s), trying to cope" % (
                           cherrypy.request.content_type,))
            for key, value in kwargs.iteritems():
                if key == 'name':
                    name = value
                elif key == 'description':
                    description = value
                elif key == 'splink':
                    # pylint: disable=unused-variable
                    (scheme, netloc, path, params, query, frag) = urlparse(
                        value
                    )
                    # minimum URL validation
                    if (scheme not in ['http', 'https'] or not netloc):
                        message = "Invalid URL for Service Provider link"
                        message_type = ADMIN_STATUS_ERROR
                        return self.form_new(message, message_type)
                    splink = value
                elif key == 'portalvisible' and value.lower() == 'on':
                    visible = True
                elif key == 'imagefile':
                    if hasattr(value, 'content_type'):
                        imagefile = value.fullvalue()
                        if len(imagefile) == 0:
                            imagefile = None
                        else:
                            imagefile = base64.b64encode(imagefile)
                    else:
                        self.debug("Invalid format for 'imagefile'")
                elif key == 'metatext':
                    if len(value) > 0:
                        meta = value
                elif key == 'metafile':
                    if hasattr(value, 'content_type'):
                        meta = value.fullvalue()
                    else:
                        self.debug("Invalid format for 'meta'")
                elif key == 'metaurl':
                    if len(value) > 0:
                        try:
                            r = requests.get(value)
                            r.raise_for_status()
                            meta = r.content
                        except Exception, e:  # pylint: disable=broad-except
                            self.debug("Failed to fetch metadata: " + repr(e))
                            message = "Failed to fetch metadata: " + repr(e)
                            message_type = ADMIN_STATUS_ERROR
                            return self.form_new(message, message_type)

            if name and meta:
                try:
                    spc = ServiceProviderCreator(self.parent.cfg)
                    sp = spc.create_from_buffer(name, meta, description,
                                                visible, imagefile, splink)
                    sp_page = self.parent.add_sp(name, sp)
                    message = "SP Successfully added"
                    message_type = ADMIN_STATUS_OK
                    return sp_page.root_with_msg(message, message_type)
                except InvalidProviderId, e:
                    message = str(e)
                    message_type = ADMIN_STATUS_ERROR
                except Exception, e:  # pylint: disable=broad-except
                    self.debug(repr(e))
                    message = "Failed to create Service Provider!"
                    message_type = ADMIN_STATUS_ERROR