OSLCQuery using OSLCAuthClient?

Hi,

I wanted to use OSLCQuery using an OslcAuthClient. I noticed that it is not possible to use oslcAuthClient.getOSLCClient().lookUpQueryCapablitiy() and all those other useful methods, which are defined in the “OSLCClient” class in Eclipse Lyo. You always get a 401 unauthorized error. Probably due to the fact, that those methods use the “getResource” method of the OSLCClient, which has no requestHeaders. Is this something which could be considered for a newer version of lyo? Essentially you would just need to copy the methods from OSLCClient to OSLCAuthClient and it would use the “getResource” method of the OSLCAuthClient, which includes the requestheaders, and therefore the oauth accesstoken.

Hi

Have a look at this sample code which illustrates how to use the Lyo Client with different authentication mechanisms.

Please note that the project is using Lyo 4.0.0. Feel free to update to latest. But it should work as is.

@jad Yeah, I have found this sample too and implemented it in my code. It works fine. Authorization works. I was referring to how the OslcClient.java has more functions (lookUpServiceProviderURL, lookUpQueryCapability etc.) than the OslcAuthClient.java, which I think should be included in the OSLCAuthClient too.

Hi @LyoUser !

OslcOAuthClient wraps OslcClient. You can see in the code, how getResource is implemented?

Maybe you can similarly extend OslcOAuthClient to cover the methods you need? My guess is that the initial implementation only wrapped the most basics methods, with the intention to extend it as needs arise.

You can even see that methods like createResource and updateResource simply through UnsupportedOperationException.

regards

Yeah, that is how I did it, by using custom classes. Still wouldn’t it make sense to update those?

@LyoUser, sure, it would make total sense! If you could please contribute your custom classes (the relevant portions), we can incorporate implementations for the methods you mentioned directly into Lyo Client.

Usually I would create a pull request in GitHub, but that is currently not possible for me, so hope it works this way:

  1. The first thing is really simple, just copy all methods starting with “lookup” (e.g. “lookupQueryCapability”) from OslcClient.java to OslcAuthClient.java. This works since it just uses the “getResource” method of the current class, which in the OslcAuthClient has the requestHeaders with the OauthToken.

  2. Remove the “UnsupportedExceptions” in all those methods in the OslcAuthClient, which do something with the resources. Instead replace them with the same methods from the OslcClient. These then need to be updated, so that they include the requestHeaders. As an example here is the updateResource method:

public Response updateResource(String url, final Object artifact, String mediaType, String acceptType, String ifMatch,
String configurationContext) {

  Response response = null;
  boolean redirect = false;

  do {
  	Builder invocationBuilder = client.target(url).request()
  			.accept(acceptType)
  			.header(OSLCConstants.OSLC_CORE_VERSION, version);

  	if(ifMatch != null) {
  		invocationBuilder.header(HttpHeaders.IF_MATCH, ifMatch);
  	}
  	if(configurationContext != null) {
  		invocationBuilder.header(OSLCConstants.CONFIGURATION_CONTEXT_HEADER, configurationContext);
  	}

    //Beginning of the new code for the Authorization for the OSLCAuthClient
    Map<String,String> headers=null;
    try {
          headers= appendAuthorizationHeader(url, HttpMethod.PUT,null);
          for (Map.Entry<String,String> entry : headers.entrySet()){
                invocationBuilder.header(entry.getKey(),entry.getValue());
              }  
          } catch (Exception e) {
             //do something
      }
      //End of new code for the OSLCAuthClient

  	response = invocationBuilder
  			.put(Entity.entity(artifact, mediaType));

  	if (Response.Status.fromStatusCode(response.getStatus()).getFamily() == Status.Family.REDIRECTION) {
  		url = response.getStringHeaders().getFirst(HttpHeaders.LOCATION);
  		response.readEntity(String.class);
  		redirect = true;
  	} else {
  		redirect = false;
  	}
  } while (redirect);

  return response;

}

The new part with the headers has to be used for the delete and create methods aswell. Just replace HttpMethod.PUT with HttpMethod.POST or HttpMethod.DELETE.
Of course you can also just go ahead and change the OSLCClient.java methods to also accept headers similar to the “getResource” method in the OSLCClient.java. Then you just need to call the OslcClient and add the headers to the method. I leave this to you, I went with replacing the OslcAuthClient.

  1. The classes OslcQuery and OslcQueryResult have to be updated to also accept the OslcAuthClient. Either by adding an option to use the OslcQuery class with the OslcAuthClient in the constructor or by creating a a new class (e.g. OslcAuthQuery). Same for the OslcQueryResult. I went with creating new classes, but since this is really simple I leave that up to you aswell. Maybe you also have a better way to implement this.

Hope this helps!

1 Like

Thank you, @LyoUser, we will use it to expand the OslcAuthClient!