Newer
Older
package org.josast.SIDS;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
private static Logger logger = Logger.getLogger("AmsatLogger");
private static String USERAGENT = "Mozilla/5.0";
private CloseableHttpClient client = null;
private HttpPost post = null;
private StringBuffer result = null;
/**
* Create a Http client for sending SIDS data. <br>
* exemple of URL
* <li>Satnogs https://db.satnogs.org/api/telemetry/
*
* @param url Database http or https adress
*/
public HttpPostSIDS(final String url) {
client = getCloseableHttpClient();
post = new HttpPost(url);
post.setHeader("User-Agent", USERAGENT);
}
/**
* Create a Http client for sending SIDS data. <br>
*
* @param url
* @param token provided by satnogs if
*/
public HttpPostSIDS(final String url, final String token) {
client = getCloseableHttpClient();
post = new HttpPost(url);
post.setHeader("User-Agent", USERAGENT);
addSatnogsToken(token);
}
public void addSatnogsToken(final String token) {
post.addHeader("Authorization", "Token " + token);
}
private CloseableHttpClient getCloseableHttpClient() {
CloseableHttpClient httpClient = null;
try {
httpClient = HttpClients.custom()
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setSSLContext(new SSLContextBuilder()
.loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(
final X509Certificate[] arg0,
final String arg1)
throws CertificateException {
return true;
}
}).build())
.build();
} catch (KeyManagementException e) {
logger.severe(
"KeyManagementException in creating http client instance "
+ e.toString());
} catch (NoSuchAlgorithmException e) {
logger.severe(
"NoSuchAlgorithmException in creating http client instance "
+ e.toString());
} catch (KeyStoreException e) {
logger.severe("KeyStoreException in creating http client instance "
+ e.toString());
}
return httpClient;
}
/**
* @param sids
* @return
*/
public int SendSIDSBasic(final SIDSData sids) {
// Data preparation
int resultat = 0;
HttpResponse response = null;
BufferedReader rd = null;
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters
.add(new BasicNameValuePair("noradID", "" + sids.getNoradID()));
urlParameters.add(new BasicNameValuePair("source", sids.getSource()));
urlParameters
.add(new BasicNameValuePair("timestamp", sids.getTimestamp()));
urlParameters.add(new BasicNameValuePair("frame", sids.getFrame()));
urlParameters.add(new BasicNameValuePair("locator", sids.getLocator()));
urlParameters.add(
new BasicNameValuePair("longitude", "" + sids.getLongitude()));
urlParameters.add(
new BasicNameValuePair("latitude", "" + sids.getLatitude()));
urlParameters.add(new BasicNameValuePair("version", "2.0.2"));
try {
post.setEntity(new UrlEncodedFormEntity(urlParameters));
} catch (UnsupportedEncodingException e) {
logger.severe("URL Encoding error" + e);
}
try {
response = client.execute(post);
resultat = response.getStatusLine().getStatusCode();
logger.info("URL Result " + resultat);
} catch (IOException e) {
logger.severe("Cient executon error" + e.toString());
}
if (response != null) {
try {
rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
} catch (UnsupportedOperationException | IOException e) {
logger.severe("Buffer reader Exception " + e.toString());
}
result = new StringBuffer();
String line = "";
try {
while ((line = rd.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
logger.severe(
"Error reading line from server " + e.toString());
}
}
return resultat;
}
public StringBuffer getResult() {
return result;
}