-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientPost.java
More file actions
151 lines (115 loc) · 4.76 KB
/
Copy pathClientPost.java
File metadata and controls
151 lines (115 loc) · 4.76 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
package com.app.client;
import com.app.Token;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.json.JSONObject;
/**
*
* @author Felipe L. Garcia
*/
public class ClientPost {
private static String urlPolls = "http://localhost:8084/apipolls/polls";
public static void main(String[] args) {
String output = postToken("usuario", "senha");
System.out.println(output);
// String output = postNewQuestion("Qual seu nome", "Felipe","João");
// System.out.println(output);
// output = postNewQuestion("Qual sua idade", "18","19");
// System.out.println(output);
// String output = postChoices(1, 2);
// System.out.println(output);
// String output = test();
// System.out.println(output);
}
public static String postChoices(int idQuest, int idChoice) {
Token token = ClientPost.getToken("usuario", "senha");
if (token == null) {
return null;
}
String url = urlPolls+"/questions/" + idQuest
+ "/choices/" + idChoice;
return restPolls(url, null, token.getAccess_token());
}
public static String postNewQuestion(String strQuestion, String... strchoices) {
Token token = ClientPost.getToken("usuario", "senha");
if (token == null) {
return null;
}
String url = urlPolls+"/questions";
String array = "[\""+String.join("\",\"",strchoices)+ "\"]";
String param = "{\"question\":\"" + strQuestion + "\""
+ ",\"choices\":" +array+ "}";
return restPolls(url, param, token.getAccess_token());
}
public static Token getToken(String username, String password) {
String response = ClientPost.postToken(username, password);
if(response==null){
return null;
}
JSONObject json = new JSONObject(response);
Token token = new Token();
token.setAccess_token(json.getString("access_token"));
token.setExpires_in(json.getLong("expires_in"));
return token;
}
public static String postToken(String username, String password) {
String url = urlPolls+"/tokens"
+ "?username="+username+"&password="+password;
return restPolls(url, null, null);
}
public static String test() {
String url = urlPolls + "/test";
// String param = "{\"qty\":100,\"name\":\"iPad 4\"}";
String strQuestion = "Qual seu nome";
String[] strchoices = {"Felipe", "João"};
String array = "[\"" + String.join("\",\"", strchoices) + "\"]";
String param = "{\"question\":\"" + strQuestion + "\""
+ ",\"choices\":" + array + "}";
//System.out.println(param);
return restPolls(url, param, null);
}
private static String restPolls(String urlPOST, String input, String token) {
System.out.println(urlPOST);
// http://localhost:8080/RESTfulExample/json/product/post
try {
URL url = new URL(urlPOST);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
//Authorization
if (token != null && !token.trim().isEmpty()) {
// System.out.println("Authorization "+token);
conn.setRequestProperty("Authorization", "token " + token);
}
if (input != null && !input.trim().isEmpty()) {
OutputStream os = conn.getOutputStream();
os.write(input.getBytes());
os.flush();
}
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output = "";
String read;
while ((read = br.readLine()) != null) {
output += read;
}
conn.disconnect();
return output;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}