JBPM REST Client 401 Error

Latest response

Hello Team,
I wrote a simple webservice to retrieve all of the organizational units within business central. I have no issues recieving a response when i paste the url in the browser or in a web based REST client. However when i call the REST service from a server side code I recieve a 401 error code.

I am running business central in JBOSS EAP 6.1. Is there any extra configuration i must make within JBOSS EAP or business central?

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
      public static void main(String[] args) {
            try {

                Client client = Client.create();

                WebResource webResource = client
                   .resource("http://localhost:8080/business-central/rest/organizationalunits");

                ClientResponse response = webResource.accept("application/json")
                           .get(ClientResponse.class);

                if (response.getStatus() != 200) {
                   throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatus());
                }

                String output = response.getEntity(String.class);

                System.out.println("Output from Server .... \n");
                System.out.println(output);

              } catch (Exception e) {

                e.printStackTrace();

              }

            }

Responses

Did you manage to solve this? 401 error is unauthorized which is strange if you have no security configured on the endpoint.

Attached the solution.
Thanks!

Issue is that theres no authentication for this client request. Implementation is below

public class OrganizaionalUnitsController {
    private static final Logger logger= LoggerFactory.getLogger(OrganizaionalUnitsController.class);
    public static void main(String[] args) throws Exception {
        String userName="bpmsAdmin";
        String passWord="whatever you want";
        URL url=new URL("http://localhost:8080/business-central/rest/organizationalunits");
        doStuff(userName,passWord,url);
    }

    public static void doStuff(String userName, String passWord,URL url) throws Exception {
        ClientRequest req = createRequest(url,userName,passWord);




        ClientResponse<String> res = req.get(String.class);
        String result = res.getEntity();
        System.out.println(result.toString());
    }
    private static ClientRequest createRequest(URL address,String userName,String passWord) {
        return getClientRequestFactory(userName,passWord).createRequest(address.toExternalForm());
        }

    private static ClientRequestFactory getClientRequestFactory(String userName,String passWord) {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST,
            AuthScope.ANY_PORT, AuthScope.ANY_REALM), new UsernamePasswordCredentials(userName,passWord));
            ClientExecutor clientExecutor = new ApacheHttpClient4Executor(httpClient);
            return new ClientRequestFactory(clientExecutor, ResteasyProviderFactory.getInstance());
    }
}