Skip to content

Commit

Permalink
Merge pull request #121 from diging/develop
Browse files Browse the repository at this point in the history
prepare release
  • Loading branch information
jdamerow authored Nov 19, 2024
2 parents a3d7045 + da5e90c commit 0c5505a
Show file tree
Hide file tree
Showing 32 changed files with 990 additions and 87 deletions.
11 changes: 9 additions & 2 deletions giles-eco/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
<version>1.5</version>
</dependency>

<!-- Apache Commons IO -->
Expand Down Expand Up @@ -446,7 +446,7 @@
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.13</version>
<version>1.18</version>
</dependency>

<!-- testing -->
Expand Down Expand Up @@ -558,6 +558,13 @@
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>

<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.asu.diging.gilesecosystem.web.api.util;

import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public interface IResponseHelper {
/**
* Generates a ResponseEntity with a JSON body based on the given map of messages and HTTP status.
* @param msgs a map of messages where the keys represent message identifiers and the values represent message content
* @param status the HTTP status code to be set in the response
* @return a ResponseEntity object with JSON content and the specified HTTP status
*/
public abstract ResponseEntity<String> generateResponse(Map<String, String> msgs, HttpStatus status);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package edu.asu.diging.gilesecosystem.web.api.util.impl;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;

import edu.asu.diging.gilesecosystem.septemberutil.properties.MessageType;
import edu.asu.diging.gilesecosystem.septemberutil.service.ISystemMessageHandler;
import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper;

@Component
public class ResponseHelper implements IResponseHelper {

@Autowired
private ISystemMessageHandler messageHandler;

@Override
public ResponseEntity<String> generateResponse(Map<String, String> msgs,
HttpStatus status) {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
ObjectNode root = mapper.createObjectNode();
for (String key : msgs.keySet()) {
root.put(key, msgs.get(key));
}

StringWriter sw = new StringWriter();
try {
mapper.writeValue(sw, root);
} catch (IOException e) {
messageHandler.handleMessage("Could not write json.", e, MessageType.ERROR);
return new ResponseEntity<String>(
"{\"errorMsg\": \"Could not write json result.\", \"errorCode\": \"errorCode\": \"500\" }",
HttpStatus.INTERNAL_SERVER_ERROR);
}

return new ResponseEntity<String>(sw.toString(), status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package edu.asu.diging.gilesecosystem.web.api.v2;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager;
import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper;
import edu.asu.diging.gilesecosystem.web.config.CitesphereToken;
import edu.asu.diging.gilesecosystem.web.core.citesphere.ICitesphereConnector;
import edu.asu.diging.gilesecosystem.web.core.model.IDocument;
import edu.asu.diging.gilesecosystem.web.core.model.IUpload;
import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalDocumentService;
import edu.asu.diging.gilesecosystem.web.core.service.core.ITransactionalUploadService;
import edu.asu.diging.gilesecosystem.web.core.service.properties.Properties;
import edu.asu.diging.gilesecosystem.web.core.service.reprocessing.IReprocessingService;
import edu.asu.diging.gilesecosystem.web.core.users.CitesphereUser;

@Controller
public class V2ReprocessDocumentController {
@Autowired
private IReprocessingService reprocessingService;

@Autowired
private ITransactionalDocumentService documentService;

@Autowired
private IResponseHelper responseHelper;

@Autowired
private IPropertiesManager propertyManager;

@Autowired
private ITransactionalUploadService uploadService;

@Autowired
private ICitesphereConnector citesphereConnector;

@Value("${giles_check_upload_endpoint_v2}")
private String uploadEndpoint;

@RequestMapping(value = "/api/v2/resources/documents/{documentId}/reprocess", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public ResponseEntity<String> reprocessDocument(@PathVariable("documentId") String documentId, CitesphereToken citesphereToken) {
IDocument document = documentService.getDocument(documentId);
Map<String, String> msgs = new HashMap<String, String>();
if (document == null) {
msgs.put("errorMsg", "Document with id: " + documentId + " does not exist.");
msgs.put("errorCode", "404");
return responseHelper.generateResponse(msgs, HttpStatus.NOT_FOUND);
}
if (!citesphereConnector.hasAccess(document.getId(), ((CitesphereUser)citesphereToken.getPrincipal()).getUsername())) {
Map<String, String> unauthorizedMsgs = new HashMap<String, String>();
unauthorizedMsgs.put("errorMsg", "User is not authorized to reprocess the document with id " + document.getId());
unauthorizedMsgs.put("errorCode", "403");
return responseHelper.generateResponse(unauthorizedMsgs, HttpStatus.FORBIDDEN);
}
reprocessingService.reprocessDocument(document);
IUpload upload = uploadService.getUpload(document.getUploadId());
msgs.put("id", documentId);
msgs.put("checkUrl", propertyManager.getProperty(Properties.GILES_URL) + uploadEndpoint + upload.getUploadProgressId());
return responseHelper.generateResponse(msgs, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import edu.asu.diging.gilesecosystem.util.exceptions.UnstorableObjectException;
import edu.asu.diging.gilesecosystem.util.properties.IPropertiesManager;
import edu.asu.diging.gilesecosystem.web.api.util.IJSONHelper;
import edu.asu.diging.gilesecosystem.web.api.util.IResponseHelper;
import edu.asu.diging.gilesecosystem.web.config.CitesphereToken;
import edu.asu.diging.gilesecosystem.web.config.IUserHelper;
import edu.asu.diging.gilesecosystem.web.core.files.impl.StorageStatus;
Expand Down Expand Up @@ -85,6 +86,9 @@ public class V2UploadFileController {

@Autowired
private IUserHelper userHelper;

@Autowired
private IResponseHelper responseHelper;

@RequestMapping(value = "/api/v2/files/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> uploadImages(
Expand All @@ -101,7 +105,7 @@ public ResponseEntity<String> uploadImages(
msgs.put("errorMsg", "Access type: " + access + " does not exist.");
msgs.put("errorCode", "400");

return generateResponse(msgs, HttpStatus.BAD_REQUEST);
return responseHelper.generateResponse(msgs, HttpStatus.BAD_REQUEST);
}

DocumentType documentType = DocumentType.valueOf(docType);
Expand All @@ -110,15 +114,15 @@ public ResponseEntity<String> uploadImages(
msgs.put("errorMsg", "Document type: " + docType + " does not exist.");
msgs.put("errorCode", "400");

return generateResponse(msgs, HttpStatus.BAD_REQUEST);
return responseHelper.generateResponse(msgs, HttpStatus.BAD_REQUEST);
}

if (files == null || files.length == 0) {
Map<String, String> msgs = new HashMap<String, String>();
msgs.put("errorMsg", "There were no files attached to request.");
msgs.put("errorCode", "400");

return generateResponse(msgs, HttpStatus.BAD_REQUEST);
return responseHelper.generateResponse(msgs, HttpStatus.BAD_REQUEST);
}

List<byte[]> fileBytes = new ArrayList<byte[]>();
Expand All @@ -132,7 +136,7 @@ public ResponseEntity<String> uploadImages(
msgs.put("errorMsg", "File bytes could not be read.");
msgs.put("errorCode", "500");

return generateResponse(msgs, HttpStatus.INTERNAL_SERVER_ERROR);
return responseHelper.generateResponse(msgs, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

Expand All @@ -144,10 +148,10 @@ public ResponseEntity<String> uploadImages(
messageHandler.handleMessage("Could not store non login user.", e,
MessageType.ERROR);
Map<String, String> msgs = new HashMap<String, String>();
msgs.put("errorMsg", "An erro occurred. Request could not be processed.");
msgs.put("errorMsg", "An error occurred. Request could not be processed.");
msgs.put("errorCode", "500");

return generateResponse(msgs, HttpStatus.INTERNAL_SERVER_ERROR);
return responseHelper.generateResponse(msgs, HttpStatus.INTERNAL_SERVER_ERROR);
}

String id = uploadService.startUpload(docAccess, documentType, files, fileBytes,
Expand All @@ -159,7 +163,7 @@ public ResponseEntity<String> uploadImages(
propertyManager.getProperty(Properties.GILES_URL) + uploadEndpoint + id);

logger.info("Uploaded file started with id " + id);
return generateResponse(msgs, HttpStatus.OK);
return responseHelper.generateResponse(msgs, HttpStatus.OK);
}

public User createUser(CitesphereToken citesphereToken)
Expand Down Expand Up @@ -194,7 +198,7 @@ public ResponseEntity<String> checkAndGetResults(HttpServletRequest request,
msgs.put("errorMsg", "User is not authorized to check status.");
msgs.put("errorCode", "401");

return generateResponse(msgs, HttpStatus.UNAUTHORIZED);
return responseHelper.generateResponse(msgs, HttpStatus.UNAUTHORIZED);
}

List<StorageStatus> statusList = uploadService.getUploadStatus(id);
Expand All @@ -203,7 +207,7 @@ public ResponseEntity<String> checkAndGetResults(HttpServletRequest request,
msgs.put("errorMsg", "Upload does not exist.");
msgs.put("errorCode", "404");

return generateResponse(msgs, HttpStatus.NOT_FOUND);
return responseHelper.generateResponse(msgs, HttpStatus.NOT_FOUND);
}

boolean complete = true;
Expand All @@ -226,7 +230,7 @@ public ResponseEntity<String> checkAndGetResults(HttpServletRequest request,
msgs.put("uploadUrl", uploadUrl);
}

return generateResponse(msgs, HttpStatus.ACCEPTED);
return responseHelper.generateResponse(msgs, HttpStatus.ACCEPTED);
}

Set<String> docIds = new HashSet<String>();
Expand Down Expand Up @@ -256,26 +260,4 @@ public ResponseEntity<String> checkAndGetResults(HttpServletRequest request,

return new ResponseEntity<String>(sw.toString(), HttpStatus.OK);
}

private ResponseEntity<String> generateResponse(Map<String, String> msgs,
HttpStatus status) {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
ObjectNode root = mapper.createObjectNode();
for (String key : msgs.keySet()) {
root.put(key, msgs.get(key));
}

StringWriter sw = new StringWriter();
try {
mapper.writeValue(sw, root);
} catch (IOException e) {
messageHandler.handleMessage("Could not write json.", e, MessageType.ERROR);
return new ResponseEntity<String>(
"{\"errorMsg\": \"Could not write json result.\", \"errorCode\": \"errorCode\": \"500\" }",
HttpStatus.INTERNAL_SERVER_ERROR);
}

return new ResponseEntity<String>(sw.toString(), status);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@ public interface IEmailNotificationManager {
*/
void sendAccountCreatedEmail(String name, String username, String adminName, String adminEmail)
throws GilesNotificationException;

/**
* Send account approval email to the user
*
* @param name name of new user
* @param userName userName of new user
* @param userEmail email id of user
* @throws GilesNotificationException
*/
void sendAccountApprovalEmail(String name, String userName, String userEmail) throws GilesNotificationException;

/**
* Send account revoked email to the user
*
* @param name name of new user
* @param userName userName of new user
* @param userEmail email id of user
* @throws GilesNotificationException
*/
void sendAccountRevokedEmail(String name, String userName, String userEmail) throws GilesNotificationException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,46 @@ public void sendAccountCreatedEmail(String name, String username, String adminNa
} catch (ParseErrorException e) {
throw new GilesNotificationException(e);
} catch (Exception e) {
//getTemplate method call under getRenderedTemplate can throw exception
throw new GilesNotificationException(e);
}
}

@Override
public void sendAccountApprovalEmail(String name, String userName, String userEmail)
throws GilesNotificationException {
Map<String, Object> contextProperties = new HashMap<String, Object>();
contextProperties.put("name", name);
contextProperties.put("userName", userName);
try {
emailSender.sendNotificationEmail(userEmail, emailMessages.getMessage("email.account_approved.subject", new String[]{}, null),
velocityBuilder.getRenderedTemplate("velocitytemplates/email/accountApproved.vm", contextProperties));
} catch (ResourceNotFoundException e) {
throw new GilesNotificationException(e);
} catch (ParseErrorException e) {
throw new GilesNotificationException(e);
} catch (Exception e) {
throw new GilesNotificationException(e);
}

}

@Override
public void sendAccountRevokedEmail(String name, String userName, String userEmail)
throws GilesNotificationException {
Map<String, Object> contextProperties = new HashMap<String, Object>();
contextProperties.put("name", name);
contextProperties.put("userName", userName);
try {
emailSender.sendNotificationEmail(userEmail, emailMessages.getMessage("email.account_revoked.subject", new String[]{}, null),
velocityBuilder.getRenderedTemplate("velocitytemplates/email/accountRevoked.vm", contextProperties));
} catch (ResourceNotFoundException e) {
throw new GilesNotificationException(e);
} catch (ParseErrorException e) {
throw new GilesNotificationException(e);
} catch (Exception e) {
throw new GilesNotificationException(e);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import edu.asu.diging.gilesecosystem.web.core.model.DocumentType;
import edu.asu.diging.gilesecosystem.web.core.model.IDocument;
import edu.asu.diging.gilesecosystem.web.core.model.IFile;
import edu.asu.diging.gilesecosystem.web.core.model.ProcessingStatus;
import edu.asu.diging.gilesecosystem.web.core.users.User;

public interface IFilesManager {
Expand Down Expand Up @@ -46,5 +47,12 @@ public abstract List<StorageStatus> addFiles(Map<IFile, byte[]> files,
* @throws UnstorableObjectException for exception while saving updated document
*/
public abstract boolean changeDocumentAccess(IDocument doc, DocumentAccess docAccess) throws UnstorableObjectException;

/**
* Changes the processing status of the specified file.
* @param file The file to update the processing status for.
* @param status The new processing status to assign to the file.
*/
public abstract void changeFileProcessingStatus(IFile file, ProcessingStatus status);

}
}
Loading

0 comments on commit 0c5505a

Please sign in to comment.