-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
154 lines (118 loc) · 5.48 KB
/
Copy pathApp.java
File metadata and controls
154 lines (118 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.jcg.maven;
import com.google.gson.Gson;
import javax.net.ssl.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyStore;
import java.security.Security;
import java.util.LinkedHashMap;
import java.util.Map;
public class App{
public static void main(String args[]) throws Exception {
App app = new App();
// app.sendPost();
String url = "https://http-hunt.thoughtworks-labs.net/challenge/input";
HttpURLConnection httpsCon =app.getConnection("GET",null,null,url,null,null);
String response = new App().readStream(httpsCon.getInputStream());
System.out.println(response);
Example example = new Gson().fromJson(response,Example.class);
String result = "true";//app.performtask(example);
System.out.println(result);
System.out.println("\nContent at " + url);
System.out.println("Response Message is " + httpsCon.getResponseMessage());
Map<Character,Integer> hMap = app.countVowel(example.getText());
Exaample2 exaample2 = new Exaample2();
// exaample2.setCount(result);
String json = new Gson().toJson(exaample2,Exaample2.class);
System.out.println(hMap);
String url2 = "https://http-hunt.thoughtworks-labs.net/challenge/output";
HttpURLConnection httpsCon2 =app.getConnection("POST","wordCount",result,url2,json,hMap);
String response2 = new App().readStream(httpsCon2.getInputStream());
System.out.println(response2);
System.out.println("\nContent at " + url2);
System.out.println("Response Message is " + httpsCon2.getResponseMessage());
}
private String readStream(InputStream in) {
BufferedReader reader = null;
StringBuffer response = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return response.toString();
}
private HttpsURLConnection getConnection(String requestType,String paramName,String paramValue,String targetUrl, String json,Map<Character,Integer> hMap) throws Exception{
System.setProperty("javax.net.ssl.trustStore", "/usr/lib/jvm/jdk1.6.0_32/jre/lib/security/cacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
//TrustStore..
char[] passphrase = "changeit".toCharArray(); //password
KeyStore keystore = KeyStore.getInstance("JKS");
//KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(new FileInputStream("C:\\Program Files\\Java\\jdk1.8.0_191\\jre\\lib\\security\\cacerts"), passphrase); //path
//TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); //instance
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keystore);
SSLContext context = SSLContext.getInstance("TLS");
TrustManager[] trustManagers = tmf.getTrustManagers();
context.init(null, trustManagers, null);
SSLSocketFactory sf = context.getSocketFactory();
URL url = new URL(targetUrl);
HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection();
httpsCon.setSSLSocketFactory(sf);
httpsCon.setRequestMethod(requestType);
httpsCon.setRequestProperty("userId","CeDOAx1t0");
if(paramValue!= null){
httpsCon.setRequestProperty("content-type","application/json");
// Send post request
httpsCon.setDoOutput(true);
// int i = Integer.parseInt(paramValue);
System.out.println(hMap);
DataOutputStream wr = new DataOutputStream(httpsCon.getOutputStream());
json = "{\"a\":" + hMap.get('A') + ",\"e\":"+ hMap.get('E') +",\"i\":"+ hMap.get('I') +",\"o\":"+ hMap.get('O') +",\"u\":" + hMap.get('U') +
"}";
System.out.println(json);
wr.writeBytes(json);
wr.flush();
wr.close();
}
return httpsCon;
}
private String performtask(Example example) {
String result;
Integer i = new Integer(example.getText().replaceAll("\\?",".").split("\\.").length);
result = i.toString();
return result;
}
private Map<Character,Integer> countVowel(String str){
String str2 = str.toUpperCase();
LinkedHashMap<Character, Integer> hMap = new LinkedHashMap();
hMap.put('A', 0);
hMap.put('E', 0);
hMap.put('I', 0);
hMap.put('O', 0);
hMap.put('U', 0);
for (int i = 0; i <= str2.length() - 1; i++) {
if (hMap.containsKey(str2.charAt(i))) {
int count = hMap.get(str2.charAt(i));
hMap.put(str2.charAt(i), ++count);
}
}
System.out.println(hMap);
return hMap;
}
}