2012년 1월 4일 수요일

RTC Project Area 목록을 구하기 위한 OSLC 코드

출처 : https://jazz.net/forums/viewtopic.php?t=12745


Http http = new Http();
HttpRequest req = new HttpRequest();

// get the root document link to the catalog

req.setEndpoint(settings[0].ServerAddress__c+settings[0].BaseDocument__c);    
req.setMethod('GET');
res = http.send(req);
if(res.getStatusCode()==200)
{
// get the providers element
Dom.XMLNode providers = res.getBodyDocument().getRootElement().getChildElement('cmServiceProviders', 'http://open-services.net/xmlns/cm/1.0/');
// and the catalog URL
String attr=providers.getAttributeValue('resource','http://www.w3.org/1999/02/22-rdf-syntax-ns#');
req.setEndpoint(attr);
req.setMethod('GET');
// set the userid/pw to operate under.
Blob headerValue = Blob.valueOf(settings[0].UserName__c + ':' + settings[0].UserPW__c);
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
// send the request
res = http.send(req);
 
// should come back with an authorization request redirect (302)
if(res.getStatusCode()==302)
{                      
// get the sessionid string from the returned cookies
String[] cookies = res.getHeader('Set-Cookie').split(';');
String sessionid='';
for(Integer i=0;i<cookies.size();i++)
{
if (cookies[i].startswith('JSESSIONID'))
{    
sessionid=cookies[i];                
break;
}
}

// set the redirect endpoint
req.setEndpoint(res.getHeader('Location'));
// and the session id
req.setHeader('Cookie',sessionid);
req.setMethod('GET');
res = http.send(req);
//
if(res.getStatusCode()==200)
{
//  we will have to deal with form logon later
String pw = settings[0].SecurityString__c;
pw=pw.replaceFirst('uname',settings[0].Username__c);
pw=pw.replaceFirst('upw',settings[0].Userpw__c);
req.setEndpoint(settings[0].ServerAddress__c + pw);
//req.setHeader('Referer',res.getHeader('Location'));
req.setMethod('GET');
req.setHeader('ContentType','application/x-www-form-urlencoded');
req.setHeader('Cookie',sessionid);
res = http.send(req);                    
}
// spin thru the remaining redirects..
while(res.getStatusCode()==302)
{                    
// redirect after login
req.setEndpoint(res.getHeader('Location'));
req.setHeader('Cookie',sessionid);
req.setMethod('GET');
res = http.send(req);
}
// we should have the project list document now
System.debug(res.getHeaderKeys());
System.debug(res.getBody());
// find the first project 'entry' node
Dom.XMLNode project = res.getBodyDocument().getRootElement().getChildElement('entry','http://open-services.net/xmlns/discovery/1.0/');
do
{
Dom.XMLnode provider = project.getChildElement('ServiceProvider','http://open-services.net/xmlns/discovery/1.0/');
String projectName =provider.getChildElement('title','http://purl.org/dc/terms/').getText();
String projectServicesURL = provider.getChildElement('services','http://open-services.net/xmlns/discovery/1.0/').getAttributeValue('resource','http://www.w3.org/1999/02/22-rdf-syntax-ns#');                                                          
req.setEndpoint(projectServicesURL);
req.setHeader('Cookie',sessionid);
req.setMethod('GET');
res = http.send(req);
// get the services list document
if(res.getStatusCode()==200)
{
DOM.XMLNode serviceslist = res.getBodydocument().getRootElement();
//System.debug(serviceslist);
Dom.XMLNode changeRequests = serviceslist.getChildElement('changeRequests','http://open-services.net/xmlns/cm/1.0/');
//System.debug(changeRequests);
Dom.XMLNode workitemFactory = changeRequests.getChildElement('factory','http://open-services.net/xmlns/cm/1.0/');
String workitemCreateUrl;
if(workitemFactory.getAttributeValue('default','http://open-services.net/xmlns/cm/1.0/')=='true')
{
workitemCreateUrl = workitemFactory.getChildElement('url','http://open-services.net/xmlns/cm/1.0/').getText();
system.debug(workitemCreateUrl);
}
String workitemQueryURL = (changeRequests.getChildElement('simpleQuery','http://open-services.net/xmlns/cm/1.0/')).getChildElement('url','http://open-services.net/xmlns/cm/1.0/').getText();
//system.debug(workitemQueryUrl);
//System.debug(workitemfactory);
ExportValue e=new ExportValue(projectName, workitemCreateUrl, workitemQueryURL);
result.add(e);
}
// find the projects parent node
Dom.XMLNode parent = project.getparent();
// remove the project node from the document
parent.removeChild(project);
// find the next project entry
project = parent.getChildElement('entry','http://open-services.net/xmlns/discovery/1.0/');
} while (project!=null);
}
}

댓글 없음:

댓글 쓰기