org.jboss.resteasy.client.ClientResponseFailure: Error status 401 Unauthorized returned

Hello,

I tried with this example to create and deploy process from my java client using API Rest

package tn.simac.common.utils.bonita5;

/**

  • Copyright (C) 2011 BonitaSoft S.A.
  • BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble
  • This library is free software; you can redistribute it and/or modify it under the terms
  • of the GNU Lesser General Public License as published by the Free Software Foundation
  • version 2.1 of the License.
  • This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  • without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  • See the GNU Lesser General Public License for more details.
  • You should have received a copy of the GNU Lesser General Public License along with this
  • program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  • Floor, Boston, MA 02110-1301, USA.
    **/

import java.util.Collection;

import javax.security.auth.login.LoginContext;

import org.ow2.bonita.facade.ManagementAPI;
import org.ow2.bonita.facade.QueryRuntimeAPI;
import org.ow2.bonita.facade.RuntimeAPI;
import org.ow2.bonita.facade.def.majorElement.ProcessDefinition;
import org.ow2.bonita.facade.runtime.ActivityState;
import org.ow2.bonita.facade.runtime.InstanceState;
import org.ow2.bonita.facade.uuid.ProcessDefinitionUUID;
import org.ow2.bonita.facade.uuid.ProcessInstanceUUID;
import org.ow2.bonita.light.LightTaskInstance;
import org.ow2.bonita.util.AccessorUtil;
import org.ow2.bonita.util.BonitaConstants;
import org.ow2.bonita.util.BusinessArchiveFactory;
import org.ow2.bonita.util.ProcessBuilder;
import org.ow2.bonita.util.SimpleCallbackHandler;

/**

  • @author Elias Ricken de Medeiros

*/
public class Client {

private static final String LOGIN = “john”;
private static final String PSSWD = “bpm”;
private static final String jaasFile = “D:\wf\jaas-standard.cfg”;

public static void main(String args) throws Exception {

//set system properties
System.setProperty(BonitaConstants.API_TYPE_PROPERTY, "REST");
System.setProperty(BonitaConstants.REST_SERVER_ADDRESS_PROPERTY, "http://localhost:8080/bonita-server-rest/");
System.setProperty(BonitaConstants.JAAS_PROPERTY, jaasFile);
 
//login
//verify the user exists
LoginContext loginContext = new LoginContext("BonitaAuth",
new SimpleCallbackHandler(LOGIN, PSSWD));
loginContext.login();
loginContext.logout();
 
//propagate the user credentials
loginContext = new LoginContext("BonitaStore",
new SimpleCallbackHandler(LOGIN, PSSWD));
loginContext.login();
 
//get he APIs
final ManagementAPI managementAPI = AccessorUtil.getManagementAPI();
final RuntimeAPI runtimeAPI = AccessorUtil.getRuntimeAPI();
final QueryRuntimeAPI queryRuntimeAPI = AccessorUtil.getQueryRuntimeAPI();

try {

    //create a simple process with process builder:
    // - one step with LOGIN as actor
    // - a Global data of String Type
    ProcessDefinition process = ProcessBuilder.createProcess("myProcess", "1.0")
    .addStringData("globalVar", "defaultValue")
    .addHuman(LOGIN)
    .addHumanTask("step1", LOGIN)
    .done();
     
    //deploy process
    process = managementAPI.deploy(BusinessArchiveFactory.getBusinessArchive(process));
     
    System.out.println("----------------\nProcess deployed\n----------------");
     
    final ProcessDefinitionUUID processUUID = process.getUUID();
    //instantiate process
    ProcessInstanceUUID instanceUUID = runtimeAPI.instantiateProcess(processUUID);
    System.out.println("----------------\nNew process instance Created\n----------------");
     
    final Collection<LightTaskInstance> taskList = queryRuntimeAPI.getLightTaskList(instanceUUID, ActivityState.READY);
    if (taskList.size() != 1) {
    throw new Exception("Incorrect list size. Actual size: " + taskList.size());
    }
     
    //execute task
    final LightTaskInstance taskInstance = taskList.iterator().next();
    runtimeAPI.executeTask(taskInstance.getUUID(), true);
    System.out.println("----------------\nTask executed\n----------------");
     
    final InstanceState state = queryRuntimeAPI.getProcessInstance(instanceUUID).getInstanceState();
    if(!state.equals(InstanceState.FINISHED)){
    throw new Exception("Incorrect state. Actual state: " + state);
    }
     
    System.out.println("----------------\nApplication executed sucessfully\n----------------");
     
    } finally {
    //delete all deployed processes
    managementAPI.deleteAllProcesses();
    loginContext.logout();
    }

}

}

But I got this error

Exception breakpoint: Client.java:111, org.jboss.resteasy.client.ClientResponseFailure, Error status 401 Unauthorized returned Exception in thread "main" org.jboss.resteasy.client.ClientResponseFailure: Error status 401 Unauthorized returned

When I use the debugger, this error occures when I arrive to deploy the process created

process = managementAPI.deploy(BusinessArchiveFactory.getBusinessArchive(process));

Is that means, that the problem is with REST API authentification or Bonita ?

Hello,

there is probably an error in your configuration files. Please, check if everything is ok based on this blog post: http://community.bonitasoft.com/blog/how-use-bonita-http-api

Verify if the rest user used in the login context BonitaStore under D:\wf\jaas-standard.cfg is coherent with login context BonitaRESTServer in the JAAS file used by your server.

well,

It is resolved now, I don’t know how, and what was the error, but now it works fine

thank you Elias for your help

This is my jaas-standard.cfg configuration file used by my java client

BonitaAuth { org.ow2.bonita.identity.auth.BonitaIdentityLoginModule required; };

BonitaStore {
org.ow2.bonita.identity.auth.BonitaRESTLoginModule required restUser=“restuser” restPassword=“restbpm”;
};

BonitaAuth-default {
org.ow2.bonita.identity.auth.BonitaIdentityLoginModule required domain=“default”;
org.ow2.bonita.identity.auth.LocalStorageLoginModule required domain=“default”;
};

BonitaStore-default {
org.ow2.bonita.identity.auth.LocalStorageLoginModule required domain=“default”;
};

/**

  • Used by the REST server
    */
    BonitaRESTServer {
    org.ow2.bonita.identity.auth.BonitaRESTServerLoginModule required logins=“restuser” passwords=“restbpm” roles=“restuser”;
    };

Under tomcat, external/security folder

I have this jaas-standard.cfg

BonitaAuth { org.ow2.bonita.identity.auth.BonitaIdentityLoginModule required; };

BonitaStore {
org.ow2.bonita.identity.auth.BonitaRESTLoginModule required restUser=“restuser” restPassword=“restbpm”;
};

BonitaAuth-default {
org.ow2.bonita.identity.auth.BonitaIdentityLoginModule required domain=“default”;
org.ow2.bonita.identity.auth.LocalStorageLoginModule required domain=“default”;
};

BonitaStore-default {
org.ow2.bonita.identity.auth.LocalStorageLoginModule required domain=“default”;
};

/**

  • Used by the REST server
    */
    BonitaRESTServer {
    org.ow2.bonita.identity.auth.BonitaRESTServerLoginModule required logins=“restuser” passwords=“restbpm” roles=“restuser”;
    };

Does this correct, or I have to change something here ?

Your configuration seems ok. Do you have the same error if you configure the User XP in the http mode? just to identify if the problems comes from client or server side?

well,

I kept the same configuration for both jaas-standard.cfg files

and I changed the setenv.bat

from

@echo on

rem Sets some variables
set BONITA_HOME=“-DBONITA_HOME=%BONITA_HOME%”
set LOG_OPTS=“-Djava.util.logging.config.file=%CATALINA_HOME%\external\logging\logging.properties”
set SECURITY_OPTS=“-Djava.security.auth.login.config=%CATALINA_HOME%\external\security\jaas-standard.cfg”
set MEMORY_OPTS=“-Xshare:auto -Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError”
set CATALINA_OPTS=%JAVA_OPTS% %LOG_OPTS% %SECURITY_OPTS% %BONITA_HOME% %MEMORY_OPTS%

#set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_43\bin
#set BONITA_OPTS=“-Dorg.ow2.bonita.environment=/conf/bonita-environment.xml”
#set SECURITY_OPTS=“-Djava.security.auth.login.config=/conf/jaas-standard.cfg”
#set LOG_OPTS=“-Djava.util.logging.config.file=/conf/logging.properties”
#set JAVA_OPTS=“$LOG_OPTS $SECURITY_OPTS $BONITA_OPTS”

to

@echo on

rem Sets some variables
set BONITA_HOME=“-DBONITA_HOME=%CATALINA_HOME%\bonita”
set LOG_OPTS=“-Djava.util.logging.config.file=%CATALINA_HOME%\external\logging\logging.properties”
set SECURITY_OPTS=“-Djava.security.auth.login.config=%CATALINA_HOME%\external\security\jaas-standard.cfg”
set MEMORY_OPTS=“-Xshare:auto -Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError”
set CATALINA_OPTS=%JAVA_OPTS% %LOG_OPTS% %SECURITY_OPTS% %BONITA_HOME% %MEMORY_OPTS%
set APITYPE_OPTS=-Dorg.ow2.bonita.api-type=“REST”
set URL_OPTS=-Dorg.ow2.bonita.rest-server-address=“http://localhost:8080/bonita-server-rest/

#set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_43\bin
#set BONITA_OPTS=“-Dorg.ow2.bonita.environment=/conf/bonita-environment.xml”
#set SECURITY_OPTS=“-Djava.security.auth.login.config=/conf/jaas-standard.cfg”
#set LOG_OPTS=“-Djava.util.logging.config.file=/conf/logging.properties”
#set JAVA_OPTS=“$LOG_OPTS $SECURITY_OPTS $BONITA_OPTS”

set CATALINA_OPTS=%CATALINA_OPTS% %SECURITY_OPTS% %BONITA_HOME% %APITYPE_OPTS% %URL_OPTS% -Dfile.encoding=UTF-8 -Xshare:auto -Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError

I got the same error

I don’t know if the problem is due to that, but you have define some system properties twice:

set CATALINA_OPTS=%JAVA_OPTS% %LOG_OPTS% %SECURITY_OPTS% %BONITA_HOME% %MEMORY_OPTS% and set CATALINA_OPTS=%CATALINA_OPTS% %SECURITY_OPTS% %BONITA_HOME% %APITYPE_OPTS% %URL_OPTS% -Dfile.encoding=UTF-8 -Xshare:auto -Xms512m -Xmx1024m -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError