use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.
the class DynamicAdminPageInterceptor method handleRequest.
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
log.debug("request: " + exc.getOriginalRequestUri());
exc.setTimeReqSent(System.currentTimeMillis());
Outcome o = dispatchRequest(exc);
exc.setReceived();
exc.setTimeResReceived(System.currentTimeMillis());
return o;
}
use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.
the class AdminRESTInterceptor method getBeautifiedBody.
@Mapping("/admin/web/exchanges/(-?\\d+)/(response|request)/body")
public Response getBeautifiedBody(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();
if (msg == null || msg.isBodyEmpty()) {
return Response.noContent().build();
}
return Response.ok().contentType(MimeType.TEXT_HTML_UTF8).body(TextUtil.formatXML(new InputStreamReader(msg.getBodyAsStreamDecoded(), msg.getCharset()), true)).build();
}
use of com.predic8.membrane.core.http.Request 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();
}
});
}
use of com.predic8.membrane.core.http.Request 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();
}
use of com.predic8.membrane.core.http.Request 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);
}
Aggregations