Search in sources :

Example 21 with Response

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

the class AdminRESTInterceptor method getRequestBody.

@Mapping("/admin/rest/exchanges/(-?\\d+)/(response|request)/body")
public Response getRequestBody(QueryParameter params, String relativeRootPath) throws Exception {
    AbstractExchange exc = router.getExchangeStore().getExchangeById(params.getGroupInt(1));
    if (exc == null) {
        return Response.notFound().build();
    }
    Message msg = params.getGroup(2).equals("response") ? exc.getResponse() : exc.getRequest();
    String ct = params.getGroup(2).equals("response") ? exc.getResponseContentType() : exc.getRequestContentType();
    if (msg == null || msg.isBodyEmpty()) {
        return Response.noContent().build();
    }
    ResponseBuilder rb = Response.ok().contentType(ct).body(msg.getBodyAsStream(), false);
    String contentEncoding = msg.getHeader().getContentEncoding();
    if (contentEncoding != null)
        rb.header(Header.CONTENT_ENCODING, contentEncoding);
    return rb.build();
}
Also used : Message(com.predic8.membrane.core.http.Message) ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder) AbstractExchange(com.predic8.membrane.core.exchange.AbstractExchange)

Example 22 with Response

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

the class AMQuota method setResponseToServiceUnavailable.

private void setResponseToServiceUnavailable(Exchange exc, PolicyQuota pq) {
    // TODO do a better response here
    Header hd = new Header();
    DateTimeFormatter dtFormatter = DateTimeFormat.forPattern("HH:mm:ss aa");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    JsonGenerator jgen = null;
    try {
        jgen = new JsonFactory().createGenerator(os);
        jgen.writeStartObject();
        jgen.writeObjectField("Statuscode", 429);
        jgen.writeObjectField("Message", "Quota Exceeded");
        jgen.writeEndObject();
        jgen.close();
    } catch (IOException ignored) {
    }
    Response resp = Response.ResponseBuilder.newInstance().status(429, "Too Many Requests.").header(hd).contentType("application/json").body(os.toByteArray()).build();
    exc.setResponse(resp);
}
Also used : Response(com.predic8.membrane.core.http.Response) Header(com.predic8.membrane.core.http.Header) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 23 with Response

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

the class AMRateLimiter method setResponseToServiceUnavailable.

public void setResponseToServiceUnavailable(Exchange exc, PolicyRateLimit prl) throws UnsupportedEncodingException {
    Header hd = new Header();
    DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'").withZoneUTC().withLocale(Locale.US);
    hd.add("Date", dateFormatter.print(DateTime.now()));
    hd.add("X-LimitDuration", PeriodFormat.getDefault().print(prl.getInterval().toPeriod()));
    hd.add("X-LimitRequests", Integer.toString(prl.getRequests()));
    String ip = exc.getRemoteAddrIp();
    DateTime availableAgainDateTime = prl.getNextCleanup();
    hd.add("X-LimitReset", Long.toString(availableAgainDateTime.getMillis()));
    /*StringBuilder bodyString = new StringBuilder();
        DateTimeFormatter dtFormatter = DateTimeFormat.forPattern("HH:mm:ss aa");
        bodyString.append(ip).append(" exceeded the rate limit of ").append(prl.getRequests())
                .append(" requests per ")
                .append(PeriodFormat.getDefault().print(prl.getInterval().toPeriod()))
                .append(". The next request can be made at ").append(dtFormatter.print(availableAgainDateTime));*/
    DateTimeFormatter dtFormatter = DateTimeFormat.forPattern("HH:mm:ss aa");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    JsonGenerator jgen = null;
    try {
        jgen = new JsonFactory().createGenerator(os);
        jgen.writeStartObject();
        jgen.writeObjectField("Statuscode", 429);
        jgen.writeObjectField("Message", "The rate limit of " + prl.getRequests() + " requests in " + prl.getInterval().getStandardSeconds() + " seconds is exceeded. The next requests can be made at " + dtFormatter.print(availableAgainDateTime));
        jgen.writeEndObject();
        jgen.close();
    } catch (IOException ignored) {
    }
    Response resp = Response.ResponseBuilder.newInstance().status(429, "Too Many Requests.").header(hd).contentType("application/json").body(os.toByteArray()).build();
    exc.setResponse(resp);
}
Also used : Response(com.predic8.membrane.core.http.Response) Header(com.predic8.membrane.core.http.Header) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Example 24 with Response

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

the class PositiveNode method toResponse.

@Override
public Response toResponse(Request request) {
    String ifModifiedSince = request.getHeader().getFirstValue(Header.IF_MODIFIED_SINCE);
    if (ifModifiedSince != null) {
        try {
            if (lastModified <= CacheInterceptor.fromRFC(ifModifiedSince))
                return Response.notModified(CacheInterceptor.toRFC(System.currentTimeMillis())).build();
        } catch (Exception e) {
            log.warn("", e);
        }
    }
    ResponseBuilder builder = Response.ok();
    if (status >= 300 && status < 400)
        builder.status(status, "Moved.");
    if (contentType != null)
        builder.contentType(contentType);
    if (location != null)
        builder.header(Header.LOCATION, location);
    if (lastModified != 0)
        return builder.header(Header.LAST_MODIFIED, CacheInterceptor.toRFC(lastModified)).body(content).build();
    else
        return builder.body(content).build();
}
Also used : ResponseBuilder(com.predic8.membrane.core.http.Response.ResponseBuilder) IOException(java.io.IOException) ParseException(java.text.ParseException)

Example 25 with Response

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

the class HttpClient method call.

public Exchange call(Exchange exc, boolean adjustHostHeader, boolean failOverOn5XX) throws Exception {
    if (exc.getDestinations().isEmpty())
        throw new IllegalStateException("List of destinations is empty. Please specify at least one destination.");
    int counter = 0;
    Exception exception = null;
    Object trackNodeStatusObj = exc.getProperty(Exchange.TRACK_NODE_STATUS);
    boolean trackNodeStatus = trackNodeStatusObj != null && trackNodeStatusObj instanceof Boolean && (Boolean) trackNodeStatusObj;
    disableStreamingForRetries(exc);
    while (counter < maxRetries) {
        Connection con = null;
        String dest = getDestination(exc, counter);
        HostColonPort target = null;
        try {
            log.debug("try # " + counter + " to " + dest);
            target = init(exc, dest, adjustHostHeader);
            if (counter == 0) {
                con = exc.getTargetConnection();
                if (con != null) {
                    if (!con.isSame(target.host, target.port)) {
                        con.close();
                        con = null;
                    } else {
                        con.setKeepAttachedToExchange(true);
                    }
                }
            }
            SSLProvider sslProvider = getOutboundSSLProvider(exc, target);
            if (con == null) {
                con = conMgr.getConnection(target.host, target.port, localAddr, sslProvider, connectTimeout, getSNIServerName(exc), proxy, proxySSLContext);
                con.setKeepAttachedToExchange(exc.getRequest().isBindTargetConnectionToIncoming());
                exc.setTargetConnection(con);
            }
            if (proxy != null && sslProvider == null)
                // if we use a proxy for a plain HTTP (=non-HTTPS) request, attach the proxy credentials.
                exc.getRequest().getHeader().setProxyAutorization(proxy.getCredentials());
            Response response;
            String newProtocol = null;
            if (exc.getRequest().isCONNECTRequest()) {
                handleConnectRequest(exc, con);
                response = Response.ok().build();
                newProtocol = "CONNECT";
            } else {
                response = doCall(exc, con);
                if (trackNodeStatus)
                    exc.setNodeStatusCode(counter, response.getStatusCode());
                if (exc.getProperty(Exchange.ALLOW_WEBSOCKET) == Boolean.TRUE && isUpgradeToResponse(response, "websocket")) {
                    log.debug("Upgrading to WebSocket protocol.");
                    newProtocol = "WebSocket";
                }
                if (exc.getProperty(Exchange.ALLOW_TCP) == Boolean.TRUE && isUpgradeToResponse(response, "tcp")) {
                    log.debug("Upgrading to TCP protocol.");
                    newProtocol = "TCP";
                }
                if (exc.getProperty(Exchange.ALLOW_SPDY) == Boolean.TRUE && isUpgradeToResponse(response, "SPDY/3.1")) {
                    log.debug("Upgrading to SPDY/3.1 protocol.");
                    newProtocol = "SPDY/3.1";
                }
            }
            if (newProtocol != null) {
                setupConnectionForwarding(exc, con, newProtocol, streamPumpStats);
                exc.getDestinations().clear();
                exc.getDestinations().add(dest);
                con.setExchange(exc);
                exc.setResponse(response);
                return exc;
            }
            boolean is5XX = 500 <= response.getStatusCode() && response.getStatusCode() < 600;
            if (!failOverOn5XX || !is5XX || counter == maxRetries - 1) {
                applyKeepAliveHeader(response, con);
                exc.getDestinations().clear();
                exc.getDestinations().add(dest);
                con.setExchange(exc);
                response.addObserver(con);
                exc.setResponse(response);
                return exc;
            }
        // java.net.SocketException: Software caused connection abort: socket write error
        } catch (ConnectException e) {
            exception = e;
            log.info("Connection to " + (target == null ? dest : target) + " refused.");
        } catch (SocketException e) {
            if (e.getMessage().contains("Software caused connection abort")) {
                log.info("Connection to " + dest + " was aborted externally. Maybe by the server or the OS Membrane is running on.");
            } else if (e.getMessage().contains("Connection reset")) {
                log.info("Connection to " + dest + " was reset externally. Maybe by the server or the OS Membrane is running on.");
            } else {
                logException(exc, counter, e);
            }
            exception = e;
        } catch (UnknownHostException e) {
            log.warn("Unknown host: " + (target == null ? dest : target));
            exception = e;
            if (exc.getDestinations().size() < 2) {
                // don't retry this host, it's useless. (it's very unlikely that it will work after timeBetweenTriesMs)
                break;
            }
        } catch (EOFWhileReadingFirstLineException e) {
            log.debug("Server connection to " + dest + " terminated before line was read. Line so far: " + e.getLineSoFar());
            exception = e;
        } catch (NoResponseException e) {
            throw e;
        } catch (Exception e) {
            logException(exc, counter, e);
            exception = e;
        } finally {
            if (trackNodeStatus) {
                if (exception != null) {
                    exc.setNodeException(counter, exception);
                }
            }
        }
        counter++;
        if (exc.getDestinations().size() == 1) {
            // as documented above, the sleep timeout is only applied between successive calls to the same destination.
            Thread.sleep(timeBetweenTriesMs);
        }
    }
    throw exception;
}
Also used : IOException(java.io.IOException) EndOfStreamException(com.predic8.membrane.core.util.EndOfStreamException) SSLProvider(com.predic8.membrane.core.transport.ssl.SSLProvider)

Aggregations

Response (com.predic8.membrane.core.http.Response)29 Exchange (com.predic8.membrane.core.exchange.Exchange)14 IOException (java.io.IOException)14 StringWriter (java.io.StringWriter)9 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)7 Request (com.predic8.membrane.core.http.Request)7 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)6 Header (com.predic8.membrane.core.http.Header)6 Test (org.junit.Test)6 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)4 Message (com.predic8.membrane.core.http.Message)4 JSONContent (com.predic8.membrane.core.interceptor.rest.JSONContent)4 ProxyRule (com.predic8.membrane.core.rules.ProxyRule)4 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)4 SQLException (java.sql.SQLException)4 Element (org.w3c.dom.Element)4 NodeList (org.w3c.dom.NodeList)4 JsonFactory (com.fasterxml.jackson.core.JsonFactory)3 MCElement (com.predic8.membrane.annot.MCElement)3 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)3