Search in sources :

Example 6 with Request

use of com.predic8.membrane.core.http.xml.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());
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Response(com.predic8.membrane.core.http.Response) CacheBuilder(com.google.common.cache.CacheBuilder)

Example 7 with Request

use of com.predic8.membrane.core.http.xml.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);
}
Also used : Response(com.predic8.membrane.core.http.Response) Header(com.predic8.membrane.core.http.Header) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Example 8 with Request

use of com.predic8.membrane.core.http.xml.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;
}
Also used : Request(com.predic8.membrane.core.http.xml.Request)

Example 9 with Request

use of com.predic8.membrane.core.http.xml.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;
}
Also used : Outcome(com.predic8.membrane.core.interceptor.Outcome)

Example 10 with Request

use of com.predic8.membrane.core.http.xml.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));
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) Request(com.predic8.membrane.core.http.xml.Request) StringReader(java.io.StringReader)

Aggregations

Request (com.predic8.membrane.core.http.Request)39 Exchange (com.predic8.membrane.core.exchange.Exchange)20 Test (org.junit.Test)12 IOException (java.io.IOException)11 Header (com.predic8.membrane.core.http.Header)8 Response (com.predic8.membrane.core.http.Response)8 AbstractExchange (com.predic8.membrane.core.exchange.AbstractExchange)5 Message (com.predic8.membrane.core.http.Message)5 Outcome (com.predic8.membrane.core.interceptor.Outcome)5 Body (com.predic8.membrane.core.http.Body)3 Request (com.predic8.membrane.core.http.xml.Request)3 EndOfStreamException (com.predic8.membrane.core.util.EndOfStreamException)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 CacheBuilder (com.google.common.cache.CacheBuilder)2 HeaderField (com.predic8.membrane.core.http.HeaderField)2 ResponseBuilder (com.predic8.membrane.core.http.Response.ResponseBuilder)2 ResolverMap (com.predic8.membrane.core.resolver.ResolverMap)2 HttpClient (com.predic8.membrane.core.transport.http.HttpClient)2 StringReader (java.io.StringReader)2 SocketException (java.net.SocketException)2