1 15 16 package com.jdon.bussinessproxy.remote.http; 17 18 import java.lang.reflect.Method ; 19 import java.net.HttpURLConnection ; 20 import java.rmi.RemoteException ; 21 22 import com.jdon.bussinessproxy.TargetMetaDef; 23 import com.jdon.bussinessproxy.remote.auth.AuthException; 24 import com.jdon.util.Debug; 25 26 27 36 public class HttpClient { 37 38 public final static String module = HttpClient.class.getName(); 39 40 private HttpServerParam httpServerParam; 41 42 private static int requestNb; 43 private String sessionId; 44 45 private String userPasswordPair = null; 46 47 48 private static final int DEFAULT_THREAD_COUNT = 1; 49 50 51 private int maxThreadCount = DEFAULT_THREAD_COUNT; 52 53 54 private int curUsedThread = 0; 55 56 protected static HttpClient httpClient = new HttpClient(); 58 public static HttpClient getInstance() { 59 return httpClient; 60 } 61 62 private HttpClient() { 63 httpServerParam = new HttpServerParam(); 64 65 } 66 67 71 public void setHttpServerParam(HttpServerParam httpServerParam) { 72 this.httpServerParam = httpServerParam; 73 } 74 75 78 public Object invoke(TargetMetaDef targetMetaDef, Method m, Object [] args) throws 79 Throwable { 80 Object result = null; 81 82 getThreadLock(); 83 84 int currentRequestNb = requestNb++; 85 Debug.logVerbose("[JdonFramework]Start remote call " + currentRequestNb + " " + 86 m.getName(), module); 87 88 HttpRequest request = new HttpRequest( 90 targetMetaDef, m.getName(), m.getParameterTypes(), args); 91 92 StringBuffer sb = new StringBuffer (httpServerParam.getServletPath(). 93 toString()); 94 if (sessionId != null) { 95 sb.append(";jsessionid="); 96 sb.append(sessionId); 97 } 98 httpServerParam.setServletPath(sb.toString()); 99 100 result = invokeHttp(request, args); 101 102 Debug.logVerbose("[JdonFramework]Ending remote call " + currentRequestNb, module); 103 releaseThreadLock(); 104 105 return result; 106 } 107 108 111 public Object invokeHttp(HttpRequest request, Object [] args) throws 112 Throwable { 113 HttpResponse httpResponse; 114 try { 115 HttpConnectionHelper httpConnectionHelper = new HttpConnectionHelper(); 116 117 HttpURLConnection httpURLConnection; 118 if (httpServerParam.isDebug()) { Debug.logVerbose("[JdonFramework]connect service..", module); 121 httpURLConnection = httpConnectionHelper.connectService(httpServerParam, null); 122 Debug.logVerbose("[JdonFramework]send request: class=" + request.getTargetMetaDef().getClassName(), module); 124 Debug.logVerbose("[JdonFramework]method=" + request.getMethodName(), module); 125 httpConnectionHelper.sendObjectRequest(httpURLConnection, request); 126 }else{ 127 httpURLConnection = httpConnectionHelper.connectService(httpServerParam, getUserPassword(args)); 128 httpConnectionHelper.sendObjectRequest(httpURLConnection, request); 130 if (httpURLConnection.getResponseCode() == 401) { 132 throw new AuthException(" http Server authentication failed!"); 133 } 134 } 135 136 httpResponse = (HttpResponse) httpConnectionHelper.getObjectResponse( 138 httpURLConnection); 139 sessionId = httpURLConnection.getHeaderField("jsessionid"); 141 142 httpURLConnection.disconnect(); 144 145 if (httpResponse.isExceptionThrown()) 146 throw httpResponse.getThrowable(); 147 return httpResponse.getResult(); 148 149 } catch (ClassNotFoundException e) { 150 Debug.logError(e, module); 151 throw new RemoteException (" Class Not Found ", e); 152 } catch (AuthException ae) { 153 throw new AuthException(ae.getMessage()); 154 } catch (Exception e) { 155 String message = "invokeHttp error:"; 156 Debug.logError(message + e, module); 157 throw new RemoteException (message, e); 158 } 159 160 } 161 162 165 public Object invokeAuth(Object [] args) throws Throwable { 166 Object result = null; 167 try { 168 169 Debug.logVerbose("[JdonFramework] begin to auth from J2EE Server", module); 170 171 HttpConnectionHelper httpConnectionHelper = new HttpConnectionHelper(); 172 173 HttpURLConnection httpURLConnection = httpConnectionHelper.connectLogin( 175 httpServerParam, getUserPassword(args)); 176 178 java.util.Hashtable params = new java.util.Hashtable (); 180 params.put("login", "1"); 181 httpConnectionHelper.sendDataRequest(httpURLConnection, params); 182 183 int status = httpURLConnection.getResponseCode(); 185 if (status == HttpURLConnection.HTTP_UNAUTHORIZED) { 186 throw new AuthException(" http Server authentication failed!"); 187 } 188 189 result = httpConnectionHelper.getStringResponse(httpURLConnection); 190 191 httpURLConnection.disconnect(); 193 194 } catch (AuthException ae) { 196 throw new AuthException(ae.getMessage()); 197 } catch (Exception e) { 198 String message = "invokeAuth error:"; 199 Debug.logError(message + e, module); 200 throw new RemoteException (message, e); 201 } 202 203 return result; 204 } 205 206 212 private String getUserPassword(Object [] args) throws AuthException { 213 if (args ==null) throw new AuthException("auth error: args is null"); 214 if ( (userPasswordPair == null) || userPasswordPair.equals("")) { 215 try { 216 StringBuffer sb = new StringBuffer (); 217 if ( (args[0] != null) && (args[1] != null)) { 218 sb.append(args[0]); 219 sb.append(":"); 220 sb.append(args[1]); 221 userPasswordPair = sb.toString(); 222 } 223 } catch (Exception e) { 224 throw new AuthException("auth error: args is null"); 225 } 226 } 227 Debug.logVerbose("[JdonFramework] url param is" + userPasswordPair, module); 228 return userPasswordPair; 229 } 230 231 236 private synchronized void getThreadLock() { 237 while (sessionId == null && curUsedThread > 1) { 238 try { 239 Debug.logVerbose( 240 "No session. Only one thread is authorized. Waiting ...", module); 241 wait(); 242 } catch (InterruptedException e) { 243 e.printStackTrace(); 244 } 245 } 246 247 while (curUsedThread >= maxThreadCount) { 248 try { 249 Debug.logVerbose("[JdonFramework]Max concurent http call reached. Waiting ...", module); 250 wait(); 251 } catch (InterruptedException e) { 252 e.printStackTrace(); 253 } 254 } 255 curUsedThread++; 256 } 257 258 private synchronized void releaseThreadLock() { 259 curUsedThread--; 260 notify(); 261 } 262 263 public int getThreadCount() { 264 return maxThreadCount; 265 } 266 267 public void setThreadCount(int threadCount) { 268 this.maxThreadCount = threadCount; 269 270 Debug.logVerbose("[JdonFramework]Max concurrent thread set to " + threadCount, module); 271 } 272 273 } 274 | Popular Tags |