Beispiel #1
0
    def __activate__(self, context):
        self.auth = context["page"].authentication
        self.log = context["log"]
        self.log.info("Manual VITAL integration request received")

        response = context["response"]
        writer = response.getPrintWriter("text/plain; charset=UTF-8")

        result = JsonConfigHelper()
        result.set("status", "error")
        result.set("message", "An unknown error has occurred")

        if self.auth.is_logged_in() and self.auth.is_admin():
            self.log.info("VITAL: Valid administrative user")
            formData = context["formData"]
            func = formData.get("func")
            oid = formData.get("oid")
            if func == "refresh":
                if oid:
                    self.log.info("VITAL: Refresh event: '{}'", oid)
                    self.sendMessage(oid)
                    result.set("status", "ok")
                    result.set("message",
                               "Object '%s' queued for refresh" % oid)
                else:
                    self.log.info("VITAL: No OID provided (500)")
                    response.setStatus(500)
                    result.set("message", "No object specified for refresh")
            else:
                self.log.info("VITAL: Unknown action '{}' (500)", func)
                response.setStatus(500)
                result.set("message", "Unknown action '%s'" % func)
        else:
            self.log.info("VITAL: Not an administrative user (500)")
            response.setStatus(500)
            result.set("message",
                       "Only administrative users can access this API")
        writer.println(result.toString(True))
        writer.close()
Beispiel #2
0
    def __processRequest(self):
        depositUrl = "%s/sword/deposit.post" % portalPath
        sword = SwordSimpleServer(depositUrl)
        try:
            p = self.vc("request").path.split(
                self.vc("portalId") + "/" + self.vc("pageName") +
                "/")[1]  # portalPath
        except:
            p = ""
        if p == "post":
            print "\n--- post ---"
            c = sword.getClient()
            c.clearProxy()
            c.clearCredentials()
            postMsg = sword.getPostMessage()
            postMsg.filetype = "application/zip"
            postMsg.filepath = "/home/ward/Desktop/Test.zip"
            depositResponse = c.postFile(postMsg)
            return str(depositResponse)
        elif p == "servicedocument":
            #print "\n--- servicedocument ---"
            sdr = sword.getServiceDocumentRequest()
            sdr.username = self.vc("formData").get("username", "test")
            sdr.password = self.vc("formData").get("password", "test")
            if self.vc("formData").get("test"):
                depositUrl += "?test=1"
            sd = sword.doServiceDocument(sdr)  # get a serviceDocument
            out = self.vc("response").getPrintWriter("text/xml; charset=UTF-8")
            out.println(str(sd))
            out.close()
            self.velocityContext["pageName"] = "-noTemplate-"
            return sd
        elif p == "deposit.post":
            #print "\n--- deposit ---  formData='%s'" % str(formData)
            inputStream = self.vc("formData").getInputStream()
            headers = {}
            for x in self.vc("formData").getHeaders().entrySet():
                headers[x.getKey()] = x.getValue()
            deposit = sword.getDeposit()
            noOp = headers.get("X-No-Op") or "false"
            deposit.noOp = (noOp.lower()=="true") or \
                (formData.get("test") is not None)
            contentDisposition = headers.get("Content-Disposition", "")
            filename = ""
            if contentDisposition != "":
                try:
                    filename = contentDisposition.split("filename=")[1]
                    deposit.filename = filename
                except:
                    pass
            slug = headers.get("Slug")
            if slug is not None and slug != "":
                deposit.slug = slug
            #elif filename!="":
            #    deposit.slug = filename

            deposit.username = "******"
            deposit.password = deposit.username
            try:
                file = File.createTempFile("tmptf", ".zip")
                file.deleteOnExit()
                fos = FileOutputStream(file.getAbsolutePath())
                IOUtils.copy(inputStream, fos)
                fos.close()
                print "copied posted data to '%s'" % file.getAbsolutePath()
            except Exception, e:
                print "--- Exception - '%s'" % str(e)
            deposit.contentDisposition = file.getAbsolutePath()  #????
            deposit.file = inputStream
            depositResponse = sword.doDeposit(deposit)
            id = str(depositResponse.getEntry().id)
            try:
                print
                #imsPlugin = PluginManager.getTransformer("ims")
                jsonConfig = JsonConfig()
                #imsPlugin.init(jsonConfig.getSystemFile())
                #harvestClient = HarvestClient(jsonConfig.getSystemFile());
                storagePlugin = PluginManager.getStorage(
                    jsonConfig.get("storage/type"))
                #storagePlugin.init(jsonConfig.getSystemFile())

                setConfigUri = self.__getPortal().getClass().getResource(
                    "/swordRule.json").toURI()
                configFile = File(setConfigUri)
                harvestConfig = JsonConfigHelper(configFile)
                tFile = File.createTempFile("harvest", ".json")
                tFile.deleteOnExit()
                harvestConfig.set("configDir", configFile.getParent())
                harvestConfig.set("sourceFile", file.getAbsolutePath())
                harvestConfig.store(FileWriter(tFile))

                zipObject = GenericDigitalObject(id)
                zipObject.addPayload(FilePayload(file, id))
                #digitalObject = imsPlugin.transform(zipObject, file)
                qStorage = QueueStorage(storagePlugin, tFile)
                qStorage.init(jsonConfig.getSystemFile())
                qStorage.addObject(zipObject)
                if deposit.noOp:
                    print "-- Testing noOp='true' --"
                else:
                    # deposit the content
                    pass
            except Exception, e:
                print "---"
                print " -- Exception - '%s'" % str(e)
                print "---"
Beispiel #3
0
    def __activate__(self, context):
        self.auth = context["page"].authentication
        self.log = context["log"]
        self.log.info("Manual VITAL integration request received")

        response = context["response"]
        writer = response.getPrintWriter("text/plain; charset=UTF-8")

        result = JsonConfigHelper()
        result.set("status", "error")
        result.set("message", "An unknown error has occurred")

        if self.auth.is_logged_in() and self.auth.is_admin():
            self.log.info("VITAL: Valid administrative user")
            formData = context["formData"]
            func = formData.get("func")
            oid = formData.get("oid")
            if func == "refresh":
                if oid:
                    self.log.info("VITAL: Refresh event: '{}'", oid)
                    self.sendMessage(oid);
                    result.set("status", "ok")
                    result.set("message", "Object '%s' queued for refresh" % oid)
                else:
                    self.log.info("VITAL: No OID provided (500)")
                    response.setStatus(500)
                    result.set("message", "No object specified for refresh")
            else:
                self.log.info("VITAL: Unknown action '{}' (500)", func)
                response.setStatus(500)
                result.set("message", "Unknown action '%s'" % func)
        else:
            self.log.info("VITAL: Not an administrative user (500)")
            response.setStatus(500)
            result.set("message", "Only administrative users can access this API")
        writer.println(result.toString(True))
        writer.close()
Beispiel #4
0
    def __processRequest(self):
        depositUrl = "%s/sword/deposit.post" % portalPath
        sword = SwordSimpleServer(depositUrl)
        try:
            p =  self.vc("request").path.split(self.vc("portalId")+"/"+self.vc("pageName")+"/")[1]  # portalPath
        except:
            p = ""
        if p=="post":
            print "\n--- post ---"
            c = sword.getClient()
            c.clearProxy()
            c.clearCredentials()
            postMsg = sword.getPostMessage();
            postMsg.filetype = "application/zip"
            postMsg.filepath = "/home/ward/Desktop/Test.zip"
            depositResponse = c.postFile(postMsg)
            return str(depositResponse)
        elif p=="servicedocument":
            #print "\n--- servicedocument ---"
            sdr = sword.getServiceDocumentRequest()
            sdr.username = self.vc("formData").get("username", "test")
            sdr.password = self.vc("formData").get("password", "test")
            if self.vc("formData").get("test"):
                depositUrl += "?test=1"
            sd = sword.doServiceDocument(sdr)  # get a serviceDocument
            out = self.vc("response").getPrintWriter("text/xml; charset=UTF-8")
            out.println(str(sd))
            out.close()
            self.velocityContext["pageName"] = "-noTemplate-"
            return sd
        elif p=="deposit.post":
            #print "\n--- deposit ---  formData='%s'" % str(formData)
            inputStream = self.vc("formData").getInputStream()
            headers = {}
            for x in self.vc("formData").getHeaders().entrySet():
                headers[x.getKey()] = x.getValue()
            deposit = sword.getDeposit()
            noOp = headers.get("X-No-Op") or "false"
            deposit.noOp = (noOp.lower()=="true") or \
                (formData.get("test") is not None)
            contentDisposition = headers.get("Content-Disposition", "")
            filename = ""
            if contentDisposition!="":
                try:
                    filename = contentDisposition.split("filename=")[1]
                    deposit.filename = filename
                except: pass
            slug = headers.get("Slug")
            if slug is not None and slug!="":
                deposit.slug = slug
            #elif filename!="":
            #    deposit.slug = filename

            deposit.username = "******"
            deposit.password = deposit.username
            try:
                file = File.createTempFile("tmptf", ".zip")
                file.deleteOnExit()
                fos = FileOutputStream(file.getAbsolutePath())
                IOUtils.copy(inputStream, fos)
                fos.close()
                print "copied posted data to '%s'" % file.getAbsolutePath()
            except Exception, e:
                print "--- Exception - '%s'" % str(e)
            deposit.contentDisposition = file.getAbsolutePath()         #????
            deposit.file = inputStream
            depositResponse = sword.doDeposit(deposit)
            id = str(depositResponse.getEntry().id)
            try:
                print
                #imsPlugin = PluginManager.getTransformer("ims")
                jsonConfig = JsonConfig()
                #imsPlugin.init(jsonConfig.getSystemFile())
                #harvestClient = HarvestClient(jsonConfig.getSystemFile());
                storagePlugin = PluginManager.getStorage(jsonConfig.get("storage/type"))
                #storagePlugin.init(jsonConfig.getSystemFile())

                setConfigUri = self.__getPortal().getClass().getResource("/swordRule.json").toURI()
                configFile = File(setConfigUri)
                harvestConfig = JsonConfigHelper(configFile);
                tFile = File.createTempFile("harvest", ".json")
                tFile.deleteOnExit()
                harvestConfig.set("configDir", configFile.getParent())
                harvestConfig.set("sourceFile", file.getAbsolutePath())
                harvestConfig.store(FileWriter(tFile))

                zipObject = GenericDigitalObject(id)
                zipObject.addPayload(FilePayload(file, id))
                #digitalObject = imsPlugin.transform(zipObject, file)
                qStorage = QueueStorage(storagePlugin, tFile)
                qStorage.init(jsonConfig.getSystemFile())
                qStorage.addObject(zipObject)
                if deposit.noOp:
                    print "-- Testing noOp='true' --"
                else:
                    # deposit the content
                    pass
            except Exception, e:
                print "---"
                print " -- Exception - '%s'" % str(e)
                print "---"