AnsweredAssumed Answered

Getting 401 when accessing rest web-service  from web application

Question asked by thiagarajan on Mar 14, 2016
Latest reply on Mar 15, 2016 by thiagarajan
I have two tomcat instances running with Activiti rest and Activiti explorer. One is in my local machine and the other is in our QA box. But both of them connect to same database.

I wrote a java code (By creating rest template) to access Activiti rest web-service. It worked perfectly. I was able to start a process, post a form….etc (Both POST and GET). The java program worked perfectly with both local tomcat rest url and QA rest url.

Then i embedded the java code in my web application (Developed using spring and deployed in Web Sphere). Local tomcat rest url worked perfectly. But when i change the url to point QA environment. I am getting 401 error.

Why I am not able to access Acitiviti rest (QA url) from my web application. It is working perfectly with local host.  I didn't find any issues with username or password. Even i am able to do web-service call from Advance REST client plugin for chrome.

Do i need to anything special to make it work from web application. Quick help is highly appreciated. 


   public void setRestTemplate(String username, String password) {
      restTemplate = (this.restTemplate == null) ? new RestTemplate() : restTemplate;
      ClientHttpRequestFactory requestFactory = null;
      try {
         requestFactory = getRequestFactory(username, password);
         restTemplate.setRequestFactory(requestFactory);
      } catch (Exception e) {
         System.out.println("Not able to create rest template" + e);
      }
   }

   public ClientHttpRequestFactory getRequestFactory(String activiti_username, String activiti_password) {

      ClientHttpRequestFactory requestFactory = null;
      try {
         final RequestConfig config = RequestConfig.custom().setConnectTimeout(TIME_OUT * 1000)
               .setConnectionRequestTimeout(TIME_OUT * 1000).setSocketTimeout(TIME_OUT * 1000).build();
         final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
         credentialsProvider.setCredentials(new AuthScope(HOST, PORT, AuthScope.ANY_REALM),
               new UsernamePasswordCredentials(activiti_username, activiti_password));
         final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config)
               .setDefaultCredentialsProvider(credentialsProvider).build();
         requestFactory = new HttpComponentsClientHttpRequestFactory(client);
      } catch (NumberFormatException e) {
         System.out.println("Not able to create request factory: getRequestFactory" + e);
      } catch (Exception e) {
         System.out.println("Not able to create request factory: getRequestFactory" + e);
      }
      return requestFactory;
   }

   private void getProcessDefinitions() {
      String url = BASE_URl + "repository/process-definitions";
      String result = restTemplate.getForObject(url, String.class);
      System.out.println(result);
   }


BASE_URl  = http://localhost:8076/activiti-rest/service/  (Both java program and web application is working)
BASE_URI = http://qahost:8076/activiti-rest/service/ ( Only java program is working but web application is throwing 401 exception)


Do i need to anything special to make it work from web application. Quick help is highly appreciated. 

Outcomes