|
Removed
Link Here
|
| 1 |
/* |
| 2 |
* Copyright 2008 Oakland Software Incorporated and others |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Oakland Software Incorporated - initial API and implementation |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <jni.h> |
| 13 |
|
| 14 |
#include <glib.h> |
| 15 |
#include <glib/gslist.h> |
| 16 |
#include <gconf/gconf-value.h> |
| 17 |
#include <gconf/gconf-client.h> |
| 18 |
|
| 19 |
#ifdef __linux__ |
| 20 |
#include <string.h> |
| 21 |
#else |
| 22 |
#include <strings.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
static GConfClient *client= NULL; |
| 26 |
|
| 27 |
static jclass proxyInfoClass; |
| 28 |
static jclass stringClass; |
| 29 |
static jmethodID proxyInfoConstructor; |
| 30 |
static jmethodID toString; |
| 31 |
|
| 32 |
static jmethodID hostMethod; |
| 33 |
static jmethodID portMethod; |
| 34 |
static jmethodID requiresAuthenticationMethod; |
| 35 |
static jmethodID userMethod; |
| 36 |
static jmethodID passwordMethod; |
| 37 |
|
| 38 |
#define CHECK_NULL(X) { if ((X) == NULL) fprintf (stderr,"JNI error at line %d\n", __LINE__); } |
| 39 |
|
| 40 |
static void gconfInit(JNIEnv *env) { |
| 41 |
client = gconf_client_get_default(); |
| 42 |
jclass cls= NULL; |
| 43 |
CHECK_NULL(cls = (*env)->FindClass(env, "org/eclipse/core/internal/net/ProxyData")); |
| 44 |
proxyInfoClass = (*env)->NewGlobalRef(env, cls); |
| 45 |
|
| 46 |
CHECK_NULL(cls = (*env)->FindClass(env, "java/lang/String")); |
| 47 |
stringClass = (*env)->NewGlobalRef(env, cls); |
| 48 |
|
| 49 |
CHECK_NULL(proxyInfoConstructor = (*env)->GetMethodID(env, proxyInfoClass, "<init>", "(Ljava/lang/String;)V")); |
| 50 |
|
| 51 |
CHECK_NULL(toString = (*env)->GetMethodID(env, proxyInfoClass, "toString", "()Ljava/lang/String;")); |
| 52 |
|
| 53 |
CHECK_NULL(hostMethod = (*env)->GetMethodID(env, proxyInfoClass, "setHost", |
| 54 |
"(Ljava/lang/String;)V")); |
| 55 |
CHECK_NULL(portMethod = (*env)->GetMethodID(env, proxyInfoClass, "setPort", |
| 56 |
"(I)V")); |
| 57 |
CHECK_NULL(requiresAuthenticationMethod= (*env)->GetMethodID(env, proxyInfoClass, "setRequiresAuthentication", |
| 58 |
"(Z)V")); |
| 59 |
CHECK_NULL(userMethod = (*env)->GetMethodID(env, proxyInfoClass, "setUserid", |
| 60 |
"(Ljava/lang/String;)V")); |
| 61 |
CHECK_NULL(passwordMethod = (*env)->GetMethodID(env, proxyInfoClass, "setPassword", |
| 62 |
"(Ljava/lang/String;)V")); |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
* Class: org_eclipse_core_internal_net_UnixProxyProvider |
| 67 |
* Method: getGConfProxyInfo |
| 68 |
* Signature: ([Ljava/lang/String); |
| 69 |
*/ |
| 70 |
JNIEXPORT jobject JNICALL Java_org_eclipse_core_internal_net_UnixProxyProvider_getGConfProxyInfo( |
| 71 |
JNIEnv *env, jclass clazz, jstring protocol) { |
| 72 |
|
| 73 |
jboolean isCopy; |
| 74 |
const char *cprotocol; |
| 75 |
|
| 76 |
jobject proxyInfo= NULL; |
| 77 |
|
| 78 |
if (client == NULL) { |
| 79 |
gconfInit(env); |
| 80 |
} |
| 81 |
|
| 82 |
CHECK_NULL(proxyInfo = (*env)->NewObject(env, proxyInfoClass, proxyInfoConstructor, protocol)); |
| 83 |
|
| 84 |
cprotocol = (*env)->GetStringUTFChars(env, protocol, &isCopy); |
| 85 |
if (cprotocol == NULL) |
| 86 |
return NULL; |
| 87 |
|
| 88 |
//printf("cprotocol: %s\n", cprotocol); |
| 89 |
|
| 90 |
if (strcasecmp(cprotocol, "http") == 0) { |
| 91 |
gboolean useProxy = gconf_client_get_bool(client, |
| 92 |
"/system/http_proxy/use_http_proxy", NULL); |
| 93 |
if (!useProxy) { |
| 94 |
proxyInfo = NULL; |
| 95 |
goto exit; |
| 96 |
} |
| 97 |
|
| 98 |
gchar *host = gconf_client_get_string(client, |
| 99 |
"/system/http_proxy/host", NULL); |
| 100 |
jobject jhost = (*env)->NewStringUTF(env, host); |
| 101 |
(*env)->CallVoidMethod(env, proxyInfo, hostMethod, jhost); |
| 102 |
|
| 103 |
gint port = gconf_client_get_int(client, "/system/http_proxy/port", |
| 104 |
NULL); |
| 105 |
(*env)->CallVoidMethod(env, proxyInfo, portMethod, port); |
| 106 |
|
| 107 |
gboolean reqAuth = gconf_client_get_bool(client, |
| 108 |
"/system/http_proxy/use_authentication", NULL); |
| 109 |
(*env)->CallVoidMethod(env, proxyInfo, |
| 110 |
requiresAuthenticationMethod, reqAuth); |
| 111 |
if (reqAuth) { |
| 112 |
|
| 113 |
gchar *user = gconf_client_get_string(client, |
| 114 |
"/system/http_proxy/authentication_user", NULL); |
| 115 |
jobject juser = (*env)->NewStringUTF(env, user); |
| 116 |
(*env)->CallVoidMethod(env, proxyInfo, userMethod, juser); |
| 117 |
|
| 118 |
gchar *password = gconf_client_get_string(client, |
| 119 |
"/system/http_proxy/authentication_password", NULL); |
| 120 |
jobject jpassword = (*env)->NewStringUTF(env, password); |
| 121 |
(*env)->CallVoidMethod(env, proxyInfo, passwordMethod, |
| 122 |
jpassword); |
| 123 |
} |
| 124 |
goto exit; |
| 125 |
} |
| 126 |
|
| 127 |
// Everything else applies only if the system proxy mode is manual |
| 128 |
gchar *mode = gconf_client_get_string(client, "/system/proxy/mode", NULL); |
| 129 |
if (strcasecmp(mode, "manual") != 0) { |
| 130 |
proxyInfo = NULL; |
| 131 |
goto exit; |
| 132 |
} |
| 133 |
|
| 134 |
char selector[100]; |
| 135 |
|
| 136 |
if (strcasecmp(cprotocol, "https") == 0) { |
| 137 |
strcpy(selector, "/system/proxy/secure_"); |
| 138 |
} else if (strcasecmp(cprotocol, "socks") == 0) { |
| 139 |
strcpy(selector, "/system/proxy/socks_"); |
| 140 |
} else if (strcasecmp(cprotocol, "ftp") == 0) { |
| 141 |
strcpy(selector, "/system/proxy/ftp_"); |
| 142 |
} else { |
| 143 |
proxyInfo = NULL; |
| 144 |
goto exit; |
| 145 |
} |
| 146 |
|
| 147 |
char useSelector[100]; |
| 148 |
strcpy(useSelector, selector); |
| 149 |
|
| 150 |
gchar *host = gconf_client_get_string(client, strcat(useSelector, "host"), |
| 151 |
NULL); |
| 152 |
jobject jhost = (*env)->NewStringUTF(env, host); |
| 153 |
(*env)->CallVoidMethod(env, proxyInfo, hostMethod, jhost); |
| 154 |
|
| 155 |
strcpy(useSelector, selector); |
| 156 |
gint port = gconf_client_get_int(client, strcat(useSelector, "port"), NULL); |
| 157 |
(*env)->CallVoidMethod(env, proxyInfo, portMethod, port); |
| 158 |
|
| 159 |
exit: if (isCopy == JNI_TRUE) |
| 160 |
(*env)->ReleaseStringUTFChars(env, protocol, cprotocol); |
| 161 |
return proxyInfo; |
| 162 |
} |
| 163 |
|
| 164 |
typedef struct { |
| 165 |
jobjectArray npHostArray; |
| 166 |
JNIEnv *env; |
| 167 |
int index; |
| 168 |
} ListProcContext; |
| 169 |
|
| 170 |
// user_data is the ListProcContext |
| 171 |
void listProc(gpointer data, gpointer user_data) { |
| 172 |
ListProcContext *lpc = user_data; |
| 173 |
jobject jnpHost = (*lpc->env)->NewStringUTF(lpc->env, (char *)data); |
| 174 |
(*lpc->env)->SetObjectArrayElement(lpc->env, lpc->npHostArray, |
| 175 |
lpc->index++, jnpHost); |
| 176 |
} |
| 177 |
|
| 178 |
/* |
| 179 |
* Class: org_eclipse_core_internal_net_UnixProxyProvider |
| 180 |
* Method: getGConfNonProxyHosts |
| 181 |
* Signature: ()[Ljava/lang/String; |
| 182 |
*/ |
| 183 |
JNIEXPORT jobjectArray JNICALL Java_org_eclipse_core_internal_net_UnixProxyProvider_getGConfNonProxyHosts( |
| 184 |
JNIEnv *env, jclass clazz) { |
| 185 |
|
| 186 |
if (client == NULL) { |
| 187 |
gconfInit(env); |
| 188 |
} |
| 189 |
|
| 190 |
GSList *npHosts; |
| 191 |
int size; |
| 192 |
|
| 193 |
npHosts = gconf_client_get_list(client, "/system/http_proxy/ignore_hosts", |
| 194 |
GCONF_VALUE_STRING, NULL); |
| 195 |
size = g_slist_length(npHosts); |
| 196 |
|
| 197 |
// TODO - I'm not sure this is really valid, it's from the JVM implementation |
| 198 |
// of ProxySelector |
| 199 |
if (size == 0) { |
| 200 |
npHosts = gconf_client_get_list(client, "/system/proxy/no_proxy_for", |
| 201 |
GCONF_VALUE_STRING, NULL); |
| 202 |
} |
| 203 |
size = g_slist_length(npHosts); |
| 204 |
|
| 205 |
jobjectArray ret = (*env)->NewObjectArray(env, size, stringClass, NULL); |
| 206 |
|
| 207 |
ListProcContext lpc; |
| 208 |
lpc.env = env; |
| 209 |
lpc.npHostArray = ret; |
| 210 |
lpc.index = 0; |
| 211 |
|
| 212 |
g_slist_foreach(npHosts, listProc, &lpc); |
| 213 |
return ret; |
| 214 |
} |
| 215 |
|
| 216 |
/* |
| 217 |
* Class: org_eclipse_core_internal_net_UnixProxyProvider |
| 218 |
* Method: getKdeProxyInfo |
| 219 |
* Signature: ([Ljava/lang/String); |
| 220 |
*/ |
| 221 |
JNIEXPORT jobject JNICALL Java_org_eclipse_core_internal_net_UnixProxyProvider_getKdeProxyInfo( |
| 222 |
JNIEnv *env, jclass clazz, jstring protocol) { |
| 223 |
//printf("getKdeProxyInfo - not implemented\n"); |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* Class: org_eclipse_core_internal_net_UnixProxyProvider |
| 229 |
* Method: getKdeNonProxyHosts |
| 230 |
* Signature: ()Ljava/lang/String; |
| 231 |
*/ |
| 232 |
JNIEXPORT jobject JNICALL Java_org_eclipse_core_internal_net_UnixProxyProvider_getKdeNonProxyHosts( |
| 233 |
JNIEnv *env, jclass clazz) { |
| 234 |
//printf("getKdeNonProxyHosts - not implemented\n"); |
| 235 |
return NULL; |
| 236 |
} |
| 237 |
|