r686 - in trunk: simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/attachment simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/factories simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine simexplorer-is-storage/src/
Author: glandais Date: 2008-02-05 15:53:04 +0000 (Tue, 05 Feb 2008) New Revision: 686 Removed: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/util/ZipStreamEncoder.java Modified: trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/attachment/Attachment.java trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java trunk/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/ElementGenerator.java trunk/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/AttachmentHandler.java trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java trunk/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementExport.java Log: Hash handling Modified: trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/attachment/Attachment.java =================================================================== --- trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/attachment/Attachment.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/attachment/Attachment.java 2008-02-05 15:53:04 UTC (rev 686) @@ -38,7 +38,7 @@ private ContentType contentType; /** Data hash. */ - private int dataHash; + private String dataHash; /** * Gets the file name. @@ -83,7 +83,7 @@ * * @return the data hash */ - public int getDataHash() { + public String getDataHash() { return dataHash; } @@ -93,7 +93,7 @@ * @param dataHash * the new data hash */ - public void setDataHash(int dataHash) { + public void setDataHash(String dataHash) { this.dataHash = dataHash; } @@ -103,52 +103,11 @@ * @return the unique id */ public String getUniqueId() { - String hexDataHash = StringUtils.leftPad(Integer.toHexString(dataHash), - 8, '0'); + String hexDataHash = StringUtils.leftPad(dataHash, 32, '0'); return hexDataHash + fileName; } /** - * Int from hex string. - * - * @param nm - * the nm - * - * @return the int - * - * @throws NumberFormatException - * the number format exception - */ - public static int intFromHexString(String nm) throws NumberFormatException { - int radix = 16; - int index = 0; - int result = 0; - - // Handle radix specifier, if present - if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { - index += 2; - radix = 16; - } else if (nm.startsWith("#", index)) { - index++; - radix = 16; - } - if (nm.length() - index > 8) { - throw new NumberFormatException(nm + " exceeds 8 digits"); - } - int digit; - for (int i = index; i < nm.length(); i++) { - result = result << 4; - digit = Character.digit(nm.charAt(i), radix); - if (digit == -1) { - throw new NumberFormatException(nm + " : \'" + nm.charAt(i) - + "\' is not a valid hex digit"); - } - result |= digit; - } - return result; - } - - /** * Gets the hash from unique id. * * @param uniqueId @@ -156,9 +115,9 @@ * * @return the hash from unique id */ - public static int getHashFromUniqueId(String uniqueId) { - String hexHash = uniqueId.substring(0, 8); - return intFromHexString("0X" + hexHash); + public static String getHashFromUniqueId(String uniqueId) { + String hexHash = uniqueId.substring(0, 32); + return hexHash; } /** @@ -170,7 +129,7 @@ * @return the file name from unique id */ public static String getFileNameFromUniqueId(String uniqueId) { - return uniqueId.substring(9); + return uniqueId.substring(33); } } Modified: trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java =================================================================== --- trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-entities/src/java/fr/cemagref/simexplorer/is/factories/MetaDataFactory.java 2008-02-05 15:53:04 UTC (rev 686) @@ -36,7 +36,9 @@ /** * A factory for creating MetaData objects. - * @param <E> MetaData + * + * @param <E> + * MetaData */ public class MetaDataFactory<E extends MetaData> extends BaseEntityFactory<MetaData> { @@ -55,8 +57,10 @@ /** * Gets the factory. - * @param <E> Metadata * + * @param <E> + * Metadata + * * @param entityClass * the entity class * @@ -151,7 +155,7 @@ String dataHash = getXMLProperty(attachmentElement, KEY_METADATA_ATTACHMENT_HASH); - attachment.setDataHash(Integer.parseInt(dataHash)); + attachment.setDataHash(dataHash); attachments.add(attachment); } @@ -239,8 +243,7 @@ KEY_METADATA_ATTACHMENT_TYPE, attachment .getContentType().getClass().getSimpleName()); setXMLProperty(document, attachmentElement, - KEY_METADATA_ATTACHMENT_HASH, Integer - .toString(attachment.getDataHash())); + KEY_METADATA_ATTACHMENT_HASH, attachment.getDataHash()); attachmentsElement.appendChild(attachmentElement); } Modified: trunk/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/ElementGenerator.java =================================================================== --- trunk/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/ElementGenerator.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/ElementGenerator.java 2008-02-05 15:53:04 UTC (rev 686) @@ -19,6 +19,7 @@ import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.math.BigInteger; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -245,7 +246,8 @@ Attachment attachment = new Attachment(); attachment.setContentType(ContentTypeFactory .getContentTypeInstance("RawType")); - attachment.setDataHash(r.nextInt()); + + attachment.setDataHash("d41d8cd98f00b204e9800998ecf8427e"); attachment.setFileName(shortString() + ".txt"); return attachment; } Modified: trunk/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java =================================================================== --- trunk/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java 2008-02-05 15:53:04 UTC (rev 686) @@ -17,6 +17,8 @@ * ##% */ package fr.cemagref.simexplorer.is.service; +import static org.codelutin.i18n.I18n._; + import java.io.IOException; import java.io.InputStream; import java.io.PipedInputStream; @@ -29,6 +31,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; +import org.codelutin.util.ZipStreamEncoder; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -46,7 +49,6 @@ import fr.cemagref.simexplorer.is.factories.XmlConstants; import fr.cemagref.simexplorer.is.storage.SimExplorerStorageException; import fr.cemagref.simexplorer.is.storage.engine.StorageEngine; -import fr.cemagref.simexplorer.is.storage.util.ZipStreamEncoder; /** * The Class StorageServiceCommon. @@ -437,10 +439,17 @@ List<Attachment> realAttachments = metaData.getAttachments(); for (Attachment attachment : realAttachments) { + String idAttachment = idsattachment.get(attachment.getUniqueId()); + // Check hash of attachments + String md5 = getStorageEngine().retrieveMD5TempData(idAttachment); + if (!md5.equals(attachment.getDataHash())) { + throw new SimExplorerServiceException( + _("simexplorer.service.invalidhash")); + } + attachments.put(attachment.getFileName(), getStorageEngine() - .retrieveTempData( - idsattachment.get(attachment.getUniqueId()))); + .retrieveTempData(idAttachment)); } getStorageEngine().saveElement(token, metaData, attachments); Modified: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/AttachmentHandler.java =================================================================== --- trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/AttachmentHandler.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/AttachmentHandler.java 2008-02-05 15:53:04 UTC (rev 686) @@ -42,8 +42,8 @@ * @throws SimExplorerStorageException * the sim explorer storage exception */ - public abstract void storeData(MetaData entity, String field, - InputStream is) throws SimExplorerStorageException; + public abstract void storeData(MetaData entity, String field, InputStream is) + throws SimExplorerStorageException; /** * Retrieve content. @@ -62,6 +62,22 @@ throws SimExplorerStorageException; /** + * Retrieve MD5. + * + * @param entity + * the entity + * @param field + * the field + * + * @return the string + * + * @throws SimExplorerStorageException + * the sim explorer storage exception + */ + public abstract String retrieveMD5Data(MetaData entity, String field) + throws SimExplorerStorageException; + + /** * Delete content. * * @param entity Modified: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java =================================================================== --- trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java 2008-02-05 15:53:04 UTC (rev 686) @@ -26,6 +26,8 @@ import java.io.IOException; import java.io.InputStream; +import org.codelutin.util.MD5; + import fr.cemagref.simexplorer.is.entities.metadata.MetaData; import fr.cemagref.simexplorer.is.storage.SimExplorerStorageException; import fr.cemagref.simexplorer.is.storage.util.Config; @@ -93,6 +95,21 @@ } /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler#retrieveMD5Data(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.lang.String) + */ + @Override + public String retrieveMD5Data(MetaData entity, String field) + throws SimExplorerStorageException { + String md5 = null; + try { + MD5.asHex(MD5.getHash(getFile(entity, field))); + } catch (Exception e) { + throw new SimExplorerStorageException(e); + } + return md5; + } + + /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler#storeData(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.lang.String, java.io.InputStream) */ @Override Modified: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java =================================================================== --- trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/database/lucene/LuceneDatabase.java 2008-02-05 15:53:04 UTC (rev 686) @@ -752,8 +752,7 @@ attachment.setFileName(fieldValue); } if (field.equals(KEY_ATTACHMENT_HASH)) { - attachment.setDataHash(Attachment.intFromHexString("0X" - + fieldValue)); + attachment.setDataHash(fieldValue); } if (field.equals(KEY_ATTACHMENT_TYPE)) { attachment.setContentType(ContentTypeFactory @@ -857,8 +856,8 @@ String key = KEY_ATTACHMENT + DOT + i; addSimpleField(document, key + DOT + KEY_ATTACHMENT_FILENAME, attachment.getFileName()); - addSimpleField(document, key + DOT + KEY_ATTACHMENT_HASH, Integer - .toHexString(attachment.getDataHash())); + addSimpleField(document, key + DOT + KEY_ATTACHMENT_HASH, + attachment.getDataHash()); addSimpleField(document, key + DOT + KEY_ATTACHMENT_TYPE, attachment.getContentType().getClass().getSimpleName()); i++; Modified: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java =================================================================== --- trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngine.java 2008-02-05 15:53:04 UTC (rev 686) @@ -305,6 +305,20 @@ throws SimExplorerStorageException; /** + * Retrieve MD5 temp data. + * + * @param id + * the id + * + * @return the string + * + * @throws SimExplorerStorageException + * the sim explorer storage exception + */ + public abstract String retrieveMD5TempData(String id) + throws SimExplorerStorageException; + + /** * Delete temporary data. * * @param id Modified: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java =================================================================== --- trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/engine/StorageEngineImpl.java 2008-02-05 15:53:04 UTC (rev 686) @@ -290,6 +290,13 @@ return is; } + @Override + public String retrieveMD5TempData(String id) + throws SimExplorerStorageException { + String md5 = attachmentHandler.retrieveMD5Data(mdTmp, id); + return md5; + } + /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.storage.engine.StorageEngine#deleteTempData(java.lang.String) */ Deleted: trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/util/ZipStreamEncoder.java =================================================================== --- trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/util/ZipStreamEncoder.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/util/ZipStreamEncoder.java 2008-02-05 15:53:04 UTC (rev 686) @@ -1,87 +0,0 @@ -/* -* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -* ##% */ -package fr.cemagref.simexplorer.is.storage.util; - -import java.io.IOException; -import java.io.InputStream; -import java.io.PipedOutputStream; -import java.util.Map; -import java.util.zip.Deflater; -import java.util.zip.ZipEntry; -import java.util.zip.ZipOutputStream; - -/** - * The Class ZipStreamEncoder. - */ -public class ZipStreamEncoder extends Thread { - - /** The Constant BUFFER. */ - static final int BUFFER = 2048; - - /** The files. */ - private Map<String, InputStream> files; - - /** The os. */ - private PipedOutputStream os; - - /** The zos. */ - private ZipOutputStream zos; - - /** - * Instantiates a new zip stream encoder. - * - * @param files - * the files - * @param os - * the os - */ - public ZipStreamEncoder(Map<String, InputStream> files, PipedOutputStream os) { - super(); - this.files = files; - this.os = os; - - zos = new ZipOutputStream(this.os); - zos.setMethod(ZipOutputStream.DEFLATED); - zos.setLevel(Deflater.BEST_COMPRESSION); - } - - /* (non-Javadoc) - * @see java.lang.Thread#run() - */ - @Override - public void run() { - byte data[] = new byte[BUFFER]; - try { - for (Map.Entry<String, InputStream> kv : files.entrySet()) { - ZipEntry entry = new ZipEntry(kv.getKey()); - InputStream origin = kv.getValue(); - zos.putNextEntry(entry); - int count; - while ((count = origin.read(data, 0, BUFFER)) != -1) { - zos.write(data, 0, count); - } - origin.close(); - } - - zos.close(); - } catch (IOException e) { - // TODO - } - } - -} Modified: trunk/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementExport.java =================================================================== --- trunk/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementExport.java 2008-02-05 14:28:57 UTC (rev 685) +++ trunk/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementExport.java 2008-02-05 15:53:04 UTC (rev 686) @@ -373,8 +373,7 @@ + attachment.getUniqueId(); node.setColumns(generateStringArray(attachment.getContentType() - .getDescription(), Integer - .toHexString(attachment.getDataHash()), generateString( + .getDescription(), attachment.getDataHash(), generateString( attachment.getFileName(), "downloadFile", context))); return node; }
participants (1)
-
glandais@users.labs.libre-entreprise.org