It's all about the answers!

Ask a question

RTC Java server side extension - performing Http Request


K. Hartig (132) | asked Mar 08 '22, 10:32 a.m.
Hello everyone,
I am working on a server extension for EWM using Java API (RTC SDK) and try to solve a basic issue (as I thought).
After saving a work item in a follow-up action I want to perform Http requests. As a starting point I try to get a Http response for a GET request using the URI of the work item. Looks like that:

// workitemURLRelative is "resource/itemName/com.ibm.team.workitem.WorkItem/26"
// workitemURLAbsolute is "https://localhost:7443/jazz/resource/itemName/com.ibm.team.workitem.WorkItem/26"
// jazzServerURL is "https://localhost:7443/jazz/"
String workitemURLRelative = Location.namedLocation(newState, getPublicRepositoryURL()).toRelativeUri().toString();
String workitemURLAbsolute = Location.namedLocation(newState, getPublicRepositoryURL()).toAbsoluteUri().toString();
IServerDescriptionService descriptionService = getService(IServerDescriptionService.class);
String jazzServerURL = descriptionService.getJazzTeamServerURI();
ProtocolSocketFactory factory = new SSLProtocolSocketFactory();
Protocol https = new Protocol("https", factory, 443);
Protocol.registerProtocol("https", https);

@SuppressWarnings("static-access")
OAuthServiceProviderInfo oAuthServiceProviderInfo = oauthHttpclient.createProviderInfo(jazzServerURL);

try {
oauthHttpclient = new OAuthHttpClient(oAuthServiceProviderInfo, factory);
} catch (HttpException e1) {
e1.printStackTrace();
}
String output = "";
try {
      GetMethod methodGet = new GetMethod(workitemURLRelative);
methodGet.addRequestHeader("accept", "application/rdf+xml");
methodGet.addRequestHeader("OSLC-Core-Version", "2.0");
oauthHttpclient.executeMethod(methodGet);
output = methodGet.getResponseBodyAsString();
methodGet.releaseConnection();
} catch (final Exception e) {
e.printStackTrace();
}
Unfortunately, I get the following error message indicating that the URI shall be relative instead of absolute.
java.lang.UnsupportedOperationException: Only host relative URIs can be used.
      at com.ibm.team.repository.common.oauth.OAuthHttpClient.executeMethod(OAuthHttpClient.java:113)
    
Using the absolute URI (workitemURLAbsolute), the request string can not be found. (However, in another REST tool I get proper responses for the used absolute request string.)
...
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 404 Not Found</title>
...

Do you have any hints how I can use absolute URIs or what the relative URI should look like?

In a client application performing Http requests worked fine, also with absolute URIs.
For the server extension I can not use the same client libraries but changed to common libs.
Are you aware of another solution to perform a simple Http GET request in a server side extension, being logged in already and without the use of client libs?

Thanks in advance


Comments
Ralph Schoon commented Mar 08 '22, 11:14 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

I can find 3 lines where the  com.ibm.team.repository.common.oauth.OAuthHttpClient is used and 3 where IOAuthHttpClient is used in the whole EWM server SDK. I can not see how that class is used, for what it is used and if it should be used or where.


K. Hartig commented Mar 08 '22, 12:14 p.m.

Does it mean, you suggest to use another approach?


Ralph Schoon commented Mar 09 '22, 2:02 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

I just point out the obvious. As I have not tried something like that yet and there is little information about what this is all about, so I am not in a position to suggest anything. 


Ralph Schoon commented Mar 09 '22, 2:37 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

I would likely search the EWM SDK for HttpClient and have a look around if there are examples and test cases using those. 


K. Hartig commented Mar 09 '22, 6:44 a.m.

I have tried out the org.apache.commons.httpclient.HttpClient before. Since the Http requests need to be send as a logged in user, I need to consider authorization in some way. I can not provide username and password. I guessed that the OAuthHttpClient is supposed to handle that situation.


Ralph Schoon commented Mar 09 '22, 7:51 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
I have not done this myself, but I know for a fact that there is stuff in the EWM SDK that should allow you to use Authentication or OAuth to do this.
I have searched the EWM (client and server) SDK for HTTPClient and found other references. e.g. com.ibm.team.jfs.app.http.client.JazzHttpClient . So there might be a service layer you can use that inherits the authentication, or you have to set up some OAuth based mechanism. Fort API other then the one you use there are at least some unit tests.
showing 5 of 6 show 1 more comments

Be the first one to answer this question!


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.