示例#1
0
def _parse_ac_request(request):

    fileURL = None
    file_contents = None
    content_type = get_content_type(request)[0]

    data = parse_json_request(request)

    if 'url' not in data:
        return build_error_response(request, 400, _('Missing widget URL'))

    fileURL = data.get('url')
    id_4CaaSt = data.get('4CaaStID')

    if id_4CaaSt is None:
        return build_error_response(request, 400, _('Missing 4CaaStID'))

    if not isinstance(id_4CaaSt, string_types) or id_4CaaSt.strip() == '':
        return build_error_response(request, 400, _('Invalid 4CaaStID'))

    try:
        downloaded_file = download_http_content(fileURL)
    except:
        return build_error_response(
            request, 409,
            _('Mashable application component could not be downloaded'))

    downloaded_file = StringIO(downloaded_file)
    file_contents = WgtFile(downloaded_file)

    # Create a custom version of the resource
    template = TemplateParser(file_contents.get_template())
    template_info = template.get_resource_info()
    template_info['name'] += '@' + id_4CaaSt

    for pref_name, pref_value in six.iteritems(data.get('preferences', {})):
        for widget_pref_index, widget_pref in enumerate(
                template_info['preferences']):
            if widget_pref['name'] == pref_name:
                template_info['preferences'][widget_pref_index][
                    'readonly'] = True
                template_info['preferences'][widget_pref_index][
                    'value'] = pref_value
                break

    # Write a new Wgt file
    new_file = StringIO()
    zin = zipfile.ZipFile(downloaded_file, 'r')
    zout = zipfile.ZipFile(new_file, 'w')
    zout.writestr('config.xml', write_rdf_description(template_info))
    for item in zin.infolist():
        if item.filename == 'config.xml':
            continue
        zout.writestr(item, zin.read(item.filename))
    zin.close()
    zout.close()

    file_contents = WgtFile(new_file)

    return id_4CaaSt, file_contents, fileURL
示例#2
0
def _parse_ac_request(request):

    fileURL = None
    file_contents = None
    content_type = get_content_type(request)[0]

    try:
        data = json.loads(request.body)
    except Exception as e:
        msg = _("malformed json data: %s") % unicode(e)
        return build_error_response(request, 400, msg)

    if 'url' not in data:
        return build_error_response(request, 400, _('Missing widget URL'))

    fileURL = data.get('url')
    id_4CaaSt = data.get('4CaaStID')

    if id_4CaaSt is None:
        return build_error_response(request, 400, _('Missing 4CaaStID'))

    if not isinstance(id_4CaaSt, string_types) or id_4CaaSt.strip() == '':
        return build_error_response(request, 400, _('Invalid 4CaaStID'))

    try:
        downloaded_file = download_http_content(fileURL)
    except:
        return build_error_response(request, 409, _('Mashable application component could not be downloaded'))

    downloaded_file = StringIO(downloaded_file)
    file_contents = WgtFile(downloaded_file)

    # Create a custom version of the resource
    template = TemplateParser(file_contents.get_template())
    template_info = template.get_resource_info()
    template_info['name'] += '@' + id_4CaaSt

    for pref_name, pref_value in six.iteritems(data.get('preferences', {})):
        for widget_pref_index, widget_pref in enumerate(template_info['preferences']):
            if widget_pref['name'] == pref_name:
                template_info['preferences'][widget_pref_index]['readonly'] = True
                template_info['preferences'][widget_pref_index]['value'] = pref_value
                break

    # Write a new Wgt file
    new_file = StringIO()
    zin = zipfile.ZipFile(downloaded_file, 'r')
    zout = zipfile.ZipFile(new_file, 'w')
    zout.writestr('config.xml', write_rdf_description(template_info))
    for item in zin.infolist():
        if item.filename == 'config.xml':
            continue
        zout.writestr(item, zin.read(item.filename))
    zin.close()
    zout.close()

    file_contents = WgtFile(new_file)

    return id_4CaaSt, file_contents, fileURL
示例#3
0
def _parse_ac_request(request):

    fileURL = None
    file_contents = None
    content_type = get_content_type(request)[0]

    try:
        data = json.loads(request.raw_post_data)
    except Exception, e:
        msg = _("malformed json data: %s") % unicode(e)
        return build_error_response(request, 400, msg)
示例#4
0
文件: views.py 项目: ciniguez/FIREWA
    def create(self, request):

        content_type = get_content_type(request)[0]
        if content_type == 'application/json':
            try:
                data = simplejson.loads(request.raw_post_data)
            except Exception, e:
                msg = _("malformed json data: %s") % unicode(e)
                return build_error_response(request, 400, msg)

            workspace_name = data.get('name', '').strip()
            mashup_id = data.get('mashup', '')
            dry_run = data.get('', 'false').lower() == 'true'
示例#5
0
    def create(self, request, workspace_id):

        content_type = get_content_type(request)[0]
        image_file = None
        if content_type == 'application/json':
            received_json = request.raw_post_data
        else:
            received_json = request.POST['json']
            image_file = request.FILES.get('image', None)

        try:
            options = json.loads(received_json)
        except ValueError, e:
            msg = _("malformed json data: %s") % unicode(e)
            return build_error_response(request, 400, msg)
示例#6
0
    def create(self, request, workspace_id):

        content_type = get_content_type(request)[0]
        image_file = None
        if content_type == 'application/json':
            received_json = request.body
        else:
            received_json = request.POST['json']
            image_file = request.FILES.get('image', None)

        try:
            options = json.loads(received_json)
        except ValueError as e:
            msg = _("malformed json data: %s") % unicode(e)
            return build_error_response(request, 400, msg)

        missing_fields = check_json_fields(options, ('name', 'vendor', 'version', 'email'))
        if len(missing_fields) > 0:
            return build_error_response(request, 400, _('Malformed JSON. The following field(s) are missing: %(fields)s.') % {'fields': missing_fields})

        if not is_valid_vendor(options['vendor']):
            return build_error_response(request, 400, _('Invalid vendor'))

        if not is_valid_name(options['name']):
            return build_error_response(request, 400, _('Invalid name'))

        if not is_valid_version(options['version']):
            return build_error_response(request, 400, _('Invalid version number'))

        workspace = get_object_or_404(Workspace, id=workspace_id)
        if image_file is not None:
            image_filename = 'images/catalogue' + os.path.splitext(image_file.name)[1]
            options['image'] = image_filename
        description = build_rdf_template_from_workspace(options, workspace, request.user)

        f = StringIO()
        zf = zipfile.ZipFile(f, 'w')
        zf.writestr('config.xml', bytes(description.serialize(format='pretty-xml')))
        if image_file is not None:
            zf.writestr(image_filename, image_file.read())
        zf.close()
        wgt_file = WgtFile(f)

        market_managers = get_market_managers(request.user)
        try:
            market_managers['local'].publish(None, wgt_file, request.user, options, request)
        except Exception, e:
            return build_error_response(request, 502, unicode(e))
示例#7
0
    def update(self, request, vendor, name, version):

        formats = ('application/json', 'application/xml')

        if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
            mimetype = 'application/json; charset=utf-8'
        else:
            mimetype = mimeparser.best_match(formats, request.META.get('HTTP_ACCEPT', ''))

        # Get the vote from the request
        content_type = get_content_type(request)[0]
        if content_type == 'application/json':
            try:
                vote = json.loads(request.raw_post_data)['vote']
            except ValueError, e:
                msg = _("malformed json data: %s") % unicode(e)
                return build_error_response(request, 400, msg)
示例#8
0
文件: views.py 项目: ciniguez/FIREWA
def _parse_ac_request(request):

    id_4CaaSt = request.GET.get('message', None)
    fileURL = None
    file_contents = None
    content_type = get_content_type(request)[0]

    if content_type == 'multipart/form-data':

        if not 'file' in request.FILES:
            return build_error_response(request, 400, _('Missing widget file'))

        downloaded_file = request.FILES['file']
        file_contents = WgtFile(downloaded_file)

    elif content_type == 'application/octet-stream':

        downloaded_file = StringIO(request.raw_post_content)
        file_contents = WgtFile(downloaded_file)

    else:

        if content_type == 'application/json':

            try:
                data = simplejson.loads(request.raw_post_data)
            except Exception, e:
                msg = _("malformed json data: %s") % unicode(e)
                return build_error_response(request, 400, msg)

            if 'url' not in data:
                return build_error_response(request, 400, _('Missing widget URL'))

            fileURL = data.get('url')
            if 'id_4caast' in data:
                id_4CaaSt = data.get('id_4caast')

        elif content_type == 'application/x-www-form-urlencoded':

            if 'url' not in request.POST:
                return build_error_response(request, 400, _('Missing widget URL'))

            fileURL = request.POST['url']
示例#9
0
文件: views.py 项目: ciniguez/FIREWA
    def create(self, request):

        force_create = False
        templateURL = None
        file_contents = None
        content_type = get_content_type(request)[0]
        if content_type == 'multipart/form-data':
            packaged = True
            force_create = request.POST.get('force_create', False) == 'true'
            if not 'file' in request.FILES:
                return build_error_response(request, 400, _('Missing file to upload'))

            downloaded_file = request.FILES['file']
            try:
                file_contents = WgtFile(downloaded_file)
            except:
                return build_error_response(request, 400, _('Bad resource file'))

        elif content_type == 'application/octet-stream':

            packaged = True
            downloaded_file = StringIO(request.raw_post_data)
            try:
                file_contents = WgtFile(downloaded_file)
            except:
                return build_error_response(request, 400, _('Bad resource file'))
        else:

            market_endpoint = None

            if content_type == 'application/json':
                try:
                    data = simplejson.loads(request.raw_post_data)
                except Exception, e:
                    msg = _("malformed json data: %s") % unicode(e)
                    return build_error_response(request, 400, msg)

                force_create = data.get('force_create', False)
                packaged = data.get('packaged', False)
                templateURL = data.get('template_uri')
                market_endpoint = data.get('market_endpoint', None)

            else:
示例#10
0
文件: views.py 项目: ciniguez/FIREWA
    def create(self, request, workspace_id):

        import wirecloud.catalogue.utils as catalogue_utils

        content_type = get_content_type(request)[0]
        image_file = None
        smartphoneimage_file = None
        extra_files = []

        if content_type == 'application/json':
            received_json = request.body.decode('utf-8')
        else:
            received_json = request.POST['json']
            image_file = request.FILES.get('image', None)
            smartphoneimage_file = request.FILES.get('smartphoneimage', None)

        try:
            options = json.loads(received_json)
        except ValueError as e:
            msg = _("malformed json data: %s") % e
            return build_error_response(request, 400, msg)

        missing_fields = check_json_fields(options, ('name', 'vendor', 'version'))
        if len(missing_fields) > 0:
            return build_error_response(request, 400, _('Malformed JSON. The following field(s) are missing: %(fields)s.') % {'fields': missing_fields})

        if not is_valid_vendor(options['vendor']):
            return build_error_response(request, 400, _('Invalid vendor'))

        if not is_valid_name(options['name']):
            return build_error_response(request, 400, _('Invalid name'))

        if not is_valid_version(options['version']):
            return build_error_response(request, 400, _('Invalid version number'))

        workspace = get_object_or_404(Workspace, id=workspace_id)
        if image_file is not None:
            options['image'] = 'images/catalogue' + os.path.splitext(image_file.name)[1]
            extra_files.append((options['image'], image_file))
        if smartphoneimage_file is not None:
            options['smartphoneimage'] = 'images/smartphone' + os.path.splitext(smartphoneimage_file.name)[1]
            extra_files.append((options['smartphoneimage'], smartphoneimage_file))
        if 'longdescription' in options:
            extra_files.append(('DESCRIPTION.md', BytesIO(options['longdescription'].encode('utf-8'))))
            options['longdescription'] = 'DESCRIPTION.md'

        description = build_xml_template_from_workspace(options, workspace, request.user)

        # Build mashup wgt file
        f = BytesIO()
        zf = zipfile.ZipFile(f, 'w')
        zf.writestr('config.xml', description.encode('utf-8'))
        for filename, extra_file in extra_files:
            zf.writestr(filename, extra_file.read())
        for resource_info in options['embedded']:
            (vendor, name, version) = (resource_info['vendor'], resource_info['name'], resource_info['version'])
            resource = CatalogueResource.objects.get(vendor=vendor, short_name=name, version=version)
            base_dir = catalogue_utils.wgt_deployer.get_base_dir(vendor, name, version)
            zf.write(os.path.join(base_dir, resource.template_uri), resource_info['src'])
        zf.close()
        wgt_file = WgtFile(f)

        resource = get_local_catalogue().publish(None, wgt_file, request.user, options, request)

        return HttpResponse(json.dumps(resource.get_processed_info(request), ensure_ascii=False), status=201, content_type='application/json; charset=utf-8')
示例#11
0
    def create(self, request):

        status_code = 201
        force_create = False
        install_embedded_resources = False
        templateURL = None
        file_contents = None
        content_type = get_content_type(request)[0]
        if content_type == 'multipart/form-data':
            force_create = request.POST.get('force_create', 'false').strip().lower() == 'true'
            install_embedded_resources = request.POST.get('install_embedded_resources', 'false').strip().lower() == 'true'
            if not 'file' in request.FILES:
                return build_error_response(request, 400, _('Missing component file in the request'))

            downloaded_file = request.FILES['file']
            try:
                file_contents = WgtFile(downloaded_file)
            except zipfile.BadZipfile:
                return build_error_response(request, 400, _('The uploaded file is not a zip file'))

        elif content_type == 'application/octet-stream':

            downloaded_file = BytesIO(request.body)
            try:
                file_contents = WgtFile(downloaded_file)
            except zipfile.BadZipfile:
                return build_error_response(request, 400, _('The uploaded file is not a zip file'))

            force_create = request.GET.get('force_create', 'false').strip().lower() == 'true'
            install_embedded_resources = request.GET.get('install_embedded_resources', 'false').strip().lower() == 'true'
        else:  # if content_type == 'application/json'

            market_endpoint = None

            data = parse_json_request(request)

            install_embedded_resources = normalize_boolean_param(request, 'install_embedded_resources', data.get('install_embedded_resources', False))
            force_create = data.get('force_create', False)
            templateURL = data.get('url')
            market_endpoint = data.get('market_endpoint', None)

            if market_endpoint is not None:

                if 'name' not in market_endpoint:
                    msg = _('Missing market name')
                    return build_error_response(request, 400, msg)

                market_id = market_endpoint['name']
                market_managers = get_market_managers(request.user)
                if market_id not in market_managers:
                    return build_error_response(request, 409, _('Unknown market: %s') % market_id)

                market_manager = market_managers[market_id]
                downloaded_file = market_manager.download_resource(request.user, templateURL, market_endpoint)

            else:

                try:
                    downloaded_file = download_http_content(templateURL)
                except:
                    return build_error_response(request, 409, _('Content cannot be downloaded from the specified url'))

            try:
                downloaded_file = BytesIO(downloaded_file)
                file_contents = WgtFile(downloaded_file)

            except zipfile.BadZipfile:

                return build_error_response(request, 400, _('The file downloaded from the marketplace is not a zip file'))

        try:

            added, resource = install_resource_to_user(request.user, file_contents=file_contents, templateURL=templateURL)

            if not added and force_create:
                return build_error_response(request, 409, _('Resource already exists'))
            elif not added:
                status_code = 200

        except zipfile.BadZipfile as e:

            return build_error_response(request, 400, _('The uploaded file is not a valid zip file'), details="{}".format(e))

        except OSError as e:

            if e.errno == errno.EACCES:
                return build_error_response(request, 500, _('Error writing the resource into the filesystem. Please, contact the server administrator.'))
            else:
                raise

        except TemplateParseException as e:

            msg = "Error parsing config.xml descriptor file: %s" % e

            details = "%s" % e
            return build_error_response(request, 400, msg, details=details)

        except (InvalidContents, UnsupportedFeature) as e:

            details = e.details if hasattr(e, 'details') else None
            return build_error_response(request, 400, e, details=six.text_type(details))

        if install_embedded_resources:

            info = {
                'resource_details': resource.get_processed_info(request),
                'extra_resources': []
            }
            if resource.resource_type() == 'mashup':
                resource_info = resource.get_processed_info(process_urls=False)
                for embedded_resource in resource_info['embedded']:
                    resource_file = BytesIO(file_contents.read(embedded_resource['src']))

                    extra_resource_contents = WgtFile(resource_file)
                    extra_resource_added, extra_resource = install_resource_to_user(request.user, file_contents=extra_resource_contents, raise_conflicts=False)
                    if extra_resource_added:
                        info['extra_resources'].append(extra_resource.get_processed_info(request))

            return HttpResponse(json.dumps(info), status=status_code, content_type='application/json; charset=UTF-8')

        else:

            return HttpResponse(json.dumps(resource.get_processed_info(request)), status=status_code, content_type='application/json; charset=UTF-8')
示例#12
0
文件: utils.py 项目: Mognom/wirecloud
 def test_get_content_type(self):
     request = self._prepare_request_mock()
     request.META['CONTENT_TYPE'] = 'application/json'
     self.assertEqual(get_content_type(request), ('application/json', {}))
示例#13
0
文件: utils.py 项目: Mognom/wirecloud
 def test_get_content_type_no_provided(self):
     request = self._prepare_request_mock()
     self.assertEqual(get_content_type(request), ('', {}))
示例#14
0
    def create(self, request):

        force_create = False
        install_embedded_resources = False
        templateURL = None
        file_contents = None
        content_type = get_content_type(request)[0]
        if content_type == "multipart/form-data":
            force_create = request.POST.get("force_create", "false").strip().lower() == "true"
            install_embedded_resources = (
                request.POST.get("install_embedded_resources", "false").strip().lower() == "true"
            )
            if not "file" in request.FILES:
                return build_error_response(request, 400, _("Missing component file in the request"))

            downloaded_file = request.FILES["file"]
            try:
                file_contents = WgtFile(downloaded_file)
            except zipfile.BadZipfile:
                return build_error_response(request, 400, _("The uploaded file is not a zip file"))

        elif content_type == "application/octet-stream":

            downloaded_file = BytesIO(request.body)
            try:
                file_contents = WgtFile(downloaded_file)
            except zipfile.BadZipfile:
                return build_error_response(request, 400, _("The uploaded file is not a zip file"))

            force_create = request.GET.get("force_create", "false").strip().lower() == "true"
            install_embedded_resources = (
                request.GET.get("install_embedded_resources", "false").strip().lower() == "true"
            )
        else:

            market_endpoint = None

            if content_type == "application/json":
                try:
                    data = json.loads(request.body)
                except ValueError as e:
                    msg = _("malformed json data: %s") % unicode(e)
                    return build_error_response(request, 400, msg)

                install_embedded_resources = normalize_boolean_param(
                    "install_embedded_resources", data.get("install_embedded_resources", False)
                )
                force_create = data.get("force_create", False)
                templateURL = data.get("template_uri")
                market_endpoint = data.get("market_endpoint", None)

            else:
                force_create = request.POST.get("force_create", False) == "true"
                if "url" in request.POST:
                    templateURL = request.POST["url"]

            if market_endpoint is not None:

                if "name" not in market_endpoint:
                    msg = _("Missing market name")
                    return build_error_response(request, 400, msg)

                market_id = market_endpoint["name"]
                market_managers = get_market_managers(request.user)
                if market_id not in market_managers:
                    return build_error_response(request, 409, _("Unknown market: %s") % market_id)

                market_manager = market_managers[market_id]
                downloaded_file = market_manager.download_resource(request.user, templateURL, market_endpoint)

            else:

                try:
                    downloaded_file = download_http_content(templateURL)
                except:
                    return build_error_response(request, 409, _("Content cannot be downloaded from the marketplace"))

            try:
                downloaded_file = BytesIO(downloaded_file)
                file_contents = WgtFile(downloaded_file)

            except zipfile.BadZipfile:

                return build_error_response(
                    request, 400, _("The file downloaded from the marketplace is not a zip file")
                )

        try:

            resource = install_resource_to_user(
                request.user, file_contents=file_contents, templateURL=templateURL, raise_conflicts=force_create
            )

        except OSError as e:

            if e.errno == errno.EACCES:
                return build_error_response(
                    request,
                    500,
                    _("Error writing the resource into the filesystem. Please, contact the server administrator."),
                )
            else:
                raise

        except TemplateParseException as e:

            msg = "Error parsing config.xml descriptor file: %s" % e

            details = "%s" % e
            return build_error_response(request, 400, msg, details=details)

        except (InvalidContents, UnsupportedFeature) as e:

            return build_error_response(request, 400, unicode(e))

        except IntegrityError:

            return build_error_response(request, 409, _("Resource already exists"))

        if install_embedded_resources:

            info = {"resource_details": resource.get_processed_info(request), "extra_resources": []}
            if resource.resource_type() == "mashup":
                resource_info = resource.get_processed_info(process_urls=False)
                for embedded_resource in resource_info["embedded"]:
                    if embedded_resource["src"].startswith("https://"):
                        resource_file = download_http_content(embedded_resource["src"])
                    else:
                        resource_file = BytesIO(file_contents.read(embedded_resource["src"]))

                    extra_resource_contents = WgtFile(resource_file)
                    extra_resource = install_resource_to_user(
                        request.user, file_contents=extra_resource_contents, raise_conflicts=False
                    )
                    info["extra_resources"].append(extra_resource.get_processed_info(request))

            return HttpResponse(json.dumps(info), status=201, content_type="application/json; charset=UTF-8")

        else:

            return HttpResponse(
                json.dumps(resource.get_processed_info(request)),
                status=201,
                content_type="application/json; charset=UTF-8",
            )
示例#15
0
    def create(self, request):

        force_create = False
        templateURL = None
        file_contents = None
        content_type = get_content_type(request)[0]
        if content_type == 'multipart/form-data':
            packaged = True
            force_create = request.POST.get('force_create', False) == 'true'
            if not 'file' in request.FILES:
                return build_error_response(request, 400, _('Missing file to upload'))

            downloaded_file = request.FILES['file']
            try:
                file_contents = WgtFile(downloaded_file)
            except:
                return build_error_response(request, 400, _('Bad resource file'))

        elif content_type == 'application/octet-stream':

            packaged = True
            downloaded_file = StringIO(request.body)
            try:
                file_contents = WgtFile(downloaded_file)
            except:
                return build_error_response(request, 400, _('Bad resource file'))
        else:

            market_endpoint = None

            if content_type == 'application/json':
                try:
                    data = json.loads(request.body)
                except ValueError as e:
                    msg = _("malformed json data: %s") % unicode(e)
                    return build_error_response(request, 400, msg)

                force_create = data.get('force_create', False)
                packaged = data.get('packaged', False)
                templateURL = data.get('template_uri')
                market_endpoint = data.get('market_endpoint', None)

            else:
                force_create = request.POST.get('force_create', False) == 'true'
                packaged = request.POST.get('packaged', False) == 'true'
                if 'url' in request.POST:
                    templateURL = request.POST['url']
                elif 'template_uri' in request.POST:
                    templateURL = request.POST['template_uri']

            if market_endpoint is not None:

                if 'name' not in market_endpoint:
                    msg = _('Missing market name')
                    return build_error_response(request, 400, msg)

                market_id = market_endpoint['name']
                market_managers = get_market_managers(request.user)
                if market_id not in market_managers:
                    return build_error_response(request, 409, _('Unknown market: %s') % market_id)

                market_manager = market_managers[market_id]
                downloaded_file = market_manager.download_resource(request.user, templateURL, market_endpoint)

            else:

                try:
                    downloaded_file = download_http_content(templateURL)
                except:
                    return build_error_response(request, 409, _('Content cannot be downloaded'))

            if packaged:

                try:
                    downloaded_file = StringIO(downloaded_file)
                    file_contents = WgtFile(downloaded_file)

                except zipfile.BadZipfile as e:

                    return build_error_response(request, 400, unicode(e))

            else:
                file_contents = downloaded_file

        try:

            resource = install_resource_to_user(request.user, file_contents=file_contents, templateURL=templateURL, packaged=packaged, raise_conflicts=force_create)

        except OSError as e:

            if e.errno == errno.EACCES:
                return build_error_response(request, 500, _('Error writing the resource into the filesystem. Please, contact the server administrator.'))
            else:
                raise

        except TemplateParseException as e:

            if packaged:
                msg = "Error parsing config.xml descriptor file: %s" % e
            else:
                msg = "Error parsing resource descriptor from the providen URL: %s" % e

            return build_error_response(request, 400, msg)

        except InvalidContents as e:

            return build_error_response(request, 400, unicode(e))

        except IntegrityError:

            return build_error_response(request, 409, _('Resource already exists'))

        return HttpResponse(json.dumps(resource.get_processed_info(request)), status=201, content_type='application/json; charset=UTF-8')
示例#16
0
    def create(self, request):

        status_code = 201
        force_create = False
        install_embedded_resources = False
        templateURL = None
        file_contents = None
        content_type = get_content_type(request)[0]
        if content_type == 'multipart/form-data':
            force_create = request.POST.get('force_create', 'false').strip().lower() == 'true'
            public = request.POST.get('public', 'false').strip().lower() == 'true'
            install_embedded_resources = request.POST.get('install_embedded_resources', 'false').strip().lower() == 'true'
            if 'file' not in request.FILES:
                return build_error_response(request, 400, _('Missing component file in the request'))

            downloaded_file = request.FILES['file']
            try:
                file_contents = WgtFile(downloaded_file)
            except zipfile.BadZipfile:
                return build_error_response(request, 400, _('The uploaded file is not a zip file'))

        elif content_type == 'application/octet-stream':

            downloaded_file = BytesIO(request.body)
            try:
                file_contents = WgtFile(downloaded_file)
            except zipfile.BadZipfile:
                return build_error_response(request, 400, _('The uploaded file is not a zip file'))

            force_create = request.GET.get('force_create', 'false').strip().lower() == 'true'
            public = request.GET.get('public', 'false').strip().lower() == 'true'
            install_embedded_resources = request.GET.get('install_embedded_resources', 'false').strip().lower() == 'true'
        else:  # if content_type == 'application/json'

            market_endpoint = None

            data = parse_json_request(request)

            install_embedded_resources = normalize_boolean_param(request, 'install_embedded_resources', data.get('install_embedded_resources', False))
            force_create = data.get('force_create', False)
            public = request.GET.get('public', 'false').strip().lower() == 'true'
            templateURL = data.get('url')
            market_endpoint = data.get('market_endpoint', None)
            headers = data.get('headers', {})

            if market_endpoint is not None:

                if 'name' not in market_endpoint:
                    msg = _('Missing market name')
                    return build_error_response(request, 400, msg)

                market_id = market_endpoint['name']
                market_managers = get_market_managers(request.user)
                if market_id not in market_managers:
                    return build_error_response(request, 409, _('Unknown market: %s') % market_id)

                market_manager = market_managers[market_id]
                downloaded_file = market_manager.download_resource(request.user, templateURL, market_endpoint)

            else:

                try:
                    context = parse_context_from_referer(request)
                except:
                    context = {}

                try:
                    context["headers"] = CaseInsensitiveDict(headers)
                    response = WIRECLOUD_PROXY.do_request(request, templateURL, "GET", context)
                    if response.status_code >= 300 or response.status_code < 200:
                        raise Exception()

                    downloaded_file = b''.join(response)
                except:
                    return build_error_response(request, 409, _('Content cannot be downloaded from the specified url'))

            try:
                downloaded_file = BytesIO(downloaded_file)
                file_contents = WgtFile(downloaded_file)

            except zipfile.BadZipfile:

                return build_error_response(request, 400, _('The file downloaded from the marketplace is not a zip file'))

        if public and not request.user.is_superuser:
            return build_error_response(request, 403, _('You are not allowed to make resources publicly available to all users'))

        try:
            fix_dev_version(file_contents, request.user)
            added, resource = install_resource_to_user(request.user, file_contents=file_contents, templateURL=templateURL)

            if not added and force_create:
                return build_error_response(request, 409, _('Resource already exists'))
            elif not added:
                status_code = 200

            if public:
                install_resource_to_all_users(executor_user=request.user, file_contents=file_contents)

        except zipfile.BadZipfile as e:

            return build_error_response(request, 400, _('The uploaded file is not a valid zip file'), details="{}".format(e))

        except OSError as e:

            if e.errno == errno.EACCES:
                return build_error_response(request, 500, _('Error writing the resource into the filesystem. Please, contact the server administrator.'))
            else:
                raise

        except TemplateParseException as e:

            msg = "Error parsing config.xml descriptor file: %s" % e

            details = "%s" % e
            return build_error_response(request, 400, msg, details=details)

        except (InvalidContents, UnsupportedFeature) as e:

            details = e.details if hasattr(e, 'details') else None
            return build_error_response(request, 400, e, details=six.text_type(details))

        if install_embedded_resources:

            info = {
                'resource_details': resource.get_processed_info(request, url_pattern_name="wirecloud.showcase_media"),
                'extra_resources': []
            }
            if resource.resource_type() == 'mashup':
                resource_info = resource.get_processed_info(process_urls=False)
                for embedded_resource in resource_info['embedded']:
                    resource_file = BytesIO(file_contents.read(embedded_resource['src']))

                    extra_resource_contents = WgtFile(resource_file)
                    if public:
                        extra_resource_added, extra_resource = install_resource_to_user(request.user, file_contents=extra_resource_contents, raise_conflicts=False)
                    else:
                        extra_resource_added, extra_resource = install_resource_to_user(request.user, file_contents=extra_resource_contents, raise_conflicts=False)
                    if extra_resource_added:
                        info['extra_resources'].append(extra_resource.get_processed_info(request, url_pattern_name="wirecloud.showcase_media"))

            return HttpResponse(json.dumps(info, sort_keys=True), status=status_code, content_type='application/json; charset=UTF-8')

        else:

            return HttpResponse(json.dumps(resource.get_processed_info(request, url_pattern_name="wirecloud.showcase_media"), sort_keys=True), status=status_code, content_type='application/json; charset=UTF-8')
示例#17
0
    def create(self, request, workspace_id):

        import wirecloud.catalogue.utils as catalogue_utils

        content_type = get_content_type(request)[0]
        image_file = None
        smartphoneimage_file = None
        extra_files = []

        if content_type == 'application/json':
            received_json = request.body.decode('utf-8')
        else:
            received_json = request.POST['json']
            image_file = request.FILES.get('image', None)
            smartphoneimage_file = request.FILES.get('smartphoneimage', None)

        try:
            options = json.loads(received_json)
        except ValueError as e:
            msg = _("malformed json data: %s") % e
            return build_error_response(request, 400, msg)

        missing_fields = check_json_fields(options,
                                           ('name', 'vendor', 'version'))
        if len(missing_fields) > 0:
            return build_error_response(
                request, 400,
                _('Malformed JSON. The following field(s) are missing: %(fields)s.'
                  ) % {'fields': missing_fields})

        if not is_valid_vendor(options['vendor']):
            return build_error_response(request, 400, _('Invalid vendor'))

        if not is_valid_name(options['name']):
            return build_error_response(request, 400, _('Invalid name'))

        if not is_valid_version(options['version']):
            return build_error_response(request, 400,
                                        _('Invalid version number'))

        workspace = get_object_or_404(Workspace, id=workspace_id)
        if image_file is not None:
            options['image'] = 'images/catalogue' + os.path.splitext(
                image_file.name)[1]
            extra_files.append((options['image'], image_file))
        if smartphoneimage_file is not None:
            options[
                'smartphoneimage'] = 'images/smartphone' + os.path.splitext(
                    smartphoneimage_file.name)[1]
            extra_files.append(
                (options['smartphoneimage'], smartphoneimage_file))
        if 'longdescription' in options:
            extra_files.append(
                ('DESCRIPTION.md',
                 BytesIO(options['longdescription'].encode('utf-8'))))
            options['longdescription'] = 'DESCRIPTION.md'

        description = build_xml_template_from_workspace(
            options, workspace, request.user)

        # Build mashup wgt file
        f = BytesIO()
        zf = zipfile.ZipFile(f, 'w')
        zf.writestr('config.xml', description.encode('utf-8'))
        for filename, extra_file in extra_files:
            zf.writestr(filename, extra_file.read())
        for resource_info in options['embedded']:
            (vendor, name,
             version) = (resource_info['vendor'], resource_info['name'],
                         resource_info['version'])
            resource = CatalogueResource.objects.get(vendor=vendor,
                                                     short_name=name,
                                                     version=version)
            base_dir = catalogue_utils.wgt_deployer.get_base_dir(
                vendor, name, version)
            zf.write(os.path.join(base_dir, resource.template_uri),
                     resource_info['src'])
        zf.close()
        wgt_file = WgtFile(f)

        resource = get_local_catalogue().publish(None, wgt_file, request.user,
                                                 options, request)

        return HttpResponse(json.dumps(resource.get_processed_info(request),
                                       ensure_ascii=False),
                            status=201,
                            content_type='application/json; charset=utf-8')
示例#18
0
    def create(self, request):

        status_code = 201
        force_create = False
        install_embedded_resources = False
        templateURL = None
        file_contents = None
        content_type = get_content_type(request)[0]
        if content_type == 'multipart/form-data':
            force_create = request.POST.get('force_create',
                                            'false').strip().lower() == 'true'
            public = request.POST.get('public',
                                      'false').strip().lower() == 'true'
            install_embedded_resources = request.POST.get(
                'install_embedded_resources',
                'false').strip().lower() == 'true'
            if 'file' not in request.FILES:
                return build_error_response(
                    request, 400, _('Missing component file in the request'))

            downloaded_file = request.FILES['file']
            try:
                file_contents = WgtFile(downloaded_file)
            except zipfile.BadZipfile:
                return build_error_response(
                    request, 400, _('The uploaded file is not a zip file'))

        elif content_type == 'application/octet-stream':

            downloaded_file = BytesIO(request.body)
            try:
                file_contents = WgtFile(downloaded_file)
            except zipfile.BadZipfile:
                return build_error_response(
                    request, 400, _('The uploaded file is not a zip file'))

            force_create = request.GET.get('force_create',
                                           'false').strip().lower() == 'true'
            public = request.GET.get('public',
                                     'false').strip().lower() == 'true'
            install_embedded_resources = request.GET.get(
                'install_embedded_resources',
                'false').strip().lower() == 'true'
        else:  # if content_type == 'application/json'

            market_endpoint = None

            data = parse_json_request(request)

            install_embedded_resources = normalize_boolean_param(
                request, 'install_embedded_resources',
                data.get('install_embedded_resources', False))
            force_create = data.get('force_create', False)
            public = request.GET.get('public',
                                     'false').strip().lower() == 'true'
            templateURL = data.get('url')
            market_endpoint = data.get('market_endpoint', None)
            headers = data.get('headers', {})

            if market_endpoint is not None:

                if 'name' not in market_endpoint:
                    msg = _('Missing market name')
                    return build_error_response(request, 400, msg)

                market_id = market_endpoint['name']
                market_managers = get_market_managers(request.user)
                if market_id not in market_managers:
                    return build_error_response(
                        request, 409,
                        _('Unknown market: %s') % market_id)

                market_manager = market_managers[market_id]
                downloaded_file = market_manager.download_resource(
                    request.user, templateURL, market_endpoint)

            else:

                try:
                    context = parse_context_from_referer(request)
                except:
                    context = {}

                try:
                    context["headers"] = CaseInsensitiveDict(headers)
                    response = WIRECLOUD_PROXY.do_request(
                        request, templateURL, "GET", context)
                    if response.status_code >= 300 or response.status_code < 200:
                        raise Exception()

                    downloaded_file = b''.join(response)
                except:
                    return build_error_response(
                        request, 409,
                        _('Content cannot be downloaded from the specified url'
                          ))

            try:
                downloaded_file = BytesIO(downloaded_file)
                file_contents = WgtFile(downloaded_file)

            except zipfile.BadZipfile:

                return build_error_response(
                    request, 400,
                    _('The file downloaded from the marketplace is not a zip file'
                      ))

        if public and not request.user.is_superuser:
            return build_error_response(
                request, 403,
                _('You are not allowed to make resources publicly available to all users'
                  ))

        try:
            fix_dev_version(file_contents, request.user)
            added, resource = install_resource_to_user(
                request.user,
                file_contents=file_contents,
                templateURL=templateURL)

            if not added and force_create:
                return build_error_response(request, 409,
                                            _('Resource already exists'))
            elif not added:
                status_code = 200

            if public:
                install_resource_to_all_users(executor_user=request.user,
                                              file_contents=file_contents)

        except zipfile.BadZipfile as e:

            return build_error_response(
                request,
                400,
                _('The uploaded file is not a valid zip file'),
                details="{}".format(e))

        except OSError as e:

            if e.errno == errno.EACCES:
                return build_error_response(
                    request, 500,
                    _('Error writing the resource into the filesystem. Please, contact the server administrator.'
                      ))
            else:
                raise

        except TemplateParseException as e:

            msg = "Error parsing config.xml descriptor file: %s" % e

            details = "%s" % e
            return build_error_response(request, 400, msg, details=details)

        except (InvalidContents, UnsupportedFeature) as e:

            details = e.details if hasattr(e, 'details') else None
            return build_error_response(request,
                                        400,
                                        e,
                                        details=six.text_type(details))

        if install_embedded_resources:

            info = {
                'resource_details':
                resource.get_processed_info(
                    request, url_pattern_name="wirecloud.showcase_media"),
                'extra_resources': []
            }
            if resource.resource_type() == 'mashup':
                resource_info = resource.get_processed_info(process_urls=False)
                for embedded_resource in resource_info['embedded']:
                    resource_file = BytesIO(
                        file_contents.read(embedded_resource['src']))

                    extra_resource_contents = WgtFile(resource_file)
                    if public:
                        extra_resource_added, extra_resource = install_resource_to_user(
                            request.user,
                            file_contents=extra_resource_contents,
                            raise_conflicts=False)
                    else:
                        extra_resource_added, extra_resource = install_resource_to_user(
                            request.user,
                            file_contents=extra_resource_contents,
                            raise_conflicts=False)
                    if extra_resource_added:
                        info['extra_resources'].append(
                            extra_resource.get_processed_info(
                                request,
                                url_pattern_name="wirecloud.showcase_media"))

            return HttpResponse(json.dumps(info, sort_keys=True),
                                status=status_code,
                                content_type='application/json; charset=UTF-8')

        else:

            return HttpResponse(json.dumps(resource.get_processed_info(
                request, url_pattern_name="wirecloud.showcase_media"),
                                           sort_keys=True),
                                status=status_code,
                                content_type='application/json; charset=UTF-8')
示例#19
0
 def test_get_content_type_no_provided(self):
     request = self._prepare_request_mock()
     self.assertEqual(get_content_type(request), ('', {}))
示例#20
0
 def test_get_content_type_invalid_mime_type(self):
     request = self._prepare_request_mock()
     request.META['CONTENT_TYPE'] = 'application/json/ji'
     self.assertEqual(get_content_type(request), ('', {}))