Example usage for java.nio ByteBuffer duplicate

List of usage examples for java.nio ByteBuffer duplicate

Introduction

In this page you can find the example usage for java.nio ByteBuffer duplicate.

Prototype

public abstract ByteBuffer duplicate();

Source Link

Document

Returns a duplicated buffer that shares its content with this buffer.

Usage

From source file:org.wso2.identity.iml.dsl.mediators.SAMLRequestProcessor.java

@Override
public boolean receive(CarbonMessage carbonMessage, CarbonCallback carbonCallback) throws Exception {

    if (log.isDebugEnabled()) {
        log.debug("Message received at " + getName());
    }//from w w  w  .  j a  v a  2  s .c o  m

    CarbonMessage newReq;

    byte[] bytes;

    String contentLength = carbonMessage.getHeader(Constants.HTTP_CONTENT_LENGTH);
    if (contentLength != null) {

        newReq = new DefaultCarbonMessage();
        bytes = new byte[Integer.parseInt(contentLength)];

        newReq.setHeaders(carbonMessage.getHeaders());
        carbonMessage.getProperties().forEach(newReq::setProperty);
        List<ByteBuffer> fullMessageBody = carbonMessage.getFullMessageBody();

        int offset = 0;

        for (ByteBuffer byteBuffer : fullMessageBody) {
            newReq.addMessageBody(byteBuffer);
            ByteBuffer duplicate = byteBuffer.duplicate();
            duplicate.get(bytes, offset, byteBuffer.capacity());
            offset = offset + duplicate.capacity();
        }
        newReq.setEndOfMsgAdded(true);

        String encodedRequest = new String(bytes);
        String urlDecodedRequest = URLDecoder.decode(encodedRequest.split("=", 2)[1],
                StandardCharsets.UTF_8.name());
        String decodedRequest = new String(Base64.getDecoder().decode(urlDecodedRequest));

        if (log.isDebugEnabled()) {
            log.debug("Decoded SAML request: " + decodedRequest);
        }

        AuthnRequest samlAuthnRequest = SAMLRequestParser(decodedRequest);

        RequestContext samlRequestContext = new RequestContext();
        samlRequestContext.setHeaders(carbonMessage.getHeaders());
        samlRequestContext.addContent("samlRequest", samlAuthnRequest);

        AuthenticationContext authenticationContext = new AuthenticationContext();
        authenticationContext.getRequestContextMap().put("saml", samlRequestContext);

        newReq.setProperty("authenticationContext", authenticationContext);

    } else {
        newReq = carbonMessage;
    }
    return next(newReq, carbonCallback);
}