This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository coselmar. See http://git.codelutin.com/coselmar.git commit ad3f5a6f2ccdc989eb10b83205ed7adccd141c5c Author: Yannick Martel <martel@©odelutin.com> Date: Tue Jan 13 15:33:49 2015 +0100 review document file management fixes #6208 manage both file and urlfor document --- coselmar-persistence/pom.xml | 6 +++ .../src/main/xmi/coselmar-model.zargo | Bin 10252 -> 10322 bytes .../services/errors/NoResultException.java | 11 +++++ .../coselmar/services/v1/DocumentsWebService.java | 52 +++++++++------------ coselmar-rest/src/main/resources/mapping | 1 + 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/coselmar-persistence/pom.xml b/coselmar-persistence/pom.xml index d5094fe..2240ed4 100644 --- a/coselmar-persistence/pom.xml +++ b/coselmar-persistence/pom.xml @@ -61,6 +61,12 @@ <artifactId>hibernate-core</artifactId> </dependency> + <!-- Tests --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> <build> diff --git a/coselmar-persistence/src/main/xmi/coselmar-model.zargo b/coselmar-persistence/src/main/xmi/coselmar-model.zargo index 4b0c5f1..c48b79e 100644 Binary files a/coselmar-persistence/src/main/xmi/coselmar-model.zargo and b/coselmar-persistence/src/main/xmi/coselmar-model.zargo differ diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/errors/NoResultException.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/errors/NoResultException.java new file mode 100644 index 0000000..4817747 --- /dev/null +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/errors/NoResultException.java @@ -0,0 +1,11 @@ +package fr.ifremer.coselmar.services.errors; + +/** + * @author ymartel (martel@codelutin.com) + */ +public class NoResultException extends Exception { + + public NoResultException(String message) { + super(message); + } +} diff --git a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java index 767c2aa..59f57b8 100644 --- a/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java +++ b/coselmar-rest/src/main/java/fr/ifremer/coselmar/services/v1/DocumentsWebService.java @@ -46,10 +46,12 @@ import fr.ifremer.coselmar.persistence.entity.Privacy; import fr.ifremer.coselmar.services.CoselmarTechnicalException; import fr.ifremer.coselmar.services.CoselmarWebServiceSupport; import fr.ifremer.coselmar.services.errors.InvalidCredentialException; +import fr.ifremer.coselmar.services.errors.NoResultException; import fr.ifremer.coselmar.services.errors.UnauthorizedException; import fr.ifremer.coselmar.services.indexation.DocumentsIndexationService; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.logging.Log; import org.apache.lucene.queryparser.classic.ParseException; import org.debux.webmotion.server.call.UploadFile; @@ -192,11 +194,13 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { String documentName = document.getName(); String contentType = null; + String filePath = null; // If document has a file, manager it ! - if (document.isWithFile()) { - contentType = managerDocumentFile(uploadFile, owner); - + if (uploadFile != null) { + Pair<String, String> pathAndContentType = managerDocumentFile(uploadFile, owner); + filePath = pathAndContentType.getLeft(); + contentType = pathAndContentType.getRight(); } // Document Metadata @@ -227,9 +231,10 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { documentEntity.setLicense(document.getLicense()); // Document resource part - if (document.isWithFile()) { + if (uploadFile != null) { documentEntity.setWithFile(true); documentEntity.setMimeType(contentType); + documentEntity.setFilePath(filePath); } else { documentEntity.setWithFile(false); } @@ -258,7 +263,7 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { } - public Render getDocumentFile(String documentId) { + public Render getDocumentFile(String documentId) throws NoResultException { // reconstitute full id String fullId =getDocumentFullId(documentId); @@ -267,7 +272,11 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { //TODO ymartel 20141103 : manage file owner ? // Get file attached to document - File documentFile = getDocumentFile(document); + String filePath = document.getFilePath(); + if (StringUtils.isBlank(filePath)) { + throw new NoResultException("No File"); + } + File documentFile = new File(filePath); String fileName = document.getName(); String fileMimeType = document.getMimeType(); @@ -280,8 +289,7 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { String message = String.format("Unable to retrieve file %s", fileName); log.error(message); } - //TODO ymartel 20141106 : manage 404 error - throw new CoselmarTechnicalException("File does not exist"); + throw new NoResultException("File does not exist"); } } @@ -313,8 +321,8 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { } // Delete physical file - if (document.isWithFile()) { - File documentFile = getDocumentFile(document); + if (StringUtils.isNotBlank(document.getFilePath())) { + File documentFile = new File(document.getFilePath()); FileUtils.deleteQuietly(documentFile); } @@ -363,7 +371,7 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { * * @return the upload file Metadata */ - protected String managerDocumentFile(UploadFile uploadFile, CoselmarUser owner) { + protected Pair<String, String> managerDocumentFile(UploadFile uploadFile, CoselmarUser owner) { Preconditions.checkNotNull(uploadFile); // Document File @@ -380,7 +388,7 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { String userPath = getUserDocumentPath(owner); Date now = getNow(); - String formattedDay = DateUtil.formatDate(now, "yyyymmdd"); + String formattedDay = DateUtil.formatDate(now, "yyyyMMddhhmm"); String prefix = formattedDay + "-"; File destFile = new File(userPath + File.separator + prefix + fileName); @@ -392,7 +400,7 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { } throw new CoselmarTechnicalException("Internal error during file transfer"); } - return contentType; + return Pair.of(destFile.getAbsolutePath(), contentType); } protected String getUserDocumentPath(CoselmarUser user) { @@ -404,24 +412,6 @@ public class DocumentsWebService extends CoselmarWebServiceSupport { } /** - * Return the document File from its Metadata. - * - * @param document : document we want the {@link java.io.File} corresponding - * to the physical document. - * @return a File pointing out the physical file. - */ - protected File getDocumentFile(Document document) { - String fileName = document.getName(); - Date depositeDate = document.getDepositDate(); - - String userDocumentPath = getUserDocumentPath(document.getOwner()); - - String formatedDay = DateUtil.formatDate(depositeDate, "yyyymmdd"); - String prefix = formatedDay + "-"; - return new File(userDocumentPath + "/" + prefix + fileName); - } - - /** * Check if an user can access to a document metadata, according to its * privacy settings and the user grant. * diff --git a/coselmar-rest/src/main/resources/mapping b/coselmar-rest/src/main/resources/mapping index a2494e8..b74e068 100644 --- a/coselmar-rest/src/main/resources/mapping +++ b/coselmar-rest/src/main/resources/mapping @@ -16,6 +16,7 @@ default.render=fr.ifremer.coselmar.services.CoselmarRender fr.ifremer.coselmar.services.errors.InvalidCredentialException ErrorAction.on401 fr.ifremer.coselmar.services.errors.UnauthorizedException ErrorAction.on403 +fr.ifremer.coselmar.services.errors.NoResultException ErrorAction.on404 fr.ifremer.coselmar.services.CoselmarTechnicalException ErrorAction.on500 org.nuiton.topia.persistence.TopiaNoResultException ErrorAction.on404 -- To stop receiving notification emails like this one, please contact codelutin.com SCM administrator <admin+scm@codelutin.com>.