Search in sources :

Example 46 with Header

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);
    }
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Request(com.predic8.membrane.core.http.Request) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 47 with Header

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;
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) URLParamUtil(com.predic8.membrane.core.util.URLParamUtil) HttpClient(com.predic8.membrane.core.transport.http.HttpClient)

Example 48 with Header

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;
}
Also used : Header(com.predic8.membrane.core.http.Header)

Aggregations

Exchange (com.predic8.membrane.core.exchange.Exchange)26 Header (com.predic8.membrane.core.http.Header)16 Request (com.predic8.membrane.core.http.Request)13 IOException (java.io.IOException)13 Response (com.predic8.membrane.core.http.Response)12 CacheBuilder (com.google.common.cache.CacheBuilder)8 Test (org.junit.Test)8 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)7 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)6 UnknownHostException (java.net.UnknownHostException)6 MCElement (com.predic8.membrane.annot.MCElement)5 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)4 AbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.AbstractExchangeSnapshot)4 DynamicAbstractExchangeSnapshot (com.predic8.membrane.core.exchange.snapshots.DynamicAbstractExchangeSnapshot)4 HeaderField (com.predic8.membrane.core.http.HeaderField)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Cache (com.google.common.cache.Cache)2