[JNI] Allow android to know that an article is a redirect.

Android need to handle the redirection by doing a redirection in the web
view, not by providing the content of the targeted article.

This is already what we do in kiwix-serve or ios.

The API should be far better by returning a Entry but for now,
we just change the given url if the article is a redirection.
This commit is contained in:
Matthieu Gautier 2019-08-12 13:03:20 +02:00
parent 513cc9c90f
commit a65e192f0f
3 changed files with 32 additions and 8 deletions

View File

@ -225,7 +225,7 @@ JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getMimeType(
} }
JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent( JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent(
JNIEnv* env, jobject obj, jstring url, jobject titleObj, jobject mimeTypeObj, jobject sizeObj) JNIEnv* env, jobject obj, jobject url, jobject titleObj, jobject mimeTypeObj, jobject sizeObj)
{ {
/* Default values */ /* Default values */
setStringObjValue("", titleObj, env); setStringObjValue("", titleObj, env);
@ -234,21 +234,24 @@ JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent(
jbyteArray data = env->NewByteArray(0); jbyteArray data = env->NewByteArray(0);
/* Retrieve the content */ /* Retrieve the content */
std::string cUrl = jni2c(url, env); std::string cUrl = getStringObjValue(url, env);
unsigned int cSize = 0; unsigned int cSize = 0;
try { try {
auto entry = READER->getEntryFromEncodedPath(cUrl); auto entry = READER->getEntryFromEncodedPath(cUrl);
bool isRedirect = entry.isRedirect();
entry = entry.getFinalEntry(); entry = entry.getFinalEntry();
cSize = entry.getSize(); cSize = entry.getSize();
setIntObjValue(cSize, sizeObj, env); setIntObjValue(cSize, sizeObj, env);
setStringObjValue(entry.getMimetype(), mimeTypeObj, env);
setStringObjValue(entry.getTitle(), titleObj, env);
if (isRedirect) {
setStringObjValue(entry.getPath(), url, env);
} else {
data = env->NewByteArray(cSize); data = env->NewByteArray(cSize);
env->SetByteArrayRegion( env->SetByteArrayRegion(
data, 0, cSize, reinterpret_cast<const jbyte*>(entry.getBlob().data())); data, 0, cSize, reinterpret_cast<const jbyte*>(entry.getBlob().data()));
}
setStringObjValue(entry.getMimetype(), mimeTypeObj, env);
setStringObjValue(entry.getTitle(), titleObj, env);
} catch (std::exception& e) { } catch (std::exception& e) {
__android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get content for url: %s", cUrl.c_str()); __android_log_print(ANDROID_LOG_ERROR, "kiwix", "Unable to get content for url: %s", cUrl.c_str());
__android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what()); __android_log_print(ANDROID_LOG_ERROR, "kiwix", e.what());

View File

@ -38,7 +38,21 @@ public class JNIKiwixReader
public native String getMimeType(String url); public native String getMimeType(String url);
public native byte[] getContent(String url, /**
* Get the content of a article.
*
* Return a byte array of the content of the article.
* Set the title, mimeType to the title and mimeType of the article.
* Set the size to the size of the returned array.
*
* If the entry doesn't exist :
* - return a empty byte array
* - set all arguments (except url) to empty/0.
* If the entry exist but is a redirection :
* - return an empty byte array
* - set all arguments (including url) to information of the targeted article.
*/
public native byte[] getContent(JNIKiwixString url,
JNIKiwixString title, JNIKiwixString title,
JNIKiwixString mimeType, JNIKiwixString mimeType,
JNIKiwixInt size); JNIKiwixInt size);

View File

@ -114,6 +114,13 @@ inline std::string jni2c(const jstring& val, JNIEnv* env)
inline int jni2c(const jint val) { return (int)val; } inline int jni2c(const jint val) { return (int)val; }
/* Method to deal with variable passed by reference */ /* Method to deal with variable passed by reference */
inline std::string getStringObjValue(const jobject obj, JNIEnv* env)
{
jclass objClass = env->GetObjectClass(obj);
jfieldID objFid = env->GetFieldID(objClass, "value", "Ljava/lang/String;");
jstring jstr = (jstring)env->GetObjectField(obj, objFid);
return jni2c(jstr, env);
}
inline void setStringObjValue(const std::string& value, inline void setStringObjValue(const std::string& value,
const jobject obj, const jobject obj,
JNIEnv* env) JNIEnv* env)