def testSimpleHttpPost(self):
        session = HttpSession.makeHttpSession(self.endpointhost, self.basepath, self.username, self.password)
        fields = [("id", "test-dataset")]
        files = []
        (reqtype, reqdata) = session.encode_multipart_formdata(fields, files)
        (responsetype, responsedata) = session.doHTTP_POST(
            endpointpath=self.basepath,
            resource="datasets",
            data=reqdata,
            data_type=reqtype,
            expect_status=201,
            expect_reason="Created",
            accept_type="*/*",
        )
        self.assertEquals(responsetype, "text/plain", "Create dataset response type: " + responsetype)
        self.assertEquals(responsedata, "201 Created", "Create dataset response data: " + responsedata)

        # self.assertTrue(re.search("<title>.*List of Datasets.*</title>", responsedata) != None, "Create dataset response data")

        # Do GET to datasets/test-dataset
        (responsetype, responsedata) = session.doHTTP_GET(
            endpointpath=self.basepath,
            resource="datasets/test-dataset",
            expect_status=200,
            expect_reason="OK",
            accept_type="*/*",
        )
        self.assertEquals(responsetype, "text/html", "Get dataset response type")
        logger.debug("responsetype" + responsetype)
        self.assertTrue(re.search(".*Root directory of.*", responsedata) != None, "Get dataset response data")
        return
 def setUp(self):
     self.endpointhost = TestConfig.HostName
     self.basepath = "/" + TestConfig.SiloName + "/"
     self.session = HttpSession.makeHttpSession(
         self.endpointhost, self.basepath, TestConfig.Username, TestConfig.Password
     )
     return
 def setUp(self):
     self.endpointhost = TestConfig.HostName
     self.basepath = "/" + TestConfig.SiloName + "/"
     self.session = HttpSession.makeHttpSession(self.endpointhost,
                                                self.basepath,
                                                TestConfig.Username,
                                                TestConfig.Password)
     return
 def testSimpleHttpGet(self):
     session = HttpSession.makeHttpSession(self.endpointhost, self.basepath, self.username, self.password)
     (responsetype, responsedata) = session.doHTTP_GET(
         endpointpath=self.basepath, resource="datasets", expect_status=200, expect_reason="OK", accept_type="*/*"
     )
     self.assertEquals(responsetype, "text/html", "List datasets response type")
     self.assertTrue(
         re.search("<title>.*List of Datasets.*</title>", responsedata) != None, "List datasets response data"
     )
     return
    def Run(self):
        selected = []
        framecollector = FrameCollector(self.dicts)

        #self.fragments=FrameCollector(self.dicts).results#tcpseq, orders: order: node: next
        httpSession = HttpSession(self.dicts, framecollector.results,
                                  framecollector.frags,
                                  framecollector.requests,
                                  framecollector.responses)
        self.sessions = httpSession.sessions
        self.timedicts = httpSession.timedicts
 def testSimpleHttpDelete(self):
     session = HttpSession.makeHttpSession(self.endpointhost, self.basepath, self.username, self.password)
     (responsetype, responsedata) = session.doHTTP_DELETE(endpointpath=self.basepath, resource="datasets/test-dataset", expect_status=200, expect_reason="OK")
     #self.assertEquals(responsetype, "text/html", "Delete dataset response type")
     #self.assertTrue(re.search("<title>.*Del Dataset.*</title>", responsedata) != None, "Delete dataset response data")
     
     # Do GET to datasets/test-dataset
     (responsetype, responsedata) = session.doHTTP_GET(endpointpath=self.basepath, resource="datasets/test-dataset", expect_status=404, expect_reason="Not Found", accept_type="*/*")
     #self.assertEquals(responsetype, "text/html", "Get dataset response type")
     #self.assertTrue(re.search(".*Root directory of.*", responsedata) != None, "Get dataset response data")       
     return
 def testSimpleHttpGet(self):
     session = HttpSession.makeHttpSession(self.endpointhost, self.basepath,
                                           self.username, self.password)
     (responsetype,
      responsedata) = session.doHTTP_GET(endpointpath=self.basepath,
                                         resource="datasets",
                                         expect_status=200,
                                         expect_reason="OK",
                                         accept_type="*/*")
     self.assertEquals(responsetype, "text/html",
                       "List datasets response type")
     self.assertTrue(
         re.search("<title>.*List of Datasets.*</title>", responsedata) !=
         None, "List datasets response data")
     return
    def testSimpleHttpDelete(self):
        session = HttpSession.makeHttpSession(self.endpointhost, self.basepath,
                                              self.username, self.password)
        (responsetype, responsedata) = session.doHTTP_DELETE(
            endpointpath=self.basepath,
            resource="datasets/test-dataset",
            expect_status=200,
            expect_reason="OK")
        #self.assertEquals(responsetype, "text/html", "Delete dataset response type")
        #self.assertTrue(re.search("<title>.*Del Dataset.*</title>", responsedata) != None, "Delete dataset response data")

        # Do GET to datasets/test-dataset
        (responsetype,
         responsedata) = session.doHTTP_GET(endpointpath=self.basepath,
                                            resource="datasets/test-dataset",
                                            expect_status=404,
                                            expect_reason="Not Found",
                                            accept_type="*/*")
        #self.assertEquals(responsetype, "text/html", "Get dataset response type")
        #self.assertTrue(re.search(".*Root directory of.*", responsedata) != None, "Get dataset response data")
        return
    def testSimpleHttpPost(self):
        session = HttpSession.makeHttpSession(self.endpointhost, self.basepath,
                                              self.username, self.password)
        fields = \
            [ ("id", "test-dataset")
            ]
        files = []
        (reqtype, reqdata) = session.encode_multipart_formdata(fields, files)
        (responsetype,
         responsedata) = session.doHTTP_POST(endpointpath=self.basepath,
                                             resource="datasets",
                                             data=reqdata,
                                             data_type=reqtype,
                                             expect_status=201,
                                             expect_reason="Created",
                                             accept_type="*/*")
        self.assertEquals(responsetype, "text/plain",
                          "Create dataset response type: " + responsetype)
        self.assertEquals(responsedata, "201 Created",
                          "Create dataset response data: " + responsedata)

        #self.assertTrue(re.search("<title>.*List of Datasets.*</title>", responsedata) != None, "Create dataset response data")

        # Do GET to datasets/test-dataset
        (responsetype,
         responsedata) = session.doHTTP_GET(endpointpath=self.basepath,
                                            resource="datasets/test-dataset",
                                            expect_status=200,
                                            expect_reason="OK",
                                            accept_type="*/*")
        self.assertEquals(responsetype, "text/html",
                          "Get dataset response type")
        logger.debug("responsetype" + responsetype)
        self.assertTrue(
            re.search(".*Root directory of.*", responsedata) != None,
            "Get dataset response data")
        return
    def POST(self):
        print "POST in AdminUIFormHandler"
        reqdata = web.data()
        print "Request POST data obtained from the client before redirection" + reqdata
        jsonInputData = json.loads(reqdata)
        print "JSON INPUT DATA =" + repr(jsonInputData)

        UserID = jsonInputData["UserID"]
        print "UserID= " + UserID
        # FullName   =  jsonInputData["UserFullName"]
        # Role       =  jsonInputData["UserRole"]
        RoomNumber = jsonInputData["UserRoomNumber"]
        print "Room Number: " + RoomNumber
        # WorkPhone  =  jsonInputData["UserWorkPhone"]
        # Password   =  jsonInputData["UserPassword"]
        Operation = jsonInputData["UserOperation"]
        print "Operation = " + Operation

        if Operation == "Modify":
            method = "PUT"
            url = "/user/" + UserID
        elif Operation == "Add":
            method = "POST"
            url = "/users/" + UserID
        elif Operation == "Delete":
            method = "DELETE"
            url = "/user/" + UserID

        endpointhost = "localhost"
        basepath = "/user/" + UserID
        responsedata = ""

        if not web.ctx.environ.has_key("HTTP_AUTHORIZATION") or not web.ctx.environ["HTTP_AUTHORIZATION"].startswith(
            "Basic "
        ):
            return web.Unauthorized()
        else:
            hash = web.ctx.environ["HTTP_AUTHORIZATION"][6:]
            remoteUser, remotePasswd = base64.b64decode(hash).split(":")

        auth = base64.encodestring("%s:%s" % (remoteUser, remotePasswd))
        headers = {"Authorization": "Basic %s" % auth}
        data = reqdata  # jsonInputData
        # data = web.http.urlencode(jsonInputData)
        print "URL encoded data before sending=" + repr(data)
        try:
            session = HttpSession.makeHttpSession(endpointhost, basepath, remoteUser, remotePasswd)
            # encodeddata = session.encode_multipart_formdata(jsonInputData)
            # (responsetype, responsedata)=session.doHTTP_PUT( resource='/user/'+UserID,data=None, data_type='application/JSON', expect_status=200, expect_reason="OK")
            # (responsetype, responsedata)=session.doHTTP_GET( resource='/users', expect_status=200, expect_reason="OK")
            connection = httplib.HTTPConnection("localhost", timeout=30)
            connection.request(method, url, body=data, headers=headers)
            # connection.endheaders()
            # connection.send(jsonInputData)
            response = connection.getresponse()
            responsedata = response.read()
        # connection.close()
        # req = urllib2.Request(url, data)
        # response = urllib2.urlopen(req)
        # responsedata = response.read()
        except session.HTTPSessionError, e:
            # AdminUIHandlerUtils.printHTMLHeaders()
            # AdminUIHandlerUtils.generateErrorResponsePage(AdminUIHandlerUtils.HTTP_ERROR,e.code, e.reason)
            # AdminUIHandlerUtils.printStackTrace()
            returnString = '{"redirect":"' + e.code + "-" + e.reason + '"}'
            print returnString
            return returnString
    def POST(self):
        print "POST in AdminUIFormHandler"
        reqdata = web.data()
        print "Request POST data obtained from the client before redirection" + reqdata
        jsonInputData = json.loads(reqdata)
        print "JSON INPUT DATA =" + repr(jsonInputData)

        UserID = jsonInputData["UserID"]
        print "UserID= " + UserID
        # FullName   =  jsonInputData["UserFullName"]
        # Role       =  jsonInputData["UserRole"]
        RoomNumber = jsonInputData["UserRoomNumber"]
        print "Room Number: " + RoomNumber
        # WorkPhone  =  jsonInputData["UserWorkPhone"]
        # Password   =  jsonInputData["UserPassword"]
        Operation = jsonInputData["UserOperation"]
        print "Operation = " + Operation

        if Operation == "Modify":
            method = "PUT"
            url = '/user/' + UserID
        elif Operation == "Add":
            method = "POST"
            url = '/users/' + UserID
        elif Operation == "Delete":
            method = "DELETE"
            url = '/user/' + UserID

        endpointhost = "localhost"
        basepath = '/user/' + UserID
        responsedata = ""

        if not web.ctx.environ.has_key(
                'HTTP_AUTHORIZATION'
        ) or not web.ctx.environ['HTTP_AUTHORIZATION'].startswith('Basic '):
            return web.Unauthorized()
        else:
            hash = web.ctx.environ['HTTP_AUTHORIZATION'][6:]
            remoteUser, remotePasswd = base64.b64decode(hash).split(':')

        auth = base64.encodestring("%s:%s" % (remoteUser, remotePasswd))
        headers = {"Authorization": "Basic %s" % auth}
        data = reqdata  #jsonInputData
        #data = web.http.urlencode(jsonInputData)
        print "URL encoded data before sending=" + repr(data)
        try:
            session = HttpSession.makeHttpSession(endpointhost, basepath,
                                                  remoteUser, remotePasswd)
            # encodeddata = session.encode_multipart_formdata(jsonInputData)
            # (responsetype, responsedata)=session.doHTTP_PUT( resource='/user/'+UserID,data=None, data_type='application/JSON', expect_status=200, expect_reason="OK")
            # (responsetype, responsedata)=session.doHTTP_GET( resource='/users', expect_status=200, expect_reason="OK")
            connection = httplib.HTTPConnection('localhost', timeout=30)
            connection.request(method, url, body=data, headers=headers)
            # connection.endheaders()
            # connection.send(jsonInputData)
            response = connection.getresponse()
            responsedata = response.read()
        # connection.close()
        # req = urllib2.Request(url, data)
        # response = urllib2.urlopen(req)
        # responsedata = response.read()
        except session.HTTPSessionError, e:
            #AdminUIHandlerUtils.printHTMLHeaders()
            # AdminUIHandlerUtils.generateErrorResponsePage(AdminUIHandlerUtils.HTTP_ERROR,e.code, e.reason)
            # AdminUIHandlerUtils.printStackTrace()
            returnString = '{"redirect":"' + e.code + "-" + e.reason + '"}'
            print returnString
            return returnString
def processDatasetSubmissionForm(formdata, outputstr):
    """
    Process form data, and print (to stdout) a new HTML page reflecting
    the outcome of the request.
    
    formdata    is a dictionary containing parameters from the dataset submission form
    """
    userName             =  SubmitDatasetUtils.getFormParam("user"        ,  formdata)
    userPass             =  SubmitDatasetUtils.getFormParam("pass"        ,  formdata)
    endpointhost         =  SubmitDatasetUtils.getFormParam("endpointhost",  formdata)
    basepath             =  SubmitDatasetUtils.getFormParam("basepath"    ,  formdata) 
    datasetName          =  SubmitDatasetUtils.getFormParam("datId"       ,  formdata)  
    title                =  SubmitDatasetUtils.getFormParam("title"       ,  formdata)  
    description          =  SubmitDatasetUtils.getFormParam("description" ,  formdata)  
    dirName              =  SubmitDatasetUtils.getFormParam("datDir"      ,  formdata)
    ElementValueList     =  [userName, datasetName, title, description]

    # Host and silo name in the form data are used for testing.
    # In a live system, these are not provided in the form: the following values are used.
    if endpointhost==None or endpointhost=="":
        endpointhost = "localhost"
    if basepath==None or basepath=="":
        basepath = siloProxyPath

    ###print("\n---- processDatasetSubmissionForm:formdata ---- \n"+repr(formdata))
  
    # Zip the selected Directory
    zipFileName = os.path.basename(dirName) +".zip"
    zipFilePath = "/tmp/" + zipFileName
    Logger.debug("zipFilePath = "+zipFilePath)

    if outputstr:
        sys.stdout = outputstr
    try:    
        # Validate the dataset name and dataset directory fields
        validateFields(dirName, datasetName)
        
        # Set user credentials       
        #session.setRequestUserPass(userName,userPass)  
         
        #Create Session
        session  = HttpSession.makeHttpSession(endpointhost, basepath, userName, userPass)   
                   
        SubmitDatasetUtils.createDataset(session, datasetName)                             
        # Update the local manifest
        manifestFilePath     = dirName + str(os.path.sep) + DefaultManifestName
        Logger.debug("Element List = " + repr(ElementUriList))
        Logger.debug("Element Value List = " + repr(ElementValueList))
        SubmitDatasetDetailsHandler.updateMetadataInDirectoryBeforeSubmission(manifestFilePath, ElementUriList, ElementValueList)       

        #Logger.debug("datasetName %s, dirName %s, zipFileName %s"%(datasetName,dirName,zipFileName))
        SubmitDatasetUtils.zipLocalDirectory(dirName, FilePat, zipFilePath)
        # Submit zip file to dataset
        SubmitDatasetUtils.submitFileToDataset(session, datasetName, zipFileName, zipFilePath, ZipMimeType, zipFileName)
        # Unzip the contents into a new dataset
        datasetUnzippedName = SubmitDatasetUtils.unzipRemoteFileToNewDataset(session, datasetName, zipFileName)       
        # Redirect to the Dataset Summary page
        redirectToSubmissionSummaryPage(dirName, datasetName+"-packed", datasetUnzippedName, convertToUriString(SuccessStatus))
        return
        
    except SubmitDatasetUtils.SubmitDatasetError, e:
        SubmitDatasetUtils.printHTMLHeaders()
        SubmitDatasetUtils.generateErrorResponsePageFromException(e)