How to create a Module/RequirementCollection using OSLC ?
2 answers
This is how you can find the same question asked in the forum: https://www.google.com/search?q=how+to+create+a+module+OSLC+API+site%3Ajazz.net
I would suggest to go through some of the answers above. https://jazz.net/library/article/1197 is an article that is suggested for more information.
Hi,
i created it using below code, in case someone needs it:
package com.bsl.service;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.InvalidCredentialsException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.wink.client.ClientResponse;
import org.eclipse.lyo.client.exception.JazzAuthErrorException;
import org.eclipse.lyo.client.exception.JazzAuthFailedException;
import org.eclipse.lyo.client.exception.ResourceNotFoundException;
import org.eclipse.lyo.client.exception.RootServicesException;
import org.eclipse.lyo.client.oslc.OSLCConstants;
import org.eclipse.lyo.client.oslc.jazz.JazzFormAuthClient;
import org.eclipse.lyo.client.oslc.jazz.JazzRootServicesHelper;
import org.eclipse.lyo.client.oslc.resources.OslcQuery;
import org.eclipse.lyo.client.oslc.resources.OslcQueryParameters;
import org.eclipse.lyo.client.oslc.resources.OslcQueryResult;
import org.eclipse.lyo.client.oslc.resources.Requirement;
import org.eclipse.lyo.client.oslc.resources.RequirementCollection;
import org.eclipse.lyo.client.oslc.resources.RmUtil;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.oslc4j.core.model.ResourceShape;
import com.bsl.Business.DngPojo;
import net.oauth.OAuthException;
/
@author vaibhavs18
*/
public class CreateDngArtifact {
static String queryCapability;
static String serviceProviderUrl;
private static JazzRootServicesHelper rootServicesHelperPost = null;
private static JazzFormAuthClient clientPost = null;
static String queryCapabilityPost;
static String serviceProviderUrlPost;
static String projectAreaPost = "Migration POC With GC (Requirements)";
private static String serverUrlPost = "https://hjalmdclm.birlasoft.com:9443/rm";
private static String usernamePost = "anurag";
private static String passwordPost="anurag";
private static String catalogUrlPost;
private static JazzRootServicesHelper rootServicesHelper = null;
private static JazzFormAuthClient client = null;
private static String catalogUrl = null;
static Map<String, String> map = new HashMap<String, String>();
public static boolean createArtifact(int flag, String username, String password, String serverUrl, String projectArea,String artifactType, String artifactDescription, String artifactTitle) throws JazzAuthFailedException, JazzAuthErrorException, RootServicesException {
try{
System.out.println("Artifact type : " + artifactType);
rootServicesHelper = new JazzRootServicesHelper(serverUrl, OSLCConstants.OSLC_RM_V2);
client = rootServicesHelper.initFormClient(username, password);
client.formLogin();
serviceProviderUrl = client.lookupServiceProviderUrl(rootServicesHelper.getCatalogUrl(), projectArea);
// queryCapability = client.lookupQueryCapability(serviceProviderUrl,OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE);
// catalogUrl= rootServicesHelper.getCatalogUrl();
if(client.formLogin()==200){
System.out.println("Logged In ...");
}
else
throw new RuntimeException("Login Failed");
if(flag ==0) {
Requirement requirement = new Requirement();
String requirementFactory = client.lookupCreationFactory(serviceProviderUrl, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE);
System.out.println( "requirement factort vccreated ");
ResourceShape featureInstanceShape = RmUtil.lookupRequirementsInstanceShapes(serviceProviderUrl, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE, client, artifactType);
requirement .setInstanceShape(featureInstanceShape.getAbout());
requirement.setDescription(artifactDescription);
requirement.setTitle(artifactTitle);
System.out.println("Requiremenet created "+ artifactTitle);
System.out.println("-----------------");
ClientResponse response= client.createResource(requirementFactory, requirement, OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_RDF_XML);
if (response.getStatusCode() == 200 || response.getStatusCode() == 201) {
return true;
}
}
else if(flag == 1){
RequirementCollection requirementCollection = new RequirementCollection();
String requirementFactory = client.lookupCreationFactory(serviceProviderUrl, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_COLLECTION_TYPE);
System.out.println( "requirement factort vccreated ");
ResourceShape featureInstanceShape = RmUtil.lookupRequirementsInstanceShapes(serviceProviderUrl, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_COLLECTION_TYPE, client, artifactType);
requirementCollection.setInstanceShape(featureInstanceShape.getAbout());
requirementCollection.setDescription(artifactDescription);
requirementCollection.setTitle(artifactTitle);
System.out.println("-----------------");
ClientResponse response= client.createResource(requirementFactory, requirementCollection, OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_RDF_XML);
if (response.getStatusCode() == 200 || response.getStatusCode() == 201) {
return true;
}
}
}
catch(ResourceNotFoundException e){
e.printStackTrace();
}
catch(IOException e){
System.out.println(e.getMessage());
}
catch(OAuthException e){
System.out.println(e.getMessage());
}
catch(URISyntaxException e){
System.out.println(e.getMessage());
}
return false;
}
public static void postArtifact() throws ClientProtocolException, IOException, InvalidCredentialsException, RootServicesException, JazzAuthFailedException, JazzAuthErrorException, ResourceNotFoundException, OAuthException, URISyntaxException {
rootServicesHelperPost = new JazzRootServicesHelper(serverUrlPost, OSLCConstants.OSLC_RM_V2);
clientPost = rootServicesHelperPost.initFormClient(usernamePost, passwordPost);
clientPost.formLogin();
catalogUrlPost= rootServicesHelperPost.getCatalogUrl();
serviceProviderUrlPost = clientPost.lookupServiceProviderUrl(catalogUrlPost, projectAreaPost);
OslcQueryParameters queryParamsPost = new OslcQueryParameters();
queryParamsPost.setPrefix("dcterms=<http://purl.org/dc/terms/>");
queryParamsPost.setWhere("dcterms:identifier="+ 816);
queryParamsPost.setSelect("dcterms:title");
String queryCapabilityPost= clientPost.lookupQueryCapability(serviceProviderUrlPost, OSLCConstants.OSLC_RM_V2, OSLCConstants.RM_REQUIREMENT_TYPE);
OslcQuery queryPost = new OslcQuery(clientPost, queryCapabilityPost, 15, queryParamsPost);
OslcQueryResult resultPost = queryPost.submit();
System.out.println("Query Running .... ");
String[] urls= resultPost.getMembersUrls();
String resourceUri=(String)urls[0];
ClientResponse response = clientPost.getResource(resourceUri , OslcMediaType.APPLICATION_RDF_XML);
String noteXml = "<rdf:RDF\r\n"
+ " xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\r\n"
+ " xmlns:rm_property=\"https://hjalmdclm.birlasoft.com:9443/rm/types/\"\r\n"
+ " xmlns:j.0=\"http://jazz.net/ns/rm/dng/attribute#\"\r\n"
+ " xmlns:acp=\"http://jazz.net/ns/acp#\"\r\n"
+ " xmlns:public_rm_10=\"http://www.ibm.com/xmlns/rm/public/1.0/\"\r\n"
+ " xmlns:calm=\"http://jazz.net/xmlns/prod/jazz/calm/1.0/\"\r\n"
+ " xmlns:jazz_rm=\"http://jazz.net/ns/rm#\"\r\n"
+ " xmlns:acc=\"http://open-services.net/ns/core/acc#\"\r\n"
+ " xmlns:process=\"http://jazz.net/ns/process#\"\r\n"
+ " xmlns:dcterms=\"http://purl.org/dc/terms/\"\r\n"
+ " xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\r\n"
+ " xmlns:oslc=\"http://open-services.net/ns/core#\"\r\n"
+ " xmlns:nav=\"http://jazz.net/ns/rm/navigation#\"\r\n"
+ " xmlns:oslc_config=\"http://open-services.net/ns/config#\"\r\n"
+ " xmlns:oslc_rm=\"http://open-services.net/ns/rm#\"\r\n"
+ " xmlns:dng_task=\"http://jazz.net/ns/rm/dng/task#\"\r\n"
+ " xmlns:rm=\"http://www.ibm.com/xmlns/rdm/rdf/\"\r\n"
+ " xmlns:oslc_auto=\"http://open-services.net/ns/auto#\" > \r\n"
+ " <rdf:Description rdf:about=\"https://hjalmdclm.birlasoft.com:9443/rm/resources/BI_qoooM6fxEeuYp_otMLsSbQ\">\r\n"
+ " <j.0:masterForeignModifiedBy rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">anurag</j.0:masterForeignModifiedBy>\r\n"
+ " <j.0:masterForeignId rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">68</j.0:masterForeignId>\r\n"
+ " <oslc_config:component rdf:resource=\"https://hjalmdclm.birlasoft.com:9443/rm/cm/component/_bqfukKfpEeuYp_otMLsSbQ\"/>\r\n"
+ " <rdf:type rdf:resource=\"http://jazz.net/ns/rm#Text\"/>\r\n"
+ " <dcterms:created rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2021-04-28T07:16:35.772Z</dcterms:created>\r\n"
+ " <j.0:masterForeignCreatedOn rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2021-04-07T13:19:19.465Z</j.0:masterForeignCreatedOn>\r\n"
+ " <dcterms:description rdf:parseType=\"Literal\"></dcterms:description>\r\n"
+ " <acp:accessControl rdf:resource=\"https://hjalmdclm.birlasoft.com:9443/rm/accessControl/_bTbSYKfpEeuYp_otMLsSbQ\"/>\r\n"
+ " <oslc:instanceShape rdf:resource=\"https://hjalmdclm.birlasoft.com:9443/rm/types/OT_a8fU4afwEeuYp_otMLsSbQ\"/>\r\n"
+ " <j.0:masterForeignCreatedBy rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">anurag</j.0:masterForeignCreatedBy>\r\n"
+ " <dcterms:identifier rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">816</dcterms:identifier>\r\n"
+ " <dcterms:title rdf:parseType=\"Literal\">This is to Rich Text 1 Fromatting in DNG: Gaurav Sandeep </dcterms:title>\r\n"
+ " <j.0:masterForeignModifiedOn rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2021-04-07T13:53:57.488Z</j.0:masterForeignModifiedOn>\r\n"
+ " <rdf:type rdf:resource=\"http://open-services.net/ns/rm#Requirement\"/>\r\n"
+ " <dcterms:contributor rdf:resource=\"https://hjalmdclm.birlasoft.com:9443/jts/users/anurag\"/>\r\n"
+ " <process:projectArea rdf:resource=\"https://hjalmdclm.birlasoft.com:9443/rm/process/project-areas/_bTbSYKfpEeuYp_otMLsSbQ\"/>\r\n"
+ " <jazz_rm:primaryText rdf:parseType=\"Literal\"><div xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"
+ "<hr/><p>This is to Rich Text 1 Fromatting in DNG: Sandeep</p>\r\n"
+ "<p><b><span style=\"font-size:12pt\">Bold <img alt=\"Embedded image\" src=\"C:/temp/Capture.PNG\"/>\r\n"
+ "</span></b></p>\r\n"
+ "<p> </p>\r\n"
+ "<p><i><span style=\"font-size:16pt\"><b>Italic</b></span></i></p>\r\n"
+ "<p><u><i><span style=\"font-size:16pt\"><b>Underline</b></span></i></u></p>\r\n"
+ "<p><u><i><span style=\"font-size:16pt\"><b><span style=\"background-color:#a52a2a\">Test</span></b></span></i></u></p>\r\n"
+ "<p><u><i><span style=\"font-size:16pt\"><b><span style=\"background-color:#a52a2a\"><span style=\"color:#ff0000\">THis is </span></span></b></span></i></u></p>\r\n"
+ "<p><b><span style=\"color:#0000ff\">This is to Test The formatting</span></b></p>\r\n"
+ "<hr/>\r\n"
+ "<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width: 500px ; table-layout: fixed ; overflow-wrap: break-word ; border-collapse: collapse ; border-color: #696969\"><tbody><tr><td colspan=\"1\" id=\"_1617802905312\" rowspan=\"1\" style=\"border-color:#696969; overflow:hidden; padding:1px; width:250px\"> </td><td colspan=\"1\" id=\"_1617802905313\" rowspan=\"1\" style=\"border-color:#696969; overflow:hidden; padding:1px; width:250px\"> </td></tr><tr><td colspan=\"1\" id=\"_1617802905314\" rowspan=\"1\" style=\"border-color:#696969; overflow:hidden; padding:1px; width:250px\"> </td><td colspan=\"1\" id=\"_1617802905315\" rowspan=\"1\" style=\"border-color:#696969; overflow:hidden; padding:1px; width:250px\"> </td></tr><tr><td colspan=\"1\" id=\"_1617802905316\" rowspan=\"1\" style=\"border-color:#696969; overflow:hidden; padding:1px; width:250px\"> </td><td colspan=\"1\" id=\"_1617802905317\" rowspan=\"1\" style=\"border-color:#696969; overflow:hidden; padding:1px; width:250px\"> </td></tr></tbody></table>\r\n"
+ "<hr/><p><a href=\"https://hjalmdclm.birlasoft.com:9443/rm/resources/MD_TVFepqfwEeuYp_otMLsSbQ\" id=\"_1617802899853\">Hyperlink:</a></p>\r\n"
+ "<p> </p>\r\n"
+ "</div></jazz_rm:primaryText>\r\n"
+ " <dcterms:creator rdf:resource=\"https://hjalmdclm.birlasoft.com:9443/jts/users/anurag\"/>\r\n"
+ " <dcterms:modified rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2021-04-28T07:16:35.772Z</dcterms:modified>\r\n"
+ " <oslc:serviceProvider rdf:resource=\"https://hjalmdclm.birlasoft.com:9443/rm/oslc_rm/_bTbSYKfpEeuYp_otMLsSbQ/services.xml\"/>\r\n"
+ " </rdf:Description>\r\n"
+ "</rdf:RDF>";
String xml=noteXml; //response.getEntity(String.class);
HttpGet httpGet = new HttpGet(urls[0]);
httpGet.setHeader("Content-Type","application/rdf+xml");
httpGet.setHeader("Accept", "application/rdf+xml");
HttpResponse httpResponsepost = clientPost.getHttpClient().execute(httpGet);
System.out.println(httpResponsepost.getStatusLine().getStatusCode());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse getResponse = CreateDngArtifact.sendGetForSecureDocument(serverUrlPost, httpGet, usernamePost, passwordPost, httpClient);
System.out.println("etag code : "+getResponse.getStatusLine().getStatusCode());
System.out.println("etag code : "+getResponse.getStatusLine());
String etag= CreateDngArtifact .getETag(getResponse);
System.out.println("etag ::: "+etag);
EntityUtils.consumeQuietly(getResponse.getEntity());
List<String> list = new ArrayList<String>();
String uri = urls[0];
StringEntity stringEntity = new StringEntity(xml,"UTF-8");
HttpPut httpPut= new HttpPut(urls[0]);
httpPut.setEntity(stringEntity);
httpPut.addHeader("Content-Type","application/rdf+xml");
httpPut.addHeader("Accept", "application/rdf+xml");
httpPut.setHeader("If-Match", etag);
HttpResponse responsePut = httpClient.execute(httpPut);
System.out.println("x : "+ responsePut.getStatusLine().getStatusCode());
}
public static String getETag(HttpResponse response){
Header[] headers = response.getAllHeaders();
for (int i = 0; i < headers.length; i++){
if(headers[i].getName().equalsIgnoreCase("etag")){
return headers[i].getValue();
}
}
return null;
}
public static HttpResponse sendGetForSecureDocument(String serverURI, HttpGet request, String login, String password, HttpClient httpClient)
throws IOException, InvalidCredentialsException {
HttpGet documentGet = new HttpGet(serverURI);
documentGet.addHeader("Accept", "application/rdf+xml");
//
// Step (1): Request the protected resource
//
HttpResponse response = httpClient.execute(request);
HttpClient tempClient = httpClient;
if (response.getStatusLine().getStatusCode() == 200) {
Header header =
response.getFirstHeader("x-com-ibm-team-repository-web-auth-msg");
if ((header!=null) && ("authrequired".equals(header.getValue()))) {
response.getEntity().consumeContent();
// The server requires an authentication: Create the login form
HttpPost formPost = new HttpPost(serverURI+"/j_security_check");
List nvps = new ArrayList();
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
//
HttpResponse formResponse = httpClient.execute(formPost);
header = formResponse.getFirstHeader("X-com-ibm-team-repository-web-auth-msg");
if ((header!=null) && ("authfailed".equals(header.getValue()))) {
// The login failed
throw new InvalidCredentialsException("Authentication failed");
} else {
// The login succeed
formResponse.getEntity().consumeContent();
//
// Step (3): Request again the protected resource
//
HttpGet documentGet2 = new HttpGet(serverURI);
documentGet2.addHeader("Accept", "application/rdf+xml");
HttpResponse res = httpClient.execute(request);
return res;
}
}
}
return response;
}
}