diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/AddNewItemController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/AddNewItemController.java index 4804dc24d..f82101891 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/AddNewItemController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/AddNewItemController.java @@ -145,39 +145,33 @@ public ResponseEntity createNewItem(Principal principal, try { citation = citationManager.createCitation(user, zoteroGroupId, collectionIds, citation); - } catch (ZoteroItemCreationFailedException | ZoteroConnectionException | ZoteroHttpStatusException e) { - logger.error("Zotero Item creation failed. ", e); - return new ResponseEntity<>("Error: Zetero Item creation failed. " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); - } catch (GroupDoesNotExistException e) { - logger.error("Group " + zoteroGroupId +" does not exists. ", e); - return new ResponseEntity<>("Error: Group " + zoteroGroupId +" does not exists. ", HttpStatus.BAD_REQUEST); - } - - if (itemWithGiles.getFiles() != null && itemWithGiles.getFiles().length > 0) { - ObjectMapper mapper = new ObjectMapper(); - ObjectNode root = mapper.createObjectNode(); - for (MultipartFile file: itemWithGiles.getFiles()) { - try { + + if (itemWithGiles.getFiles() != null && itemWithGiles.getFiles().length > 0) { + ObjectMapper mapper = new ObjectMapper(); + ObjectNode root = mapper.createObjectNode(); + for (MultipartFile file: itemWithGiles.getFiles()) { IGilesUpload job = jobManager.createGilesJob(user, file, file.getBytes(), zoteroGroupId, citation.getKey()); gilesUtil.createJobObjectNode(root, job); - } catch (GroupDoesNotExistException e) { - logger.error("Could not create job because group does not exist.", e); - return new ResponseEntity<>("Error: Could not create job because group does not exist.", HttpStatus.BAD_REQUEST); - } catch (CannotFindCitationException | CitationIsOutdatedException e) { - logger.error("Error with newly created citation.", e); - return new ResponseEntity<>("Error: Error with newly created citation." + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); - } catch (IOException e) { - logger.error("Could not read file from the request. ", e); - return new ResponseEntity<>("Error: Could not read file from the request.", HttpStatus.BAD_REQUEST); - } catch (ZoteroHttpStatusException | ZoteroConnectionException | ZoteroItemCreationFailedException e) { - logger.error("Zotero exception occured ", e); - return new ResponseEntity<>("Error: Zotero Exception occured: "+ e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); - } catch (HttpClientErrorException.Unauthorized e) { - logger.error("Unauthorized to upload files to Giles ", e); - return new ResponseEntity<>("Error: Unauthorized to upload files to Giles.", HttpStatus.UNAUTHORIZED); } } + } catch (GroupDoesNotExistException e) { + logger.error("Could not create job because group does not exist.", e); + return new ResponseEntity<>("Error: Could not create job because group does not exist.", HttpStatus.BAD_REQUEST); + } catch (CannotFindCitationException | CitationIsOutdatedException e) { + logger.error("Error with newly created citation.", e); + return new ResponseEntity<>("Error: Error with newly created citation." + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } catch (IOException e) { + logger.error("Could not read file from the request. ", e); + return new ResponseEntity<>("Error: Could not read file from the request.", HttpStatus.BAD_REQUEST); + } catch (ZoteroHttpStatusException | ZoteroItemCreationFailedException e) { + logger.error("Zotero exception occured ", e); + return new ResponseEntity<>("Error: Zotero Exception occured: "+ e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } catch (ZoteroConnectionException e) { + return new ResponseEntity<>("Error: Zetero Item creation failed. Please check Zoetro Key Permissions. " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } catch (HttpClientErrorException.Unauthorized e) { + logger.error("Unauthorized to upload files to Giles ", e); + return new ResponseEntity<>("Error: Unauthorized to upload files to Giles.", HttpStatus.UNAUTHORIZED); } return new ResponseEntity<>(citation, HttpStatus.OK); } diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/ItemTextController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/ItemTextController.java index 6cebd355d..6b2e42c58 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/ItemTextController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/api/v1/user/ItemTextController.java @@ -85,9 +85,11 @@ public ResponseEntity addItemFile(@PathVariable("groupId") String groupI try { job = jobManager.createGilesJob(user, files[i], files[i].getBytes(), groupId, citation.getKey()); - } catch (ZoteroHttpStatusException | ZoteroConnectionException | ZoteroItemCreationFailedException e) { + } catch (ZoteroHttpStatusException | ZoteroItemCreationFailedException e) { logger.error("Zotero could not process the file. ", e); return new ResponseEntity<>("Error: Zotero could not process the file: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } catch (ZoteroConnectionException e) { + return new ResponseEntity<>("Error: Could not add file to citation. Please check Zoetro Key Permissions. " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } catch (GroupDoesNotExistException e) { logger.error("Could not create job because group does not exist.", e); return new ResponseEntity<>("Error: Could not create job because group does not exist.", HttpStatus.NOT_FOUND); diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/config/core/ZoteroConfig.java b/citesphere/src/main/java/edu/asu/diging/citesphere/config/core/ZoteroConfig.java index 220bd496b..513d0f2db 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/config/core/ZoteroConfig.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/config/core/ZoteroConfig.java @@ -5,6 +5,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.social.zotero.connect.ZoteroConnectionFactory; +import edu.asu.diging.citesphere.core.zotero.custom.CustomZoteroConnectionFactory; @Configuration @PropertySource("classpath:/config.properties") @@ -18,7 +19,9 @@ public class ZoteroConfig { @Bean - public ZoteroConnectionFactory zoteroConnectionFactory() { - return new ZoteroConnectionFactory(zoteroKey, zoteroSecret); + public CustomZoteroConnectionFactory zoteroConnectionFactory( + @Value("${_zotero_client_key}") String zoteroKey, + @Value("${_zotero_client_secret}") String zoteroSecret) { + return new CustomZoteroConnectionFactory(zoteroKey, zoteroSecret); } } diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/core/zotero/custom/CustomOAuth1Operations.java b/citesphere/src/main/java/edu/asu/diging/citesphere/core/zotero/custom/CustomOAuth1Operations.java new file mode 100644 index 000000000..c17258627 --- /dev/null +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/core/zotero/custom/CustomOAuth1Operations.java @@ -0,0 +1,50 @@ +package edu.asu.diging.citesphere.core.zotero.custom; + +import org.springframework.social.oauth1.OAuth1Operations; +import org.springframework.social.oauth1.OAuth1Parameters; +import org.springframework.social.oauth1.OAuthToken; +import org.springframework.social.oauth1.AuthorizedRequestToken; +import org.springframework.util.MultiValueMap; + +public class CustomOAuth1Operations implements OAuth1Operations { + private final OAuth1Operations delegate; + + public CustomOAuth1Operations(OAuth1Operations delegate) { + this.delegate = delegate; + } + + @Override + public String buildAuthorizeUrl(String requestToken, OAuth1Parameters parameters) { + if (parameters == null) { + parameters = new OAuth1Parameters(); + } + parameters.set("write_access", "1"); + parameters.set("all_groups", "write"); + return delegate.buildAuthorizeUrl(requestToken, parameters); + } + + @Override + public String buildAuthenticateUrl(String requestToken, OAuth1Parameters parameters) { + if (parameters == null) { + parameters = new OAuth1Parameters(); + } + parameters.set("write_access", "1"); + parameters.set("all_groups", "write"); + return delegate.buildAuthenticateUrl(requestToken, parameters); + } + + @Override + public OAuthToken fetchRequestToken(String callbackUrl, MultiValueMap additionalParameters) { + return delegate.fetchRequestToken(callbackUrl, additionalParameters); + } + + @Override + public OAuthToken exchangeForAccessToken(AuthorizedRequestToken requestToken, MultiValueMap additionalParameters) { + return delegate.exchangeForAccessToken(requestToken, additionalParameters); + } + + @Override + public org.springframework.social.oauth1.OAuth1Version getVersion() { + return delegate.getVersion(); + } +} \ No newline at end of file diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/core/zotero/custom/CustomZoteroConnectionFactory.java b/citesphere/src/main/java/edu/asu/diging/citesphere/core/zotero/custom/CustomZoteroConnectionFactory.java new file mode 100644 index 000000000..fde86777c --- /dev/null +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/core/zotero/custom/CustomZoteroConnectionFactory.java @@ -0,0 +1,17 @@ +package edu.asu.diging.citesphere.core.zotero.custom; + +import org.springframework.social.zotero.connect.ZoteroConnectionFactory; +import org.springframework.social.oauth1.OAuth1Operations; + +public class CustomZoteroConnectionFactory extends ZoteroConnectionFactory { + + public CustomZoteroConnectionFactory(String consumerKey, String consumerSecret) { + super(consumerKey, consumerSecret); + } + + @Override + public OAuth1Operations getOAuthOperations() { + OAuth1Operations ops = super.getOAuthOperations(); + return new CustomOAuth1Operations(ops); + } +} \ No newline at end of file diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/AddItemController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/AddItemController.java index 3140e9987..4242572a1 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/AddItemController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/AddItemController.java @@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import edu.asu.diging.citesphere.core.exceptions.GroupDoesNotExistException; import edu.asu.diging.citesphere.core.exceptions.ZoteroHttpStatusException; @@ -88,8 +89,8 @@ public String show(Model model, Authentication authentication, @RequestMapping(value = "/auth/group/{zoteroGroupId}/items/create", method = RequestMethod.POST) public String create(@ModelAttribute CitationForm form, Authentication authentication, Model model, - @PathVariable("zoteroGroupId") String zoteroGroupId) - throws ZoteroConnectionException, GroupDoesNotExistException, ZoteroHttpStatusException { + @PathVariable("zoteroGroupId") String zoteroGroupId, RedirectAttributes redirectAttributes) + throws GroupDoesNotExistException, ZoteroHttpStatusException { ICitation citation = new Citation(); List collectionIds = new ArrayList<>(); if (form.getCollectionId() != null && !form.getCollectionId().trim().isEmpty()) { @@ -110,6 +111,14 @@ public String create(@ModelAttribute CitationForm form, Authentication authentic : "Sorry, item creation failed."; model.addAttribute("alert_msg", msg); return "auth/group/editItem"; + } catch (ZoteroConnectionException e) { + redirectAttributes.addFlashAttribute("form", form); + redirectAttributes.addFlashAttribute("zoteroGroupId", zoteroGroupId); + redirectAttributes.addFlashAttribute("show_alert", true); + redirectAttributes.addFlashAttribute("alert_type", "danger"); + String msg = "Sorry, item creation failed. Please check the Zotero Key permissions."; + redirectAttributes.addFlashAttribute("alert_msg", msg); + return "redirect:/auth/group/{zoteroGroupId}/items/create"; } return "redirect:/auth/group/{zoteroGroupId}/items/" + citation.getKey(); diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/AddReferenceController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/AddReferenceController.java index f2e4e115a..e5995a44c 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/AddReferenceController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/AddReferenceController.java @@ -53,7 +53,7 @@ public ResponseEntity addReference(Authentication authentication, logger.error("Zotero threw exception.", e); return new ResponseEntity<>("{\"error\": \"" + e.getMessage() + "\"}", HttpStatus.INTERNAL_SERVER_ERROR); } catch (ZoteroConnectionException e) { - logger.error("Zotero connection failed.", e); + logger.error("Zotero connection failed. Please check Zotero Key permissions. ", e); return new ResponseEntity<>("{\"error\": \"" + e.getMessage() + "\"}", HttpStatus.INTERNAL_SERVER_ERROR); } catch (CitationIsOutdatedException e) { logger.error("Citation is outdated.", e); diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/EditItemController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/EditItemController.java index bd526ef6d..3a32edd3c 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/EditItemController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/EditItemController.java @@ -125,8 +125,9 @@ public String storeItem(@ModelAttribute CitationForm form, Authentication authen @RequestParam(required = false, value = "index") String index, @RequestParam(defaultValue = "1", required = false, value = "page") int page, @RequestParam(value = "collectionId", required = false) String collectionId, - @RequestParam(defaultValue = "title", required = false, value = "sortBy") String sortBy) - throws ZoteroConnectionException, GroupDoesNotExistException, CannotFindCitationException, + @RequestParam(defaultValue = "title", required = false, value = "sortBy") String sortBy, + RedirectAttributes redirectAttributes) + throws GroupDoesNotExistException, CannotFindCitationException, ZoteroHttpStatusException, ZoteroItemCreationFailedException { ICitation citation = citationManager.getCitation((IUser) authentication.getPrincipal(), zoteroGroupId, itemId); // load authors and editors before detaching @@ -161,6 +162,13 @@ public String storeItem(@ModelAttribute CitationForm form, Authentication authen model.addAttribute("collectionId", collectionId); model.addAttribute("sortBy", sortBy); return "auth/group/editConflict"; + } catch (ZoteroConnectionException e) { + redirectAttributes.addFlashAttribute("form", form); + redirectAttributes.addFlashAttribute("zoteroGroupId", zoteroGroupId); + redirectAttributes.addFlashAttribute("show_alert", true); + redirectAttributes.addFlashAttribute("alert_type", "danger"); + redirectAttributes.addFlashAttribute("alert_msg", "Sorry, item updation failed. Please check the Zotero Key permissions."); + return "redirect:/auth/group/{zoteroGroupId}/items/{itemId}/edit?index=" + index +"&page="+page +"&sortBy="+sortBy +"&collectionId="+collectionId; } return "redirect:/auth/group/{zoteroGroupId}/items/{itemId}?index=" + index +"&page="+page +"&sortBy="+sortBy +"&collectionId="+collectionId; } diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/RevertCitationController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/RevertCitationController.java index 9b0af9546..56cb4670e 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/RevertCitationController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/RevertCitationController.java @@ -36,10 +36,13 @@ public String revertCitationVersion(Authentication authentication, Model model, citationVersionManager.revertCitationVersion((IUser) authentication.getPrincipal(), zoteroGroupId, itemId, version); return "redirect:/auth/group/" + zoteroGroupId + "/items/" + itemId; - } catch (GroupDoesNotExistException | ZoteroConnectionException | CitationIsOutdatedException + } catch (GroupDoesNotExistException | CitationIsOutdatedException | ZoteroHttpStatusException | CannotFindCitationVersionException | CannotFindCitationException | ZoteroItemCreationFailedException e) { logger.error("Error while restoring citation version", e); return "error/404"; + } catch (ZoteroConnectionException e) { + logger.error("Error while restoring citation version. Please check the Zotero Key permissions", e); + return "error/500"; } } diff --git a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/UploadItemFileController.java b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/UploadItemFileController.java index e9b045bd0..5ea57439c 100644 --- a/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/UploadItemFileController.java +++ b/citesphere/src/main/java/edu/asu/diging/citesphere/web/user/UploadItemFileController.java @@ -48,7 +48,7 @@ public class UploadItemFileController { public ResponseEntity uploadFile(Principal principal, @PathVariable String zoteroGroupId, @PathVariable String itemId, @RequestParam("files") MultipartFile[] files) throws AccessForbiddenException, CannotFindCitationException, ZoteroHttpStatusException, - ZoteroConnectionException, CitationIsOutdatedException, ZoteroItemCreationFailedException { + CitationIsOutdatedException, ZoteroItemCreationFailedException { User user = null; if (principal instanceof UsernamePasswordAuthenticationToken) { user = (User) ((UsernamePasswordAuthenticationToken) principal).getPrincipal(); @@ -68,12 +68,15 @@ public ResponseEntity uploadFile(Principal principal, @PathVariable Stri } } - IGilesUpload job; + IGilesUpload job = null; try { job = jobManager.createGilesJob(user, files[0], fileBytes.get(0), zoteroGroupId, itemId); } catch (GroupDoesNotExistException e) { logger.error("Could not create job because group does not exist.", e); return new ResponseEntity(HttpStatus.BAD_REQUEST); + } catch (ZoteroConnectionException e) { + logger.error("Could not create job. Please check Zotero Key permissions. ", e); + return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); } ObjectMapper mapper = new ObjectMapper(); diff --git a/citesphere/src/main/webapp/WEB-INF/views/error/500.html b/citesphere/src/main/webapp/WEB-INF/views/error/500.html index cb2f11f53..86b3d73b0 100644 --- a/citesphere/src/main/webapp/WEB-INF/views/error/500.html +++ b/citesphere/src/main/webapp/WEB-INF/views/error/500.html @@ -1,7 +1,7 @@ - -
+ +
- + \ No newline at end of file