3 Replies Latest reply on Aug 13, 2014 1:08 PM by jaredmladner

    REST-API

    love-dev.1988

      Hi Experts;

       

      I'm trying to complete a task in kie-wb using eclipse, I'm based on REST-API.

      I added the class rest.java to my project:

       

      1. package com.acme.rewards.ejb;
      2. import java.io.BufferedReader;
      3. import java.io.IOException;
      4. import java.io.InputStreamReader;
      5. import java.io.OutputStream;
      6. import java.io.OutputStreamWriter;
      7. import java.io.Writer;
      8. import java.net.HttpURLConnection;
      9. import java.net.URL;
      10. import java.net.URLEncoder;
      11. public class rest {
      12.         public static String httpGet(String urlStr) throws IOException {
      13.                   URL url = new URL(urlStr);
      14.                   HttpURLConnection conn =(HttpURLConnection) url.openConnection();
      15.                   System.out.println(conn.getResponseCode());
      16.                   if (conn.getResponseCode() != 200) {
      17.                     throw new IOException(conn.getResponseMessage());
      18.                   }
      19.                   BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      20.                   StringBuilder sb = new StringBuilder();
      21.                   String line;
      22.                   while ((line = rd.readLine()) != null) {
      23.                     sb.append(line);
      24.                   }
      25.                   rd.close();
      26.                   conn.disconnect();
      27.                   return sb.toString();
      28.                 }

      29.    public static String httpPost(String urlStr, String[] paramName,
      30.                         String[] paramVal) throws Exception {
      31.                           URL url = new URL(urlStr);
      32.                           HttpURLConnection conn =(HttpURLConnection) url.openConnection();
      33.                           conn.setRequestMethod("POST");
      34.                           conn.setDoOutput(true);
      35.                           conn.setDoInput(true);
      36.                           conn.setUseCaches(false);
      37.                           conn.setAllowUserInteraction(false);
      38.                           conn.setRequestProperty("Content-Type",
      39.                               "application/x-www-form-urlencoded");
      40.                           // Create the form content
      41.                           OutputStream out = conn.getOutputStream();
      42.                           Writer writer = new OutputStreamWriter(out, "UTF-8");
      43.                           for (int i = 0; i < paramName.length; i++) {
      44.                             writer.write(paramName[i]);
      45.                             writer.write("=");
      46.                             writer.write(URLEncoder.encode(paramVal[i], "UTF-8"));
      47.                             writer.write("&");
      48.                           }
      49.                           writer.close();
      50.                           out.close();
      51.                           if (conn.getResponseCode() != 200) {
      52.                             throw new IOException(conn.getResponseMessage());
      53.                           }
      54.                           BufferedReader rd = new BufferedReader(
      55.                           new InputStreamReader(conn.getInputStream()));
      56.                           StringBuilder sb = new StringBuilder();
      57.                           String line;
      58.                           while ((line = rd.readLine()) != null) {
      59.                             sb.append(line);
      60.                           }
      61.                           rd.close();
      62.                           conn.disconnect();
      63.                           return sb.toString();
      64.                         }
      65.       
      66.       public static void main(String[] args) {
      67.                 String urlStr="http://localhost:8080/jbpm-console/rest/task/778";                           
      68.                 try {
      69.                         httpGet(urlStr);
      70.                 } catch (IOException e) {
      71.                         e.printStackTrace();
      72.                 }
      73.         }
      74.       
      75.       
      76. }

       

      When I http://localhost:8080/jbpm-console/rest/task/778 on the browser, I obtained the file XML: So it's well.

      The problem is :Once I run as java Application, I obtained this error:

       

      1. 401
      2. java.io.IOException: Unauthorized
      3.         at com.acme.rewards.ejb.rest.httpGet(rest.java:23)
      4.         at com.acme.rewards.ejb.rest.main(rest.java:91)

       

      Could you please help me solving this error ?

      Thanks in advance.

        • 1. Re: REST-API
          jaredmladner

          My REST client accessing jBPM REST API is in NodeJS. In NodeJS I am using the Restify module to create an HTTP connection and we had to set the Basic Auth credentials. It works in your browser because you have probably logged into the console previously and still have the cookie cached.

           

          Try setting the Basic Auth creds and see if that works.

          1 of 1 people found this helpful
          • 2. Re: Re: REST-API
            love-dev.1988

            Hi Jared,

             

             

            Thanks for your reply.

            I'm using Advanced REST Client available in GOOGLE Chrome,

                      ---> So it's OK .

                    ---> So it's OK .

            ==> So Basic Auth credentials are correct.


            The problem is when I run as java application the class rest.java, I recieve the error presented previously.

            I didn't know what I missed   !!!!?.

             

            Thanks for help.

            • 3. Re: REST-API
              jaredmladner

              I still think your problem is that you are not setting the Basic Authorization Header and credentials in rest.java. The Advanced REST client and Google Chrome will use the your cached authorization cookies if you have already logged into the jbpm-console manually. If you logout of the jbpm-console and try accessing the URL in Advanced REST client and Google Chrome it should not work and you will get Unauthorized.

               

              Your rest.java HttpURLConnection needs to set the "Authorization" header with the credentials.

               

              HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
              String encoded = Base64.encode(username+":"+password);
              connection
              .setRequestProperty("Authorization", "Basic "+encoded);

               

              We are using using the Apache HTTP Client as below and works for us:

               

                public static String doHttpGet(String uri, String user, String pass)

                {

                HttpGet httpget = new HttpGet(uri);

               

                //Set the Authorization Header

                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, pass);

                      BasicScheme scheme = new BasicScheme();

               

                      Header authorizationHeader;

                try {

                authorizationHeader = scheme.authenticate(credentials, httpget, null);

                httpget.addHeader(authorizationHeader);

                } catch (AuthenticationException e) {

                logger.error("Couldn't add authorization header.", e);

                }

                     

                return makeHttpRequest(httpget);

                }