I am using Maximo 7.5 REST API and want to write REST client in java to consume this RESTful service. I have enabled Maximo security in Maximo 7.5 side to keep the Maximo users to access its own REST services. Below is my web.xml for Maximo RESTful service looks like.
<security-constraint>
<web-resource-collection>
<web-resource-name>REST Servlet for Web App</web-resource-name>
<description>Object Structure Service Servlet (HTTP POST) accessible by authorized users</description>
<url-pattern>/rest/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description>Roles that have access to Object Structure Service Servlet (HTTP POST)</description>
<role-name>maximouser</role-name>
</auth-constraint>
<user-data-constraint>
<description>data transmission gaurantee</description>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>REST Web Application Realm</realm-name>
</login-config>
I am successfully able to query the Maximo REST service using the Chrome's Postman plugin. Below 2 are my Postman's (REST client) Headers. 1. MAXAUTH - bWF4YWRtaW46bWF4YWRtaW4= 2. Accept - Application/xml
Though I have given the authorization (MAXAUTH) in headers, I used to get the pop-up window to enter username and password to query Maximo REST service. Once I give the credentials, I get the response (shown below)
The below is my Java code to consume above same RESTful service. I am continuously getting 401 error and though I am giving credentials as property, it is not authorizing it.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class RESTConsume {
// http://localhost:8080/RESTfulExample/json/product/get
public static void main(String[] args) {
try {
URL url = new URL("HOSTNAME/maxrest/rest/os/mxperson?personid=maxadmin");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "Application/xml");
connection.setRequestProperty("MAXAUTH", "bWF4YWRtaW46bWF4YWRtaW4=");
System.out.println("Output from Server ....1 \n");
/*
* if (conn.getResponseCode() != 200) {
* System.out.println("Output from Server ....2 \n");
*
* throw new RuntimeException("Failed : HTTP error code : "+
* conn.getResponseCode()); }
*/
System.out.println("Output from Server ....3 \n");
BufferedReader br = new BufferedReader(new InputStreamReader(
(connection.getInputStream())));
System.out.println("Output from Server ....4 \n");
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Below is my output:
Output from Server ....1
Output from Server ....3
java.io.IOException: Server returned HTTP response code: 401 for URL: http://vhost0043.dc1.co.us.compute.ihost.com/maxrest/rest/os/mxperson?personid=maxadmin
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at RESTConsume.main(RESTConsume.java:35)
It is working for different RESTful service (not Maximo RESTful service)and getting the response as expected for which we don't have any security was not enabled. Please let me know if i need to do something extra to consume Maximo RESTful service.
Either the service is expecting username and password or the maxauth is invalid and hence in case of browser it's prompting you to enter explicitly but in case of maximo you are getting 401. please try supplying the user name and password headers also in the request.