Search in sources :

Example 1 with ProbeResult

use of org.apache.sis.storage.ProbeResult in project sis by apache.

the class StoreProviderTest method testProbeContentFromReader.

/**
 * Implementation of {@link #testProbeContentFromReader()}.
 */
private static void testProbeContentFromReader(final boolean isSupported, final int version, final StoreProvider p, final String wkt) throws DataStoreException {
    final StorageConnector c = new StorageConnector(new StringReader(wkt));
    final ProbeResult r = p.probeContent(c);
    c.closeAllExcept(null);
    assertEquals("isSupported", isSupported, r.isSupported());
    if (isSupported) {
        assertEquals("mimeType", "application/wkt", r.getMimeType());
        assertEquals("version", version, r.getVersion().getMajor());
    }
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) StringReader(java.io.StringReader) ProbeResult(org.apache.sis.storage.ProbeResult)

Example 2 with ProbeResult

use of org.apache.sis.storage.ProbeResult in project sis by apache.

the class AbstractProvider method probeContent.

/**
 * Returns {@link ProbeResult#SUPPORTED} if the given storage appears to be supported by the data store.
 * Returning {@code SUPPORTED} from this method does not guarantee that reading or writing will succeed,
 * only that there appears to be a reasonable chance of success based on a brief inspection of the storage
 * header.
 *
 * @return {@link ProbeResult#SUPPORTED} if the given storage seems to be readable as a XML file.
 * @throws DataStoreException if an I/O or SQL error occurred.
 */
@Override
public ProbeResult probeContent(final StorageConnector connector) throws DataStoreException {
    /*
         * Usual case. This includes InputStream, DataInput, File, Path, URL, URI.
         */
    final ByteBuffer buffer = connector.getStorageAs(ByteBuffer.class);
    if (buffer != null) {
        if (buffer.remaining() < HEADER.length) {
            return ProbeResult.INSUFFICIENT_BYTES;
        }
        // Quick check for "<?xml " header.
        for (int i = 0; i < HEADER.length; i++) {
            if (buffer.get(i) != HEADER[i]) {
                return ProbeResult.UNSUPPORTED_STORAGE;
            }
        }
        // Now check for a more accurate MIME type.
        buffer.position(HEADER.length);
        final ProbeResult result = new MimeTypeDetector(types) {

            @Override
            int read() {
                if (buffer.hasRemaining()) {
                    return buffer.get();
                }
                insufficientBytes = (buffer.limit() != buffer.capacity());
                return -1;
            }
        }.probeContent();
        buffer.position(0);
        return result;
    }
    /*
         * We should enter in this block only if the user gave us explicitely a Reader.
         * A common case is a StringReader wrapping a String object.
         */
    final Reader reader = connector.getStorageAs(Reader.class);
    if (reader != null)
        try {
            // Quick check for "<?xml " header.
            reader.mark(HEADER.length + READ_AHEAD_LIMIT);
            for (int i = 0; i < HEADER.length; i++) {
                if (reader.read() != HEADER[i]) {
                    reader.reset();
                    return ProbeResult.UNSUPPORTED_STORAGE;
                }
            }
            // Now check for a more accurate MIME type.
            final ProbeResult result = new MimeTypeDetector(types) {

                private int remaining = READ_AHEAD_LIMIT;

                @Override
                int read() throws IOException {
                    return (--remaining >= 0) ? IOUtilities.readCodePoint(reader) : -1;
                }
            }.probeContent();
            reader.reset();
            return result;
        } catch (IOException e) {
            throw new DataStoreException(e);
        }
    return ProbeResult.UNSUPPORTED_STORAGE;
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Reader(java.io.Reader) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ProbeResult(org.apache.sis.storage.ProbeResult)

Example 3 with ProbeResult

use of org.apache.sis.storage.ProbeResult in project sis by apache.

the class MimeTypeDetector method probeContent.

/**
 * Wraps the call to {@link #getMimeType()} for catching {@link IOException} and for
 * instantiating the {@link ProbeResult}.
 */
final ProbeResult probeContent() throws DataStoreException {
    boolean isSupported = true;
    String mimeType;
    try {
        mimeType = getMimeType();
    } catch (IOException e) {
        throw new DataStoreException(e);
    }
    if (mimeType == null) {
        if (insufficientBytes) {
            return ProbeResult.INSUFFICIENT_BYTES;
        }
        isSupported = false;
        mimeType = StoreProvider.MIME_TYPE;
    }
    return new ProbeResult(isSupported, mimeType, null);
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) ProbeResult(org.apache.sis.storage.ProbeResult)

Example 4 with ProbeResult

use of org.apache.sis.storage.ProbeResult in project sis by apache.

the class NetcdfStoreProvider method probeContent.

/**
 * Returns {@link ProbeResult#SUPPORTED} if the given storage appears to be supported by {@link NetcdfStore}.
 * Returning {@code SUPPORTED} from this method does not guarantee that reading or writing will succeed,
 * only that there appears to be a reasonable chance of success based on a brief inspection of the
 * {@linkplain StorageConnector#getStorage() storage object} or contents.
 *
 * @param  connector  information about the storage (URL, stream, {@link ucar.nc2.NetcdfFile} instance, <i>etc</i>).
 * @return {@code SUPPORTED} if the given storage seems to be usable by {@code NetcdfStore} instances.
 * @throws DataStoreException if an I/O error occurred.
 */
@Override
public ProbeResult probeContent(final StorageConnector connector) throws DataStoreException {
    int version = 0;
    boolean hasVersion = false;
    boolean isSupported = false;
    final ByteBuffer buffer = connector.getStorageAs(ByteBuffer.class);
    if (buffer != null) {
        if (buffer.remaining() < Integer.BYTES) {
            return ProbeResult.INSUFFICIENT_BYTES;
        }
        final int header = buffer.getInt(buffer.position());
        if ((header & 0xFFFFFF00) == ChannelDecoder.MAGIC_NUMBER) {
            hasVersion = true;
            version = header & 0xFF;
            isSupported = (version >= 1 && version <= ChannelDecoder.MAX_VERSION);
        }
    }
    /*
         * If we failed to check using the embedded decoder, tries using the UCAR library.
         * The UCAR library is an optional dependency. If that library is present and the
         * input is a String, then the following code may trigs a large amount of classes
         * loading.
         *
         * Note that the UCAR library expects a String argument, not a File, because it
         * has special cases for "file:", "http:", "nodods:" and "slurp:" protocols.
         */
    if (!isSupported) {
        final String path = connector.getStorageAs(String.class);
        if (path != null) {
            ensureInitialized(false);
            final Method method = canOpenFromPath;
            if (method != null)
                try {
                    isSupported = (Boolean) method.invoke(null, path);
                } catch (IllegalAccessException e) {
                    // Should never happen, since the method is public.
                    throw (Error) new IncompatibleClassChangeError("canOpen").initCause(e);
                } catch (InvocationTargetException e) {
                    final Throwable cause = e.getCause();
                    if (cause instanceof DataStoreException)
                        throw (DataStoreException) cause;
                    if (cause instanceof RuntimeException)
                        throw (RuntimeException) cause;
                    if (cause instanceof Error)
                        throw (Error) cause;
                    if (cause instanceof FileNotFoundException || cause instanceof NoSuchFileException) {
                        /*
                         * Happen if the String argument uses any protocol not recognized by the UCAR library,
                         * in which case UCAR tries to open it as a file even if it is not a file. For example
                         * we get this exception for "jar:file:/file.jar!/entry.nc".
                         */
                        Logging.recoverableException(Logging.getLogger(Modules.NETCDF), netcdfFileClass, "canOpen", cause);
                        return ProbeResult.UNSUPPORTED_STORAGE;
                    }
                    // The cause may be IOException.
                    throw new DataStoreException(e);
                }
        } else {
            /*
                 * Check if the given input is itself an instance of the UCAR oject.
                 * We check classnames instead of netcdfFileClass.isInstance(storage)
                 * in order to avoid loading the UCAR library if not needed.
                 */
            for (Class<?> type = connector.getStorage().getClass(); type != null; type = type.getSuperclass()) {
                if (UCAR_CLASSNAME.equals(type.getName())) {
                    isSupported = true;
                    break;
                }
            }
        }
    }
    /*
         * At this point, the readability status has been determined. The file version number
         * is unknown if we are able to open the file only through the UCAR library.
         */
    if (hasVersion) {
        return new ProbeResult(isSupported, MIME_TYPE, Version.valueOf(version));
    }
    return isSupported ? new ProbeResult(true, MIME_TYPE, null) : ProbeResult.UNSUPPORTED_STORAGE;
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) Method(java.lang.reflect.Method) ByteBuffer(java.nio.ByteBuffer) InvocationTargetException(java.lang.reflect.InvocationTargetException) ProbeResult(org.apache.sis.storage.ProbeResult)

Example 5 with ProbeResult

use of org.apache.sis.storage.ProbeResult in project sis by apache.

the class NetcdfStoreProviderTest method testProbeContentFromStream.

/**
 * Tests {@link NetcdfStoreProvider#probeContent(StorageConnector)} for an input stream which shall
 * be recognized as a classic netCDF file.
 *
 * @throws DataStoreException if a logical error occurred.
 */
@Test
public void testProbeContentFromStream() throws DataStoreException {
    final StorageConnector c = new StorageConnector(IOTestCase.getResourceAsStream(NCEP));
    final NetcdfStoreProvider provider = new NetcdfStoreProvider();
    final ProbeResult probe = provider.probeContent(c);
    assertTrue("isSupported", probe.isSupported());
    assertEquals("getMimeType", NetcdfStoreProvider.MIME_TYPE, probe.getMimeType());
    assertEquals("getVersion", new Version("1"), probe.getVersion());
    c.closeAllExcept(null);
}
Also used : StorageConnector(org.apache.sis.storage.StorageConnector) Version(org.apache.sis.util.Version) ProbeResult(org.apache.sis.storage.ProbeResult) ChannelDecoderTest(org.apache.sis.internal.netcdf.impl.ChannelDecoderTest) Test(org.junit.Test)

Aggregations

ProbeResult (org.apache.sis.storage.ProbeResult)8 StorageConnector (org.apache.sis.storage.StorageConnector)4 ByteBuffer (java.nio.ByteBuffer)3 DataStoreException (org.apache.sis.storage.DataStoreException)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ChannelDecoderTest (org.apache.sis.internal.netcdf.impl.ChannelDecoderTest)2 FileNotFoundException (java.io.FileNotFoundException)1 Reader (java.io.Reader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ByteOrder (java.nio.ByteOrder)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Version (org.apache.sis.util.Version)1 NetcdfFile (ucar.nc2.NetcdfFile)1