Search in sources :

Example 1 with INTERNET_CACHE_ENTRY_INFO

use of com.sun.jna.platform.win32.Wininet.INTERNET_CACHE_ENTRY_INFO in project jna by java-native-access.

the class WininetUtil method getCache.

/**
     * Helper function for traversing wininet's cache and returning all entries.
     * <br>
     * Some entries are cookies, some entries are history items, and some are
     * actual files.<br>
     * 
     * @return A map of cache URL => local file (or URL => empty string for
     *         cookie and history entries)
     */
public static Map<String, String> getCache() {
    List<INTERNET_CACHE_ENTRY_INFO> items = new ArrayList<Wininet.INTERNET_CACHE_ENTRY_INFO>();
    HANDLE cacheHandle = null;
    Win32Exception we = null;
    int lastError = 0;
    // return
    Map<String, String> cacheItems = new LinkedHashMap<String, String>();
    try {
        IntByReference size = new IntByReference();
        // for every entry, we call the API twice:
        // once to get the size into the IntByReference
        // then again to get the actual item
        cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, null, size);
        lastError = Native.getLastError();
        // if there's nothing in the cache, we're done.
        if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
            return cacheItems;
        } else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {
            throw new Win32Exception(lastError);
        }
        INTERNET_CACHE_ENTRY_INFO entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
        cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, entry, size);
        if (cacheHandle == null) {
            throw new Win32Exception(Native.getLastError());
        }
        items.add(entry);
        while (true) {
            size = new IntByReference();
            // for every entry, we call the API twice:
            // once to get the size into the IntByReference
            // then again to get the actual item
            boolean result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, null, size);
            if (!result) {
                lastError = Native.getLastError();
                if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
                    break;
                } else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {
                    throw new Win32Exception(lastError);
                }
            }
            entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
            result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, entry, size);
            if (!result) {
                lastError = Native.getLastError();
                if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
                    break;
                } else if (lastError != WinError.ERROR_SUCCESS && lastError != WinError.ERROR_INSUFFICIENT_BUFFER) {
                    throw new Win32Exception(lastError);
                }
            }
            items.add(entry);
        }
        for (INTERNET_CACHE_ENTRY_INFO item : items) {
            cacheItems.put(item.lpszSourceUrlName.getWideString(0), item.lpszLocalFileName == null ? "" : item.lpszLocalFileName.getWideString(0));
        }
    } catch (Win32Exception e) {
        we = e;
    } finally {
        if (cacheHandle != null) {
            if (!Wininet.INSTANCE.FindCloseUrlCache(cacheHandle)) {
                if (we != null) {
                    Win32Exception e = new Win32Exception(Native.getLastError());
                    e.addSuppressed(we);
                    we = e;
                }
            }
        }
    }
    if (we != null) {
        throw we;
    }
    return cacheItems;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ArrayList(java.util.ArrayList) INTERNET_CACHE_ENTRY_INFO(com.sun.jna.platform.win32.Wininet.INTERNET_CACHE_ENTRY_INFO) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with INTERNET_CACHE_ENTRY_INFO

use of com.sun.jna.platform.win32.Wininet.INTERNET_CACHE_ENTRY_INFO in project jna by java-native-access.

the class WininetTest method testFindNextUrlCacheEntry.

@Test
public void testFindNextUrlCacheEntry() {
    HANDLE cacheHandle = null;
    try {
        IntByReference size = new IntByReference();
        int lastError = 0;
        // for every entry, we call the API twice:
        // once to get the size into the IntByReference
        // then again to get the actual item
        cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, null, size);
        lastError = Native.getLastError();
        assertNull("FindFirstUrlCacheEntry should have returned null.", cacheHandle);
        // just to ensure the mapping gets tested
        if (lastError == WinError.ERROR_NO_MORE_ITEMS) {
            boolean result = Wininet.INSTANCE.FindNextUrlCacheEntry(null, null, size);
            lastError = Native.getLastError();
            assertFalse("FindNextUrlCacheEntry should have returned false.", result);
            assertEquals("GetLastError should have returned ERROR_INVALID_PARAMETER.", WinError.ERROR_INVALID_PARAMETER, lastError);
            return;
        }
        assertEquals("GetLastError should have returned ERROR_INSUFFICIENT_BUFFER.", WinError.ERROR_INSUFFICIENT_BUFFER, lastError);
        INTERNET_CACHE_ENTRY_INFO entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
        cacheHandle = Wininet.INSTANCE.FindFirstUrlCacheEntry(null, entry, size);
        lastError = Native.getLastError();
        assertNotNull("FindFirstUrlCacheEntry should not have returned null.", cacheHandle);
        assertEquals("GetLastError should have returned ERROR_SUCCESS.", WinError.ERROR_SUCCESS, lastError);
        size = new IntByReference();
        // for every entry, we call the API twice:
        // once to get the size into the IntByReference
        // then again to get the actual item
        boolean result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, null, size);
        lastError = Native.getLastError();
        assertFalse("FindNextUrlCacheEntry should have returned false.", result);
        assertEquals("GetLastError should have returned ERROR_INSUFFICIENT_BUFFER.", WinError.ERROR_INSUFFICIENT_BUFFER, lastError);
        entry = new INTERNET_CACHE_ENTRY_INFO(size.getValue());
        result = Wininet.INSTANCE.FindNextUrlCacheEntry(cacheHandle, entry, size);
        lastError = Native.getLastError();
        assertTrue("FindNextUrlCacheEntry should have returned true.", result);
        assertEquals("GetLastError should have returned ERROR_SUCCESS.", WinError.ERROR_SUCCESS, lastError);
    } finally {
        if (cacheHandle != null) {
            if (!Wininet.INSTANCE.FindCloseUrlCache(cacheHandle)) {
                throw new Win32Exception(Native.getLastError());
            }
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) INTERNET_CACHE_ENTRY_INFO(com.sun.jna.platform.win32.Wininet.INTERNET_CACHE_ENTRY_INFO) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

Aggregations

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)2 INTERNET_CACHE_ENTRY_INFO (com.sun.jna.platform.win32.Wininet.INTERNET_CACHE_ENTRY_INFO)2 IntByReference (com.sun.jna.ptr.IntByReference)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1