Response Status 401 while trying to retrieve project areas from RM Catalog!!!
Hi
I am trying to retrieve project areas from the RM catalog through OSLC URL https://localhost:9443/rm/discovery/RMCatalog through the following java code
private String fetchProjectXML(String login, String password,
String catalogURI) {
String server = "https://localhost:9443/rm";
String mediatype = "application/rdf+xml";
String node = null;
try {
// Setup the HttClient
HttpClient httpclient = new DefaultHttpClient();
HttpUtils.setupLazySSLSupport(httpclient);
HttpGet documentGet = new HttpGet(catalogURI);
System.out.println("document get :: "+documentGet.toString());
documentGet.addHeader("Accept", mediatype);
documentGet.addHeader("OSLC-Core-Version", "2.0");
HttpResponse response = HttpUtils.sendGetForSecureDocument(server,
documentGet, login, password, httpclient);
System.out.println("Response"+response.toString());
System.out.println("Response Status Code"+response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println("response=="+responseString);
node = responseString;
HttpUtils.printResponseHeaders(response);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidCredentialsException e) {
e.printStackTrace();
} finally {
// Shutdown the HTTP connection
// httpclient.getConnectionManager().shutdown();
}
return node;
}
public static HttpResponse sendGetForSecureDocument(String serverURI, HttpGet request, String login, String password, HttpClient httpClient)
throws IOException, InvalidCredentialsException {
DEBUG=true;
// Step (1): Request the protected resource
if (DEBUG) System.out.println(">> GET(1) "+request.getURI());
HttpResponse documentResponse = httpClient.execute(request);
System.out.println("Reponse status"+documentResponse.getStatusLine().getStatusCode());
if (DEBUG) {
System.out.println(">> Response Headers:");
HttpUtils.printResponseHeaders(documentResponse);
}
if (documentResponse.getStatusLine().getStatusCode() == 200) {
Header header = documentResponse.getFirstHeader(AUTHREQUIRED);
if ((header!=null) && ("authrequired".equals(header.getValue()))) {
documentResponse.getEntity().consumeContent();
// The server requires an authentication: Create the login form
HttpPost formPost = new HttpPost(serverURI+"/j_security_check");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("j_username", login));
nvps.add(new BasicNameValuePair("j_password", password));
formPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
//Step (2): The client submits the login form
if (DEBUG) System.out.println(">> POST "+formPost.getURI());
HttpResponse formResponse = httpClient.execute(formPost);
if (DEBUG) HttpUtils.printResponseHeaders(formResponse);
header = formResponse.getFirstHeader(AUTHREQUIRED);
if ((header!=null) && ("authfailed".equals(header.getValue()))) {
// The login failed
throw new InvalidCredentialsException("Authentication failed");
} else {
formResponse.getEntity().consumeContent();
// The login succeed
// Step (3): Request again the protected resource
if (DEBUG) System.out.println(">> GET(2) "+request.getURI());
HttpGet documentGet2;
try {
documentGet2 = (HttpGet)(request.clone());
return httpClient.execute(documentGet2);
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return documentResponse;
}
I am getting a 401 response status for the above code. I understand that this error occurs if the user is unauthorized, but care is taken that the user has JazzAdmin Repository permission as well as necessary license.
document get :: org.apache.http.client.methods.HttpGet@9f671b
>> GET(1) https://localhost:9443/rm/discovery/RMCatalog
Reponse status401
>> Response Headers:
- Server: Apache-Coyote/1.1
- set-cookie: jfs-oauth-access-token0=; expires=Thu, 01-Jan-70 00:00:00 GMT; path=/rm
- set-cookie: jfs-oauth-access_token-secret0=; expires=Thu, 01-Jan-70 00:00:00 GMT; path=/rm
- set-cookie: jfs-request-token-cd09914f0509468b9d577cea0664502c="C+t6iqxZNdy0Gn0dHLun92cnBSNosZaLXA6vOl9kJF0="; Version=1; expires=Wed, 05-Jun-13 08:51:49 GMT; path=/rm
- X-jazz-web-oauth-url: https://localhost:9443/jts/oauth-authorize?oauth_token=cd09914f0509468b9d577cea0664502c
- WWW-Authenticate: OAuth realm=https://localhost:9443/jts/oauth-authorize
- Content-Type: text/html
- Content-Length: 324
- Date: Wed, 05 Jun 2013 08:46:49 GMT
Responseorg.apache.http.message.BasicHttpResponse@10382a9
Response Status Code401
Fe
Jun 5, 2013 2:16:49 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "set-cookie: jfs-request-token-cd09914f0509468b9d577cea0664502c="C+t6iqxZNdy0Gn0dHLun92cnBSNosZaLXA6vOl9kJF0="; Version=1; expires=Wed, 05-Jun-13 08:51:49 GMT; path=/rm". Unable to parse expires attribute: Wed
Jun 5, 2013 2:16:49 PM org.apache.http.impl.client.DefaultRequestDirector handleResponse
WARNING: Authentication error: Unable to respond to any of these challenges: {oauth=WWW-Authenticate: OAuth realm=https://localhost:9443/jts/oauth-authorize}
It would be very helpful if some one could address this issue.
Thanks in advance.
I am trying to retrieve project areas from the RM catalog through OSLC URL https://localhost:9443/rm/discovery/RMCatalog through the following java code
private String fetchProjectXML(String login, String password,
String catalogURI) {
String server = "https://localhost:9443/rm";
String mediatype = "application/rdf+xml";
String node = null;
try {
// Setup the HttClient
HttpClient httpclient = new DefaultHttpClient();
HttpUtils.setupLazySSLSupport(httpclient);
HttpGet documentGet = new HttpGet(catalogURI);
System.out.println("document get :: "+documentGet.toString());
documentGet.addHeader("Accept", mediatype);
documentGet.addHeader("OSLC-Core-Version", "2.0");
HttpResponse response = HttpUtils.sendGetForSecureDocument(server,
documentGet, login, password, httpclient);
System.out.println("Response"+response.toString());
System.out.println("Response Status Code"+response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "UTF-8");
System.out.println("response=="+responseString);
node = responseString;
HttpUtils.printResponseHeaders(response);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidCredentialsException e) {
e.printStackTrace();
} finally {
// Shutdown the HTTP connection
// httpclient.getConnectionManager().shutdown();
}
return node;
}
public static HttpResponse sendGetForSecureDocument(String serverURI, HttpGet request, String login, String password, HttpClient httpClient)
throws IOException, InvalidCredentialsException {
DEBUG=true;
// Step (1): Request the protected resource
if (DEBUG) System.out.println(">> GET(1) "+request.getURI());
HttpResponse documentResponse = httpClient.execute(request);
System.out.println("Reponse status"+documentResponse.getStatusLine().getStatusCode());
if (DEBUG) {
System.out.println(">> Response Headers:");
HttpUtils.printResponseHeaders(documentResponse);
}
if (documentResponse.getStatusLine().getStatusCode() == 200) {
Header header = documentResponse.getFirstHeader(AUTHREQUIRED);
if ((header!=null) && ("authrequired".equals(header.getValue()))) {
documentResponse.getEntity().consumeContent();
// The server requires an authentication: Create the login form
HttpPost formPost = new HttpPost(serverURI+"/j_security_check");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("j_username", login));
nvps.add(new BasicNameValuePair("j_password", password));
formPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
//Step (2): The client submits the login form
if (DEBUG) System.out.println(">> POST "+formPost.getURI());
HttpResponse formResponse = httpClient.execute(formPost);
if (DEBUG) HttpUtils.printResponseHeaders(formResponse);
header = formResponse.getFirstHeader(AUTHREQUIRED);
if ((header!=null) && ("authfailed".equals(header.getValue()))) {
// The login failed
throw new InvalidCredentialsException("Authentication failed");
} else {
formResponse.getEntity().consumeContent();
// The login succeed
// Step (3): Request again the protected resource
if (DEBUG) System.out.println(">> GET(2) "+request.getURI());
HttpGet documentGet2;
try {
documentGet2 = (HttpGet)(request.clone());
return httpClient.execute(documentGet2);
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return documentResponse;
}
I am getting a 401 response status for the above code. I understand that this error occurs if the user is unauthorized, but care is taken that the user has JazzAdmin Repository permission as well as necessary license.
document get :: org.apache.http.client.methods.HttpGet@9f671b
>> GET(1) https://localhost:9443/rm/discovery/RMCatalog
Reponse status401
>> Response Headers:
- Server: Apache-Coyote/1.1
- set-cookie: jfs-oauth-access-token0=; expires=Thu, 01-Jan-70 00:00:00 GMT; path=/rm
- set-cookie: jfs-oauth-access_token-secret0=; expires=Thu, 01-Jan-70 00:00:00 GMT; path=/rm
- set-cookie: jfs-request-token-cd09914f0509468b9d577cea0664502c="C+t6iqxZNdy0Gn0dHLun92cnBSNosZaLXA6vOl9kJF0="; Version=1; expires=Wed, 05-Jun-13 08:51:49 GMT; path=/rm
- X-jazz-web-oauth-url: https://localhost:9443/jts/oauth-authorize?oauth_token=cd09914f0509468b9d577cea0664502c
- WWW-Authenticate: OAuth realm=https://localhost:9443/jts/oauth-authorize
- Content-Type: text/html
- Content-Length: 324
- Date: Wed, 05 Jun 2013 08:46:49 GMT
Responseorg.apache.http.message.BasicHttpResponse@10382a9
Response Status Code401
Fe
Jun 5, 2013 2:16:49 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "set-cookie: jfs-request-token-cd09914f0509468b9d577cea0664502c="C+t6iqxZNdy0Gn0dHLun92cnBSNosZaLXA6vOl9kJF0="; Version=1; expires=Wed, 05-Jun-13 08:51:49 GMT; path=/rm". Unable to parse expires attribute: Wed
Jun 5, 2013 2:16:49 PM org.apache.http.impl.client.DefaultRequestDirector handleResponse
WARNING: Authentication error: Unable to respond to any of these challenges: {oauth=WWW-Authenticate: OAuth realm=https://localhost:9443/jts/oauth-authorize}
It would be very helpful if some one could address this issue.
Thanks in advance.
Accepted answer
Hi Rayma,
Your code handles the case where the server returns a status code of 200 and the "X-com-ibm-team-repository-web-auth-msg" header. But when the server returns a status code of 401 along with the "X-jazz-web-oauth-url" header you'll need to use different authentication steps.
For sample code, download the OSLC Workshop and see the example net.jazz.oslc.consumer.rm.client/src/net/jazz/oslc/consumer/examples/Example03.java. The actual authentication is handled by the doRRCOAuth method in the file net.jazz.oslc.consumer.rm.client/src/net/jazz/oslc/utils/HttpUtils.java
Regards,
Jon
One other answer
Hi Ramya,
This issue has come up before in this forum question https://jazz.net/forum/questions/110556/unable-to-parse-expires-attribute-mon . From that forum post, https://issues.apache.org/jira/browse/HTTPCLIENT-896 links to a discussion of this issue and a possible workaround involving "CookieSpecPNames.DATE_PATTERNS". I hope this helps.
Regards,
Jason
This issue has come up before in this forum question https://jazz.net/forum/questions/110556/unable-to-parse-expires-attribute-mon . From that forum post, https://issues.apache.org/jira/browse/HTTPCLIENT-896 links to a discussion of this issue and a possible workaround involving "CookieSpecPNames.DATE_PATTERNS". I hope this helps.
Regards,
Jason
Comments
Stef van Dijk
FORUM MODERATOR / JAZZ DEVELOPER Jun 05 '13, 11:17 a.m.Stef van Dijk
FORUM MODERATOR / JAZZ DEVELOPER Jun 05 '13, 11:21 a.m.