def update_resource_catalogue_cache(orm=None):

    from wirecloud.catalogue.utils import wgt_deployer

    if orm is not None:
        resources = orm.CatalogueResource.objects.all()
    else:
        from wirecloud.catalogue.models import CatalogueResource
        resources = CatalogueResource.objects.all()

    for resource in resources:

        try:

            if resource.fromWGT:
                base_dir = wgt_deployer.get_base_dir(resource.vendor, resource.short_name, resource.version)
                wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
                template = wgt_file.get_template()
                wgt_file.close()
            else:
                template = download_http_content(resource.template_uri)

            template_parser = TemplateParser(template)
            resource.json_description = json.dumps(template_parser.get_resource_info())
            resource.save()

        except TemplateParseException as e:

            from django.conf import settings

            if getattr(settings, 'WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION', False) is not True:
                raise e

            print('    Removing %s' % (resource.vendor + '/' + resource.short_name + '/' + resource.version))
            resource.delete()
Example #2
0
    def create(self, request, fromWGT=False):

        try:
            if 'file' in request.FILES:

                request_file = request.FILES['file']
                resource = add_widget_from_wgt(request_file, request.user)

            elif 'template_uri' in request.POST:

                template_uri = request.POST['template_uri']
                downloaded_file = download_http_content(template_uri, user=request.user)
                if request.POST.get('packaged', 'false').lower() == 'true':
                    resource = add_widget_from_wgt(StringIO(downloaded_file), request.user)
                else:
                    resource = add_resource_from_template(template_uri, downloaded_file, request.user)

            else:

                return build_error_response(request, 400, _("Missing parameter: template_uri or file"))

        except TemplateParseException as e:

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

        except IntegrityError:

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

        resource.users.add(request.user)
        return HttpResponse(resource.json_description, content_type='application/json; charset=UTF-8')
        def wrapper(self, *args, **kwargs):

            owner_user = User.objects.get(username=owner)

            if shared:
                base = self.shared_test_data_dir
            else:
                base = self.test_data_dir

            with open(os.path.join(base, file_name), 'rb') as f:
                wgt = WgtFile(f)
                template = TemplateParser(wgt.get_template())

                resource_info = template.get_resource_processed_info(process_urls=False)
                if resource_info["type"] != 'mashup':
                    raise Exception

                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(wgt.read(embedded_resource['src']))

                    extra_resource_contents = WgtFile(resource_file)
                    install_resource_to_user(owner_user, file_contents=extra_resource_contents)

                buildWorkspaceFromTemplate(template, owner_user)

            return test_func(self, *args, **kwargs)
Example #4
0
        def wrapper(self, *args, **kwargs):

            owner_user = User.objects.get(username=owner)

            if shared:
                base = self.shared_test_data_dir
            else:
                base = self.test_data_dir

            with open(os.path.join(base, file_name), 'rb') as f:
                wgt = WgtFile(f)
                template = TemplateParser(wgt.get_template())

                resource_info = template.get_resource_processed_info(
                    process_urls=False)
                if resource_info["type"] != 'mashup':
                    raise Exception

                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(
                            wgt.read(embedded_resource['src']))

                    extra_resource_contents = WgtFile(resource_file)
                    install_resource_to_user(
                        owner_user, file_contents=extra_resource_contents)

                buildWorkspaceFromTemplate(template, owner_user)

            return test_func(self, *args, **kwargs)
Example #5
0
    def create(self, request):

        try:
            if 'file' in request.FILES:

                request_file = request.FILES['file']
                resource = add_packaged_resource(request_file, request.user)

            elif 'template_uri' in request.POST:

                template_uri = request.POST['template_uri']
                downloaded_file = download_http_content(template_uri,
                                                        user=request.user)
                resource = add_packaged_resource(BytesIO(downloaded_file),
                                                 request.user)

            else:

                return build_error_response(
                    request, 400, _("Missing parameter: template_uri or file"))

        except TemplateParseException as e:

            return build_error_response(request, 400, e.msg)

        except IntegrityError:

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

        resource.users.add(request.user)

        return HttpResponse(resource.json_description,
                            content_type='application/json; charset=UTF-8')
Example #6
0
    def create(self, request):

        try:
            if 'file' in request.FILES:

                request_file = request.FILES['file']
                resource = add_packaged_resource(request_file, request.user)

            elif 'template_uri' in request.POST:

                template_uri = request.POST['template_uri']
                downloaded_file = download_http_content(template_uri, user=request.user)
                resource = add_packaged_resource(BytesIO(downloaded_file), request.user)

            else:

                return build_error_response(request, 400, _("Missing parameter: template_uri or file"))

        except TemplateParseException as e:

            return build_error_response(request, 400, e.msg)

        except IntegrityError:

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

        resource.users.add(request.user)
        return HttpResponse(resource.json_description, content_type='application/json; charset=UTF-8')
    def search_resource(self, vendor, name, version, user):

        if self._name == 'local':
            resources = CatalogueResource.objects.filter(vendor=vendor, short_name=name, version=version)[:1]

            if len(resources) == 1:
                resource = resources[0]
                base_dir = catalogue_utils.wgt_deployer.get_base_dir(vendor, name, version)
                downloaded_file = download_local_file(os.path.join(base_dir, resource.template_uri))

                return {
                    'downloaded_file': downloaded_file,
                }
            else:
                return None
        else:

            path = '/'.join(('catalogue', 'resource', vendor, name, version))
            url = iri_to_uri(urljoin(self._options['url'], path))
            response = requests.get(url)

            if response.status_code == 200:
                data = json.loads(response.content)
                downloaded_file = download_http_content(data['uriTemplate'], user=user)
                return {
                    'downloaded_file': downloaded_file,
                    'template_url': data['uriTemplate'],
                    'packaged': data['packaged']
                }
            else:
                return None
Example #8
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
Example #9
0
    def read(self, request, vendor, name, version):

        widget = get_object_or_404(
            Widget, resource__vendor=vendor, resource__short_name=name, resource__version=version
        )
        if not widget.is_available_for(request.user):
            raise Http403()

        # check if the xhtml code has been cached
        if widget.xhtml.cacheable:

            cache_key = widget.xhtml.get_cache_key(get_current_domain(request))
            cache_entry = cache.get(cache_key)
            if cache_entry is not None:
                response = HttpResponse(cache_entry["code"], mimetype="%s; charset=UTF-8" % cache_entry["content_type"])
                patch_cache_headers(response, cache_entry["timestamp"], cache_entry["timeout"])
                return response

        # process xhtml
        xhtml = widget.xhtml

        content_type = xhtml.content_type
        if not content_type:
            content_type = "text/html"

        force_base = False
        base_url = xhtml.url
        if not base_url.startswith(("http://", "https://")):
            base_url = get_absolute_reverse_url(
                "wirecloud_showcase.media", args=(base_url.split("/", 4)), request=request
            )
            force_base = True

        code = xhtml.code
        if not xhtml.cacheable or code == "":
            try:
                if xhtml.url.startswith(("http://", "https://")):
                    code = downloader.download_http_content(urljoin(base_url, xhtml.url), user=request.user)
                else:
                    code = downloader.download_http_content(
                        "file://" + os.path.join(showcase_utils.wgt_deployer.root_dir, xhtml.url), user=request.user
                    )

            except Exception, e:
                # FIXME: Send the error or use the cached original code?
                msg = _("XHTML code is not accessible: %(errorMsg)s") % {"errorMsg": e.message}
                return HttpResponse(get_xml_error_response(msg), mimetype="application/xml; charset=UTF-8")
Example #10
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
Example #11
0
def create_widget_from_template(template, user, request=None, base=None):

    """Creates a widget from a template"""

    if isinstance(template, TemplateParser):
        parser = template
    else:
        template_content = download_http_content(template, user=user)
        if base is None:
            base = template
        parser = TemplateParser(template_content, base=base)

    if parser.get_resource_type() != 'widget':
        raise Exception()

    widget_info = parser.get_resource_info()
    check_requirements(widget_info)

    widget = Widget()
    widget.resource = CatalogueResource.objects.get(vendor=parser.get_resource_vendor(), short_name=parser.get_resource_name(), version=parser.get_resource_version())
    widget_code = parser.get_absolute_url(widget_info['contents']['src'], base)
    widget.xhtml = XHTML.objects.create(
        uri=widget.uri + "/xhtml",
        url=widget_code,
        content_type=widget_info['contents']['contenttype'],
        use_platform_style=widget_info['contents']['useplatformstyle'],
        cacheable=widget_info['contents']['cacheable']
    )

    widget.width = widget_info['widget_width']
    widget.height = widget_info['widget_height']

    widget.save()

    for preference in widget_info['preferences']:
        vDef = VariableDef.objects.create(
            name=preference['name'],
            type=parser.typeText2typeCode(preference['type']),
            aspect='PREF',
            readonly=preference['readonly'],
            default_value=preference['default'],
            value=preference['value'],
            widget=widget,
            secure=preference['secure']
        )

    for prop in widget_info['properties']:
        vDef = VariableDef.objects.create(
            name=prop['name'],
            type=parser.typeText2typeCode(prop['type']),
            aspect='PROP',
            default_value=prop['default'],
            widget=widget,
            secure=prop['secure'],
        )

    return widget
Example #12
0
    def read(self, request, vendor, name, version):

        resource = get_object_or_404(CatalogueResource.objects.select_related('widget__xhtml'), vendor=vendor, short_name=name, version=version)
        if not resource.is_available_for(request.user):
            raise Http403()

        if resource.resource_type() != 'widget':
            raise Http404()

        widget_info = json.loads(resource.json_description)

        # check if the xhtml code has been cached
        if widget_info['code_cacheable'] is True:

            cache_key = resource.widget.xhtml.get_cache_key(get_current_domain(request))
            cache_entry = cache.get(cache_key)
            if cache_entry is not None:
                response = HttpResponse(cache_entry['code'], mimetype=cache_entry['mimetype'])
                patch_cache_headers(response, cache_entry['timestamp'], cache_entry['timeout'])
                return response

        # process xhtml
        xhtml = resource.widget.xhtml
        content_type = widget_info.get('code_content_type', 'text/html')
        charset = widget_info.get('code_charset', 'utf-8')

        force_base = False
        base_url = xhtml.url
        if not base_url.startswith(('http://', 'https://')):
            base_url = get_absolute_reverse_url('wirecloud_showcase.media', args=(base_url.split('/', 4)), request=request)
            force_base = True

        code = xhtml.code
        if not xhtml.cacheable or code == '':
            try:
                if xhtml.url.startswith(('http://', 'https://')):
                    code = downloader.download_http_content(urljoin(base_url, xhtml.url), user=request.user)
                else:
                    code = downloader.download_http_content('file://' + os.path.join(showcase_utils.wgt_deployer.root_dir, url2pathname(xhtml.url)), user=request.user)

            except Exception, e:
                msg = _("XHTML code is not accessible: %(errorMsg)s") % {'errorMsg': e.message}
                return build_error_response(request, 502, msg)
Example #13
0
def create_widget_from_wgt(wgt, user, deploy_only=False):

    if isinstance(wgt, WgtFile):
        wgt_file = wgt
    else:
        wgt_file = WgtFile(BytesIO(download_http_content(wgt)))

    template = wgt_deployer.deploy(wgt_file)
    if not deploy_only:
        return create_widget_from_template(template, user)
    def search_resource(self, vendor, name, version, user):

        if self._options['name'] == 'local':
            resources = CatalogueResource.objects.filter(vendor=vendor, short_name=name, version=version)[:1]

            if len(resources) == 1:
                resource = resources[0]

                if resource.template_uri.startswith(('http://', 'https://')):
                    downloaded_file = downloader.download_http_content(resource.template_uri, user=user)
                else:
                    base_dir = catalogue_utils.wgt_deployer.get_base_dir(vendor, name, version)
                    downloaded_file = downloader.download_http_content('file://' + os.path.join(base_dir, resource.template_uri), user=user)

                return {
                    'downloaded_file': downloaded_file,
                    'template_url': resource.template_uri,
                    'packaged': resource.fromWGT
                }
            else:
                return None
        else:

            opener = urllib2.build_opener()
            path = '/'.join(('catalogue', 'resource', vendor, name, version))
            url = iri_to_uri(urljoin(self._options['url'], path))
            request = MethodRequest('GET', url)
            response = opener.open(request)

            if response.code == 200:
                data = json.loads(response.read())
                downloaded_file = downloader.download_http_content(data['uriTemplate'], user=user)
                return {
                    'downloaded_file': downloaded_file,
                    'template_url': data['uriTemplate'],
                    'packaged': data['packaged']
                }
            else:
                return None
Example #15
0
def update_resource_catalogue_cache(orm=None):

    if orm is not None:
        resources = orm.CatalogueResource.objects.all()
    else:
        resources = CatalogueResource.objects.all()

    resources_to_remove = []
    for resource in resources:

        try:

            if getattr(resource, 'fromWGT', True):
                base_dir = wgt_deployer.get_base_dir(resource.vendor,
                                                     resource.short_name,
                                                     resource.version)
                wgt_file = WgtFile(
                    os.path.join(base_dir, resource.template_uri))
                template = wgt_file.get_template()
                wgt_file.close()
            else:
                # fromWGT attribute support was removed from Wirecloud in version 0.7.0
                template = download_http_content(resource.template_uri)

            template_parser = TemplateParser(template)
            resource.json_description = json.dumps(
                template_parser.get_resource_info())
            resource.save()

        except (IOError, TemplateParseException) as e:

            if isinstance(e, IOError) and e.errno != errno.ENOENT:
                raise e

            resources_to_remove.append(resource)

    if len(resources_to_remove) > 0 and getattr(
            settings, 'WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION',
            False) is False:
        raise Exception(
            'There are some mashable application components that are not supported anymore (use WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION for removing automatically them in the migration process'
        )

    for resource in resources_to_remove:
        print('    Removing %s' %
              (resource.vendor + '/' + resource.short_name + '/' +
               resource.version))
        resource.delete()
Example #16
0
def create_workspace(owner, f=None, mashup=None, new_name=None, new_title=None, preferences={}, searchable=True, public=False):

    from wirecloud.platform.workspace.mashupTemplateParser import buildWorkspaceFromTemplate

    if mashup is not None and f is not None:
        raise Exception

    if f is not None:

        wgt = f if isinstance(f, WgtFile) else WgtFile(f)
        template = TemplateParser(wgt.get_template())

        resource_info = template.get_resource_processed_info(process_urls=False)
        if resource_info["type"] != 'mashup':
            raise Exception

        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(wgt.read(embedded_resource['src']))

            extra_resource_contents = WgtFile(resource_file)
            install_resource_to_user(owner, file_contents=extra_resource_contents)
    else:
        values = mashup.split('/', 3)
        if len(values) != 3:
            raise TypeError(_('invalid mashup id'))

        (mashup_vendor, mashup_name, mashup_version) = values
        try:
            resource = CatalogueResource.objects.get(vendor=mashup_vendor, short_name=mashup_name, version=mashup_version)
            if not resource.is_available_for(owner) or resource.resource_type() != 'mashup':
                raise CatalogueResource.DoesNotExist
        except CatalogueResource.DoesNotExist:
            raise Exception(_('Mashup not found: %(mashup)s') % {'mashup': mashup})

        base_dir = catalogue.wgt_deployer.get_base_dir(mashup_vendor, mashup_name, mashup_version)
        wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
        template = TemplateParser(wgt_file.get_template())

    workspace, _foo = buildWorkspaceFromTemplate(template, owner, new_name=new_name, new_title=new_title, searchable=searchable, public=public)

    if len(preferences) > 0:
        update_workspace_preferences(workspace, preferences, invalidate_cache=False)

    return workspace
Example #17
0
def update_resource_catalogue_cache(orm=None):

    if orm is not None:
        resources = orm.CatalogueResource.objects.all()
    else:
        resources = CatalogueResource.objects.all()

    resources_to_remove = []
    for resource in resources:

        try:

            if getattr(resource, "fromWGT", True):
                base_dir = wgt_deployer.get_base_dir(resource.vendor, resource.short_name, resource.version)
                wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
                template = wgt_file.get_template()
                wgt_file.close()
            else:
                # fromWGT attribute support was removed from Wirecloud in version 0.7.0
                template = download_http_content(resource.template_uri)

            template_parser = TemplateParser(template)
            resource.json_description = json.dumps(template_parser.get_resource_info())
            resource.save()

        except (IOError, TemplateParseException) as e:

            if isinstance(e, IOError) and e.errno != errno.ENOENT:
                raise e

            resources_to_remove.append(resource)

    if (
        len(resources_to_remove) > 0
        and getattr(settings, "WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION", False) is False
    ):
        raise Exception(
            "There are some mashable application components that are not supported anymore (use WIRECLOUD_REMOVE_UNSUPPORTED_RESOURCES_MIGRATION for removing automatically them in the migration process"
        )

    for resource in resources_to_remove:
        print("    Removing %s" % (resource.vendor + "/" + resource.short_name + "/" + resource.version))
        resource.delete()
Example #18
0
def create_workspace(owner, f):

    from wirecloud.platform.workspace.mashupTemplateParser import buildWorkspaceFromTemplate

    wgt = f if isinstance(f, WgtFile) else WgtFile(f)
    template = TemplateParser(wgt.get_template())

    resource_info = template.get_resource_processed_info(process_urls=False)
    if resource_info["type"] != 'mashup':
        raise Exception

    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(wgt.read(embedded_resource['src']))

        extra_resource_contents = WgtFile(resource_file)
        install_resource_to_user(owner, file_contents=extra_resource_contents)

    workspace, _ = buildWorkspaceFromTemplate(template, owner)
    return workspace
Example #19
0
def create_workspace(owner, f):

    from wirecloud.platform.workspace.mashupTemplateParser import buildWorkspaceFromTemplate

    wgt = f if isinstance(f, WgtFile) else WgtFile(f)
    template = TemplateParser(wgt.get_template())

    resource_info = template.get_resource_processed_info(process_urls=False)
    if resource_info["type"] != 'mashup':
        raise Exception

    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(wgt.read(embedded_resource['src']))

        extra_resource_contents = WgtFile(resource_file)
        install_resource_to_user(owner, file_contents=extra_resource_contents)

    workspace, _ = buildWorkspaceFromTemplate(template, owner)
    return workspace
def update_resource_catalogue_cache(orm=None):

    from wirecloud.catalogue.utils import wgt_deployer

    if orm is not None:
        resources = orm.CatalogueResource.objects.all()
    else:
        from wirecloud.catalogue.models import CatalogueResource
        resources = CatalogueResource.objects.all()

    for resource in resources:

        if resource.fromWGT:
            base_dir = wgt_deployer.get_base_dir(resource.vendor, resource.short_name, resource.version)
            wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
            template = wgt_file.get_template()
            wgt_file.close()
        else:
            template = download_http_content(resource.template_uri)

        template_parser = TemplateParser(template)
        resource.json_description = json.dumps(template_parser.get_resource_info())
        resource.save()
Example #21
0
    def create(self, request, fromWGT=False):

        try:
            if 'file' in request.FILES:

                request_file = request.FILES['file']
                resource = add_widget_from_wgt(request_file, request.user)

            elif 'template_uri' in request.POST:

                template_uri = request.POST['template_uri']
                downloaded_file = downloader.download_http_content(template_uri, user=request.user)
                if str(request.POST.get('packaged', 'false')).lower() == 'true':
                    resource = add_widget_from_wgt(StringIO(downloaded_file), request.user)
                else:
                    resource = add_resource_from_template(template_uri, downloaded_file, request.user)

            else:

                return build_error_response(request, 400, _("Missing parameter: template_uri or file"))

        except TemplateParseException, e:

            return build_error_response(request, 400, unicode(e.msg))
Example #22
0
    def search_resource(self, vendor, name, version, user):

        if self._name == 'local':
            resources = CatalogueResource.objects.filter(vendor=vendor,
                                                         short_name=name,
                                                         version=version)[:1]

            if len(resources) == 1:
                resource = resources[0]
                base_dir = catalogue_utils.wgt_deployer.get_base_dir(
                    vendor, name, version)
                downloaded_file = download_local_file(
                    os.path.join(base_dir, resource.template_uri))

                return {
                    'downloaded_file': downloaded_file,
                }
            else:
                return None
        else:

            path = '/'.join(('catalogue', 'resource', vendor, name, version))
            url = iri_to_uri(urljoin(self._options['url'], path))
            response = requests.get(url)

            if response.status_code == 200:
                data = json.loads(response.content)
                downloaded_file = download_http_content(data['uriTemplate'],
                                                        user=user)
                return {
                    'downloaded_file': downloaded_file,
                    'template_url': data['uriTemplate'],
                    'packaged': data['packaged']
                }
            else:
                return None
Example #23
0
    def read(self, request, vendor, name, version):

        resource = get_object_or_404(CatalogueResource.objects.select_related('widget__xhtml'), vendor=vendor, short_name=name, version=version)
        # For now, all widgets are freely accessible/distributable
        #if not resource.is_available_for(request.user):
        #    return build_error_response(request, 403, "Forbidden")

        if resource.resource_type() != 'widget':
            raise Http404()

        mode = request.GET.get('mode', 'classic')
        widget_info = json.loads(resource.json_description)

        # check if the xhtml code has been cached
        if widget_info['contents']['cacheable'] is True:

            cache_key = resource.widget.xhtml.get_cache_key(get_current_domain(request), mode)
            cache_entry = cache.get(cache_key)
            if cache_entry is not None:
                response = HttpResponse(cache_entry['code'], content_type=cache_entry['content_type'])
                patch_cache_headers(response, cache_entry['timestamp'], cache_entry['timeout'])
                return response

        # process xhtml
        xhtml = resource.widget.xhtml
        content_type = widget_info['contents'].get('contenttype', 'text/html')
        charset = widget_info['contents'].get('charset', 'utf-8')

        force_base = False
        base_url = xhtml.url
        if not base_url.startswith(('http://', 'https://')):
            # Newer versions of Django urlencode urls created using reverse
            # Fix double encoding
            base_url = urlunquote(base_url)
            base_url = get_absolute_reverse_url('wirecloud.showcase_media', args=(base_url.split('/', 3)), request=request)
            force_base = True

        code = xhtml.code
        if not xhtml.cacheable or code == '':
            try:
                if xhtml.url.startswith(('http://', 'https://')):
                    code = download_http_content(urljoin(base_url, xhtml.url), user=request.user)
                else:
                    code = download_local_file(os.path.join(showcase_utils.wgt_deployer.root_dir, url2pathname(xhtml.url)))

            except Exception as e:
                return build_response(request, 502, {'error_msg': _("(X)HTML code is not accessible"), 'details': "%s" % e}, WIDGET_ERROR_FORMATTERS)
        else:
            # Code contents comes as unicode from persistence, we need bytes
            code = code.encode(charset)

        if xhtml.cacheable and (xhtml.code == '' or xhtml.code_timestamp is None):
            try:
                xhtml.code = code.decode(charset)
            except UnicodeDecodeError:
                msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
                return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)

            xhtml.code_timestamp = time.time() * 1000
            xhtml.save()
        elif not xhtml.cacheable and xhtml.code != '':
            xhtml.code = ''
            xhtml.code_timestamp = None
            xhtml.save()

        try:
            code = fix_widget_code(code, base_url, content_type, request, charset, xhtml.use_platform_style, process_requirements(widget_info['requirements']), force_base, mode)
        except UnicodeDecodeError:
            msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
            return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)
        except Exception as e:
            msg = _('Error processing widget code')
            return build_response(request, 502, {'error_msg': msg, 'details':"%s" % e}, WIDGET_ERROR_FORMATTERS)

        if xhtml.cacheable:
            cache_timeout = 31536000  # 1 year
            cache_entry = {
                'code': code,
                'content_type': '%s; charset=%s' % (content_type, charset),
                'timestamp': xhtml.code_timestamp,
                'timeout': cache_timeout,
            }
            cache.set(cache_key, cache_entry, cache_timeout)
        else:
            cache_timeout = 0

        response = HttpResponse(code, content_type='%s; charset=%s' % (content_type, charset))
        patch_cache_headers(response, xhtml.code_timestamp, cache_timeout)
        return response
Example #24
0
                return build_error_response(request, 422, _('invalid mashup id'))

            (mashup_vendor, mashup_name, mashup_version) = values
            try:
                resource = CatalogueResource.objects.get(vendor=mashup_vendor, short_name=mashup_name, version=mashup_version)
                if not resource.is_available_for(request.user) or resource.resource_type() != 'mashup':
                    raise CatalogueResource.DoesNotExist
            except CatalogueResource.DoesNotExist:
                return build_error_response(request, 422, _('Mashup not found: %(mashup_id)s') % {'mashup_id': mashup_id})

            if resource.fromWGT:
                base_dir = catalogue.wgt_deployer.get_base_dir(mashup_vendor, mashup_name, mashup_version)
                wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
                template = TemplateParser(wgt_file.get_template())
            else:
                template = downloader.download_http_content(resource.template_uri, user=request.user)
                try:
                    template = TemplateParser(template)
                except:
                    build_error_response(request, 424, _('Downloaded invalid resource description from: %(url)s') % {'url': resource.template_uri})

            try:
                check_mashup_dependencies(template, request.user)
            except MissingDependencies, e:
                details = {
                    'missingDependencies': e.missing_dependencies,
                }
                return build_error_response(request, 422, unicode(e), details=details)

            if dry_run:
                return HttpResponse(status_code=204)
Example #25
0
def create_workspace(owner,
                     f=None,
                     mashup=None,
                     new_name=None,
                     new_title=None,
                     preferences={},
                     searchable=True,
                     public=False):

    from wirecloud.platform.workspace.mashupTemplateParser import buildWorkspaceFromTemplate

    if mashup is not None and f is not None:
        raise Exception

    if f is not None:

        wgt = f if isinstance(f, WgtFile) else WgtFile(f)
        template = TemplateParser(wgt.get_template())

        resource_info = template.get_resource_processed_info(
            process_urls=False)
        if resource_info["type"] != 'mashup':
            raise Exception

        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(wgt.read(embedded_resource['src']))

            extra_resource_contents = WgtFile(resource_file)
            install_resource_to_user(owner,
                                     file_contents=extra_resource_contents)
    else:
        values = mashup.split('/', 3)
        if len(values) != 3:
            raise TypeError(_('invalid mashup id'))

        (mashup_vendor, mashup_name, mashup_version) = values
        try:
            resource = CatalogueResource.objects.get(vendor=mashup_vendor,
                                                     short_name=mashup_name,
                                                     version=mashup_version)
            if not resource.is_available_for(
                    owner) or resource.resource_type() != 'mashup':
                raise CatalogueResource.DoesNotExist
        except CatalogueResource.DoesNotExist:
            raise Exception(
                _('Mashup not found: %(mashup)s') % {'mashup': mashup})

        base_dir = catalogue.wgt_deployer.get_base_dir(mashup_vendor,
                                                       mashup_name,
                                                       mashup_version)
        wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
        template = TemplateParser(wgt_file.get_template())

    workspace, _foo = buildWorkspaceFromTemplate(template,
                                                 owner,
                                                 new_name=new_name,
                                                 new_title=new_title,
                                                 searchable=searchable,
                                                 public=public)

    if len(preferences) > 0:
        update_workspace_preferences(workspace,
                                     preferences,
                                     invalidate_cache=False)

    return workspace
Example #26
0
    def create(self, request):

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

        workspace_name = data.get('name', '').strip()
        workspace_id = data.get('workspace', '')
        mashup_id = data.get('mashup', '')
        try:
            allow_renaming = normalize_boolean_param('allow_renaming', data.get('allow_renaming', False))
            dry_run = normalize_boolean_param('allow_renaming', data.get('dry_run', False))
        except (TypeError, ValueError) as e:
            return build_error_response(request, 422, unicode(e))

        if mashup_id == '' and workspace_id == '' and workspace_name == '':
            return build_error_response(request, 422, _('Missing name parameter'))
        elif  mashup_id != '' and workspace_id != '':
            return build_error_response(request, 422, _('Workspace and mashup parameters cannot be used at the same time'))

        if mashup_id == '' and workspace_id == '':

            if not is_valid_name(workspace_name):
                return build_error_response(request, 422, _('invalid workspace name'))

            if dry_run:
                return HttpResponse(status=204)

            try:
                workspace = createEmptyWorkspace(workspace_name, request.user, allow_renaming=allow_renaming)
            except IntegrityError:
                msg = _('A workspace with the given name already exists')
                return build_error_response(request, 409, msg)
        else:

            if mashup_id != '':
                values = mashup_id.split('/', 3)
                if len(values) != 3:
                    return build_error_response(request, 422, _('invalid mashup id'))

                (mashup_vendor, mashup_name, mashup_version) = values
                try:
                    resource = CatalogueResource.objects.get(vendor=mashup_vendor, short_name=mashup_name, version=mashup_version)
                    if not resource.is_available_for(request.user) or resource.resource_type() != 'mashup':
                        raise CatalogueResource.DoesNotExist
                except CatalogueResource.DoesNotExist:
                    return build_error_response(request, 422, _('Mashup not found: %(mashup_id)s') % {'mashup_id': mashup_id})

                if resource.fromWGT:
                    base_dir = catalogue.wgt_deployer.get_base_dir(mashup_vendor, mashup_name, mashup_version)
                    wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
                    template = TemplateParser(wgt_file.get_template())
                else:
                    template = download_http_content(resource.template_uri, user=request.user)
                    try:
                        template = TemplateParser(template)
                    except:
                        build_error_response(request, 422, _('Downloaded invalid resource description from: %(url)s') % {'url': resource.template_uri})

            else:

                from_ws = get_object_or_404(Workspace, id=workspace_id)
                if from_ws.public is False and not request.user.is_superuser and from_ws.creator != request.user:
                    return build_error_response(request, 403, _('You are not allowed to read from workspace %s') % workspace_id)

                options = {
                    'vendor': 'api',
                    'name': from_ws.name,
                    'version': '1.0',
                    'title': '',
                    'description': 'Temporal mashup for the workspace copy operation',
                    'email': '*****@*****.**',
                }

                template = TemplateParser(build_json_template_from_workspace(options, from_ws, from_ws.creator))

            try:
                check_mashup_dependencies(template, request.user)
            except MissingDependencies as e:
                details = {
                    'missingDependencies': e.missing_dependencies,
                }
                return build_error_response(request, 422, unicode(e), details=details)

            if dry_run:
                return HttpResponse(status=204)

            if workspace_name == '':
                workspace_name = None

            try:
                workspace, _junk = buildWorkspaceFromTemplate(template, request.user, allow_renaming=allow_renaming, new_name=workspace_name)
            except IntegrityError:
                msg = _('A workspace with the given name already exists')
                return build_error_response(request, 409, msg)

        workspace_data = get_global_workspace_data(workspace, request.user)

        return workspace_data.get_response(status_code=201, cacheable=False)
Example #27
0
def create_widget_from_template(template, user, request=None, base=None):

    """Creates a widget from a template"""

    if isinstance(template, TemplateParser):
        parser = template
    else:
        template_content = downloader.download_http_content(template, user=user)
        if base is None:
            base = template
        parser = TemplateParser(template_content, base=base)

    if parser.get_resource_type() != 'widget':
        raise Exception()

    widget_info = parser.get_resource_info()
    check_requirements(widget_info)

    widget = Widget()
    widget.resource = CatalogueResource.objects.get(vendor=parser.get_resource_vendor(), short_name=parser.get_resource_name(), version=parser.get_resource_version())
    widget_code = parser.get_absolute_url(widget_info['code_url'], base)
    widget.xhtml = XHTML.objects.create(
        uri=widget.uri + "/xhtml",
        url=widget_code,
        content_type=widget_info['code_content_type'],
        use_platform_style=widget_info['code_uses_platform_style'],
        cacheable=widget_info['code_cacheable']
    )

    widget.width = widget_info['widget_width']
    widget.height = widget_info['widget_height']

    widget.save()

    variable_definitions = {}
    user_options = {}

    order = 0
    for preference in widget_info['preferences']:
        vDef = VariableDef.objects.create(
            name=preference['name'],
            order=order,
            description=preference['description'],
            type=parser.typeText2typeCode(preference['type']),
            aspect='PREF',
            friend_code=None,
            readonly=preference['readonly'],
            label=preference['label'],
            default_value=preference['default_value'],
            value=preference['value'],
            widget=widget,
            secure=preference['secure']
        )
        variable_definitions[vDef.name] = vDef
        user_options[vDef.name] = {}
        for option in preference.get('options', ()):
            upo = UserPrefOption.objects.create(
                value=option['value'],
                name=option['label'],
                variableDef=vDef
            )
            user_options[vDef.name][upo.name] = upo

        order += 1

    order = 0
    for prop in widget_info['properties']:
        vDef = VariableDef.objects.create(
            name=prop['name'],
            order=order,
            description=prop['description'],
            type=parser.typeText2typeCode(prop['type']),
            aspect='PROP',
            friend_code=None,
            label=prop['label'],
            default_value=prop['default_value'],
            widget=widget,
            secure=prop['secure'],
        )
        variable_definitions[vDef.name] = vDef
        order += 1

    order = 0
    for input_endpoint in widget_info['wiring']['inputs']:
        vDef = VariableDef.objects.create(
            name=input_endpoint['name'],
            order=order,
            description=input_endpoint['description'],
            type=parser.typeText2typeCode(input_endpoint['type']),
            aspect='SLOT',
            friend_code=input_endpoint['friendcode'],
            label=input_endpoint['label'],
            action_label=input_endpoint['actionlabel'],
            widget=widget,
        )
        variable_definitions[vDef.name] = vDef
        order += 1

    order = 0
    for output_endpoint in widget_info['wiring']['outputs']:
        vDef = VariableDef.objects.create(
            name=output_endpoint['name'],
            order=order,
            description=output_endpoint['description'],
            type=parser.typeText2typeCode(output_endpoint['type']),
            aspect='EVEN',
            friend_code=output_endpoint['friendcode'],
            label=output_endpoint['label'],
            widget=widget,
        )
        variable_definitions[vDef.name] = vDef
        order += 1

    for context in widget_info['context']:
        vDef = VariableDef.objects.create(
            name=context['name'],
            type=parser.typeText2typeCode(context['type']),
            aspect=context['aspect'],
            widget=widget,
        )
        ContextOption.objects.create(concept=context['concept'], varDef=vDef)

    for lang in widget_info['translations']:
        translation = widget_info['translations'][lang]
        for index in translation:
            value = translation[index]
            usages = widget_info['translation_index_usage'][index]
            for use in usages:
                if use['type'] == 'vdef':
                    vDef = variable_definitions[use['variable']]
                    table = vDef._get_table_id()
                    element_id = vDef.id
                elif use['type'] == 'upo':
                    upo = user_options[use['variable']][use['option']]
                    table = upo._get_table_id()
                    element_id = upo.id
                else:
                    continue

                Translation.objects.create(
                    text_id=index,
                    element_id=element_id,
                    table=table,
                    language=lang,
                    value=value,
                    default=widget_info['default_lang'] == lang
                )

    return widget
Example #28
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 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'
            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)

            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'))

        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')
Example #29
0
    def process(self, request, to_ws_id):

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

        mashup_id = data.get('mashup', '')
        workspace_id = data.get('workspace', '')

        if mashup_id == '' and workspace_id == '':
            return build_error_response(request, 422, _('Missing workspace or mashup parameter'))
        elif  mashup_id != '' and workspace_id != '':
            return build_error_response(request, 422, _('Workspace and mashup parameters cannot be used at the same time'))

        to_ws = get_object_or_404(Workspace, id=to_ws_id)
        if not request.user.is_superuser and to_ws.creator != request.user:
            return build_error_response(request, 403, _('You are not allowed to update this workspace'))

        if mashup_id != '':
            values = mashup_id.split('/', 3)
            if len(values) != 3:
                return build_error_response(request, 422, _('invalid mashup id'))

            (mashup_vendor, mashup_name, mashup_version) = values
            try:
                resource = CatalogueResource.objects.get(vendor=mashup_vendor, short_name=mashup_name, version=mashup_version)
                if not resource.is_available_for(request.user) or resource.resource_type() != 'mashup':
                    raise CatalogueResource.DoesNotExist
            except CatalogueResource.DoesNotExist:
                return build_error_response(request, 422, _('Mashup not found: %(mashup_id)s') % {'mashup_id': mashup_id})

            if resource.fromWGT:
                base_dir = catalogue.wgt_deployer.get_base_dir(mashup_vendor, mashup_name, mashup_version)
                wgt_file = WgtFile(os.path.join(base_dir, resource.template_uri))
                template = TemplateParser(wgt_file.get_template())
            else:
                template = download_http_content(resource.template_uri, user=request.user)
                try:
                    template = TemplateParser(template)
                except:
                    build_error_response(request, 424, _('Downloaded invalid resource description from: %(url)s') % {'url': resource.template_uri})

        else:

            from_ws = get_object_or_404(Workspace, id=workspace_id)
            if not request.user.is_superuser and from_ws.creator != request.user:
                return build_error_response(request, 403, _('You are not allowed to read from workspace %s') % workspace_id)

            options = {
                'vendor': 'api',
                'name': 'merge_op',
                'version': '1.0',
                'title': '',
                'description': 'Temporal mashup for merging operation',
                'email': '*****@*****.**',
            }

            template = TemplateParser(build_json_template_from_workspace(options, from_ws, from_ws.creator))

        try:
            check_mashup_dependencies(template, request.user)
        except MissingDependencies, e:
            details = {
                'missingDependencies': e.missing_dependencies,
            }
            return build_error_response(request, 422, unicode(e), details=details)
Example #30
0
                    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_managers = get_market_managers(request.user)
                market_manager = market_managers[market_endpoint['name']]
                downloaded_file = market_manager.download_resource(request.user, templateURL, market_endpoint)

            else:

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

            if packaged:
                downloaded_file = StringIO(downloaded_file)
                file_contents = WgtFile(downloaded_file)
            else:
                file_contents = downloaded_file

        # TODO for now, install dependencies if force_create is true
        install_dep = force_create
        try:
            resource = install_resource_to_user(request.user, file_contents=file_contents, templateURL=templateURL, packaged=packaged, raise_conflicts=force_create)

        except TemplateParseException, e:
Example #31
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')
Example #32
0
    def read(self, request, vendor, name, version):

        resource = get_object_or_404(CatalogueResource.objects.select_related('widget__xhtml'), vendor=vendor, short_name=name, version=version)
        # For now, all widgets are freely accessible/distributable
        #if not resource.is_available_for(request.user):
        #    return build_error_response(request, 403, "Forbidden")

        if resource.resource_type() != 'widget':
            raise Http404()

        mode = request.GET.get('mode', 'classic')
        widget_info = json.loads(resource.json_description)

        # check if the xhtml code has been cached
        if widget_info['contents']['cacheable'] is True:

            cache_key = resource.widget.xhtml.get_cache_key(get_current_domain(request), mode)
            cache_entry = cache.get(cache_key)
            if cache_entry is not None:
                response = HttpResponse(cache_entry['code'], content_type=cache_entry['content_type'])
                patch_cache_headers(response, cache_entry['timestamp'], cache_entry['timeout'])
                return response

        # process xhtml
        xhtml = resource.widget.xhtml
        content_type = widget_info['contents'].get('contenttype', 'text/html')
        charset = widget_info['contents'].get('charset', 'utf-8')

        force_base = False
        base_url = xhtml.url
        if not base_url.startswith(('http://', 'https://')):
            # Newer versions of Django urlencode urls created using reverse
            # Fix double encoding
            base_url = urlunquote(base_url)
            base_url = get_absolute_reverse_url('wirecloud.showcase_media', args=(base_url.split('/', 4)), request=request)
            force_base = True

        code = xhtml.code
        if not xhtml.cacheable or code == '':
            try:
                if xhtml.url.startswith(('http://', 'https://')):
                    code = download_http_content(urljoin(base_url, xhtml.url), user=request.user)
                else:
                    code = download_local_file(os.path.join(showcase_utils.wgt_deployer.root_dir, url2pathname(xhtml.url)))

            except Exception as e:
                return build_response(request, 502, {'error_msg': _("(X)HTML code is not accessible"), 'details': "%s" % e}, WIDGET_ERROR_FORMATTERS)
        else:
            # Code contents comes as unicode from persistence, we need bytes
            code = code.encode(charset)

        if xhtml.cacheable and (xhtml.code == '' or xhtml.code_timestamp is None):
            try:
                xhtml.code = code.decode(charset)
            except UnicodeDecodeError:
                msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
                return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)

            xhtml.code_timestamp = time.time() * 1000
            xhtml.save()
        elif not xhtml.cacheable and xhtml.code != '':
            xhtml.code = ''
            xhtml.code_timestamp = None
            xhtml.save()

        try:
            code = fix_widget_code(code, base_url, content_type, request, charset, xhtml.use_platform_style, process_requirements(widget_info['requirements']), force_base, mode)
        except UnicodeDecodeError:
            msg = _('Widget code was not encoded using the specified charset (%(charset)s as stated in the widget description file).') % {'charset': charset}
            return build_response(request, 502, {'error_msg': msg}, WIDGET_ERROR_FORMATTERS)
        except Exception as e:
            msg = _('Error processing widget code')
            return build_response(request, 502, {'error_msg': msg, 'details':"%s" % e}, WIDGET_ERROR_FORMATTERS)

        if xhtml.cacheable:
            cache_timeout = 31536000  # 1 year
            cache_entry = {
                'code': code,
                'content_type': '%s; charset=%s' % (content_type, charset),
                'timestamp': xhtml.code_timestamp,
                'timeout': cache_timeout,
            }
            cache.set(cache_key, cache_entry, cache_timeout)
        else:
            cache_timeout = 0

        response = HttpResponse(code, content_type='%s; charset=%s' % (content_type, charset))
        patch_cache_headers(response, xhtml.code_timestamp, cache_timeout)
        return response
Example #33
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",
            )
Example #34
0
    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)

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

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

    try:
        downloaded_file = downloader.download_http_content(fileURL)
    except:
        return build_error_response(request, 409, _('Widget content 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 data.get('preferences', {}).iteritems():
        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
Example #35
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')