java equivalent for curl post command

I am looking for an equivalent in java for the following  curl command. I know I can do a Runtime.exec() etc to run this, but I was looking for a way to do this using some Jersey client API or some other similar solutions.

curl -v \
--user admin:admin123 \
-H X-Requested-By:MyClient \
-H Accept:application/json \
-H Content-Type:application/json \
-d "{"name":"Partition0",
"properties" : [
{ "name" : "resourceGroups",
        "properties" : [
                { "name" : "g1",
                "properties" : [
                        { "name" : "resourceGroupTemplate", "value" : "template1" },
                        { "name" : "targets" , "value" : "VirtualTarget-0"}]}]},
{"name" : "availableTargets" , "value" : "VirtualTarget-0"},
{"name":"primaryIdentityDomain","value":"p0-identity-domain"},
{"name" : "defaultTarget" , "value" : "VirtualTarget-0"},
{"name" : "securityRealm" , "value" : "myrealm"}
]}" \
-X POST http://myhost.us.company.com:8971/management/lifecycle/latest/runtimes/MyTestRuntime/partitions
LVL 1
ctoanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gheistCommented:
You can use libcurl java bindings.
0
ctoanAuthor Commented:
I did consider libcurl, but I heard the code may not be portable and may not work on all platforms. Moreover, I don't see any good examples of libcurl with Java.
0
gheistCommented:
Why you do all that in CURL firsthand? Java has it's web services and restful services APIs builtin?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ctoanAuthor Commented:
Well, I'm new to this and that's why I want to know how this can be done in Java. I got this curl command and would like to know the Java equivalent. I have seen examples using Jersey Client API and HttpClient etc, but I'm not sure how this particular example I posted can be converted.

Thanks.
0
gheistCommented:
it is done using official APIs, like jersay client api you mentioned, completely abstracting from having linux or windows or solaris, using http/1.0 or  http/2.0 over ipv4 or ipv6, using SSL or not....
0
ctoanAuthor Commented:
The example at the following link (with some changes) helped me to get this working

http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import javax.ws.rs.core.MediaType;


public class JerseyClientPost {

      public static void main(String[] args) {

            try {

                  Client client = Client.create();
                  client.addFilter(new HTTPBasicAuthFilter("admin","admin123"));

                  WebResource webResource = client
                              .resource("http://myhost.us.company.com:8971/management/lifecycle/latest/runtimes");
                  String input = ""{"name\":"Partition0\",\"properties\" : [{ \"name\" : \"resourceGroups\",\"properties\" : [{ \"name\" : \"g1\", ....... ";

                  JSONObject json = new JSONObject(input);

                  ClientResponse response = webResource.type("application/json")
                              .header("X-Requested-By", "MyClient")
                              .header("Accept", "application/json")
                              .post(ClientResponse.class, json.toString());

                  if (response.getStatus() != 201) {
                        throw new RuntimeException("Failed : HTTP error code : "
                                    + response.getStatus()+ response.toString());
                  }

                  System.out.println("Output from Server .... \n");
                  String output = response.getEntity(String.class);
                  System.out.println(output);

            } catch (Exception e) {

                  e.printStackTrace();

            }

      }

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gheistCommented:
You should use JSR 353 API and not call jetson classes directly. Sooner or later (read java 9 or Java 10) such sidestep will be punished with deprecation. At that moment you will have too much codebase to change it to normal API and your app will be stuck with old java version (like java 5 is burried end of may, and java7 gets costly in september)
0
ctoanAuthor Commented:
Didn't really receive any useful information from anyone else. All the info provided was already stated in my question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.