def rest_delete_proc (uri_aa): delete = DeleteMethod (uri_aa) # httpclient = HttpClient () result = httpclient.executeMethod (delete) print ("Response status code: %d" % result) delete.releaseConnection ()
def rest_put_proc (uri_aa,str_data_in,type_in): # print ("*** rest_put_proc ***") put = PutMethod (uri_aa) entity = StringRequestEntity (str_data_in, type_in, "UTF-8") put.setRequestEntity (entity) httpclient = HttpClient() result = httpclient.executeMethod(put) # print ("Response status code: %d" % result) put.releaseConnection()
def doHttpGet(url, timeout=20000, requestedData='body', headerName=None): """ Performs HTTP(S) Connection to the specified URL Returns data according to the requestedData flag: 'body': Full Response Body as String 'header': Returns the response header with the specified headerName """ if requestedData == 'header': method = HeadMethod(url) else: method = GetMethod(url) client = HttpClient() client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout) client.getHttpConnectionManager().getParams().setSoTimeout(timeout) client.executeMethod(method) if (requestedData == 'body'): return method.getResponseBodyAsString() elif (requestedData == 'header'): if headerName: return method.getResponseHeader(headerName) else: result = method.getResponseHeaders() return ''.join([s.toString() for s in result]) else: raise ValueError('Response part %s in not supported' % requestedData)
def saveHM(filename): # Create new HTTP client client = HttpClient() client.getParams().setAuthenticationPreemptive(True) defaultcreds = UsernamePasswordCredentials(user, password) client.getState().setCredentials(AuthScope.ANY, defaultcreds) # Get data across HTTP url = ( "http://" + host + ":" + str(port) + "/admin/savedataview.egi?type=" + type + "&data_format=NEXUSHDF5_LZW_6&data_saveopen_action=OPEN_ONLY" ) getMethod = GetMethod(url) getMethod.setDoAuthentication(True) client.executeMethod(getMethod) # Save locally file = File(directory + "/" + filename) out = FileOutputStream(file) out.write(getMethod.getResponseBody()) out.close() # Clean up getMethod.releaseConnection()
def enhancePages(self, project, connector_url, cq_user, cq_password, page_query, page_queryLib): # Validate Parameters if not self.validInputs(connector_url, cq_user, cq_password, "asset_drive", "asset_filenameAttr", "asset_pathAttr", page_query, page_queryLib, "ia_path", "ia_cacheCols"): return # HTTP Connection Details client = HttpClient() base64EncodedCredentials = Base64.encodeString("%s:%s" % (cq_user, cq_password)) header = Header("Authorization", "Basic %s" % (base64EncodedCredentials)) # Cycle through contentdescriptors specified by scope query query = queryLibrarianService.findQueryByName(page_queryLib, page_query) params = HashMap() params.put('projectId', project.id) res = queryLibrarianService.executeQuery(query, params) # Get cd for r in res: cd = r[0] post = PostMethod(connector_url) post.addRequestHeader(header) get = GetMethod(connector_url) get.addRequestHeader(header) try: payload = cd.getContent()[0].getContentData().encode("utf-8") [loadStatus, failureReason] = self.sendPayload(client, get, post, payload, cd.url) cd.metadata["Load.Status"] = loadStatus cd.metadata["Load.Failure Reason"] = failureReason if loadStatus != "SUCCESS": logger.error("url: %s, reason: %s" % (cd.url, failureReason)) except: cd.metadata["Load.Status"] = "FAILURE" cd.metadata["Load.Failure Reason"] = "Null content" logger.error("url: %s, reason: %s" % (cd.url, failureReason))
def enhanceIA(self, connector_url, cq_user, cq_password, ia_path, ia_cacheCols): # Validate Parameters if not self.validInputs(connector_url, cq_user, cq_password, "asset_drive", "asset_filenameAttr", "asset_pathAttr", "page_query", "page_queryLib", ia_path, ia_cacheCols): return # HTTP Connection Details client = HttpClient() base64EncodedCredentials = Base64.encodeString("%s:%s" % (cq_user, cq_password)) header = Header("Authorization", "Basic %s" % (base64EncodedCredentials)) # IA Build cached_IA = self.cacheIA(ia_path, ia_cacheCols) for placeholder in cached_IA: post = PostMethod(connector_url) post.addRequestHeader(header) get = GetMethod(connector_url) get.addRequestHeader(header) # Prep Payload path = placeholder["Path"] filename = placeholder["Filename"] template_path = placeholder["Template Path"] label = placeholder["Label"] # Send Payload payload = '<page path="%s" filename="%s" template_path="%s" label="%s"><node path="jcr:content" type="nt:unstructured"><prop name="jcr:title" type="String">%s</prop></node></page>' % ( path, filename, template_path, label, label) [loadStatus, failureReason] = self.sendPayload(client, get, post, payload, path + filename) if loadStatus != "SUCCESS": logger.error("url: %s, reason: %s" % (placeholder["URL"], failureReason))
def enhance(self, contentDescriptor, content, connector_url, cq_user, cq_password, asset_doWebDAV, asset_skeleton, asset_transformationRules, asset_drive, asset_filenameAttr, asset_pathAttr): # Validate Parameters if not self.validInputs(connector_url, cq_user, cq_password, asset_drive, asset_filenameAttr, asset_pathAttr, "page_query", "page_querylib", "ia_path", "ia_cacheCols"): return if asset_doWebDAV: # Save Asset to WebDAV Drive drive = asset_drive[0] path = re.sub("/content/dam/", "/var/dam/", contentDescriptor.metadata[asset_pathAttr]) filename = contentDescriptor.metadata[asset_filenameAttr] filepath = "%s:%s/%s" % (drive, path, filename) if not exists("%s:%s" % (drive, path)): makedirs("%s:%s" % (drive, path)) file = BufferedOutputStream(FileOutputStream(filepath)) data = Base64.decode(content.contentData) try: file.write(data, 0, len(data)) file.close() contentDescriptor.metadata["Load.Status"] = "SUCCESS" except: logger.error("! [WebDAV] Could not write %s to file system" % (contentDescriptor.url)) file.close() contentDescriptor.metadata["Load.Status"] = "FAILURE" contentDescriptor.metadata[ "Load.Failure Reason"] = "! [WebDAV] Could not deposit file on file system" return # Build & Send Asset Metadata Payload if required if asset_skeleton and asset_transformationRules: # Build Payload payload = self.buildAssetPayload(asset_skeleton, asset_transformationRules, content, contentDescriptor) # HTTP Connection Details client = HttpClient() base64EncodedCredentials = Base64.encodeString( "%s:%s" % (cq_user, cq_password)) header = Header("Authorization", "Basic %s" % (base64EncodedCredentials)) # Send Metadata Payload post = PostMethod(connector_url) post.addRequestHeader(header) get = GetMethod(connector_url) get.addRequestHeader(header) [loadStatus, failureReason] = self.sendPayload(client, get, post, payload, contentDescriptor.url) contentDescriptor.metadata["Load.Status"] = loadStatus if loadStatus == "SUCCESS": contentDescriptor.metadata["Load.Failure Reason"] = '' else: contentDescriptor.metadata[ "Load.Failure Reason"] = "Metadata: " + failureReason logger.error("! [METADATA] %s" % (failureReason))