Search in sources :

Example 21 with Message

use of com.predic8.membrane.core.http.Message in project service-proxy by membrane.

the class HeaderFilterInterceptor method handleMessage.

private void handleMessage(Message msg) {
    if (msg == null)
        return;
    Header h = msg.getHeader();
    if (h == null)
        return;
    for (HeaderField hf : h.getAllHeaderFields()) for (Rule r : rules) if (r.matches(hf.getHeaderName().toString())) {
        switch(r.getAction()) {
            case REMOVE:
                log.debug("Removing HTTP header " + hf.getHeaderName().toString());
                h.remove(hf);
                break;
            case KEEP:
                break;
        }
        break;
    }
}
Also used : Header(com.predic8.membrane.core.http.Header) HeaderField(com.predic8.membrane.core.http.HeaderField)

Example 22 with Message

use of com.predic8.membrane.core.http.Message in project service-proxy by membrane.

the class LimitInterceptor method handleMessage.

private Outcome handleMessage(Exchange exc, Message msg) throws IOException {
    if (maxBodyLength == -1)
        return Outcome.CONTINUE;
    long len = msg.getHeader().getContentLength();
    if (len != -1 && len > maxBodyLength) {
        log.info("Message length (" + len + ") exceeded limit (" + maxBodyLength + ")");
        exc.setResponse(createFailureResponse());
        return Outcome.ABORT;
    }
    msg.setBody(new Body(new LengthLimitingStream(msg.getBodyAsStream())));
    return Outcome.CONTINUE;
}
Also used : Body(com.predic8.membrane.core.http.Body)

Example 23 with Message

use of com.predic8.membrane.core.http.Message in project service-proxy by membrane.

the class LimitedMemoryExchangeStore method addObservers.

private void addObservers(AbstractExchange exc, AbstractExchange excCopy, Flow flow) throws Exception {
    Message msg = null;
    if (flow == Flow.REQUEST) {
        msg = exc.getRequest();
    } else
        msg = exc.getResponse();
    msg.addObserver(new MessageObserver() {

        @Override
        public void bodyRequested(AbstractBody body) {
        }

        @Override
        public void bodyComplete(AbstractBody body) {
            try {
                cleanSnapshot(Exchange.updateCopy(exc, excCopy));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    });
    exc.addExchangeViewerListener(new AbstractExchangeViewerListener() {

        @Override
        public void setExchangeFinished() {
            try {
                cleanSnapshot(Exchange.updateCopy(exc, excCopy));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    cleanSnapshot(Exchange.updateCopy(exc, excCopy));
}
Also used : AbstractExchangeViewerListener(com.predic8.membrane.core.model.AbstractExchangeViewerListener)

Example 24 with Message

use of com.predic8.membrane.core.http.Message in project service-proxy by membrane.

the class HttpUtil method setHTMLErrorResponse.

public static Response setHTMLErrorResponse(ResponseBuilder responseBuilder, String message, String comment) {
    Response response = responseBuilder.build();
    response.setHeader(createHeaders(MimeType.TEXT_HTML_UTF8));
    response.setBodyContent(getHTMLErrorBody(message, comment).getBytes(Constants.UTF_8_CHARSET));
    return response;
}
Also used : Response(com.predic8.membrane.core.http.Response)

Example 25 with Message

use of com.predic8.membrane.core.http.Message 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)

Aggregations

Message (com.predic8.membrane.core.http.Message)9 Response (com.predic8.membrane.core.http.Response)7 IOException (java.io.IOException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5 JsonFactory (com.fasterxml.jackson.core.JsonFactory)4 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)4 Header (com.predic8.membrane.core.http.Header)4 Request (com.predic8.membrane.core.http.Request)4 Exchange (com.predic8.membrane.core.exchange.Exchange)2 Body (com.predic8.membrane.core.http.Body)2 HeaderField (com.predic8.membrane.core.http.HeaderField)2 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)2 AbstractExchangeViewerListener (com.predic8.membrane.core.model.AbstractExchangeViewerListener)2 ResolverMap (com.predic8.membrane.core.resolver.ResolverMap)2 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)2 StringWriter (java.io.StringWriter)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)2 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1