-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.java
69 lines (63 loc) · 1.94 KB
/
Connection.java
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
package api;
import java.io.*;
import java.net.*;
import java.util.logging.*;
import javax.net.ssl.*;
/**
* Modularization of TwitterFeed. TODO: Change this header and package name.
* @author Etienne
*/
public class Connection {
/**
* Connects to the stream.
* @param endpoint the URL of the API.
* @return the connection if succeeded, null otherwise.
*/
private HttpsURLConnection connect(URL endpoint){
try {
return (HttpsURLConnection)endpoint.openConnection();
} catch (IOException ex) {
Logger.getLogger(TwitterFeed.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
/**
* Writes a request to a connection
* @param connection the connection to write the request over.
* @param request the httprequest for a response.
* @return true if succeeded, false otherwise.
*/
public static boolean writeRequest(HttpsURLConnection connection, String request) {
connection.setDoOutput(true);
try {
try (BufferedWriter wr = new BufferedWriter(
new OutputStreamWriter(
connection.getOutputStream()))) {
wr.write(request);
}
return true;
}
catch (IOException e) {
Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, e);
return false;
}
}
/**
* Reads a response for a given connection and returns it as a string.
* @param connection the connection to read from.
* @return
*/
@SuppressWarnings("UnusedAssignment")
public static String readResponse(HttpsURLConnection connection) {
try {
StringBuilder str = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
while((line = br.readLine()) != null) {
str.append(line).append(System.getProperty("line.separator"));
}
return str.toString();
}
catch (IOException e) { return new String(); }
}
}