use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.
the class ProjectsController method syncProject.
/**
* Get a {@link Project} from a remote api and mark it to be synchronized in
* this IRIDA installation
*
* @param url the URL of the remote project
* @param syncFrequency How often to sync the project
* @param model Model for the view
* @return Redirect to the new project. If an oauth exception occurs it will
* be forwarded back to the creation page.
*/
@RequestMapping(value = "/projects/synchronize", method = RequestMethod.POST)
public String syncProject(@RequestParam String url, @RequestParam ProjectSyncFrequency syncFrequency, Model model) {
try {
Project read = projectRemoteService.read(url);
read.setId(null);
read.getRemoteStatus().setSyncStatus(SyncStatus.MARKED);
read.setSyncFrequency(syncFrequency);
read = projectService.create(read);
return "redirect:/projects/" + read.getId() + "/metadata";
} catch (IridaOAuthException ex) {
Map<String, String> errors = new HashMap<>();
errors.put("oauthError", ex.getMessage());
model.addAttribute("errors", errors);
return getSynchronizeProjectPage(model);
} catch (EntityNotFoundException ex) {
Map<String, String> errors = new HashMap<>();
errors.put("urlError", ex.getMessage());
model.addAttribute("errors", errors);
return getSynchronizeProjectPage(model);
}
}
use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.
the class RemoteAPIControllerTest method testCheckApiStatusInactive.
@Test
public void testCheckApiStatusInactive() {
Long apiId = 1L;
RemoteAPI client = new RemoteAPI("name", "http://uri", "a description", "id", "secret");
when(remoteAPIService.read(apiId)).thenReturn(client);
when(projectRemoteService.getServiceStatus(client)).thenThrow(new IridaOAuthException("invalid token", client));
String checkApiStatus = remoteAPIController.checkApiStatus(apiId);
assertEquals(RemoteAPIController.INVALID_OAUTH_TOKEN, checkApiStatus);
verify(remoteAPIService).read(apiId);
verify(projectRemoteService).getServiceStatus(client);
}
use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.
the class ProjectSynchronizationService method findMarkedProjectsToSync.
/**
* Find projects which should be synchronized and launch a synchornization
* task.
*/
public synchronized void findMarkedProjectsToSync() {
// mark any projects which should be synched first
findProjectsToMark();
List<Project> markedProjects = projectService.getProjectsWithRemoteSyncStatus(SyncStatus.MARKED);
logger.trace("Checking for projects to sync");
for (Project project : markedProjects) {
/*
* Set the correct authorization for the user who's syncing the
* project
*/
User readBy = project.getRemoteStatus().getReadBy();
setAuthentication(readBy);
logger.debug("Syncing project at " + project.getRemoteStatus().getURL());
try {
RemoteAPI api = project.getRemoteStatus().getApi();
tokenService.updateTokenFromRefreshToken(api);
syncProject(project);
} catch (IridaOAuthException e) {
logger.trace("Can't sync project " + project.getRemoteStatus().getURL() + " due to oauth error:", e);
project.getRemoteStatus().setSyncStatus(SyncStatus.UNAUTHORIZED);
projectService.update(project);
} catch (Exception e) {
logger.debug("An error occurred while synchronizing project " + project.getRemoteStatus().getURL(), e);
project.getRemoteStatus().setSyncStatus(SyncStatus.ERROR);
projectService.update(project);
} finally {
// clear the context holder when you're done
SecurityContextHolder.clearContext();
logger.debug("Done project " + project.getRemoteStatus().getURL());
}
}
}
use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.
the class OAuthTokenRestTemplate method createRequest.
/**
* Add an OAuth token from the tokenRepository to the request
*/
@Override
protected ClientHttpRequest createRequest(URI uri, HttpMethod method) throws IOException {
RemoteAPIToken token;
try {
token = tokenService.getToken(remoteAPI);
} catch (EntityNotFoundException ex) {
logger.debug("No token found for service " + remoteAPI);
throw new IridaOAuthException("No token fround for service", remoteAPI, ex);
}
if (token.isExpired()) {
logger.debug("Token for service is expired " + remoteAPI);
throw new IridaOAuthException("Token is expired for service", remoteAPI);
}
ClientHttpRequest createRequest = super.createRequest(uri, method);
createRequest.getHeaders().add("Authorization", "Bearer " + token.getTokenString());
return createRequest;
}
use of ca.corefacility.bioinformatics.irida.exceptions.IridaOAuthException in project irida by phac-nml.
the class IridaOAuthErrorHandler method handleError.
/**
* Overriding this method to throw a {@link IridaOAuthException} in case of
* an HTTP UNAUTHORIZED response.
*/
@Override
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = response.getStatusCode();
logger.trace("Checking error type " + statusCode.toString());
switch(statusCode) {
case UNAUTHORIZED:
logger.trace("Throwing new IridaOAuthException for this error");
throw new IridaOAuthException("User is unauthorized for this service", remoteAPI);
default:
logger.trace("Passing error to superclass");
super.handleError(response);
}
}
Aggregations