/*
 * Copyright (c) 2003 Alex Leigh, All Rights Reserved
 * 
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ALEX LEIGH The copyright
 * notice above does not evidence any actual or intended publication of such
 * source code.
 * 
 * Use, disclosure, copying, distribution, or transmission of this code without
 * express permission is prohibited.
 */

#include "com_ashpool_continuity_ContRequest.h"
#include <continuity.h>
#include <http.h>

JNIEXPORT void JNICALL
    Java_com_ashpool_continuity_ContRequest_setResponseTuple(JNIEnv * env,
                                                             jclass class,
                                                             jstring key,
                                                             jstring value)
{
    httpTtrans *t = httpFthread_gettrans();
    jbyte *kstr;
    jbyte *vstr;

    if (t == NULL) {
        logFmsg(3, "mod/java: entrance thread had an orphaned context.");
        return;
    }
    kstr = (jbyte *) (*env)->GetStringUTFChars(env, key, NULL);
    vstr = (jbyte *) (*env)->GetStringUTFChars(env, value, NULL);

    logFmsg(3, "Setting %s/%s", kstr, vstr);

    lstFset_update(t->res_hdrs, (char *) kstr, (char *) vstr);

    (*env)->ReleaseStringUTFChars(env, key, (const char *) kstr);
    (*env)->ReleaseStringUTFChars(env, value, (const char *) vstr);

    return;
}


JNIEXPORT jstring JNICALL
    Java_com_ashpool_continuity_ContRequest_getResponseTuple(JNIEnv * env,
                                                             jclass class,
                                                             jstring key) {
    httpTtrans *t;
    char *v;
    const jbyte *kstr;

    logFmsg(3, "getResponseTuple() called.");

    t == httpFthread_gettrans();

    if (t == NULL) {
        logFmsg(3, "mod/java: entrance thread had an orphaned context.");
        return;
    }
    kstr = (jbyte *) (*env)->GetStringUTFChars(env, key, NULL);

    v = lstFset_get(t->res_hdrs, (char *) kstr);

    (*env)->ReleaseStringUTFChars(env, key, (const char *) kstr);

    if (v == NULL)
        return NULL;

    return (*env)->NewStringUTF(env, v);
}

JNIEXPORT void JNICALL
    Java_com_ashpool_continuity_ContRequest_setRequestTuple(JNIEnv * env,
                                                            jclass class,
                                                            jstring key,
                                                            jstring value)
{
    httpTtrans *t = httpFthread_gettrans();
    jbyte *kstr;
    jbyte *vstr;


    logFmsg(3, "setRequestTuple() called.");

    if (t == NULL) {
        logFmsg(3, "mod/java: entrance thread had an orphaned context.");
        return;
    }
    kstr = (jbyte *) (*env)->GetStringUTFChars(env, key, NULL);
    vstr = (jbyte *) (*env)->GetStringUTFChars(env, value, NULL);

    logFmsg(3, "Setting %s/%s", kstr, vstr);

    lstFset_update(t->req_hdrs, (char *) kstr, (char *) vstr);

    (*env)->ReleaseStringUTFChars(env, key, (const char *) kstr);
    (*env)->ReleaseStringUTFChars(env, value, (const char *) vstr);

    return;
}


JNIEXPORT jstring JNICALL
    Java_com_ashpool_continuity_ContRequest_getRequestTuple(JNIEnv * env,
                                                            jclass class,
                                                            jstring key) {
    httpTtrans *t;
    char *v;
    const jbyte *kstr;

    logFmsg(3, "getRequestTuple() called.");

    t = httpFthread_gettrans();


    if (t == NULL) {
        logFmsg(3, "mod/java: entrance thread had an orphaned context.");
        return;
    }
    kstr = (jbyte *) (*env)->GetStringUTFChars(env, key, NULL);

    v = lstFset_get(t->req_hdrs, (char *) kstr);

    (*env)->ReleaseStringUTFChars(env, key, (const char *) kstr);

    if (v == NULL)
        return NULL;

    return (*env)->NewStringUTF(env, v);
}

JNIEXPORT void JNICALL Java_com_ashpool_continuity_ContRequest_setVarTuple
    (JNIEnv * env, jclass class, jstring key, jstring value) {
    httpTtrans *t = httpFthread_gettrans();
    jbyte *kstr;
    jbyte *vstr;

    if (t == NULL) {
        logFmsg(3, "mod/java: entrance thread had an orphaned context.");
        return;
    }
    kstr = (jbyte *) (*env)->GetStringUTFChars(env, key, NULL);
    vstr = (jbyte *) (*env)->GetStringUTFChars(env, value, NULL);

    logFmsg(3, "Setting %s/%s", kstr, vstr);

    lstFset_update(t->vars, (char *) kstr, (char *) vstr);

    (*env)->ReleaseStringUTFChars(env, key, (const char *) kstr);
    (*env)->ReleaseStringUTFChars(env, value, (const char *) vstr);

    return;
}


JNIEXPORT jstring JNICALL
    Java_com_ashpool_continuity_ContRequest_getVarTuple(JNIEnv * env,
                                                        jclass class,
                                                        jstring key) {
    httpTtrans *t = httpFthread_gettrans();
    char *v;
    const jbyte *kstr;

    logFmsg(3, "getVarTuple() called.");

    if (t == NULL) {
        logFmsg(3, "mod/java: entrance thread had an orphaned context.");
        return;
    }
    kstr = (jbyte *) (*env)->GetStringUTFChars(env, key, NULL);

    v = lstFset_get(t->vars, (char *) kstr);

    (*env)->ReleaseStringUTFChars(env, key, (const char *) kstr);

    if (v == NULL)
        return NULL;

    return (*env)->NewStringUTF(env, v);
}

JNIEXPORT void JNICALL Java_com_ashpool_continuity_ContRequest_write___3BI
    (JNIEnv * env, jclass class, jbyteArray bytes, jint length) {

    httpTtrans *t = httpFthread_gettrans();

    logFmsg(3, "write() called.");

    if (t == NULL) {
        logFmsg(3, "mod/java: entrance thread had an orphaned context.");
        return;
    }
    httpFwrite(t, (char *) bytes, length);
}

JNIEXPORT void JNICALL Java_com_ashpool_continuity_ContRequest_logMessage
    (JNIEnv * env, jclass class, jint level, jstring msg) {
    char buf[128];
    const jbyte *str;

    logFmsg(3, "logMessage() called.");

    str = (jbyte *) (*env)->GetStringUTFChars(env, msg, NULL);
    if (str == NULL)
        return;

    logFmsg(level, (char *) str);

    (*env)->ReleaseStringUTFChars(env, msg, (const char *) str);

    return;
}

JNIEXPORT void JNICALL
    Java_com_ashpool_continuity_ContRequest_startResponse(JNIEnv * env,
                                                          jclass class,
                                                          jint status) {
    httpTtrans *t = httpFthread_gettrans();

    logFmsg(3, "startResponse() called.");

    if (t == NULL) {
        logFmsg(3, "mod/java: entrance thread had an orphaned context.");
        return;
    }
    logFmsg(3, "Setting status to %d and starting..", status);

    httpFset_status(t, status, NULL);
    httpFstart_response(t);

    return;
}

JNIEXPORT void JNICALL
    Java_com_ashpool_continuity_ContRequest_write__Ljava_lang_String_2
    (JNIEnv * env, jclass class, jstring strv) {
    const jbyte *str;
    httpTtrans *t = httpFthread_gettrans();

    if (t == NULL) {
        logFmsg(3, "mod/java: entrance thread had an orphaned context.");
        return;
    }
    str = (jbyte *) (*env)->GetStringUTFChars(env, strv, NULL);
    if (str == NULL)
        return;

    httpFwrite(t, (char *) str, strlen(str));

    (*env)->ReleaseStringUTFChars(env, strv, (const char *) str);
    return;
}


/* Let them come - There is still one dwarf in Moria who yet draws breath */

/*
 * Class:     com_ashpool_continuity_ContRequest Method:    glueObject
 * Signature: (Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;)V
 */

/* obj method name */

JNIEXPORT void JNICALL Java_com_ashpool_continuity_ContRequest_glueObject
    (JNIEnv * env, jclass class, jobject obj, jstring method,
     jstring name) {

    /*
     * right now we don't support any other container systems, so we are going
     * to go ahead and just set this object as the catalina connector in
     * modjava.
     */

    /* javaFsetCatalinaConnector(env,obj); */

    return;
}
