AnsweredAssumed Answered

[Java + WebService] Creazione Utenti esterna da Liferay

Question asked by alnitakzeta on May 9, 2012
Latest reply on Oct 9, 2012 by alnitakzeta
Salve,

io dovrei creare in Liferay un Hook che dopo la creazione dell'utente in automatico mi crea un utente in alfresco.

Ho creato questa classe che contatta i web service di alfresco.

Invio un JSON con i parametri della creazione utente e l'autenticazione la faccio attraverso il nome utente e la password.

Se utilizzo il comando da terminale CURL la creazione dell'utente va a buon fine.

Questo codice mi ritorna come responseCode 400. Qualcuno sa darmi un aiuto?

      
      String _userName = username;
      String _password = password;
      String _firstName = fristname;
      String _lastName = lastname;
      String _mail = mail;
      String _disableAccount = "false";
      String _quota = "-1";
      
      
      
        String urlParameters = "{ \"username\" : \"" + _userName +"\", \"password\" : \"" + _password +"\"" +
                          ",\"firstName\" : \"" + _firstName +"\", \"lastName\" : \"" + _lastName +"\"" +
                           ",\"email\" : " +  _mail +
                             ",\"disableAccount\" : false , \"quota\" : -1 ,\"groups\":[] }";
       
        //System.out.println("——-> " + urlParameters);
        // {"userName":"jack","password":"1111","firstName":"jack","lastName":"den","email":"test@test.net","disableAccount":false,"quota":-1,"groups":[]};
        //urlParameters ="{\"username\":\"jack\",\"password\":\"jack\",\"firstName\":\"jack\",\"lastName\":\"jack\",\"email\":jack@jack.it,\"disableAccount\":false,\"quota\":-1,\"groups\":[]};";
       
        int _resultCode = 0;
      
       String _userNameLogin = "admin";
       String _passwordLogin = "admin";      

         URL url;
         HttpURLConnection connection = null;
         try
         {
           //JSON che verrà inviato alla post per la login
            //String urlParameters = "{ \"username\" : \"" + _userName +"\", \"password\" : \"" + _password +"\" }";
           
            // Apri la connessione
            url = new URL("http://localhost:8080/alfresco/service/api/people");
            connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod("POST");
           
            //Imposto autenticazione
            BASE64Encoder enc = new sun.misc.BASE64Encoder();
            String userpassword = _userNameLogin + ":" + _passwordLogin;
            String encodedAuthorization = enc.encode( userpassword.getBytes() );
            connection.setRequestProperty("Authorization", "Basic "+ encodedAuthorization);
           
           
//            String ticket = this.autorizathionUser(_userNameLogin, _passwordLogin);
           
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");
//            connection.setRequestProperty("ticket", ticket);
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);
           
           
           
            // Invia la richiesta
            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();
           
//            OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
//            osw.write(urlParameters);
//            osw.close();
           
            connection.connect();
           
            _resultCode = connection.getResponseCode();

         }
         catch (Exception e)
         {
            e.printStackTrace();
            return _resultCode;
         }
         finally
         {
            if (connection != null)
            {
               connection.disconnect();
            }
         }
        
      return _resultCode;
   }

Grazie e Buon Lavoro

Outcomes