package com.org.rb.dm.linking;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.InvalidCredentialsException;
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.params.HttpClientParams;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.wink.client.ClientResponse;
import org.eclipse.lyo.client.exception.ResourceNotFoundException;
import org.eclipse.lyo.client.oslc.OAuthRedirectException;
import org.eclipse.lyo.client.oslc.OSLCConstants;
import org.eclipse.lyo.client.oslc.OslcClient;
import org.eclipse.lyo.client.oslc.OslcOAuthClient;
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.RmConstants;
import org.eclipse.lyo.client.oslc.resources.RmUtil;
import org.eclipse.lyo.oslc4j.core.model.CreationFactory;
import org.eclipse.lyo.oslc4j.core.model.Link;
import org.eclipse.lyo.oslc4j.core.model.OslcMediaType;
import org.eclipse.lyo.oslc4j.core.model.ResourceShape;
import org.eclipse.lyo.oslc4j.core.model.Service;
import org.eclipse.lyo.oslc4j.core.model.ServiceProvider;
import org.eclipse.lyo.oslc4j.provider.jena.AbstractOslcRdfXmlProvider;
import org.w3c.dom.Element;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Selector;
import com.hp.hpl.jena.rdf.model.SimpleSelector;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;
import net.oauth.OAuthException;
public class DOORStoDM_New {
private static final Logger logger = Logger.getLogger(DOORStoDM_New.class.getName());
static String linkUrlToAdd;
static String linkName;
static String identifier;
static String queryCapability;
static int count = 0;
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
logger.info("Execution started at " + new Timestamp(System.currentTimeMillis()));
System.out.println("Execution started at " + new Timestamp(System.currentTimeMillis()));
File file = new File("configP.properties");
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
File file2 = new File("credentials.properties");
FileInputStream fileInputCredentials = new FileInputStream(file2);
Properties propertiesCredentials = new Properties();
propertiesCredentials.load(fileInputCredentials);
String webContextUrl = properties.getProperty("webContextUrl");
String user = propertiesCredentials.getProperty("user");
String passwd = propertiesCredentials.getProperty("passwd");
String projectArea = properties.getProperty("projectArea");
String consumerSecret = properties.getProperty("consumerSecret");
String consumerKey = properties.getProperty("consumerKey");
linkName = properties.getProperty("linkName");
List<String> listUrl = new ArrayList<String>();
List<String> listIdentifier = new ArrayList<String>();
List<String> urlList = new ArrayList<String>();
List identifierList = new ArrayList();
String[] arr;
Map mapLinkUrl = new HashMap<>();
JazzRootServicesHelper helper = new JazzRootServicesHelper(webContextUrl + "/public", OSLCConstants.OSLC_RM_V2);
OslcOAuthClient client = helper.initOAuthClient(consumerKey, consumerSecret);
// System.out.println("client" + client);
if (client != null) {
try {
client.getResource(webContextUrl, OslcMediaType.APPLICATION_RDF_XML);
System.out.println(1);
} catch (OAuthRedirectException oauthE) {
validateTokens(client, oauthE.getRedirectURL() + "?oauth_token=" + oauthE.getAccessor().requestToken,
user, passwd, webContextUrl + "/j_acegi_security_check");
ClientResponse response = client.getResource(webContextUrl, OSLCConstants.CT_RDF);
response.getEntity(InputStream.class).close();
}
String catalogUrl = helper.getCatalogUrl();
// System.out.println("catalogUrl : " + catalogUrl);
// System.out.println("projectArea : " + projectArea);
// String serviceProviderUrl =
// client.lookupServiceProviderUrl(catalogUrl, "Services for
// " + projectArea);
String serviceProviderUrl = null;
try {
// System.out.println("calling serviceProviderUrl " + serviceProviderUrl);
serviceProviderUrl = lookupServiceProviderUrl(catalogUrl, "Services for " + projectArea, client);
// client.lookupServiceProviderUrl(catalogUrl, "Services for "
// +projectArea);
System.out.println("callrd serviceProviderUrl" + serviceProviderUrl);
} catch (Exception ex) {
System.out.println("serviceProviderUrl Exception" + ex);
}
// System.out.println("serviceProviderUrl" + serviceProviderUrl);
queryCapability = client.lookupQueryCapability(serviceProviderUrl, OSLCConstants.OSLC_RM_V2,
OSLCConstants.RM_REQUIREMENT_TYPE);
// System.out.println("queryCapability" + queryCapability);
String requirementFactory = client.lookupCreationFactory(serviceProviderUrl, OSLCConstants.OSLC_RM_V2,
OSLCConstants.RM_REQUIREMENT_TYPE);
/// System.out.println("requirementFactory" + requirementFactory);
/
* reading data from text file
/
/
* try { FileInputStream fstream = new
* FileInputStream("U:/DoorsDmLink/link.txt");
* System.out.println("------------------------------------");
* System.out.println("Execution started at : " + new
* Timestamp(System.currentTimeMillis())); DataInputStream in = new
* DataInputStream(fstream); BufferedReader br = new
* BufferedReader(new InputStreamReader(in)); String strLine;
* while ((strLine = br.readLine()) != null) { arr = strLine.split(
* " ");
* listUrl.add(arr[0]); listIdentifier.add(arr[1]); }
* for (Object urls : listUrl) { System.out.println(urls.toString()
* + "\n"); }
* } catch (Exception ex) { System.out.println(" Exception : " +
* ex); }
*/
/
* reading data from excel file
/
try {
String excelFilePath = "U:/DoorsDmLink/excelReadP.xlsx";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
urlList.add(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
// double d = cell.getNumericCellValue();
identifierList.add((int) cell.getNumericCellValue());
break;
}
}
}
for (int i = 0; i < urlList.size(); i++) {
// System.out.println(identifierList.get(i) + " " + urlList.get(i));
}
} catch (Exception ex) {
System.out.println(ex);
}
}
for (int i = 0; i < urlList.size(); i++) {
try {
OslcQueryParameters queryParams = new OslcQueryParameters();
queryParams.setPrefix("dcterms=<http://purl.org/dc/terms/>");
queryParams.setWhere("dcterms:identifier=" + identifierList.get(i));
OslcQuery query = new OslcQuery(client, queryCapability, 10, queryParams);
OslcQueryResult result = query.submit();
String requirementURL = null;
// Get the URL of returned requirement
if (result != null) {
String[] returnedURLS = result.getMembersUrls();
if ((returnedURLS != null) && (returnedURLS.length > 0)) {
requirementURL = returnedURLS[0];
}
}
if (requirementURL != null) {
// Get the requirement
ClientResponse getResponse = client.getResource(requirementURL, OslcMediaType.APPLICATION_RDF_XML);
System.setProperty(AbstractOslcRdfXmlProvider.OSLC4J_STRICT_DATATYPES, "false");
// FLushig the console to avoid runtime warning
// messagesvahiu
// final PrintStream err = new PrintStream(System.err);
// System.setErr(new PrintStream("/dev/null"));
Requirement requirement = getResponse.getEntity(Requirement.class);
// System.setErr(err);
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
Date date = new Date();
// System.out.println(dateFormat.format(date));
DateFormat dateFormat2 = new SimpleDateFormat("HH:mm:ss");
Date date2 = new Date();
// requirement.setTitle("Title " + new Timestamp(System.currentTimeMillis()));
String primaryText = requirement.getTitle() + " " + new Timestamp(System.currentTimeMillis());
//requirement.getTitle() + " " + new Timestamp(System.currentTimeMillis());
//"DOORS to DM Linking Component Test on " + dateFormat.format(date) + " at " + dateFormat2.format(date2);
Element obj = RmUtil.convertStringToHTML(primaryText);
// requirement.setTitle("Title");
// requirement.setShortTitle("short title ");
// requirement.addSubject("subject");
requirement.getExtendedProperties().put(RmConstants.PROPERTY_PRIMARY_TEXT, obj);
// requirement.addImplementedBy(new Link(new
// "ImplementedBy - DM Link"));
// not wrking are SpecifiedBy, derived by ElaboratedBy
requirement.addSatisfies(new Link(new URI(urlList.get(i)), linkName));
//addImplementedBy(new Link(new URI(urlList.get(i)), linkName));
String etag = getResponse.getHeaders().getFirst(OSLCConstants.ETAG);
getResponse.consumeContent();
ClientResponse updateResponse = client.updateResource(requirementURL, requirement,
OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_RDF_XML, etag);
// ClientResponse updateResponse =
// client.updateResource(requirementURL,
// w.toString(), OslcMediaType.APPLICATION_RDF_XML,
// OslcMediaType.APPLICATION_RDF_XML, etag);
System.out.println(updateResponse.getStatusCode());
updateResponse.consumeContent();
}
} catch (Exception e) {
System.out.println(e);
// logger.log(Level.FATAL, e.getMessage(), e);
}
}
System.out.println("Execution completed at : " + new Timestamp(System.currentTimeMillis()));
logger.info("Execution completed at : " + new Timestamp(System.currentTimeMillis()));
logger.info("------------------------------------------------------------------------------");
logger.info(" ");
}
@SuppressWarnings("unchecked")
private static void processPagedQueryResults(OslcQueryResult result, OslcClient client, boolean asJavaObjects) {
int page = 1;
// For now, just show first 5 pages
do {
System.out.println("\nPage " + page + ":\n");
result = processCurrentPage(result, client, asJavaObjects);
if (result.hasNext() && page < 5) {
result = result.next();
page++;
} else {
break;
}
} while (true);
}
@SuppressWarnings("unchecked")
private static OslcQueryResult processCurrentPage(OslcQueryResult result, OslcClient client,
boolean asJavaObjects) {
for (String resultsUrl : result.getMembersUrls()) {
System.out.println(resultsUrl);
ClientResponse response = null;
try {
// Get a single artifact by its URL
response = client.getResource(resultsUrl, OSLCConstants.CT_RDF);
if (response != null) {
// De-serialize it as a Java object
if (asJavaObjects) {
// Requirement req =
// response.getEntity(Requirement.class);
// printRequirementInfo(req); //print a few attributes
} else {
processRawResponse(response);
}
}
} catch (Exception e) {
System.out.println("Unable to process artfiact");
// logger.log(Level.FATAL, "Unable to process artfiact at url: "
// + resultsUrl, e);
}
}
return result;
}
@SuppressWarnings("unchecked")
private static void processRawResponse(ClientResponse response) throws IOException {
InputStream is = response.getEntity(InputStream.class);
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
System.out.println();
response.consumeContent();
}
/
* Print out the HTTPResponse headers
*/
@SuppressWarnings("unchecked")
public static void printResponseHeaders(HttpResponse response) {
Header[] headers = response.getAllHeaders();
for (int i = 0; i < headers.length; i++) {
System.out.println("\t- " + headers[i].getName() + ": " + headers[i].getValue());
}
}
@SuppressWarnings("unchecked")
public static Map<String, String> getQueryMap(String query) {
Map<String, String> map = new HashMap<String, String>();
String[] params = query.split("&"); //$NON-NLS-1$
for (String param : params) {
String name = param.split("=")[0]; //$NON-NLS-1$
String value = param.split("=")[1]; //$NON-NLS-1$
map.put(name, value);
}
return map;
}
@SuppressWarnings("unchecked")
private static void validateTokens(OslcOAuthClient client, String redirect, String user, String password,
String authURL) throws Exception {
HttpGet request2 = new HttpGet(redirect);
HttpClientParams.setRedirecting(request2.getParams(), false);
HttpResponse response = client.getHttpClient().execute(request2);
EntityUtils.consume(response.getEntity());
// Get the location
Header location = response.getFirstHeader("Location");
HttpGet request3 = new HttpGet(location.getValue());
HttpClientParams.setRedirecting(request3.getParams(), false);
response = client.getHttpClient().execute(request3);
EntityUtils.consume(response.getEntity());
// POST to login form
// The server requires an authentication: Create the login form
// Following line should be like :
// "https://server:port/dwa/j_acegi_security_check"
HttpPost formPost = new HttpPost(authURL);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("j_username", user));
nvps.add(new BasicNameValuePair("j_password", password));
formPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse formResponse = client.getHttpClient().execute(formPost);
EntityUtils.consume(formResponse.getEntity());
location = formResponse.getFirstHeader("Location");
// Third GET
HttpGet request4 = new HttpGet(location.getValue());
HttpClientParams.setRedirecting(request4.getParams(), false);
response = client.getHttpClient().execute(request4);
EntityUtils.consume(response.getEntity());
Map<String, String> oAuthMap = getQueryMap(location.getValue());
String oauthToken = oAuthMap.get("oauth_token");
String oauthverifier = oAuthMap.get("oauth_verifier");
// The server requires an authentication: Create the login form
HttpPost formPost2 = new HttpPost(authURL);
formPost2.getParams().setParameter("oauth_token", oauthToken);
formPost2.getParams().setParameter("oauth_verifier", oauthverifier);
formPost2.getParams().setParameter("authorize", "true");
formPost2.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
formResponse = client.getHttpClient().execute(formPost2);
EntityUtils.consume(formResponse.getEntity());
Header header = formResponse.getFirstHeader("Content-Length");
if ((header != null) && (!("0".equals(header.getValue())))) {
// The login failed
throw new InvalidCredentialsException("Authentication failed");
} else {
// The login succeed
// Step (3): Request again the protected resource
EntityUtils.consume(formResponse.getEntity());
}
}
/
* Lookup the URL of a specific OSLC Service Provider in an OSLC Catalog
* using the service provider's title
* @param catalogUrl
* @param serviceProviderTitle
* @return
* @throws IOException
* @throws OAuthException
* @throws URISyntaxException
* @throws ResourceNotFoundException
/
@SuppressWarnings("unchecked")
public static String lookupServiceProviderUrl(final String catalogUrl, final String serviceProviderTitle,
final OslcOAuthClient client)
throws IOException, OAuthException, URISyntaxException, ResourceNotFoundException {
String retval = null;
ClientResponse response = client.getResource(catalogUrl, OSLCConstants.CT_RDF);
Model rdfModel = ModelFactory.createDefaultModel();
rdfModel.read(response.getEntity(InputStream.class), catalogUrl);
// Step 1 Check if it is the service provider we are looking for by
// comparing the name
// System.out.println("List creation started at " + new Timestamp(System.currentTimeMillis()) );
ResIterator listResources = rdfModel.listResourcesWithProperty(RDF.type,
rdfModel.createResource("http://open-services.net/ns/core#ServiceProvider"));
// System.out.println("List creation ended at " + new Timestamp(System.currentTimeMillis()) );
Property titleProp = rdfModel.createProperty(OSLCConstants.DC, "title");
// check each serviceProvider's title and match it to the one passed in+
while (listResources.hasNext()) {
// System.out.println("whle loop strat " + new Timestamp(System.currentTimeMillis()) );
//
Resource resource = listResources.next();
Statement titlestatement = resource.getProperty(titleProp);
if (titlestatement == null)
continue;
String mytitle = titlestatement.getLiteral().getString();
// System.out.println("mytitle " + mytitle);
count++;
if ((mytitle != null) && (mytitle.equalsIgnoreCase(serviceProviderTitle))) {
// System.out.println("Project Found");
retval = catalogUrl;
}
// System.out.println("while loop ends " + new Timestamp(System.currentTimeMillis()) );
}
// Step 2 Check if there are Service providers properties to recursively
// look in them
if (retval == null) {
Property spPredicate = rdfModel.createProperty(OSLCConstants.OSLC_V2, "serviceProvider");
Selector select = new SimpleSelector(null, spPredicate, (RDFNode) null);
StmtIterator listStatements = rdfModel.listStatements(select);
// check each serviceProvider's title and match it to the one passed
// in
while (listStatements.hasNext()) {
Statement thisSP = listStatements.nextStatement();
com.hp.hpl.jena.rdf.model.Resource spRes = thisSP.getResource();
if (spRes.isResource()) {
// Recursively look for the project Name
String newURL = spRes.getURI();
try {
return lookupServiceProviderUrl(newURL, serviceProviderTitle, client);
} catch (ResourceNotFoundException nf) {
}
}
}
}
// Step 3 Check if there are ServiceProvider catalog and recursively
// look in them
if (retval == null) {
Property spcPredicate = rdfModel.createProperty(OSLCConstants.OSLC_V2, "serviceProviderCatalog");
Selector select = new SimpleSelector(null, spcPredicate, (RDFNode) null);
StmtIterator listStatements = rdfModel.listStatements(select);
// check each serviceProvider's title and match it to the one passed
// in
while (listStatements.hasNext()) {
Statement thisSP = listStatements.nextStatement();
com.hp.hpl.jena.rdf.model.Resource spRes = thisSP.getResource();
if (spRes.isResource()) {
// Recursively look for the project Name
String newURL = spRes.getURI();
try {
return lookupServiceProviderUrl(newURL, serviceProviderTitle, client);
} catch (ResourceNotFoundException nf) {
}
}
}
}
if (retval == null) {
throw new ResourceNotFoundException(catalogUrl, serviceProviderTitle);
}
System.out.println("retval " + retval);
System.out.println("Total number of moudles : "+count);
return retval;
}
/
* Find the OSLC Instance Shape URL for a given OSLC resource type
* @param serviceProviderUrl
* @param oslcDomain
* @param oslcResourceType
- the resource type of the desired query capability. This may
differ from the OSLC artifact type.
* @return URL of requested Creation Factory or null if not found.
* @throws IOException
* @throws OAuthException
* @throws URISyntaxException
* @throws ResourceNotFoundException
/
@SuppressWarnings("unchecked")
public static ResourceShape lookupRequirementsInstanceShapesOLD(final String serviceProviderUrl,
final String oslcDomain, final String oslcResourceType, OslcOAuthClient client,
String requiredInstanceShape)
throws IOException, OAuthException, URISyntaxException, ResourceNotFoundException {
ClientResponse response = client.getResource(serviceProviderUrl, OSLCConstants.CT_RDF);
ServiceProvider serviceProvider = response.getEntity(ServiceProvider.class);
if (serviceProvider != null) {
for (Service service : serviceProvider.getServices()) {
URI domain = service.getDomain();
if (domain != null && domain.toString().equals(oslcDomain)) {
CreationFactory[] creationFactories = service.getCreationFactories();
if (creationFactories != null && creationFactories.length > 0) {
for (CreationFactory creationFactory : creationFactories) {
for (URI resourceType : creationFactory.getResourceTypes()) {
// return as soon as domain + resource type are
// matched
if (resourceType.toString() != null
&& resourceType.toString().equals(oslcResourceType)) {
URI[] instanceShapes = creationFactory.getResourceShapes();
if (instanceShapes != null) {
for (URI typeURI : instanceShapes) {
response = client.getResource(typeURI.toString(), OSLCConstants.CT_RDF);
ResourceShape resourceShape = response.getEntity(ResourceShape.class);
String typeTitle = resourceShape.getTitle();
if ((typeTitle != null)
&& (typeTitle.equalsIgnoreCase(requiredInstanceShape))) {
return resourceShape;
}
}
}
}
}
}
}
}
}
}
throw new ResourceNotFoundException(serviceProviderUrl, "InstanceShapes");
}
/
* Remove XML Escape indicators
* @param content
* @return String
*/
@SuppressWarnings("unchecked")
public static String removeXMLEscape(String content) {
content = content.replaceAll("<", "<"); //$NON-NLS-1$ //$NON-NLS-2$
content = content.replaceAll(">", ">"); //$NON-NLS-1$ //$NON-NLS-2$
content = content.replaceAll(""", "\""); //$NON-NLS-1$ //$NON-NLS-2$
content = content.replaceAll("'", "\'"); //$NON-NLS-1$ //$NON-NLS-2$
content = content.replaceAll("&", "&"); //$NON-NLS-1$ //$NON-NLS-2$
content = content.trim();
return content;
}
}