use of com.predic8.membrane.core.http.Header in project service-proxy by membrane.
the class TelekomSMSTokenProvider method sendSMS.
@Override
protected void sendSMS(String text, String recipientNumber) {
recipientNumber = recipientNumber.replaceAll("^00", "\\+");
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonFactory jsonFactory = new JsonFactory();
JsonGenerator jg = jsonFactory.createGenerator(baos, JsonEncoding.UTF8);
jg.writeStartObject();
jg.writeObjectFieldStart("outboundSMSMessageRequest");
jg.writeArrayFieldStart("address");
jg.writeString("tel:" + recipientNumber);
jg.writeEndArray();
jg.writeStringField("senderAddress", senderAddress);
jg.writeObjectFieldStart("outboundSMSTextMessage");
jg.writeStringField("message", text);
jg.writeEndObject();
jg.writeStringField("outboundEncoding", "7bitGSM");
jg.writeStringField("clientCorrelator", "" + ((long) (Math.random() * Long.MAX_VALUE)));
if (senderName != null)
jg.writeStringField("senderName", senderName);
jg.writeEndObject();
jg.writeEndObject();
jg.close();
Exchange exc = new Request.Builder().post("https://gateway.developer.telekom.com/plone/sms/rest/" + environment.name().toLowerCase() + "/smsmessaging/v1/outbound/" + URLEncoder.encode(senderAddress, "UTF-8") + "/requests").header("Host", "gateway.developer.telekom.com").header("Authorization", "OAuth realm=\"developergarden.com\",oauth_token=\"" + getAccessToken() + "\"").header("Accept", "application/json").header("Content-Type", "application/json").body(baos.toByteArray()).buildExchange();
hc.call(exc, false, true);
if (exc.getResponse().getStatusCode() != 201)
throw new RuntimeException("Could not send SMS: " + exc.getResponse());
log.debug("sent SMS to " + recipientNumber);
} catch (Exception e2) {
throw new RuntimeException(e2);
}
}
use of com.predic8.membrane.core.http.Header in project service-proxy by membrane.
the class TelekomSMSTokenProvider method getAccessToken.
private synchronized String getAccessToken() throws Exception {
long now = System.currentTimeMillis();
if (token == null || tokenExpiration < now) {
Exchange exc = new Request.Builder().post("https://global.telekom.com/gcp-web-api/oauth").header(Header.HOST, "global.telekom.com").header(Header.AUTHORIZATION, "Basic " + new String(Base64.encodeBase64((clientId + ":" + clientSecret).getBytes("UTF-8")), "UTF-8")).header(Header.ACCEPT, "application/json").header(Header.USER_AGENT, Constants.PRODUCT_NAME + " " + Constants.VERSION).header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded").body(new URLParamUtil.ParamBuilder().add("grant_type", "client_credentials").add("scope", scope).build()).buildExchange();
new HttpClient().call(exc, false, true);
if (exc.getResponse().getStatusCode() != 200)
throw new RuntimeException("Telekom Authentication Server returned: " + exc.getResponse());
HashMap<String, String> values = Util.parseSimpleJSONResponse(exc.getResponse());
if (!values.containsKey("access_token") || !values.containsKey("expires_in"))
throw new Exception("Telekom Authentication: Received 200 and JSON body, but no access_token or no expires_in.");
token = values.get("access_token");
tokenExpiration = Long.parseLong(values.get("expires_in")) + System.currentTimeMillis() - 2000;
}
return token;
}
use of com.predic8.membrane.core.http.Header in project service-proxy by membrane.
the class HttpServletHandler method createHeader.
private Header createHeader() {
Header header = new Header();
Enumeration<?> e = request.getHeaderNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
Enumeration<?> e2 = request.getHeaders(key);
while (e2.hasMoreElements()) {
String value = (String) e2.nextElement();
header.add(key, value);
}
}
return header;
}
Aggregations