beautypg.com

Java code – Google Search Appliance Feeds Protocol Developers Guide User Manual

Page 26

background image

Google Search Appliance: Feeds Protocol Developer’s Guide

26

def PostMultipart(theurl, fields, files):

"""Create the POST request by encoding data and adding headers."""
content_type, body = EncodeMultipartFormdata(fields, files)
headers = {}
headers["Content-type"] = content_type
headers["Content-length"] = str(len(body))
return urllib2.Request(theurl, body, headers)

def EncodeMultipartFormdata(fields, files):

"""Create data in multipart/form-data encoding."""
boundary = "----------boundary_of_feed_data$"
crlf = "\r\n"
l = []
for (key, value) in fields:

l.append("--" + boundary)
l.append('Content-Disposition: form-data; name="%s"' % key)
l.append("")
l.append(value)

for (key, filename, value) in files:

l.append("--" + boundary)
l.append('Content-Disposition: form-data; name="%s"; filename="%s"'

% (key, filename))

l.append("Content-Type: %s" % GetContentType(filename))
l.append("")
l.append(value)

l.append("--" + boundary + "--")
l.append("")
body = crlf.join(l)
content_type = "multipart/form-data; boundary=%s" % boundary
return content_type, body

def GetContentType(filename):

"""Determine the content-type of data file."""
return mimetypes.guess_type(filename)[0] or "application/octet-stream"

if __name__ == "__main__":

main(sys.argv)

For more information about developing a feed client, see “Pushing a Feed to the Google Search
Appliance” on page 27
.

Java Code

In Java, use the DocIdPusher.pushGroupDefinitions() method to send groups to the Google Search
Appliance.