1 2 3 Previous Next 38 Replies Latest reply: Dec 28, 2011 8:32 AM by Ram Biradar RSS

Connecting to GWT Console by Rest interface, login problem

Serkan Kasapbasi Newbie

Hi everyone,

I am trying to request process definitions, and task lists of specific users etc. As it is documented locally,gwt-console-server/rs/server/resources, there are URL(s) to request for particular usage.

 

When I try those URL's from browser, it first redirects to login page, let me login, then produces output.

However, what i need is to call these URLs from my code, so, when i try to request these URLs with basic authentication, it doesn't let me in, and redirects to login page.

 

How can authenticate my requests?

  • 1. Connecting to GWT Console by Rest interface, login problem
    Jeff Yu Master

    Now, it uses the form authentication, not the basic authentication any more.

     

    You can take a look at one example in riftsaw console for the form authentication at http://anonsvn.jboss.org/repos/riftsaw/trunk/samples/quickstart/management/src/org/jboss/riftsaw/management/ManagementClient.java .

     

    HTH

    Jeff

  • 2. Connecting to GWT Console by Rest interface, login problem
    Serkan Kasapbasi Newbie

    thank you, i have managed to pass login with form authentication you provided. But, there is a problem in your example, actually it is not yours HttpClient's newest version is 4.something, and it is not backward compatible, so your code doesnt compile. I used version 3 instead and looks good.

     

    Thanks again.

  • 3. Re: Connecting to GWT Console by Rest interface, login problem
    Chris Melas Apprentice

    If you want to use httpclient 4 the following code is what i'm using and it works fine

    Note that this method only authenticates the user so after authentication takes place then you can call the other methods. I suppose it could be possible that this could happen on the actual call of a specific REST url, however i didn't like the idea of sending the credentials all the time.

     

    public static String KEY_USERNAME = "j_username";

    public static String KEY_PASSWORD = "j_password";

     

     

    public String authenticate(String address, String username, String password) {

            String responseString = "";

            List<NameValuePair> formparams = new ArrayList<NameValuePair>();

            formparams.add(new BasicNameValuePair(KEY_USERNAME, username));

            formparams.add(new BasicNameValuePair(KEY_PASSWORD, password));

     

     

            HttpPost httpPost = new HttpPost("http://" + address + "/gwt-console-server/rs/process/j_security_check");

            InputStreamReader inputStreamReader = null;

            BufferedReader bufferedReader = null;

            try {

                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");//UrlEncodedFormEntity(formparams, "multipart/form-data");

                httpPost.setEntity(entity);

                HttpResponse response = httpClient.execute(httpPost);

                InputStream inputStream = response.getEntity().getContent();

                inputStreamReader = new InputStreamReader(inputStream);

                bufferedReader = new BufferedReader(inputStreamReader);

                StringBuilder stringBuilder = new StringBuilder();

                String line = bufferedReader.readLine();

                while (line != null) {

                    stringBuilder.append(line);

                    line = bufferedReader.readLine();

                }

                responseString = stringBuilder.toString();

            } catch (Exception e) {

                throw new RuntimeException(e);

            } finally {

                if (inputStreamReader != null) {

                    try {

                        inputStreamReader.close();

                    } catch (Exception e) {

                        throw new RuntimeException(e);

                    }

                }

                if (bufferedReader != null) {

                    try {

                        bufferedReader.close();

                    } catch (Exception e) {

                        throw new RuntimeException(e);

                    }

                }

            }

            return responseString;

        }

  • 4. Re: Connecting to GWT Console by Rest interface, login problem
    Jeff Yu Master

    sorry, I should have pointed out that I used the httpclient 3.x version earlier.

     

    You can use the Chris approach if you like to use the httpclient 4.x version.

     

    Regards

    Jeff

  • 5. Re: Connecting to GWT Console by Rest interface, login problem
    kiruba garan Newbie

    I used the code like this but it is not working, i used httpclient-4.0.3.jar, did i made any mistake here please let me know

     

    public static String KEY_USERNAME = "j_username";

    public static String KEY_PASSWORD = "j_password";

     

    public String authenticate(String address, String username, String password) throws URISyntaxException {

                   

            String responseString = "";

     

            List<NameValuePair> formparams = new ArrayList<NameValuePair>();

     

            formparams.add(new BasicNameValuePair(KEY_USERNAME, username));

            formparams.add(new BasicNameValuePair(KEY_PASSWORD, password));

     

           

            HttpPost httpPost = new HttpPost("http://" + address + "/gwt-console-server/rs/process/j_security_check");

          

            InputStreamReader inputStreamReader = null;

            BufferedReader bufferedReader = null;

            try {

                DefaultHttpClient httpclient = new DefaultHttpClient();

                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");//UrlEncodedFormEntity(formparams, "multipart/form-data");

              

                httpPost.setEntity(entity);

              

                HttpResponse response = httpclient.execute(httpPost);

                InputStream inputStream = response.getEntity().getContent();

                inputStreamReader = new InputStreamReader(inputStream);

                bufferedReader = new BufferedReader(inputStreamReader);

                StringBuilder stringBuilder = new StringBuilder();

                String line = bufferedReader.readLine();

                while (line != null) {

                    stringBuilder.append(line);

                    line = bufferedReader.readLine();

                }

                responseString = stringBuilder.toString();

     

            } catch (Exception e) {

     

                throw new RuntimeException(e);

     

            } finally {

     

                if (inputStreamReader != null) {

     

                    try {

     

                        inputStreamReader.close();

     

                    } catch (Exception e) {

                        throw new RuntimeException(e);

                    }

                }

                if (bufferedReader != null) {

                    try {

                        bufferedReader.close();

                    } catch (Exception e) {

                        throw new RuntimeException(e);

                    }

                }

            }

            return responseString;

        }

    }

  • 6. Re: Connecting to GWT Console by Rest interface, login problem
    Jeff Yu Master

    May I know what error did you get?

  • 7. Re: Connecting to GWT Console by Rest interface, login problem
    kiruba garan Newbie

    Hi Jeff,

     

    Thankyou for ur reply

     

     

    It is printing the login html file not a JSON object response file. It is not yet validated

     

    Line 2 ====><html>

    Line 2 ====><head>

    Line 2 ====>  <title>HTTP 401</title> <!-- Do not remove -->

    Line 2 ====></head>

    Line 2 ====><body>

    Line 2 ====><form method="POST" action="j_security_check">

    Line 2 ====>

    Line 2 ====>  <center/>

    Line 2 ====>  <br><br>

    Line 2 ====>

    Line 2 ====>  <div style="font-family:sans-serif;border:1px solid black; width:270;padding:15px;">

    Line 2 ====>    <h2>BPM Console Server</h2>

    Line 2 ====>   

    Line 2 ====>    <table with=250>

    Line 2 ====>    <tr>

    Line 2 ====>      <td>Username:</td>

    Line 2 ====>      <td><input type="text" name="j_username"></td>

    Line 2 ====>    </tr>

    Line 2 ====>    <tr>

    Line 2 ====>      <td>Password:</td>

    Line 2 ====>      <td><input type="password" name="j_password"></td>

    Line 2 ====>    </tr>

    Line 2 ====>    <tr>

    Line 2 ====>      <td></td>

    Line 2 ====>      <td align=right><input type="submit"/></td>

    Line 2 ====>    </tr>

    Line 2 ====>  </table>

    Line 2 ====>  </div>

    Line 2 ====></form>

    Line 2 ====></body>

    Line 2 ====></html>

    response621

     

    Caused by: org.json.JSONException: A JSONObject text must begin with '{' at character 1 of <html>

        at org.json.JSONTokener.syntaxError(JSONTokener.java:450)

        at org.json.JSONObject.<init>(JSONObject.java:179)

        at org.json.JSONObject.<init>(JSONObject.java:326)

        at roseindia.SimpleLogin.getProcess(SimpleLogin.java:127)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at org.apache.el.parser.AstValue.invoke(AstValue.java:172)

        at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)

        at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:68)

        ... 22 more

     

    Regards,

     

    Kiru

  • 8. Re: Connecting to GWT Console by Rest interface, login problem
    Jeff Yu Master

    OK, this means the authentication failed.. I haven't tested against the httpclient 4.x series, if you wanted to use it right away, you may test with the httpclient 3.x with the riftsaw example as I shown in my previous reply.

  • 9. Re: Connecting to GWT Console by Rest interface, login problem
    Jeff Yu Master

    Meanwhile, I'll find a time to test the httpclient 4.x and then let you know.

  • 10. Re: Connecting to GWT Console by Rest interface, login problem
    Chris Melas Apprentice

    Hello,

    Sorry for not being clear enough. The way i'm using it is as follows

     

    1. i find the service that i want to call from http://your_ipaddress:and_port/gwt-console-server/rs/server/resources  i.e. /gwt-console-server/rs/process/definition/{id}/new_instance in order to create a new process

    2. i call the service by using the post method i have created (i will post code at the end),

    3. normally at first i will get an authentication error, so at that point i will run the authenticate method (as posted by me above, i will post it again at the bottom) with the proper credentials

    4. then i call the service from step 2

    Any consecutive calls will not need authentication, well not until the session expires. It is easy to figure out if something went wrong from the result you get when you call the web service.

     

    So the authentication method is as follows,

     

    public static String KEY_USERNAME = "j_username";

    public static String KEY_PASSWORD = "j_password";

    private DefaultHttpClient httpClient = new DefaultHttpClient(); // keep this out of the method in order to reuse the object for calling other services without losing session

     

     

    public String authenticate(String address, String username, String password) {

            String responseString = "";

            List<NameValuePair> formparams = new ArrayList<NameValuePair>();

            formparams.add(new BasicNameValuePair(KEY_USERNAME, username));

            formparams.add(new BasicNameValuePair(KEY_PASSWORD, password));

     

     

            HttpPost httpPost = new HttpPost("http://" + address + "/gwt-console-server/rs/process/j_security_check");

            InputStreamReader inputStreamReader = null;

            BufferedReader bufferedReader = null;

            try {

                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");//UrlEncodedFormEntity(formparams, "multipart/form-data");

                httpPost.setEntity(entity);

                HttpResponse response = httpClient.execute(httpPost);

                InputStream inputStream = response.getEntity().getContent();

                inputStreamReader = new InputStreamReader(inputStream);

                bufferedReader = new BufferedReader(inputStreamReader);

                StringBuilder stringBuilder = new StringBuilder();

                String line = bufferedReader.readLine();

                while (line != null) {

                    stringBuilder.append(line);

                    line = bufferedReader.readLine();

                }

                responseString = stringBuilder.toString();

            } catch (Exception e) {

                throw new RuntimeException(e);

            } finally {

                if (inputStreamReader != null) {

                    try {

                        inputStreamReader.close();

                    } catch (Exception e) {

                        throw new RuntimeException(e);

                    }

                }

                if (bufferedReader != null) {

                    try {

                        bufferedReader.close();

                    } catch (Exception e) {

                        throw new RuntimeException(e);

                    }

                }

            }

            return responseString;

        }

     

    and the post method i use,

     

    public String requestPostService(String url, Map<String, Object> parameters, boolean multipart) {

            String responseString = "";

     

            MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

            List<NameValuePair> formparams = new ArrayList<NameValuePair>();

            if (parameters == null) {

                parameters = new HashMap<String, Object>();

            }

            Set<String> keys = parameters.keySet();

            for (Iterator<String> keysIterator = keys.iterator(); keysIterator.hasNext();) {

                String keyString = keysIterator.next();

                String value = parameters.get(keyString).toString();

                formparams.add(new BasicNameValuePair(keyString, value));

                if (multipart) {

                    try {

                        StringBody stringBody = new StringBody(value, "text/plain", Charset.forName("UTF-8"));

                        multiPartEntity.addPart(keyString, (ContentBody) stringBody);

                    } catch (Exception e) {

                        throw new RuntimeException(e);

                    }

                }

            }

            HttpPost httpPost = new HttpPost(url);

     

            InputStreamReader inputStreamReader = null;

            BufferedReader bufferedReader = null;

            try {

                if (multipart) {

                    httpPost.setEntity(multiPartEntity);

                } else {

                    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");//UrlEncodedFormEntity(formparams, "multipart/form-data");

                    httpPost.setEntity(entity);

                }

                HttpResponse response = httpClient.execute(httpPost);

                InputStream inputStream = response.getEntity().getContent();

                inputStreamReader = new InputStreamReader(inputStream);

                bufferedReader = new BufferedReader(inputStreamReader);

                StringBuilder stringBuilder = new StringBuilder();

                String line = bufferedReader.readLine();

                while (line != null) {

                    stringBuilder.append(line);

                    line = bufferedReader.readLine();

                }

                responseString = stringBuilder.toString();

            } catch (Exception e) {

                throw new RuntimeException(e);

            } finally {

                if (inputStreamReader != null) {

                    try {

                        inputStreamReader.close();

                    } catch (Exception e) {

                        throw new RuntimeException(e);

                    }

                }

                if (bufferedReader != null) {

                    try {

                        bufferedReader.close();

                    } catch (Exception e) {

                        throw new RuntimeException(e);

                    }

                }

            }

            return responseString;

        }

     

    notice the multipart boolean is for the web services that need multipart/form-data post

     

    so by calling the following code,

     

    String address = "your_ipaddress:and_port";

    String responseString = "";

     

    responseString = requestPostService("http://" + address + "/gwt-console-server/rs/process/definition/yourProcessName/new_instance", null, false);

            System.out.println("start process1:------>"+responseString+"\n\n\n\n");

            responseString = authenticate(address, "krisv", "krisv");

            System.out.println("authentication2:------>"+responseString + "\n\n\n\n");

            responseString = requestPostService("http://" + address + "/gwt-console-server/rs/process/definition/yourProcessName/new_instance", null, false);

            System.out.println("start process2:------>"+responseString+"\n\n\n\n");

            responseString = requestPostService("http://" + address + "/gwt-console-server/rs/process/definition/yourProcessName/new_instance", null, false);

            System.out.println("start process3:------>"+responseString+"\n\n\n\n");

            responseString = requestPostService("http://" + address + "/gwt-console-server/rs/process/definition/yourProcessName/new_instance", null, false);

            System.out.println("start process4:------>"+responseString+"\n\n\n\n");

     

    you will get the following output,

     

    start process1:------><html><head>  <title>HTTP 401</title> <!-- Do not remove --></head><body><form method="POST" action="j_security_check">  <center/>  <br><br>  <div style="font-family:sans-serif;border:1px solid black; width:270;padding:15px;">    <h2>BPM Console Server</h2>        <table with=250>    <tr>      <td>Username:</td>      <td><input type="text" name="j_username"></td>    </tr>    <tr>      <td>Password:</td>      <td><input type="password" name="j_password"></td>    </tr>    <tr>      <td></td>      <td align=right><input type="submit"/></td>    </tr>  </table>  </div></form></body></html>

     

    authentication2:------>

     

    start process2:------>{"id":"104","definitionId":"yourProcessName","startDate":"2011-06-03 12:43:45","suspended":false,"rootToken":{"id":"1","name":"","currentNodeName":"","children":[],"availableSignals":[],"canBeSignaled":false}}

     

    start process3:------>{"id":"105","definitionId":"yourProcessName","startDate":"2011-06-03 12:43:45","suspended":false,"rootToken":{"id":"2","name":"","currentNodeName":"","children":[],"availableSignals":[],"canBeSignaled":false}}

     

    start process4:------>{"id":"106","definitionId":"yourProcessName","startDate":"2011-06-03 12:43:45","suspended":false,"rootToken":{"id":"3","name":"","currentNodeName":"","children":[],"availableSignals":[],"canBeSignaled":false}}

  • 11. Re: Connecting to GWT Console by Rest interface, login problem
    kiruba garan Newbie

    Thankyou very much Chris Melas

     

    It is working ......

  • 12. Re: Connecting to GWT Console by Rest interface, login problem
    Victor Valencia Newbie

    Hi Cris,

     

    I can't connect my application, when I try to connect, the server responded this:

     

    <html><head><title>JBoss Web/2.1.3.GA - Informe de Error</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>Estado HTTP 408 - El tiempo permitido para realizar login ha sido excedido. Si deseas continuar, debes hacer clik dos veces y volver a hacer clik otra vez o cerrar y reabrir tu navegador</h1><HR size="1" noshade="noshade"><p><b>type</b> Informe de estado</p><p><b>mensaje</b> <u>El tiempo permitido para realizar login ha sido excedido. Si deseas continuar, debes hacer clik dos veces y volver a hacer clik otra vez o cerrar y reabrir tu navegador</u></p><p><b>descripción</b> <u>El cliente no produjo un requerimiento dentro del tiempo en que el servidor estaba preparado esperando (El tiempo permitido para realizar login ha sido excedido. Si deseas continuar, debes hacer clik dos veces y volver a hacer clik otra vez o cerrar y reabrir tu navegador).</u></p><HR size="1" noshade="noshade"><h3>JBoss Web/2.1.3.GA</h3></body></html>

     

    any suggestions?

     

    Regards

  • 13. Re: Connecting to GWT Console by Rest interface, login problem
    Chris Melas Apprentice

    Hello,

    Well this happens because the server is not expecting authentication parameters and suddenly it receives authentication parameters. So this occurrs when you call directly the authenticate method/web service shown above.

    So in order to make it work you need to do the following in the order stated,

    1. call a web service

    1a. if you're authenticated the server will respond with the result of the web service and you're done :-)

    1b. if you're not authenitcated the server will respond with error and it will expect authentication parameters

    2. you send authentication parameters via the authenticate method mentioned above

    3. then you call your web service from step 1

  • 14. Re: Connecting to GWT Console by Rest interface, login problem
    Victor Valencia Newbie

    Hello Cris!

     

    Thank you very much.... it is working   

     

    Regards

1 2 3 Previous Next