use of com.predic8.membrane.core.http.Header 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.Header in project service-proxy by membrane.
the class LoginInterceptor method handleResponse.
@Override
public Outcome handleResponse(Exchange exc) throws Exception {
Header header = exc.getResponse().getHeader();
header.setNoCacheResponseHeaders();
return super.handleResponse(exc);
}
use of com.predic8.membrane.core.http.Header 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);
}
use of com.predic8.membrane.core.http.Header 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);
}
use of com.predic8.membrane.core.http.Header in project service-proxy by membrane.
the class XOPReconstitutor method split.
@SuppressWarnings("deprecation")
private HashMap<String, Part> split(Message message, String boundary) throws IOException, EndOfStreamException, MalformedStreamException {
HashMap<String, Part> parts = new HashMap<String, Part>();
MultipartStream multipartStream = new MultipartStream(MessageUtil.getContentAsStream(message), boundary.getBytes(Constants.UTF_8_CHARSET));
boolean nextPart = multipartStream.skipPreamble();
while (nextPart) {
Header header = new Header(multipartStream.readHeaders());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
multipartStream.readBodyData(baos);
// see http://www.iana.org/assignments/transfer-encodings/transfer-encodings.xml
String cte = header.getFirstValue("Content-Transfer-Encoding");
if (cte != null && !cte.equals("binary") && !cte.equals("8bit") && !cte.equals("7bit"))
throw new RuntimeException("Content-Transfer-Encoding '" + cte + "' not implemented.");
Part part = new Part(header, baos.toByteArray());
String id = part.getContentID();
if (id != null) {
parts.put(id, part);
}
nextPart = multipartStream.readBoundary();
}
return parts;
}
Aggregations