public static PairStringSerializerImpl<String, Checksum> getFileChecksumPairSerializer(){ PairStringSerializerImpl<String, Checksum> serializer = new PairStringSerializerImpl<String, Checksum>(); serializer.setSerializerT(new StringStringSerializerImpl()); serializer.setSerializerU(new ChecksumStringSerializerImpl()); return serializer; } }
public String serialize(Pair<T,U> pair){ StringBuilder sb = new StringBuilder(); sb.append(escapeString(stringSerializerT.serialize(pair.getKey()))); sb.append(delimiter); sb.append(stringSerializerU.serialize(pair.getValue())); return sb.toString(); }
/** * Given the map of manifest file asserted file paths and checksums, along with the checksum algorithm, * this method returns a List of Pair objects, whose keys are the file path with file system's specific File Separators, * and values are ChecksumImpl objects. * * @param fileChecksumMap * @param alg * @return */ protected List<Pair<String, Checksum>> parseData(Map<String, String> fileChecksumMap, String alg) { List<Pair<String, Checksum>> checksumPairList = new ArrayList<Pair<String, Checksum>>(); for (String key : fileChecksumMap.keySet()) { String checksumValue = fileChecksumMap.get(key); key = key.replace("/", File.separator); checksumPairList.add(new Pair<String, Checksum>(key, new ChecksumImpl(alg, checksumValue))); } return checksumPairList; }
@Override public void addChecksum(String hash, String file, String checksum) { List<Checksum> existingChecksums = checksums.get(file); if (existingChecksums != null) { existingChecksums.add(new ChecksumImpl(hash, checksum)); } else { List<Checksum> newChecksums = new ArrayList<Checksum>(); newChecksums.add(new ChecksumImpl(hash, checksum)); checksums.put(file, newChecksums); } }
@Override public IngestWorkflowState newInstance() { final InMemoryEventManager em = new InMemoryEventManager(); final AttributeSetManagerImpl asm = new AttributeSetManagerImpl(); final BusinessObjectManagerImpl bom = new BusinessObjectManagerImpl(); final IngestWorkflowStateImpl state = new IngestWorkflowStateImpl(); em.setIdService(idCreationService); state.setAttributeSetManager(asm); state.setBusinessObjectManager(bom); state.setEventManager(em); state.setPackage(new PackageImpl(new DescriptionImpl(), new SerializationImpl())); return state; }
public Pair<T,U> deserialize(String serializedString) throws IllegalArgumentException{ String keyString = null; String valueString = null; boolean escaped = false; boolean delimiterFound = false; for(int i = 0, n = serializedString.length() ; i < n ; i++) { char c = serializedString.charAt(i); if (!escaped && c == delimiter) { keyString = unescapeString(serializedString.substring(0, i)); valueString = serializedString.substring(i + 1); delimiterFound = true; break; } if (!escaped && c == escapeChar) { escaped = true; } else { escaped = false; } } if(!delimiterFound){ throw new IllegalArgumentException("Could not deserialize the string " + serializedString); } return new Pair(stringSerializerT.deserialize(keyString),stringSerializerU.deserialize(valueString)); }
@Override public URI createPackageURI(String pkg_name, File file) throws URISyntaxException { File base = new File(getExtractDir(), getBaseDir().getPath()); // Go through some hoops to convert file path to a URI path starting with the package name file = relativize(base, file); file = new File(pkg_name, file.getPath()); File cwd = new File(System.getProperty("user.dir")); URI rel = cwd.toURI().relativize(file.toURI()); URI uri = new URI("file", null, "/", null); return uri.resolve(rel); } }
/** * Goes through BagIt attribute set, get check sum Attributes and store in a map, keyed by file's path for easy looking * when attribute sets for files are collected. */ private Map<String, Attribute> getAssertedFileChecksumMap(AttributeSet attributeSet) throws StatefulIngestServiceException { PairStringSerializerImpl<String, Checksum> fileChecksumPairSerializer = PairSerializerUtil.getFileChecksumPairSerializer(); if (!attributeSet.getName().equals(AttributeSetName.BAGIT)) { throw new StatefulIngestServiceException("Expected " + AttributeSetName.BAGIT + " attribute set, but given " + attributeSet.getName() + "."); } Map<String, Attribute> assertedFileChecksumMap = new HashMap<String, Attribute>(); java.util.Collection<Attribute> checksumAttributes = attributeSet.getAttributesByName(Metadata.BAGIT_CHECKSUM); for (Attribute attribute : checksumAttributes) { Pair<String, Checksum> checksumKVPair = fileChecksumPairSerializer.deserialize(attribute.getValue()); //file path value used as key = data/.../.../<file-name> assertedFileChecksumMap.put((String) checksumKVPair.getKey(), attribute); } return assertedFileChecksumMap; }
@Override public List<File> getFiles(boolean relativize) { final ArrayList<File> results = new ArrayList<File>(files.size()); final File baseDir = absolutize(extractDirectory, packageBaseDirectory); if (relativize) { for (File f : files) { results.add(relativize(baseDir, f)); } } else { for (File f : files) { results.add(absolutize(baseDir, f)); } } return Collections.unmodifiableList(results); }
@Override public File resolvePackageFileUri(URI uri) { if (uri.getScheme() == null || !uri.getScheme().equals("file")) { throw new IllegalArgumentException("Not a file URI: " + uri); } File base = new File(getExtractDir(), getBaseDir().getPath()); // The package URI is defined to start with the last component of the // base directory return new File(base.getParentFile(), uri.getPath()); }
public String toString(HierarchicalPrettyPrinter hpp) { if (!hasHpp(description)) { hpp.appendWithIndentAndNewLine("Description: " + description); } else { hpp.appendWithIndentAndNewLine("Description:"); hpp.incrementDepth(); invokeHpp(hpp, description); hpp.decrementDepth(); } if (!hasHpp(serialization)) { hpp.appendWithIndentAndNewLine("Serialization: " + serialization); } else { hpp.appendWithIndentAndNewLine("Serialization: "); hpp.incrementDepth(); invokeHpp(hpp, serialization); hpp.decrementDepth(); } return hpp.toString(); }
@Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { PackageSerialization serialization = null; PackageDescription description = null; while (reader.hasMoreChildren()) { reader.moveDown(); String name = getElementName(reader); if (name.equals(PackageSerializationConverter.E_PACKAGE_SERIALIZATION)) { serialization = (PackageSerialization) context.convertAnother(serialization, PackageSerialization.class); } else if (name.equals(PackageDescriptionConverter.E_PACKAGE_DESCRIPTION)) { description = (PackageDescription) context.convertAnother(description, PackageDescription.class); } reader.moveUp(); } return new PackageImpl(description, serialization); }
@Override public URL resolvePackageUri(URI uri) throws MalformedURLException { if (uri.getScheme() != null && uri.getScheme().equals("file")) { return resolvePackageFileUri(uri).toURI().toURL(); } else { return uri.toURL(); } }
@Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { String subject = reader.getAttribute(A_SUBJECT); String predicate = reader.getAttribute(A_PREDICATE); String object = reader.getAttribute(A_OBJECT); Triple triple = new TripleImpl(subject, predicate, object); return triple; }
/** * Creates MetadataAttribute objects which then get added to MetadataAttributeSet and returned as a collection. * * @param parsedData * @return Collection<AttributeSet> */ protected Collection<AttributeSet> getAttributeSets(List<Pair<String, Checksum>> parsedData) { Collection<AttributeSet> result = new ArrayList<AttributeSet>(); //set up pair serializer PairStringSerializerImpl<String, Checksum> serializer = new PairStringSerializerImpl<String, Checksum>(); serializer.setSerializerT(new StringStringSerializerImpl()); serializer.setSerializerU(new ChecksumStringSerializerImpl()); MetadataAttributeSet manifest = new MetadataAttributeSet( MetadataAttributeSetName.BAGIT_METADATA); for (Pair<String, Checksum> pair : parsedData) { MetadataAttribute attribute = new MetadataAttribute(Metadata.BAGIT_CHECKSUM, MetadataAttributeType.PAIR, serializer.serialize(pair)); manifest.addAttribute(attribute); } result.add(manifest); return result; }
/** * Given the map of manifest file asserted file paths and checksums, along with the checksum algorithm, * this method returns a List of Pair objects, whose keys are the file path with file system's specific File Separators, * and values are ChecksumImpl objects. * * @param fileChecksumMap * @param alg * @return */ protected List<Pair<String, Checksum>> parseData(Map<String, String> fileChecksumMap, String alg) { List<Pair<String, Checksum>> checksumPairList = new ArrayList<Pair<String, Checksum>>(); for (String key : fileChecksumMap.keySet()) { String checksumValue = fileChecksumMap.get(key); key = key.replace("/", File.separator); checksumPairList.add(new Pair<String, Checksum>(key, new ChecksumImpl(alg, checksumValue))); } return checksumPairList; }
@Override public Checksum deserialize(String s) throws IllegalArgumentException { String algorithmString = null; String valueString = null; boolean delimiterFound = false; for (int i = 0, n = s.length(); i < n; i++) { char c = s.charAt(i); if (c == delimiter) { algorithmString = s.substring(0, i); valueString = s.substring(i + 1); delimiterFound = true; break; } } if(!delimiterFound){ throw new IllegalArgumentException("Could not deserialize the string " + s); } return new ChecksumImpl(algorithmString, valueString); }
/** * Creates MetadataAttribute objects which then get added to MetadataAttributeSet and returned as a collection. * * @param parsedData * @return Collection<AttributeSet> */ protected Collection<AttributeSet> getAttributeSets(List<Pair<String, Checksum>> parsedData) { Collection<AttributeSet> result = new ArrayList<AttributeSet>(); //set up pair serializer PairStringSerializerImpl<String, Checksum> serializer = new PairStringSerializerImpl<String, Checksum>(); serializer.setSerializerT(new StringStringSerializerImpl()); serializer.setSerializerU(new ChecksumStringSerializerImpl()); MetadataAttributeSet manifest = new MetadataAttributeSet( MetadataAttributeSetName.BAGIT_METADATA); for (Pair<String, Checksum> pair : parsedData) { MetadataAttribute attribute = new MetadataAttribute(Metadata.BAGIT_CHECKSUM, MetadataAttributeType.PAIR, serializer.serialize(pair)); manifest.addAttribute(attribute); } result.add(manifest); return result; }
checksum = new ChecksumImpl(algorithm, fixity);
@Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { String algorithm = reader.getAttribute(A_ALGORITHM); String value = reader.getAttribute(A_VALUE); Checksum checksum = new ChecksumImpl(algorithm, value); return checksum; }