Java Code Examples for android.webkit.WebResourceResponse#getData()

The following examples show how to use android.webkit.WebResourceResponse#getData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Web3View.java    From Web3View with GNU General Public License v3.0 6 votes vote down vote up
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    WebResourceResponse response = externalClient.shouldInterceptRequest(view, request);
    if (response != null) {
        try {
            InputStream in = response.getData();
            int len = in.available();
            byte[] data = new byte[len];
            int readLen = in.read(data);
            if (readLen == 0) {
                throw new IOException("Nothing is read.");
            }
            String injectedHtml = jsInjectorClient.injectJS(new String(data));
            response.setData(new ByteArrayInputStream(injectedHtml.getBytes()));
        } catch (IOException ex) {
            Log.d("INJECT AFTER_EXTRNAL", "", ex);
        }
    } else {
        response = internalClient.shouldInterceptRequest(view, request);
    }
    return response;
}
 
Example 2
Source File: Web3View.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    WebResourceResponse response = externalClient.shouldInterceptRequest(view, request);
    if (response != null) {
        try {
            InputStream in = response.getData();
            int len = in.available();
            byte[] data = new byte[len];
            int readLen = in.read(data);
            if (readLen == 0) {
                throw new IOException("Nothing is read.");
            }
            String injectedHtml = jsInjectorClient.injectJS(new String(data));
            response.setData(new ByteArrayInputStream(injectedHtml.getBytes()));
        } catch (IOException ex) {
            Log.d("INJECT AFTER_EXTRNAL", "", ex);
        }
    } else {
        response = internalClient.shouldInterceptRequest(view, request);
    }
    return response;
}
 
Example 3
Source File: BaseInterceptor.java    From HybridCache with MIT License 5 votes vote down vote up
@Override
public WebResourceResponse intercept(Chain chain) {
    final String url = chain.getRequestUrl();
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    InputStream inputStream = null;
    // if the current interceptor doesn't intercept the current request,pass to the next interceptor
    if (!isIntercept(url, chain.getRequestHeaders())) {
        return chain.proceed(url, chain.getRequestHeaders());
    }
    if (cacheProvider != null && !cacheProvider.isClosed()) {
        inputStream = cacheProvider.get(url);
    }
    // if cache existed,we return the cache directly
    // in this case,the other interceptors had no opportunity to run
    if (inputStream != null) {
        return new WebResourceResponse(mimeType, "UTF-8", inputStream);
    }
    // init the downloader to download the res
    HttpConnectionDownloader downloader = HttpConnectionDownloader.getInstance();
    downloader.setConnectTimeout(getResConnectTimeout());
    downloader.setReadTimeout(getResReadTimeout());
    HttpConnectionDownloader.DownloadResult result = downloader.downloadRes(chain.getRequestUrl(), chain.getRequestHeaders());
    // it seems that something wrong happened as the response code>=400
    if (result != null && result.responseCode >= 400 && onErrorListener != null) {
        dispatchError(url, result.responseCode, result.responseMsg);
    }
    WebResourceResponse response = buildWebResponse(result);
    // use a Proxy mode,wrap the source stream with a FileWriter,so that we can store the resource in the cache as long as the stream is read
    if (response != null && response.getData() != null) {
        String tempPath = String.format("%s%s%s", tempDir, File.separator, MD5.md5(url));
        TmpFileWriteListener listener = new TmpFileWriteListener(cacheProvider, url);
        InputStream wrapperInputStream = new WebResInputStreamWrapper(response.getData(), new TempFileWriter(tempPath, listener));
        response.setData(wrapperInputStream);
    }
    return response;

}