1 16 package org.jmanage.core.remote.client; 17 18 import org.jmanage.core.util.JManageProperties; 19 import org.jmanage.core.remote.InvocationResult; 20 import org.jmanage.core.remote.RemoteInvocation; 21 import org.jmanage.core.services.ServiceException; 22 import org.jmanage.core.util.ErrorCodes; 23 import org.jmanage.core.util.Loggers; 24 import org.jmanage.core.util.CoreUtils; 25 26 import javax.net.ssl.HostnameVerifier; 27 import javax.net.ssl.HttpsURLConnection; 28 import javax.net.ssl.SSLSession; 29 import java.io.*; 30 import java.lang.reflect.InvocationHandler ; 31 import java.lang.reflect.Method ; 32 import java.net.ConnectException ; 33 import java.net.HttpURLConnection ; 34 import java.net.MalformedURLException ; 35 import java.net.URL ; 36 import java.util.logging.Level ; 37 import java.util.logging.Logger ; 38 39 40 49 public class HttpServiceProxy implements InvocationHandler { 50 51 private static final Logger logger = Loggers.getLogger(HttpServiceProxy.class); 52 53 private static String REQUEST_CONTENT_TYPE = 54 "application/x-java-serialized-object; class=org.jmanage.core.remote.RemoteInvocation"; 55 56 private static URL remoteURL; 57 58 static{ 59 60 if(JManageProperties.getJManageURL() != null) 61 setRemoteURL(JManageProperties.getJManageURL()); 62 63 65 if(JManageProperties.isIgnoreBadSSLCertificate()){ 66 System.setProperty("javax.net.ssl.trustStore", 67 CoreUtils.getConfigDir() + "/cacerts"); 68 System.setProperty("javax.net.ssl.trustStorePassword", 69 JManageProperties.getSSLTrustStorePassword()); 70 71 HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){ 72 public boolean verify(String s, String s1) { 73 return true; 74 } 75 76 public boolean verify(String s, SSLSession sslSession) { 77 return true; 78 } 79 }); 80 } 81 } 82 83 public static void setRemoteURL(String remoteURL) { 84 try { 85 HttpServiceProxy.remoteURL = new URL (remoteURL + "/invokeService"); 86 } catch (MalformedURLException e) { 87 throw new RuntimeException (e); 88 } 89 } 90 91 public Object invoke(Object proxy, Method method, Object [] args) 92 throws Throwable { 93 94 logger.log(Level.FINE, "Invoking service method: {0}", 95 method.getDeclaringClass().getName() + ":" + method.getName()); 96 97 try { 98 RemoteInvocation invocation = new RemoteInvocation(method, args); 99 return invoke(invocation); 100 } catch (ConnectException e) { 101 throw new ServiceException(ErrorCodes.JMANAGE_SERVER_CONNECTION_FAILED, 102 JManageProperties.getJManageURL()); 103 } 104 } 105 106 private Object invoke(RemoteInvocation invocation) 107 throws Exception { 108 109 HttpURLConnection conn = (HttpURLConnection ) remoteURL.openConnection(); 110 conn.setDoInput(true); 111 conn.setDoOutput(true); 112 conn.setRequestProperty("ContentType", REQUEST_CONTENT_TYPE); 113 conn.setRequestMethod("POST"); 114 OutputStream os = conn.getOutputStream(); 115 ObjectOutputStream oos = new ObjectOutputStream(os); 116 try { 117 oos.writeObject(invocation); 118 oos.flush(); 119 } catch (ObjectStreamException e) { 120 throw new RuntimeException (e); 121 } 122 123 124 InputStream is = conn.getInputStream(); 125 ObjectInputStream ois = new ObjectInputStream(is); 126 InvocationResult result = (InvocationResult) ois.readObject(); 127 128 ois.read(); ois.close(); 130 oos.close(); 131 132 Object output = result.get(); 133 if(output instanceof Exception ){ 134 throw (Exception )output; 135 } 136 return output; 137 } 138 } 139 | Popular Tags |