Search in sources :

Example 1 with HeaderField

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

the class HttpServletHandler method writeResponse.

@SuppressWarnings("deprecation")
protected void writeResponse(Response res) throws Exception {
    response.setStatus(res.getStatusCode(), res.getStatusMessage());
    for (HeaderField header : res.getHeader().getAllHeaderFields()) {
        if (header.getHeaderName().equals(Header.TRANSFER_ENCODING))
            continue;
        response.addHeader(header.getHeaderName().toString(), header.getValue());
    }
    ServletOutputStream out = response.getOutputStream();
    res.getBody().write(new PlainBodyTransferrer(out));
    out.flush();
    response.flushBuffer();
    exchange.setTimeResSent(System.currentTimeMillis());
    exchange.collectStatistics();
}
Also used : HeaderField(com.predic8.membrane.core.http.HeaderField) ServletOutputStream(javax.servlet.ServletOutputStream) PlainBodyTransferrer(com.predic8.membrane.core.http.PlainBodyTransferrer)

Example 2 with HeaderField

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

the class MyInterceptor method handleRequest.

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
    System.out.println("MyInterceptor invoked.");
    System.out.println("Request headers:");
    for (HeaderField headerField : exc.getRequest().getHeader().getAllHeaderFields()) System.out.print(headerField.getHeaderName() + ": " + headerField.getValue());
    return Outcome.CONTINUE;
}
Also used : HeaderField(com.predic8.membrane.core.http.HeaderField)

Example 3 with HeaderField

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

the class AdminRESTInterceptor method getRequestHeader.

@Mapping("/admin/rest/exchanges/(-?\\d+)/(response|request)/header")
public Response getRequestHeader(QueryParameter params, String relativeRootPath) throws Exception {
    final AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    final Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    if (msg == null) {
        return Response.noContent().build();
    }
    return json(new JSONContent() {

        public void write(JsonGenerator gen) throws Exception {
            gen.writeStartObject();
            gen.writeArrayFieldStart("headers");
            for (HeaderField hf : msg.getHeader().getAllHeaderFields()) {
                gen.writeStartObject();
                gen.writeStringField("name", hf.getHeaderName().toString());
                gen.writeStringField("value", hf.getValue());
                gen.writeEndObject();
            }
            gen.writeEndArray();
            gen.writeEndObject();
        }
    });
}
Also used : Message(com.predic8.membrane.core.http.Message) JSONContent(com.predic8.membrane.core.interceptor.rest.JSONContent) HeaderField(com.predic8.membrane.core.http.HeaderField) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange) SQLException(java.sql.SQLException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Example 4 with HeaderField

use of com.predic8.membrane.core.http.HeaderField 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 5 with HeaderField

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

the class HeaderFilterInterceptorTest method doit.

@Test
public void doit() throws Exception {
    Exchange exc = new Exchange(null);
    exc.setResponse(Response.ok().header("a", "b").header("c", "d").header("c", "d2").header("e", "f").build());
    HeaderFilterInterceptor fhi = new HeaderFilterInterceptor();
    fhi.setRules(Lists.newArrayList(new Rule[] { // implicitly set by Response.ok()
    new Rule("Server", Action.REMOVE), new Rule("a", Action.KEEP), new Rule("c.*", Action.REMOVE) }));
    fhi.handleResponse(exc);
    HeaderField[] h = exc.getResponse().getHeader().getAllHeaderFields();
    assertEquals(3, h.length);
    assertEquals("Content-Length", h[0].getHeaderName().toString());
    assertEquals("a", h[1].getHeaderName().toString());
    assertEquals("e", h[2].getHeaderName().toString());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) HeaderField(com.predic8.membrane.core.http.HeaderField) Rule(com.predic8.membrane.core.interceptor.HeaderFilterInterceptor.Rule) Test(org.junit.Test)

Aggregations

HeaderField (com.predic8.membrane.core.http.HeaderField)5 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)1 Exchange (com.predic8.membrane.core.exchange.Exchange)1 Header (com.predic8.membrane.core.http.Header)1 Message (com.predic8.membrane.core.http.Message)1 PlainBodyTransferrer (com.predic8.membrane.core.http.PlainBodyTransferrer)1 Rule (com.predic8.membrane.core.interceptor.HeaderFilterInterceptor.Rule)1 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Test (org.junit.Test)1