Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -145,39 +145,33 @@ public ResponseEntity<Object> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ public ResponseEntity<Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> additionalParameters) {
return delegate.fetchRequestToken(callbackUrl, additionalParameters);
}

@Override
public OAuthToken exchangeForAccessToken(AuthorizedRequestToken requestToken, MultiValueMap<String, String> additionalParameters) {
return delegate.exchangeForAccessToken(requestToken, additionalParameters);
}

@Override
public org.springframework.social.oauth1.OAuth1Version getVersion() {
return delegate.getVersion();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> collectionIds = new ArrayList<>();
if (form.getCollectionId() != null && !form.getCollectionId().trim().isEmpty()) {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class UploadItemFileController {
public ResponseEntity<String> 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();
Expand All @@ -68,12 +68,15 @@ public ResponseEntity<String> 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<String>(HttpStatus.BAD_REQUEST);
} catch (ZoteroConnectionException e) {
logger.error("Could not create job. Please check Zotero Key permissions. ", e);
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}

ObjectMapper mapper = new ObjectMapper();
Expand Down
6 changes: 3 additions & 3 deletions citesphere/src/main/webapp/WEB-INF/views/error/500.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html layout:decorate="~{layouts/main}">

<div>
<body>
<div layout:fragment="content">
<img class="img-responsive" style="height: calc(100vh - 200px);-webkit-user-select: none;margin: auto;" src="https://http.cat/500">
</div>

</body>
</html>