1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.adopter.registration;
23
24 import org.apache.http.HttpResponse;
25 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
26 import org.apache.http.client.methods.HttpPost;
27 import org.apache.http.entity.StringEntity;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.apache.http.impl.client.HttpClientBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import java.io.BufferedReader;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.InputStreamReader;
37 import java.net.URI;
38 import java.net.URL;
39 import java.nio.charset.StandardCharsets;
40
41
42
43
44 public class AdopterRegistrationSender {
45
46
47 private static final Logger logger = LoggerFactory.getLogger(AdopterRegistrationSender.class);
48
49
50 private String baseUrl;
51
52
53
54 private static final String GENERAL_DATA_URL_SUFFIX = "api/1.0/adopter";
55 private static final String STATISTIC_URL_SUFFIX = "api/1.0/statistic";
56
57 private static final String EXTRA_URL_INFIX = "api/1.0";
58
59
60
61
62
63
64
65
66
67 public AdopterRegistrationSender(String statisticServerBaseUrl) {
68 if (!statisticServerBaseUrl.endsWith("/")) {
69 statisticServerBaseUrl += "/";
70 }
71 this.baseUrl = statisticServerBaseUrl;
72 }
73
74
75
76
77
78
79
80
81
82
83 public void sendGeneralData(String json) throws IOException {
84 send(json, GENERAL_DATA_URL_SUFFIX);
85 }
86
87
88
89
90
91
92 public void deleteGeneralData(String json) throws IOException {
93 send(json, GENERAL_DATA_URL_SUFFIX, "DELETE");
94 }
95
96
97
98
99
100
101 public void sendStatistics(String json) throws IOException {
102 send(json, STATISTIC_URL_SUFFIX);
103 }
104
105
106
107
108
109
110
111 public void sendExtraData(String key, String json) throws IOException {
112 send(json, EXTRA_URL_INFIX + "/" + key);
113 }
114
115
116
117
118
119
120 public void deleteStatistics(String json) throws IOException {
121 send(json, STATISTIC_URL_SUFFIX, "DELETE");
122 }
123
124
125
126
127
128
129
130
131 private void send(String json, String urlSuffix) throws IOException {
132 send(json, urlSuffix, "GET");
133 }
134
135
136
137
138
139
140
141
142 private void send(String json, String urlSuffix, String method) throws IOException {
143 try (CloseableHttpClient client = HttpClientBuilder.create().useSystemProperties().build()) {
144 String url = new URL(baseUrl + urlSuffix).toString();
145 HttpEntityEnclosingRequestBase request = null;
146 if ("DELETE".equals(method)) {
147 request = new HttpDeleteWithEntity(url);
148 } else {
149 request = new HttpPost(url);
150 }
151 request.addHeader("Content-Type", "application/json; utf-8");
152 request.addHeader("Accept", "application/json");
153 request.setEntity(new StringEntity(json, StandardCharsets.UTF_8));
154
155 HttpResponse resp = client.execute(request);
156 int httpStatus = resp.getStatusLine().getStatusCode();
157 boolean errorOccurred = httpStatus < 200 || httpStatus > 299;
158 InputStream responseStream = resp.getEntity().getContent();
159
160 try (BufferedReader br = new BufferedReader(new InputStreamReader(responseStream, StandardCharsets.UTF_8))) {
161 StringBuilder response = new StringBuilder();
162 String responseLine;
163 while ((responseLine = br.readLine()) != null) {
164 response.append(responseLine.trim());
165 }
166 if (errorOccurred) {
167 String errorMessage = String.format("HttpStatus: %s, HttpResponse: %s", httpStatus, response);
168 throw new RuntimeException(errorMessage);
169 }
170 }
171 }
172 }
173
174
175 static class HttpDeleteWithEntity extends HttpEntityEnclosingRequestBase {
176
177 public static final String METHOD_NAME = "DELETE";
178
179 HttpDeleteWithEntity(final String uri) {
180 super();
181 setURI(URI.create(uri));
182 }
183
184 @Override
185 public String getMethod() {
186 return METHOD_NAME;
187 }
188 }
189
190 }