REST API: Java Code examples

Version 2
    Share This:

    This document contains official content from the BMC Software Knowledge Base. It is automatically updated when the knowledge article is modified.


    PRODUCT:

    Remedy AR System Server


    COMPONENT:

    Remedy AR System Server



    QUESTION:

    Java code examples for REST API


    ANSWER:

    Here are some of the more common functions of the REST API. I use the external library java-json.jar for JSON parsing. Notice the function createRequest.

      public static String login(String baseURL, String userName, String password) {
           String output = "";
           
            if (!baseURL.endsWith("/")) {
                baseURL = baseURL + "/";
            }
      
            HttpURLConnection urlConnection = null;       
            
            try {        
                URL urltorequest = new URL(baseURL + "api/jwt/login");
                urlConnection = (HttpURLConnection) urltorequest.openConnection();
                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);
                
                String str =  "username=" + userName + "&password=" + password;
                byte[] outputInBytes = str.getBytes("UTF-8");
                OutputStream os = urlConnection.getOutputStream();
                os.write( outputInBytes );    
                os.close();

                //int statusCode = urlConnection.getResponseCode();
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                output = "AR-JWT " + getResponseText(in);        
            
            } catch (MalformedURLException ex) {

            } catch (IOException ex) {

            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }          
            return output;
        } 
       
        public static int logout(String baseURL, String token) {
           HttpURLConnection urlConnection = null;    
           int statusCode = 0;
           
            if (!baseURL.endsWith("/")) {
                baseURL = baseURL + "/";
            }       
            
            try {        
                URL urltorequest = new URL(baseURL + "api/jwt/logout");
                urlConnection = (HttpURLConnection) urltorequest.openConnection();
                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                urlConnection.setRequestProperty("Authorization", token); 
                statusCode = urlConnection.getResponseCode();           

            } catch (MalformedURLException ex) {

            } catch (IOException ex) {

            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }         
            return statusCode;
        }   
        
        public static JSONObject getEntry(String baseURL, String token, String formName, String entryID) {
           HttpURLConnection urlConnection = null;      
           
            if (!baseURL.endsWith("/")) {
                baseURL = baseURL + "/";
            }       
            
            try {        
                URL urltorequest = new URL(baseURL + "api/arsys/v1/entry/" + formName + "/" + entryID);
                urlConnection = (HttpURLConnection) urltorequest.openConnection();
                urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                urlConnection.setRequestProperty("Authorization", token);

                //int statusCode = urlConnection.getResponseCode();
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                return new JSONObject(getResponseText(in));        
            
            } catch (MalformedURLException ex) {

            } catch (IOException ex) {

            } catch (JSONException ex) {

            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }        
            return null;        
        }   
        
        public static String createEntry(String baseURL, String token, String formName, JSONObject entryData) {

            HttpURLConnection urlConnection = null;
            
            if (!baseURL.endsWith("/")) {
                baseURL = baseURL + "/";
            }        

            try {
                URL urltorequest = new URL(baseURL + "api/arsys/v1/entry/" + URLEncoder.encode(formName, "UTF-8"));
                urlConnection = (HttpURLConnection) urltorequest.openConnection();
                urlConnection.setRequestProperty("Authorization", token);
                urlConnection.setRequestMethod("POST");
                urlConnection.setRequestProperty("Content-Type", "application/json");

                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);

                String str = entryData.toString();
                byte[] outputInBytes = str.getBytes("UTF-8");
                OutputStream os = urlConnection.getOutputStream();
                os.write(outputInBytes);
                os.close();

                int statusCode = urlConnection.getResponseCode();
                switch (statusCode) {
                    case 201:
                        return "OK";
                    case 204:
                        return "OK";
                    default:
                        InputStream in = urlConnection.getErrorStream();
                        return "NOK: " + getResponseText(in);
                }

            } catch (MalformedURLException ex) {

            } catch (IOException ex) {

            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
            return null;
        }    
       
        private static String getResponseText(InputStream inStream) {
            return new Scanner(inStream).useDelimiter("\\A").next();
        }


        public static JSONObject createRequest() {
            JSONObject objEntry = new JSONObject();
            JSONObject objValues = new JSONObject();
            try {
                objValues.put("Submitter__c", "REST TEST");
                objValues.put("Short Description__c", "REST TEST");
                objValues.put("Status__c", "New");
                objEntry.put("values", objValues);
            } catch (JSONException ex) {

            }

            return objEntry;
        } 
     


    Article Number:

    000131148


    Article Type:

    FAQ/Procedural



      Looking for additional information?    Search BMC Support  or  Browse Knowledge Articles