Advertisement
Guest User

Untitled

a guest
Jun 9th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.93 KB | None | 0 0
  1. 08-26 10:43:27.776: E/dalvikvm(1311): Could not find class 'com.example.hcpandroid.SSLAuthenticate$1', referenced from method com.example.hcpandroid SSLAuthenticate.authenticate
  2. 08-26 10:43:27.786: E/AndroidRuntime(1311): FATAL EXCEPTION: main
  3. 08-26 10:43:27.786: E/AndroidRuntime(1311): java.lang.NoClassDefFoundError: com.example.hcpandroid.SSLAuthenticate$1
  4. 08-26 10:43:27.786: E/AndroidRuntime(1311): at com.example.hcpandroid.SSLAuthenticate.authenticate(SSLAuthenticate.java:86)
  5. 08-26 10:43:27.786: E/AndroidRuntime(1311): at com.example.hcpandroid.LogIn.LogIn(LogIn.java:48)
  6. 08-26 10:43:27.786: E/AndroidRuntime(1311): at com.example.hcpandroid.LogIn$1.onClick(LogIn.java:29)
  7. 08-26 10:43:27.786: E/AndroidRuntime(1311): at android.view.View.performClick(View.java:4128)
  8. 08-26 10:43:27.786: E/AndroidRuntime(1311): at android.view.View$PerformClick.run(View.java:17142)
  9. 08-26 10:43:27.786: E/AndroidRuntime(1311): at android.os.Handler.handleCallback(Handler.java:615)
  10. 08-26 10:43:27.786: E/AndroidRuntime(1311): at android.os.Handler.dispatchMessage(Handler.java:92)
  11. 08-26 10:43:27.786: E/AndroidRuntime(1311): at android.os.Looper.loop(Looper.java:213)
  12. 08-26 10:43:27.786: E/AndroidRuntime(1311): at android.app.ActivityThread.main(ActivityThread.java:4787)
  13. 08-26 10:43:27.786: E/AndroidRuntime(1311): at java.lang.reflect.Method.invokeNative(Native Method)
  14. 08-26 10:43:27.786: E/AndroidRuntime(1311): at java.lang.reflect.Method.invoke(Method.java:511)
  15. 08-26 10:43:27.786: E/AndroidRuntime(1311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
  16. 08-26 10:43:27.786: E/AndroidRuntime(1311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
  17. 08-26 10:43:27.786: E/AndroidRuntime(1311): at dalvik.system.NativeStart.main(Native Method)
  18.  
  19. package com.example.hcpandroid;
  20.  
  21. import com.example.hcpandroid.R.id;
  22.  
  23. import android.os.Bundle;
  24. import android.app.Activity;
  25. import android.view.Menu;
  26. import android.view.View;
  27. import android.view.View.OnClickListener;
  28. import android.widget.Button;
  29. import android.widget.EditText;
  30. import android.widget.Toast;
  31. import com.example.hcpandroid.SSLAuthenticate;
  32.  
  33. public class LogIn extends Activity {
  34.  
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_log_in);
  39.  
  40. Button btnLogIn = (Button) findViewById(id.btnLogIn);
  41.  
  42. btnLogIn.setOnClickListener(new OnClickListener() {
  43.  
  44. @Override
  45. public void onClick(View v) {
  46. LogIn();
  47. }
  48. });
  49. }
  50.  
  51. @Override
  52. public boolean onCreateOptionsMenu(Menu menu) {
  53. // Inflate the menu; this adds items to the action bar if it is present.
  54. getMenuInflater().inflate(R.menu.log_in, menu);
  55. return true;
  56. }
  57.  
  58. public void LogIn(){
  59.  
  60. EditText txtUname = (EditText) findViewById(id.txtUsername);
  61. EditText txtPword = (EditText) findViewById(id.txtPassword);
  62.  
  63. SSLAuthenticate ssl = new SSLAuthenticate(txtUname.getText().toString(), txtPword.getText().toString());
  64.  
  65. int authCode = ssl.authenticate();
  66.  
  67. Toast toast= Toast.makeText(getApplicationContext(), authCode, Toast.LENGTH_LONG);
  68. toast.show();
  69. }
  70.  
  71. }
  72.  
  73. package com.example.hcpandroid;
  74.  
  75. /*
  76. * To change this template, choose Tools | Templates
  77. * and open the template in the editor.
  78. */
  79.  
  80. import java.io.IOException;
  81. import java.math.BigInteger;
  82. import java.security.KeyManagementException;
  83. import java.security.KeyStore;
  84. import java.security.KeyStoreException;
  85. import java.security.MessageDigest;
  86. import java.security.NoSuchAlgorithmException;
  87. import java.security.UnrecoverableKeyException;
  88. import java.security.cert.CertificateException;
  89. import java.security.cert.X509Certificate;
  90. import java.util.LinkedHashMap;
  91. import java.util.Map;
  92. import java.util.logging.Level;
  93. import java.util.logging.Logger;
  94. import javax.net.ssl.SSLContext;
  95. import javax.net.ssl.TrustManager;
  96. import net.sf.json.JSON;
  97. import net.sf.json.JSONSerializer;
  98. import net.sf.json.xml.XMLSerializer;
  99. import org.apache.http.HttpResponse;
  100. import org.apache.http.client.HttpClient;
  101. import org.apache.http.client.methods.HttpPost;
  102. import org.apache.http.client.params.ClientPNames;
  103. import org.apache.http.client.params.CookiePolicy;
  104. import org.apache.http.conn.ClientConnectionManager;
  105. import org.apache.http.conn.scheme.Scheme;
  106. import org.apache.http.conn.scheme.SchemeRegistry;
  107. import org.apache.http.conn.ssl.SSLSocketFactory;
  108. import org.apache.http.conn.ssl.TrustStrategy;
  109. import org.apache.http.entity.StringEntity;
  110. import org.apache.http.impl.client.DefaultHttpClient;
  111. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  112. import org.apache.http.params.BasicHttpParams;
  113. import org.apache.http.params.HttpParams;
  114. import org.apache.http.protocol.HTTP;
  115. import org.apache.http.util.EntityUtils;
  116. import org.json.simple.JSONValue;
  117. import sun.misc.BASE64Encoder;
  118.  
  119. /**
  120. * @author sajithru
  121. */
  122. public class SSLAuthenticate {
  123.  
  124. private String username;
  125. private String password;
  126. private DefaultHttpClient httpClient;
  127. private String cookie;
  128.  
  129. public SSLAuthenticate(String username, String password) {
  130. this.username = username;
  131. this.password = password;
  132. }
  133.  
  134. public SSLAuthenticate() {
  135. }
  136.  
  137. public DefaultHttpClient getHttpClient() {
  138. return httpClient;
  139. }
  140.  
  141. public String getCookie() {
  142. return cookie;
  143. }
  144.  
  145. public String getUsername() {
  146. return username;
  147. }
  148.  
  149. public int authenticate() {
  150.  
  151. HttpResponse responce = null;
  152. try {
  153. //Authenticate SSL Certification
  154. TrustStrategy easyStrategy = new TrustStrategy() {
  155. @Override
  156. public boolean isTrusted(X509Certificate[] chain, String authType)
  157. throws CertificateException {
  158. // eh, why not?
  159. return true;
  160. }
  161. };
  162.  
  163. SchemeRegistry schemeRegistry = new SchemeRegistry();
  164. SSLContext sslcontext = SSLContext.getInstance("TLS");
  165. sslcontext.init(null, null, null);
  166. SSLSocketFactory ssf = new SSLSocketFactory((KeyStore) easyStrategy);
  167. ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  168.  
  169. Scheme httpsScheme = new Scheme("https", ssf, 443);
  170. schemeRegistry.register(httpsScheme);
  171.  
  172. TrustManager trustMgr = new TrustManager() {
  173. };
  174. sslcontext.init(null, new TrustManager[]{trustMgr}, null);
  175.  
  176. HttpParams params = new BasicHttpParams();
  177. ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schemeRegistry);
  178. httpClient = new DefaultHttpClient(connMgr, params);
  179.  
  180. //Encode username into BASE64 encode format
  181. BASE64Encoder base64Encoder = new BASE64Encoder();
  182. String uname64 = base64Encoder.encode(username.getBytes());
  183.  
  184. //Encode password into MD5 Hash encode format
  185. MessageDigest messageDigest = MessageDigest.getInstance("MD5");
  186. messageDigest.update(password.getBytes(), 0, password.length());
  187. String md5Password = new BigInteger(1, messageDigest.digest()).toString(16);
  188.  
  189. //Set HTTPS request header- Authentication
  190. cookie = "hcp-ns-auth=" + uname64 + ":" + md5Password;
  191. System.out.println("Username: " + username + " Password: " + password);
  192. System.out.println(cookie);
  193.  
  194. responce = adminAuth(cookie, httpClient);
  195.  
  196. } catch (NoSuchAlgorithmException ex) {
  197. Logger.getLogger(SSLAuthenticate.class.getName()).log(Level.SEVERE, null, ex);
  198. } catch (KeyManagementException ex) {
  199. Logger.getLogger(SSLAuthenticate.class.getName()).log(Level.SEVERE, null, ex);
  200. } catch (KeyStoreException ex) {
  201. Logger.getLogger(SSLAuthenticate.class.getName()).log(Level.SEVERE, null, ex);
  202. } catch (UnrecoverableKeyException ex) {
  203. Logger.getLogger(SSLAuthenticate.class.getName()).log(Level.SEVERE, null, ex);
  204. }
  205.  
  206. int responceCode = responce.getStatusLine().getStatusCode();
  207.  
  208. return responceCode;
  209. }
  210.  
  211. private HttpResponse adminAuth(String cookie, HttpClient hClient) {
  212.  
  213. HttpResponse response = null;
  214. try {
  215. //Creating HTTP Post object
  216. String url = "https://millennium-test.hcp.millenniumit.com/query";
  217. //String url = "https://hitachi.hcp1.hdspoc.com/query";
  218. HttpPost httpPost = new HttpPost(url);
  219. httpPost.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
  220. //Setting HTTP Post headers for Authentication, Request type, Respond type and Encode type
  221. httpPost.addHeader("Cookie", cookie);
  222. httpPost.addHeader("Content-Type", "application/xml");
  223. httpPost.addHeader("Accept", "application/json");
  224. //httpPost.addHeader("Content-Encoding", "gzip");
  225. //httpPost.addHeader("Accept-Encoding", "gzip");
  226.  
  227. Map<String, String> obj = new LinkedHashMap<String, String>();
  228. obj.put("query", "+(namespace:"data-set1.Millennium-Test")");
  229. obj.put("contentProperties", "false");
  230. obj.put("objectProperties", "shred,retention");
  231. obj.put("sort", "changeTimeMilliseconds+asc");
  232. String jsonText = "{"object" :" + JSONValue.toJSONString(obj) + "}";
  233. System.out.println(jsonText);
  234.  
  235. XMLSerializer serializer = new XMLSerializer();
  236. JSON json = JSONSerializer.toJSON(jsonText);
  237. serializer.setRootName("queryRequest");
  238. serializer.setTypeHintsEnabled(false);
  239. String xml = serializer.write(json);
  240.  
  241. xml = xml.replaceAll("\<\?xml(.+?)\?\>", "").trim();
  242. System.out.println(xml);
  243.  
  244. StringEntity stringEntity = new StringEntity(xml, HTTP.UTF_8);
  245. httpPost.setEntity(stringEntity);
  246. response = hClient.execute(httpPost);
  247. System.out.println(response.toString());
  248.  
  249. String sJson = EntityUtils.toString(response.getEntity());
  250. System.out.println(sJson);
  251.  
  252. HCP_Logger myLogger = new HCP_Logger();
  253. myLogger.writeToLog(username, xml, response.toString());
  254.  
  255. } catch (IOException ex) {
  256. Logger.getLogger(SSLAuthenticate.class.getName()).log(Level.SEVERE, null, ex);
  257. }
  258.  
  259. return response;
  260. }
  261.  
  262. private HttpResponse tenantAuth(String cookie, HttpClient hClient) {
  263.  
  264. HttpResponse response = null;
  265. try {
  266. //Creating HTTP Post object
  267. String url = "sample url";
  268. //String url = "sample url";
  269. HttpPost httpPost = new HttpPost(url);
  270. httpPost.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
  271. //Setting HTTP Post headers for Authentication, Request type, Respond type and Encode type
  272. httpPost.addHeader("Cookie", cookie);
  273. httpPost.addHeader("Content-Type", "application/xml");
  274. httpPost.addHeader("Accept", "application/json");
  275. //httpPost.addHeader("Content-Encoding", "gzip");
  276. //httpPost.addHeader("Accept-Encoding", "gzip");
  277.  
  278. Map obj = new LinkedHashMap();
  279. obj.put("query", "+(namespace:"sample")");
  280. obj.put("contentProperties", "false");
  281. obj.put("objectProperties", "shred,retention");
  282. obj.put("sort", "changeTimeMilliseconds+asc");
  283. String jsonText = "{"object" :" + JSONValue.toJSONString(obj) + "}";
  284. //System.out.println(jsonText);
  285.  
  286. XMLSerializer serializer = new XMLSerializer();
  287. JSON json = JSONSerializer.toJSON(jsonText);
  288. serializer.setRootName("queryRequest");
  289. serializer.setTypeHintsEnabled(false);
  290. String xml = serializer.write(json);
  291.  
  292. xml = xml.replaceAll("\<\?xml(.+?)\?\>", "").trim();
  293. //System.out.println(xml);
  294.  
  295. //String xmll = "<queryRequest><object><query>namespace:"data-set1.Millennium-Test"</query><objectProperties>shred,retention</objectProperties><sort>changeTimeMilliseconds+asc</sort></object></queryRequest>";
  296. //System.out.println(xmll);
  297.  
  298. StringEntity stringEntity = new StringEntity(xml, HTTP.UTF_8);
  299. httpPost.setEntity(stringEntity);
  300.  
  301. response = hClient.execute(httpPost);
  302. //System.out.println(response.toString());
  303.  
  304. // String sJson = EntityUtils.toString(response.getEntity());
  305. //System.out.println(sJson);
  306.  
  307. // HCP_Logger myLogger = new HCP_Logger();
  308. // myLogger.writeToLog(username, xml, response.toString());
  309.  
  310. } catch (IOException ex) {
  311. Logger.getLogger(SSLAuthenticate.class.getName()).log(Level.SEVERE, null, ex);
  312. }
  313.  
  314. return response;
  315. }
  316. }
  317.  
  318. project -> Build Path -> Configure Build Path -> Order and Export Tab.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement