Java - Login, Create a Community, Invite User, Logout

Java - Login, Create a Community, Invite User, Logout
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import com.ikanow.infinit.e.data_model.api.ResponsePojo;
import com.ikanow.infinit.e.data_model.api.social.community.CommunityPojoApiMap;
import com.ikanow.infinit.e.data_model.store.social.community.CommunityPojo;


public class test 
{

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception 
    {
        //login
        String address = "http://infinite.ikanow.com/api/auth/login/sterling_archer@ikanow.com/WZRHGrsBESr8wYFZ9sx0tPURuZgG2lmzyvWpwXPKz8U%3D"; //don't forget to URLEncode your arguments        
        String loginresult = sendRequest(address);
        ResponsePojo response = ResponsePojo.fromApi(loginresult, ResponsePojo.class);
        if ( response.getResponse().isSuccess())
        {
            //create a community
            String communityName = "testcommunity4";
            String communityDesc = "some long desc";
            String communityTags = "testtag1,testtag2";            
            String createCommunityAddress = "http://infinite.ikanow.com/api/social/community/add/" + URLEncoder.encode(communityName,"UTF-8") + "/" + 
				URLEncoder.encode(communityDesc,"UTF-8") + "/" + URLEncoder.encode(communityTags,"UTF-8");
            String communityresult = sendRequest(createCommunityAddress);
            //We need to convert the result object into a response with a person object in it
            response = ResponsePojo.fromApi(communityresult, ResponsePojo.class, CommunityPojo.class, new CommunityPojoApiMap());
            if ( response.getResponse().isSuccess() )
            {                
                CommunityPojo communityResult = (CommunityPojo)response.getData();
                String personID = "abcde12345"; //some other users id that you want to invite
                //now invite a user
                //to use ids, you need to import a mongo.jar available at https://github.com/mongodb/mongo-java-driver/downloads
                String inviteUserAddress = "http://infinite.ikanow.com/api/social/community/member/invite/" + communityResult.getId().toString() + "/" + personID;
                String inviteresult = sendRequest(inviteUserAddress);
                response = ResponsePojo.fromApi(inviteresult, ResponsePojo.class);
                if ( response.getResponse().isSuccess())
                {
                    //invited user successfully! now we are done
                }
            }
        }
        else
        {
            System.out.println("error logging in: " + response.getResponse().getMessage());
        }
        //logout when we are done, this will deactivate our cookie
        sendRequest("http://infinite.ikanow.com/api/auth/logout");
    }

    private static String cookie = null;
    public static String sendRequest(String urlAddress ) throws Exception
    {
        URL url = new URL(urlAddress);
        URLConnection urlConnection = url.openConnection();
        if ( cookie != null )
            urlConnection.setRequestProperty("Cookie", cookie);
        ((HttpURLConnection)urlConnection).setRequestMethod("GET");
                
        //read back result
        BufferedReader inStream = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        StringBuilder strBuilder = new StringBuilder();
        String buffer;            
        while ( (buffer = inStream.readLine()) != null )
        {
            strBuilder.append(buffer);
        }        
        inStream.close();
        
        //save cookie if cookie is null
        if ( cookie == null )
        {
            String headername;
            for ( int i = 1; (headername = urlConnection.getHeaderFieldKey(i)) != null; i++ )
            {
                if ( headername.equals("Set-Cookie") )
                {
                    cookie = urlConnection.getHeaderField(i);
                    break;
                }
            }
        }        
        return strBuilder.toString();
    }        
}