Hafiz,

There are 2 distinct issues.  First is can you create a repository by POST’ing 
to Ranger’s REST api endpoint.  Second is to have valid java/scala code that 
can do that.  I’d suggest that you separate the two issues.

Let’s first use tool like curl to confirm if REST api is working right.  Sort 
out any problems with adequacy and structure of your input data.  Once that is 
in place then you can tackle the 2nd problem independently as at that point it 
is a non-ranger related issue.

That said, did you try @Ramesh’s suggestion of using curl below?

From: Hafiz Mujadid
Reply-To: 
"[email protected]<mailto:[email protected]>"
Date: Sunday, August 9, 2015 at 2:32 AM
To: "[email protected]<mailto:[email protected]>"
Subject: Re: issue with rest api

Hi Ramesh!
I tried following code to create a repository but it is giving 404 error. I am 
passing json as entity in my post request. But I am failed to create a 
repository


def createReposity() {
    val httpclient = new DefaultHttpClient()
    val httpPost = new 
HttpPost("http://localhost:6080/service/public/api/repository";)
    httpPost.setHeader("content-type", "application/json")
    httpPost.setHeader("Accept", "application/json")
    httpPost.addHeader(BasicScheme.authenticate(
      new UsernamePasswordCredentials(username, password),
      "UTF-8", false))
    val reqEntity = new StringEntity(getJson)
    httpPost.setEntity(reqEntity)
    val response = httpclient.execute(httpPost)
    println(response.getStatusLine().getStatusCode())
  }def getJson(): String = {
    var json = "{\"name\": \"hadoopd_hdfs\",\"description\": \"hdfs repository 
using curl\","
    json += "\"repositoryType\": \"hdfs\",\"config\": \"{"
    json += "\"username\": \"admin\","
    json += "\"password\": \"admin\","
    json += "\"fs.default.name<http://fs.default.name>\": 
\"hdfs://localhost:9000\","
    json += "\"hadoop.security.authorization\": \"true\","
    json += "\"hadoop.security.authentication\": \"simple\","
    json += "\"hadoop.security.auth_to_local\": \"\","
    json += "\"dfs.datanode.kerberos.principal\": \"\","
    json += "\"dfs.namenode.kerberos.principal\": \"\","
    json += "\"dfs.secondary.namenode.kerberos.principal\": \"\","
    json += "\"commonNameForCertificate\": \"\"}\","
    json += "\"isActive\": true}"
    json
  }



On Sun, Aug 9, 2015 at 2:28 AM, Hafiz Mujadid 
<[email protected]<mailto:[email protected]>> wrote:
thanks ramesh :)

On Sun, Aug 9, 2015 at 2:11 AM, Ramesh Mani 
<[email protected]<mailto:[email protected]>> wrote:
Hafiz,

There is no code sample for it now. To create one , you need to go through the 
code base for Ranger Public Api.

Check it in git hub for ranger source public api 
https://github.com/apache/incubator-ranger/blob/master/security-admin/src/main/java/org/apache/ranger/rest/PublicAPIs.java

Idea is what ever the responses you got in your GET operation, you need to map 
it to the POST operation’s object with new values for repo and do the post 
call. ( you will be doing a of bunch of set operation on the object and do POST 
in your java )

Hope this gives you the idea.

Thanks,
Ramesh



From: Hafiz Mujadid <[email protected]<mailto:[email protected]>>
Reply-To: 
"[email protected]<mailto:[email protected]>" 
<[email protected]<mailto:[email protected]>>
Date: Saturday, August 8, 2015 at 1:27 PM
To: "[email protected]<mailto:[email protected]>" 
<[email protected]<mailto:[email protected]>>
Subject: Re: issue with rest api

Hi Ramesh

thanks for your response. I got the solution. here is my working code


val httpclient = new DefaultHttpClient()
    val httpget = new HttpGet(repo_url + "/" + id)
    httpget.addHeader(BasicScheme.authenticate(
      new UsernamePasswordCredentials(username, password),
      "UTF-8", false))
    httpget.setHeader("Accept", "application/json")
    val response = httpclient.execute(httpget)
    EntityUtils.toString(response.getEntity())


But I am failing in how to create a repository using rest api. is there any 
java code example?

thanks

On Sun, Aug 9, 2015 at 12:40 AM, Ramesh Mani 
<[email protected]<mailto:[email protected]>> wrote:
Hafiz

Please try using the following if it fetches.

"http://localhost:6080/service/public/api/repository<http://localhost:6080/service/public/api/repository/1>?type=hdfs”

For a quicker check you can use curl calls.

curl -i -X GET --header "Accept:application/json" -H "Content-Type: 
application/json" -u admin:admin 
http://localhost:6080/service/public/api/repository?type=hdfs

Also let me know the name of  service/ repository which you created and which 
you are querying to download.

Try this and let me know.

Regards,
Ramesh

From: Hafiz Mujadid <[email protected]<mailto:[email protected]>>
Reply-To: 
"[email protected]<mailto:[email protected]>" 
<[email protected]<mailto:[email protected]>>
Date: Saturday, August 8, 2015 at 10:18 AM
To: "[email protected]<mailto:[email protected]>" 
<[email protected]<mailto:[email protected]>>
Subject: issue with rest api

Hi

I am trying to use rest api's in my java/scala code

here is my code
 val httpclient = new DefaultHttpClient()
    val auth = new AuthScope(host, AuthScope.ANY_PORT)
    val credentials = new UsernamePasswordCredentials("admin", "admin")
    httpclient.getCredentialsProvider()
      .setCredentials(auth, credentials)
    val httpget = new 
HttpGet("http://localhost:6080/service/public/api/repository/1";)
    httpget.setHeader("Accept", "application/xml")
    val response = httpclient.execute(httpget)
    val entity = response.getEntity
    if (entity != null) {
      val in = new BufferedReader(new InputStreamReader(entity.getContent()))
      var line = in.readLine()
      var response = new StringBuffer()
      while (line != null) {
        response.append(line + "/n")
        line = in.readLine()
      }
      in.close()
      println(response.toString())
    }


But it is not returning repository details



any suggestion how can i use rest services in my java application?

Thanks



--
Regards: HAFIZ MUJADID



--
Regards: HAFIZ MUJADID



--
Regards: HAFIZ MUJADID

Reply via email to