def export(self, exportType): exportQuery = "%s:%s" % (self.facetField, self.facetFieldValue) outputType = "text/%s; charset=UTF-8" % type responseHeader = "attachment; filename=%s.%s" % (self.facetFieldValue, exportType) try: out = ByteArrayOutputStream() recnumreq = SearchRequest(exportQuery) recnumreq.setParam("fl","create_timestamp") recnumreq.setParam("rows", "0") self.indexer.search(recnumreq, out) recnumres = SolrResult(ByteArrayInputStream(out.toByteArray())) self.__rowsFoundSolr = "%s" % recnumres.getNumFound() except: self.errorMsg = "Export query failure. The issue has been logged (%s - %s)." % (sys.exc_info()[0], sys.exc_info()[1]) self.log.error("Export query threw an exception (package type was %s): %s - %s" % (self.facetFieldValue, sys.exc_info()[0], sys.exc_info()[1])) return out = ByteArrayOutputStream() req = SearchRequest(exportQuery) req.setParam("wt", exportType) req.setParam("rows", self.__rowsFoundSolr) self.indexer.search(req, out) self.response.setHeader("Content-Disposition", responseHeader) writer = self.response.getPrintWriter(outputType) writer.println(out.toString("UTF-8")) writer.close()
def __call__(self, _input, profile_run=False, **kw): nsmap = dict(xsl='http://www.w3.org/1999/XSL/Transform') output_method = 'xml' output_encoding = 'utf-8' nodes = self.xsl_tree.xpath('xsl:output/@method', namespaces=nsmap) if len(nodes) > 0: output_method = nodes[0] nodes = self.xsl_tree.xpath('xsl:output/@encoding', namespaces=nsmap) if len(nodes) > 0: output_encoding = nodes[0] #print tostring(_input) if isinstance(_input, _ElementTree): doc_source = DOMSource(_input._dom_doc) elif isinstance(_input, _Element): # Xalan-J 2.7.1 does not support a DOMSource from an Element # so we build new document dom_doc = builder.newDocument() dom_root = dom_doc.importNode(_input._dom_element, True) dom_doc.appendChild(dom_root) doc_source = DOMSource(dom_doc) else: raise NotImplementedError() if output_method in ('xml', 'html'): # TODO: for testing outputstream = ByteArrayOutputStream() result = StreamResult(outputstream) self.transformer.transform(doc_source, result) bytes = outputstream.toByteArray() inputstream = ByteArrayInputStream(bytes) try: dom_doc = builder.parse(inputstream) except: import sys sys.stderr.write(bytes.tostring()) raise result_tree = _ElementTree(dom_doc) return result_tree result = DOMResult() self.transformer.transform(doc_source, result) dom_doc = result.getNode() result_tree = _ElementTree(dom_doc) #print tostring(result_tree) return result_tree else: outputstream = ByteArrayOutputStream() result = StreamResult(outputstream) self.transformer.transform(doc_source, result) resultdoc = builder.newDocument() resulttree = _XSLTResultTree(resultdoc) resulttree._text = outputstream.toString(output_encoding) return resulttree
def compile(wd,args): print "changing working directory to %s" % wd System.setProperty("user.dir",wd); print args outstream = ByteArrayOutputStream() errstream = ByteArrayOutputStream() System.setOut(PrintStream(outstream)) System.setErr(PrintStream(errstream)) try: Tom.exec(config + args) except: pass return str((outstream.toString(),errstream.toString()))
def showImgWithLegend(width=None, height=None): """ This function shows the image and legend from current IDV window while in GUI mode. Optional arguments are width and height in pixels, they currently default to 600 and 400""" from java.util import Base64 ##only in java8 from javax.imageio import ImageIO from java.io import ByteArrayOutputStream from ucar.unidata.ui.ImageUtils import resize, toBufferedImage import java import java.awt.Robot as Robot import java.awt.Rectangle as Rectangle import java.awt.Toolkit as Toolkit from ucar.unidata.util import Misc VM = idv.getViewManager() VMC = VM.getContents() VMCC = VMC.getComponent( 1 ) # the view and legend ; 0 is left most part of view window with controls for perspective views siz = VMCC.getSize() loc = VMCC.getLocationOnScreen() gc = VMCC.getGraphicsConfiguration() loc.x -= gc.getBounds().x loc.y -= gc.getBounds().y robotx = Robot(gc.getDevice()) VM.toFront() Misc.sleep(250) img = robotx.createScreenCapture( Rectangle(loc.x, loc.y, siz.width, siz.height)) if width != None and height != None: img = toBufferedImage(resize(img, width, height)) bos = ByteArrayOutputStream() ImageIO.write(img, "png", Base64.getEncoder().wrap(bos)) data = bos.toString("UTF-8") return {"display": "image", "data": data}
def image_byte_array(self): baos = ByteArrayOutputStream() ImageIO.write(self.image(), "png", baos) baos.flush() image_bytes = baos.toByteArray() baos.close() return image_bytes
def handle(self): try: raw_xml = self.rfile.readline().strip() marc_xmlfile = NamedTemporaryFile(delete=False) marc_xmlfile.write(raw_xml) marc_xmlfile.close() print(marc_xmlfile.name) dynamic_context = saxon.query.DynamicQueryContext(CONFIG) ## xml_doc = CONFIG.buildDocument( ## StreamSource(ByteArrayInputStream(raw_xml))) ## dynamic_context.setContextItem(xml_doc) dynamic_context.setParameter( "baseuri", INFO.get('base_uri', 'http://catalog/')) dynamic_context.setParameter( "marcxmluri", os.path.normpath(marc_xmlfile.name).replace("\\", "/")) dynamic_context.setParameter("serialization", "rdfxml") output_stream = ByteArrayOutputStream() result = StreamResult(output_stream) print("Before query") COMPLIED_XQUERY.run(dynamic_context, result, None) self.wfile.write(output_stream.toString().encode('ascii', errors='ignore')) os.remove(marc_xmlfile.name) except: self.wfile.write("Error processing MARC XML:\n\t{}".format(sys.exc_info()[0]))
def checkEventLogForEdits(self, oid): req = SearchRequest("oid:" + oid + " AND context:Workflow AND eventType:Save") out = ByteArrayOutputStream() self.indexer.searchByIndex(req, out, "eventLog") solrResult = SolrResult(ByteArrayInputStream(out.toByteArray())) return solrResult.getRows() > 0
def search_solr(self): query = "(rootUri:" if self.rootUriList: query += "(" + " OR ".join(self.rootUriList) + ")" else: query += "\"" + self.rootUri + "\"" if self.type: query += " AND type:\"" + self.type + "\"" query += ")" #print "**********", query req = SearchRequest(query) req.setParam("facet", "false") req.setParam("rows", str(99999)) req.setParam("sort", "dateCreated asc") req.setParam("start", str(0)) #security_roles = page.authentication.get_roles_list(); #security_query = 'security_filter:("' + '" OR "'.join(security_roles) + '")' #req.addParam("fq", security_query) out = ByteArrayOutputStream() Services.indexer.annotateSearch(req, out) result = SolrResult(ByteArrayInputStream( out.toByteArray())).getResults() # Every annotation for this URI if self.type == "http://www.purl.org/anotar/ns/type/0.1#Tag": return self.process_tags(result) else: return self.process_response(result)
def numberOfModifiedRecord(self): indexer = self.services.getIndexer() portalQuery = self.services.getPortalManager().get(self.portal.getName()).getQuery() portalSearchQuery = self.services.getPortalManager().get(self.portal.getName()).getSearchQuery() # Security prep work current_user = self.page.authentication.get_username() security_roles = self.page.authentication.get_roles_list() security_filter = 'security_filter:("' + '" OR "'.join(security_roles) + '")' security_exceptions = 'security_exception:"' + current_user + '"' owner_query = 'owner:"' + current_user + '"' security_query = "(" + security_filter + ") OR (" + security_exceptions + ") OR (" + owner_query + ")" req = SearchRequest("modified:true") req.setParam("fq", 'item_type:"object"') if portalQuery: req.addParam("fq", portalQuery) if portalSearchQuery: req.addParam("fq", portalSearchQuery) req.addParam("fq", "") req.setParam("rows", "0") if not self.page.authentication.is_admin(): req.addParam("fq", security_query) out = ByteArrayOutputStream() indexer.search(req, out) self.__result = JsonSimpleConfig(ByteArrayInputStream(out.toByteArray())) return self.__result.getString(None, "response", "numFound")
def showImgWithFullWindow(width=None, height=None): """ This function shows the image from current IDV window while in GUI mode. optional arguments are width and height in pixels, they currently default to 600 and 400""" from java.util import Base64 ##only in java8 from javax.imageio import ImageIO from java.io import ByteArrayOutputStream from ucar.unidata.ui.ImageUtils import resize, toBufferedImage import java import java.awt.Robot as Robot import java.awt.Rectangle as Rectangle import java.awt.Toolkit as Toolkit from ucar.unidata.util import Misc VM = idv.getViewManager() myframe = VM.getDisplayWindow().getComponent() robotx = Robot(myframe.getGraphicsConfiguration().getDevice()) VM.toFront() #robotx.delay(250) Misc.sleep(350) pause() img = robotx.createScreenCapture( Rectangle(myframe.getX(), myframe.getY(), myframe.getWidth(), myframe.getHeight())) if width != None and height != None: img = toBufferedImage(resize(img, width, height)) bos = ByteArrayOutputStream() ImageIO.write(img, "png", Base64.getEncoder().wrap(bos)) data = bos.toString("UTF-8") return {"display": "image", "data": data}
def handleGrantNumber(self): out = ByteArrayOutputStream() req = SearchRequest("grant_numbers:%s*" % self.term) req.setParam("fq", 'item_type:"object"') req.setParam("fq", 'workflow_id:"dataset"') req.setParam("rows", "1000") self.indexer.search(req, out) res = SolrResult(ByteArrayInputStream(out.toByteArray())) hits = HashSet() if (res.getNumFound() > 0): creatorResults = res.getResults() for creatorRes in creatorResults: creatorList = creatorRes.getList("grant_numbers") if (creatorList.isEmpty()==False): for hit in creatorList: hits.add(hit) self.writer.print("[") hitnum = 0 for hit in hits: if (hitnum > 0): self.writer.print(",\"%s\"" % hit) else: self.writer.print("\"%s\"" % hit) hitnum += 1 self.writer.print("]") else: self.writer.println("[\"\"]") self.writer.close()
def __getUsers(self, oid): indexer = Services.getIndexer() req = SearchRequest("id:" + oid) req.setParam("fl", "security_exception,owner") out = ByteArrayOutputStream() indexer.search(req, out) rtJson = "" try: qresult = SolrResult(ByteArrayInputStream( out.toByteArray())).getResults().get(0) owner = qresult.getString(None, 'owner') secException = qresult.getArray('security_exception') if secException is None: secException = JSONArray() self.log.debug("Owner of object: " + owner) self.log.debug("Viewer(s) of object: " + secException.toString()) if secException.contains(owner): secException.remove(owner) return '{"owner":"' + owner + '", "viewers": ' + secException.toString( ) + '}' except Exception, e: self.log.error("Error during query/package ownership data" + str(e))
def buildRequest(self, payload): """ This is the main method through which an extension interacts with a IScannerInsertionPoint instance. They provide the payload through the payload parameter and we replace it in our request. If the parameter type is something that could be handled by Burp's helpers we update it in that way, otherwise we do it by modifying the byte arrays directly. Args: payload: the active scanner payload provided by the extension. """ start = self.start end = self.end if self.type == IScannerInsertionPoint.INS_PARAM_JSON: start, end, payload = self.encodeJson(start, end, payload) elif self.type == IScannerInsertionPoint.INS_HEADER: pass elif self.type in [IScannerInsertionPoint.INS_PARAM_XML, IScannerInsertionPoint.INS_PARAM_XML_ATTR]: start, end, payload = self.encodeXml(start, end, payload) else: start, end, payload = self.encodeUrl(start, end, payload) stream = ByteArrayOutputStream() stream.write(self.request[0:start]) stream.write(payload) stream.write(self.request[end:]) newRequestBytes = self.updateContentLength(stream.toByteArray()) return newRequestBytes
def __search(self): query = self.formData.get("query") searchQuery = self.sessionState.get("searchQuery") if query is None or query == "": query = "*:*" if searchQuery and query == "*:*": query = searchQuery elif searchQuery: query += " AND " + searchQuery facetField = self.formData.get("facet.field") req = SearchRequest(query) req.setParam("facet", "true") req.setParam("fl", "id") req.setParam("rows", "0") req.setParam("facet.limit", "-1") req.setParam("facet.field", facetField) fq = self.sessionState.get("fq") if fq is not None: req.setParam("fq", fq) req.addParam("fq", 'item_type:"object"') # Make sure 'fq' has already been set in the session security_roles = self.auth.get_roles_list() security_query = 'security_filter:("' + '" OR "'.join( security_roles) + '")' req.addParam("fq", security_query) out = ByteArrayOutputStream() indexer = self.services.indexer indexer.search(req, out) result = SolrResult(ByteArrayInputStream(out.toByteArray())) return FacetList(facetField, result)
def __feed(self): self.portal = self.services.getPortalManager().get(self.vc("portalId")) recordsPerPage = self.portal.recordsPerPage pageNum = self.vc("sessionState").get("pageNum", 1) query = "*:*" if self.vc("formData").get("query"): query = self.vc("formData").get("query") query = self.__escapeQuery(query) req = SearchRequest(query) req.setParam("facet", "true") req.setParam("rows", str(recordsPerPage)) req.setParam("facet.field", self.portal.facetFieldList) req.setParam("facet.sort", "true") req.setParam("facet.limit", str(self.portal.facetCount)) req.setParam("sort", "f_dc_title asc") portalQuery = self.portal.query if portalQuery: req.addParam("fq", portalQuery) else: fq = self.vc("sessionState").get("fq") if fq is not None: req.setParam("fq", fq) req.setParam("start", str((pageNum - 1) * recordsPerPage)) self.log.debug(" * Query: '{}'", query) self.log.debug(" * portalQuery: '{}'", portalQuery) self.log.debug(" * feed.py: '{}'", req) out = ByteArrayOutputStream() self.services.indexer.search(req, out) self.__result = SolrResult(ByteArrayInputStream(out.toByteArray()))
def parseFFmpeg(self, parent): if parent is not None: object = parent.getObject() if object is not None: payload = None try: payload = object.getPayload("ffmpeg.info") # Stream the content out to string out = ByteArrayOutputStream() IOUtils.copy(payload.open(), out) payload.close() self.__ffmpegRaw = out.toString("UTF-8") out.close() payload.close() # And parse it jsonData = JsonConfigHelper(self.__ffmpegRaw) if jsonData is None: return False else: self.__ffmpegData = jsonData.getJsonMap("/outputs") return True except: if payload is not None: payload.close() return False
def tostring(element_or_tree, encoding=None, method='xml', xml_declaration=None, pretty_print=False, with_tail=True, standalone=None, doctype=None, exclusive=False, with_comments=True, inclusive_ns_prefixes=None): if isinstance(element_or_tree, _ElementTree): source = DOMSource(element_or_tree._dom_doc) else: source = DOMSource(element_or_tree._dom_element) outputstream = ByteArrayOutputStream() result = StreamResult(outputstream) transformer = transformfac.newTransformer() if xml_declaration: transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, 'no') else: transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, 'yes') if pretty_print: transformer.setOutputProperty(OutputKeys.INDENT, 'yes') else: transformer.setOutputProperty(OutputKeys.INDENT, 'no') transformer.transform(source, result) if encoding is None: encoding = 'ascii' return outputstream.toString(encoding)
def findPublishedRecords(self): #req = SearchRequest("published:\"true\"") req = SearchRequest("storage_id:\"c6a214670dc644e5ebdaede4a2243f67\"") out = ByteArrayOutputStream() self.indexer.search(req, out) solrResult = SolrResult(ByteArrayInputStream(out.toByteArray())) return solrResult.getResults()
def handleWorkflowStep(self): out = ByteArrayOutputStream() req = SearchRequest("workflow_step_label:[* TO *]" ) req.setParam("fq", 'item_type:"object"') req.setParam("fq", 'workflow_id:"dataset"') req.setParam("rows", "1000") self.indexer.search(req, out) res = SolrResult(ByteArrayInputStream(out.toByteArray())) hits = HashSet() if (res.getNumFound() > 0): recordTypeResults = res.getResults() for recordTypeResult in recordTypeResults: recordTypeList = recordTypeResult.getList("workflow_step_label") if (recordTypeList.isEmpty()==False): for hit in recordTypeList: hits.add(hit) self.writer.println("[") hitnum = 0 for hit in hits: if (hitnum > 0): self.writer.println(",{\"value\": \"%s\",\n\"label\": \"%s\"}" % (hit,hit)) else: self.writer.println("{\"value\": \"%s\",\n\"label\": \"%s\"}" % (hit,hit)) hitnum += 1 self.writer.println("]") else: self.writer.println("[\"\"]") self.writer.close()
def handleQuery(self, query, fieldName, formatStr): out = ByteArrayOutputStream() req = SearchRequest(query) req.setParam("fq", 'item_type:"object"') req.setParam("fq", 'workflow_id:"dataset"') req.setParam("rows", "1000") self.indexer.search(req, out) res = SolrResult(ByteArrayInputStream(out.toByteArray())) hits = HashSet() if (res.getNumFound() > 0): results = res.getResults() for searchRes in results: searchResList = searchRes.getList(fieldName) if (searchResList.isEmpty() == False): for hit in searchResList: if self.term is not None: if hit.find(self.term) != -1: hits.add(hit) else: hits.add(hit) self.writer.print("[") hitnum = 0 for hit in hits: if (hitnum > 0): self.writer.print("," + formatStr % {"hit": hit}) else: self.writer.print(formatStr % {"hit": hit}) hitnum += 1 self.writer.print("]") else: self.writer.println("[\"\"]") self.writer.close()
def _toPDF(fis) : b = ByteArrayOutputStream() # create a new document document = Document(); # get Instance of the PDFWriter pdfWriter = PdfWriter.getInstance(document, b) # document header attributes document.addAuthor("betterThanZero") document.addCreationDate() document.addProducer() document.addCreator("MySampleCode.com") document.addTitle("Demo for iText XMLWorker") document.setPageSize(PageSize.LETTER); # open document document.open(); # To convert a HTML file from the filesystem # String File_To_Convert = "docs/SamplePDF.html"; # //FileInputStream fis = new FileInputStream(File_To_Convert); # URL for HTML page # get the XMLWorkerHelper Instance worker = XMLWorkerHelper.getInstance(); # convert to PDF worker.parseXHtml(pdfWriter, document, fis); # close the document document.close(); # close the writer pdfWriter.close(); return b.toByteArray()
def getProperBase64EncodingOfFloatArr(cur_scan_arr): baos = ByteArrayOutputStream() ds = DataOutputStream(baos) for i in cur_scan_arr: ds.writeFloat(i) ds.flush() return base64.b64encode(baos.toByteArray())
def _addPropertyValueToTFMeta(self, object, tfMetaPropertyValue): objectMetadata = object.getMetadata() objectMetadata.setProperty("copyTFPackage", tfMetaPropertyValue) output = ByteArrayOutputStream(); objectMetadata.store(output, None); input = ByteArrayInputStream(output.toByteArray()); StorageUtils.createOrUpdatePayload(object,"TF-OBJ-META",input);
def printError(self, e): from org.python.core import PyException from java.io import ByteArrayOutputStream, PrintStream import traceback if isinstance(e, PyException): self.reflectChanges = 0 out = ByteArrayOutputStream() e.printStackTrace(PrintStream(out)) self.addOutput(self.errorColor, u"\n%s" % unicode(out)[:-1])
def __searchExecute(self, search, count): try: search.setParam("start", str(count)) out = ByteArrayOutputStream() self.services.indexer.search(search, out) return SolrResult(ByteArrayInputStream(out.toByteArray())) except Exception, e: self.log.error("Error during search: ", e) return None
def BufferedImgToNotebook(img): from java.io import ByteArrayOutputStream from java.util import Base64 ##only in java8 from javax.imageio import ImageIO bos = ByteArrayOutputStream() ImageIO.write(img, "png", Base64.getEncoder().wrap(bos)) data = bos.toString("UTF-8") return {"display": "image", "data": data}
def buildLarchJar(outputStream, additionalFilesAsNameBytesPairs, filterFn=None, larchJarURL=None): """ Build a JAR from an existing Larch JAR, along with additional files to make a packaged program :param outputStream: The output stream to which the JAR is to be written :param additionalFilesAsNameBytesPairs: Additional files in the form of a sequence of tuples consisting of (path, bytes) :param filterFn: (optional) A filter function that can be used to exclude files from the existing Larch JAR; takes the form of function(name) -> boolean, should return True if file should be included :param larchJarURL: (optional) A URL at which the existing Larch JAR can be obtained. If None, it will use the JAR from which Larch was started. Raises an error if no URL provided and Larch was not started from a JAR """ if larchJarURL is None: larchJarURL = _larchJarURL if larchJarURL is None: raise RuntimeError, 'Larch was not loaded from a JAR file and no Larch JAR file was provided' jarIn = JarInputStream(larchJarURL.openStream()) manifestIn = jarIn.getManifest() manifestOut = Manifest(manifestIn) jarOut = JarOutputStream(outputStream, manifestOut) bytesBuffer = jarray.zeros(_BYTES_BUFFER_SIZE, 'b') entryIn = jarIn.getNextJarEntry() while entryIn is not None: name = entryIn.getName() if filterFn is None or filterFn(name): bufferStream = ByteArrayOutputStream() while True: bytesRead = jarIn.read(bytesBuffer, 0, _BYTES_BUFFER_SIZE) if bytesRead == -1: break bufferStream.write(bytesBuffer, 0, bytesRead) entryOut = ZipEntry(name) entryOut.setSize(bufferStream.size()) jarOut.putNextEntry(entryOut) bufferStream.writeTo(jarOut) jarOut.closeEntry() entryIn = jarIn.getNextJarEntry() for name, bytes in additionalFilesAsNameBytesPairs: size = len(bytes) entryOut = ZipEntry(name) entryOut.setSize(size) jarOut.putNextEntry(entryOut) jarOut.write(bytes, 0, size) jarOut.closeEntry() jarOut.finish()
def __init__(self, stdout=True): if stdout: self._original = System.out self._set_stream = System.setOut else: self._original = System.err self._set_stream = System.setErr self._bytes = ByteArrayOutputStream() self._stream = PrintStream(self._bytes, False, 'UTF-8') self._set_stream(self._stream)
def test_serialization(self): s = set(range(5, 10)) output = ByteArrayOutputStream() serializer = ObjectOutputStream(output) serializer.writeObject(s) serializer.close() input = ByteArrayInputStream(output.toByteArray()) unserializer = ObjectInputStream(input) self.assertEqual(s, unserializer.readObject())
def getFileBytes(path): baos = ByteArrayOutputStream() fis = FileInputStream(File(path)) bytes = jarray.zeros(1024, 'b') while 1 == 1: readSize = fis.read(bytes) if (readSize == -1): break baos.write(bytes, 0, readSize) return baos.toByteArray()