Skip to content
This repository was archived by the owner on Jul 2, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -19,7 +19,6 @@
* under the License.
*/


import java.io.IOException;
import java.net.URI;
import java.nio.file.ClosedFileSystemException;
Expand Down Expand Up @@ -49,7 +48,6 @@ protected BundleFileSystem(FileSystem origFS, URI baseURI) {
this.baseURI = baseURI;
this.separator = origFS.getSeparator();
this.source = findSource();

}

@Override
Expand All @@ -58,26 +56,26 @@ public void close() throws IOException {
return;
}
origFS.close();
// De-reference the original ZIP file system so it can be
// garbage collected
origFS = null;
}

protected Path findSource() {
Path zipRoot = getRootDirectory().getZipPath();
URI uri = zipRoot.toUri();
String schemeSpecific;
if (provider().getJarDoubleEscaping()) {
schemeSpecific = uri.getSchemeSpecificPart();
} else {
// http://dev.mygrid.org.uk/issues/browse/T3-954
schemeSpecific = uri.getRawSchemeSpecificPart();
URI uri = this.baseURI;

if (uri.getScheme().equals("arcp")) {
// Handle arcp:// URI scheme separately
return Paths.get(uri.getPath());
}
if (!schemeSpecific.endsWith("!/")) { // sanity check
throw new IllegalStateException("Can't parse JAR URI: " + uri);

// Default handling for zip URIs
Path zipRoot = getRootDirectory().getZipPath();
String schemeSpecific = uri.getRawSchemeSpecificPart();

if (!schemeSpecific.endsWith("!/")) {
throw new IllegalStateException("Can't parse URI: " + uri);
}
URI zip = URI.create(schemeSpecific.substring(0,
schemeSpecific.length() - 2));

URI zip = URI.create(schemeSpecific.substring(0, schemeSpecific.length() - 2));
return Paths.get(zip); // Look up our path
}

Expand All @@ -86,21 +84,14 @@ public URI getBaseURI() {
}

protected BundleFileStore getFileStore() {
// We assume there's only one file store, as is true for ZipProvider
return new BundleFileStore(this, getOrigFS().getFileStores().iterator()
.next());
return new BundleFileStore(this, getOrigFS().getFileStores().iterator().next());
}

@Override
public Iterable<FileStore> getFileStores() {
return Collections.<FileStore> singleton(getFileStore());
return Collections.singleton(getFileStore());
}

/**
* Thread-safe ClosedFileSystemException test
*
* @return
*/
protected FileSystem getOrigFS() {
FileSystem orig = origFS;
if (orig == null || !orig.isOpen()) {
Expand All @@ -117,8 +108,7 @@ public Path getPath(String first, String... more) {

@Override
public PathMatcher getPathMatcher(String syntaxAndPattern) {
final PathMatcher zipMatcher = getOrigFS().getPathMatcher(
syntaxAndPattern);
final PathMatcher zipMatcher = getOrigFS().getPathMatcher(syntaxAndPattern);
return new PathMatcher() {
@Override
public boolean matches(Path path) {
Expand All @@ -129,7 +119,7 @@ public boolean matches(Path path) {

@Override
public Iterable<Path> getRootDirectories() {
return Collections.<Path> singleton(getRootDirectory());
return Collections.singleton(getRootDirectory());
}

public BundlePath getRootDirectory() {
Expand All @@ -152,10 +142,7 @@ public UserPrincipalLookupService getUserPrincipalLookupService() {

@Override
public boolean isOpen() {
if (origFS == null) {
return false;
}
return origFS.isOpen();
return origFS != null && origFS.isOpen();
}

@Override
Expand Down Expand Up @@ -183,13 +170,11 @@ public Set<String> supportedFileAttributeViews() {

protected Path unwrap(Path bundlePath) {
if (!(bundlePath instanceof BundlePath)) {
// assume it's already unwrapped for some reason (for instance being
// null)
return bundlePath;
}
return ((BundlePath) bundlePath).getZipPath();
}

protected static Path withoutSlash(Path dir) {
if (dir == null) {
return null;
Expand All @@ -198,7 +183,7 @@ protected static Path withoutSlash(Path dir) {
if (fname == null) // Root directory?
return dir;
String fnameStr = fname.toString();
if (! fnameStr.endsWith("/") && ! fnameStr.equals("/"))
if (!fnameStr.endsWith("/") && !fnameStr.equals("/"))
return dir;
return dir.resolveSibling(fnameStr.replace("/", ""));
}
Expand All @@ -208,10 +193,9 @@ protected BundlePath wrap(Path zipPath) {
return null;
}
if (zipPath instanceof BundlePath) {
throw new IllegalArgumentException("Did not expect BundlePath: "
+ zipPath);
throw new IllegalArgumentException("Did not expect BundlePath: " + zipPath);
}

return new BundlePath(this, withoutSlash(zipPath));
}

Expand All @@ -233,5 +217,4 @@ public void remove() {
}
};
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
package org.apache.taverna.robundle.fs;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
Expand All @@ -37,54 +17,52 @@
public class BundleFileTypeDetector extends FileTypeDetector {

private static final String APPLICATION_ZIP = "application/zip";
private static final Charset ASCII = Charset.forName("ASCII");
private static final Charset LATIN1 = Charset.forName("ISO-8859-1");
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final String MIMETYPE = "mimetype";
private static final String ZIP_MAGIC_NUMBER = "PK";

@Override
public String probeContentType(Path path) throws IOException {

ByteBuffer buf = ByteBuffer.allocate(256);
try (SeekableByteChannel byteChannel = Files.newByteChannel(path,
StandardOpenOption.READ)) {

try (SeekableByteChannel byteChannel = Files.newByteChannel(path, StandardOpenOption.READ)) {
int read = byteChannel.read(buf);
if (read < 38) {
return null;
}
;
}
buf.flip();

// Look for PK

buf.flip();
byte[] firstBytes = buf.array();
String pk = new String(firstBytes, 0, 2, LATIN1);
if (!(pk.equals("PK") && firstBytes[2] == 3 && firstBytes[3] == 4)) {
// Did not match magic numbers of ZIP as specified in ePub OCF
// http://www.idpf.org/epub/30/spec/epub30-ocf.html#app-media-type

// Look for ZIP magic number ("PK")
String pk = new String(firstBytes, 0, 2, UTF_8);
if (!pk.equals(ZIP_MAGIC_NUMBER) || firstBytes[2] != 3 || firstBytes[3] != 4) {
// Not a ZIP file
return null;
}

String mimetype = new String(firstBytes, 30, 8, LATIN1);
String mimetype = new String(firstBytes, 30, 8, UTF_8);
if (!mimetype.equals(MIMETYPE)) {
return APPLICATION_ZIP;
}
// Read the 'mimetype' file.
try (ZipInputStream is = new ZipInputStream(new ByteArrayInputStream(
firstBytes))) {
ZipEntry entry = is.getNextEntry();
if (!MIMETYPE.equals(entry.getName())) {

// Read the 'mimetype' file from the ZIP
try (ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(firstBytes))) {
ZipEntry entry = zipStream.getNextEntry();
if (entry == null || !MIMETYPE.equals(entry.getName())) {
return APPLICATION_ZIP;
}

byte[] mediaTypeBuffer = new byte[256];
int size = is.read(mediaTypeBuffer);
int size = zipStream.read(mediaTypeBuffer);
if (size < 1) {
return APPLICATION_ZIP;
}
return new String(mediaTypeBuffer, 0, size, ASCII);
return new String(mediaTypeBuffer, 0, size, UTF_8);
} catch (ZipException | ZipError e) {
// Log the error (optional)
return null;
}
}

}
Loading