use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.
the class OAuth2ResourceInterceptor method refreshAccessToken.
private void refreshAccessToken(Session session) throws Exception {
if (!refreshingOfAccessTokenIsNeeded(session))
return;
OAuth2AnswerParameters oauth2Params = OAuth2AnswerParameters.deserialize(session.getUserAttributes().get(OAUTH2_ANSWER));
Exchange refreshTokenExchange = new Request.Builder().post(auth.getTokenEndpoint()).header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded").header(Header.ACCEPT, "application/json").header(Header.USER_AGENT, Constants.USERAGENT).body("&grant_type=refresh_token" + "&refresh_token=" + oauth2Params.getRefreshToken()).buildExchange();
Response refreshTokenResponse = auth.doRequest(refreshTokenExchange);
if (!refreshTokenResponse.isOk()) {
refreshTokenResponse.getBody().read();
throw new RuntimeException("Statuscode from authorization server for refresh token request: " + refreshTokenResponse.getStatusCode());
}
HashMap<String, String> json = Util.parseSimpleJSONResponse(refreshTokenResponse);
if (json.get("access_token") == null || json.get("refresh_token") == null) {
refreshTokenResponse.getBody().read();
throw new RuntimeException("Statuscode was ok but no access_token and refresh_token was received: " + refreshTokenResponse.getStatusCode());
}
oauth2Params.setAccessToken(json.get("access_token"));
oauth2Params.setRefreshToken(json.get("refresh_token"));
oauth2Params.setExpiration(json.get("expires_in"));
oauth2Params.setReceivedAt(LocalDateTime.now());
if (json.containsKey("id_token")) {
if (idTokenIsValid(json.get("id_token")))
oauth2Params.setIdToken(json.get("id_token"));
else
oauth2Params.setIdToken("INVALID");
}
session.getUserAttributes().put(OAUTH2_ANSWER, oauth2Params.serialize());
}
use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.
the class RateLimitInterceptor method setResponseToServiceUnavailable.
public void setResponseToServiceUnavailable(Exchange exc) 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(rateLimitStrategy.requestLimitDuration.toPeriod()));
hd.add("X-LimitRequests", Integer.toString(rateLimitStrategy.requestLimit));
String ip = exc.getRemoteAddrIp();
DateTime availableAgainDateTime = rateLimitStrategy.getServiceAvailableAgainTime(ip);
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(rateLimitStrategy.requestLimit).append(" requests per ").append(PeriodFormat.getDefault().print(rateLimitStrategy.requestLimitDuration.toPeriod())).append(". The next request can be made at ").append(dtFormatter.print(availableAgainDateTime));
Response resp = ResponseBuilder.newInstance().status(429, "Too Many Requests.").contentType(MimeType.TEXT_PLAIN_UTF8).header(hd).body(bodyString.toString()).build();
exc.setResponse(resp);
}
use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.
the class HTTP2XMLInterceptor method handleRequest.
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
log.debug("uri: " + exc.getRequest().getUri());
String res = new Request(exc.getRequest()).toXml();
log.debug("http-xml: " + res);
exc.getRequest().setBodyContent(res.getBytes("UTF-8"));
// TODO
exc.getRequest().setMethod("POST");
exc.getRequest().getHeader().setSOAPAction("");
return Outcome.CONTINUE;
}
use of com.predic8.membrane.core.http.Request in project service-proxy by membrane.
the class RESTInterceptor 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 SOAPRESTHelper method getRequestXMLSource.
protected StreamSource getRequestXMLSource(Exchange exc) throws Exception {
Request req = new Request(exc.getRequest());
String res = req.toXml();
log.debug("http-xml: " + res);
return new StreamSource(new StringReader(res));
}
Aggregations