It's all about the answers!

Ask a question

How to connect to RTC Rest API from external web application ?


Sujith Gannamaneni (1637) | asked Aug 20 '14, 2:04 p.m.
edited Aug 27 '14, 11:43 a.m. by Ralph Earle (25739)
Hello,

         I'm developing a web application using Spring MVC and Hibernate. I want to access the RTC workitems and perform a search operation which can display the result on the web page. For this I m planning to use the Java API. So, how can I authenticate and get access to the RTC and search the WorkItems. Are there any examples which can help me out to do so. I m an entry level developer, so this question might be nooby.

Thanks
Sujith

One answer



permanent link
Kevin Eifinger (1211510) | answered Oct 01 '14, 11:40 a.m.
edited Oct 01 '14, 11:43 a.m.
 Hi Sujith,

for logging into RTC take a look at Snippet 1 that comes along the plain-java-api.

For the query part here is the code I used to find a query, execute it and retrieve the results as a csv file:

  

private IQueryDescriptor findQuery(final String queryName) throws Exception {

IWorkItemClient workItemClient = (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);

IQueryClient queryClient = workItemClient.getQueryClient();


List<IProjectAreaHandle> sharingTargets = new ArrayList<IProjectAreaHandle>();

sharingTargets.add(projectArea);


List<IQueryDescriptor> personalQueries = queryClient.findPersonalQueries(projectArea,

(IContributorHandle) teamRepository.loggedInContributor().getItemHandle(), QueryTypes.WORK_ITEM_QUERY,

QueryDescriptor.FULL_PROFILE, monitor);


for (IQueryDescriptor query : personalQueries) {

if (queryName.equalsIgnoreCase(query.getName())) {

logger.info("Query '" + queryName + "' has been found: " + query);

return query;

}

}


return null;

}


private Map<String, List<String>> createParameters(final IQueryDescriptor query) throws TeamRepositoryException {

ArrayList<String> columns = new ArrayList<String>();

Statement statement = (Statement) query.getExpression();

for (String column : statement.getSelectClause().getColumnIdentifiers()) {

columns.add(column);

}


HashMap<String, List<String>> parameters = new HashMap<String, List<String>>();

parameters.put(ExportLocations.URL_PARAM_COLUMNS, columns);

parameters.put(ExportLocations.URL_PARAM_LINK_STYLE,

Collections.singletonList(ExportLocations.URL_PARAM_LINK_STYLE_ABSOLUTE));

parameters.put(ExportLocations.URL_PARAM_FIELD_DELIMITER, Collections.singletonList("\t"));

parameters.put(ExportLocations.URL_PARAM_ENCODING, Collections.singletonList("UTF-16LE"));

parameters.put(ExportLocations.URL_PARAM_QUOTE_STRINGS, Collections.singletonList(Boolean.toString(true)));

parameters.put(ExportLocations.URL_PARAM_HEADERS_INCLUDED, Collections.singletonList(Boolean.toString(true)));


return parameters;

}


private void fetchContents(final Location location, final String exportFileName) throws Exception {

File file = new File(exportFileName);

logger.debug("Work Items are exported to: " + file.getAbsolutePath());


OutputStream outputStream = new FileOutputStream(file);

ITeamRestServiceClient restClient = ((TeamRepository) teamRepository).getItemRestService();

IRestClientConnection connection = restClient.getConnection(location);

Response response = connection.doGet();

InputStream inputStream = response.getResponseStream();

ClientUtils.copyStream(inputStream, outputStream, monitor);

}

Your answer


Register or to post your answer.