KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > remote > client > HttpServiceProxy


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.net.ConnectException JavaDoc;
33 import java.net.HttpURLConnection JavaDoc;
34 import java.net.MalformedURLException JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.logging.Level JavaDoc;
37 import java.util.logging.Logger JavaDoc;
38
39
40 /**
41  * Acts as a Proxy for Service layer method calls. This is used by the
42  * ServiceFactory when the factory runs in the Remote mode.
43  *
44  * @see org.jmanage.core.remote.server.ServiceCallHandler
45  *
46  * date: Feb 24, 2005
47  * @author Rakesh Kalra
48  */

49 public class HttpServiceProxy implements InvocationHandler JavaDoc {
50
51     private static final Logger JavaDoc logger = Loggers.getLogger(HttpServiceProxy.class);
52
53     private static String JavaDoc REQUEST_CONTENT_TYPE =
54             "application/x-java-serialized-object; class=org.jmanage.core.remote.RemoteInvocation";
55
56     private static URL JavaDoc remoteURL;
57
58     static{
59         /* set the default remote URL from jmanage.properties */
60         if(JManageProperties.getJManageURL() != null)
61             setRemoteURL(JManageProperties.getJManageURL());
62
63         /* do we need to ignore bad cert (in case we are using SSL for remove
64             invocation */

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 JavaDoc s, String JavaDoc s1) {
73                     return true;
74                 }
75
76                 public boolean verify(String JavaDoc s, SSLSession sslSession) {
77                     return true;
78                 }
79             });
80         }
81     }
82
83     public static void setRemoteURL(String JavaDoc remoteURL) {
84         try {
85             HttpServiceProxy.remoteURL = new URL JavaDoc(remoteURL + "/invokeService");
86         } catch (MalformedURLException JavaDoc e) {
87             throw new RuntimeException JavaDoc(e);
88         }
89     }
90
91     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
92             throws Throwable JavaDoc {
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 JavaDoc e) {
101             throw new ServiceException(ErrorCodes.JMANAGE_SERVER_CONNECTION_FAILED,
102                     JManageProperties.getJManageURL());
103         }
104     }
105
106     private Object JavaDoc invoke(RemoteInvocation invocation)
107             throws Exception JavaDoc {
108
109         HttpURLConnection JavaDoc conn = (HttpURLConnection JavaDoc) 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 JavaDoc(e);
121         }
122
123         /* read the response */
124         InputStream is = conn.getInputStream();
125         ObjectInputStream ois = new ObjectInputStream(is);
126         InvocationResult result = (InvocationResult) ois.readObject();
127
128         ois.read(); // jsse connection pooling hack
129
ois.close();
130         oos.close();
131
132         Object JavaDoc output = result.get();
133         if(output instanceof Exception JavaDoc){
134             throw (Exception JavaDoc)output;
135         }
136         return output;
137     }
138 }
139
Popular Tags