Search in sources :

Example 1 with XmlPayloadIO

use of io.apiman.gateway.engine.io.XmlPayloadIO in project apiman by apiman.

the class ApiRequestExecutorImpl method parsePayload.

/**
 * Parse the inbound request's body into a payload object.  The object that is
 * produced will depend on the type and content-type of the API.  Options
 * include, but may not be limited to:
 * <ul>
 *   <li>REST+json</li>
 *   <li>REST+xml</li>
 *   <li>SOAP+xml</li>
 * </ul>
 * @param payloadResultHandler
 */
protected void parsePayload(IAsyncResultHandler<Object> payloadResultHandler) {
    // Strip out any content-length header from the request.  It will very likely
    // no longer be accurate.
    // $NON-NLS-1$
    request.getHeaders().remove("Content-Length");
    // Configure the api's max payload buffer size, if it's not already set.
    if (api.getMaxPayloadBufferSize() <= 0) {
        api.setMaxPayloadBufferSize(maxPayloadBufferSize);
    }
    // Now "handle" the inbound request stream, which will cause bytes to be streamed
    // to the writeStream we provide (which will store the bytes in a buffer for parsing)
    final ByteBuffer buffer = new ByteBuffer(2048);
    inboundStreamHandler.handle(new ISignalWriteStream() {

        private boolean done = false;

        @Override
        public void abort(Throwable t) {
            done = true;
            // $NON-NLS-1$
            payloadResultHandler.handle(AsyncResultImpl.create(new RuntimeException("Inbound request stream aborted.", t)));
        }

        @Override
        public boolean isFinished() {
            return done;
        }

        @Override
        public void write(IApimanBuffer chunk) {
            if (done) {
                return;
            }
            if (buffer.length() > api.getMaxPayloadBufferSize()) {
                // $NON-NLS-1$
                payloadResultHandler.handle(AsyncResultImpl.create(new Exception("Max request payload size exceeded.")));
                done = true;
                return;
            }
            buffer.append(chunk);
        }

        @Override
        public void end() {
            if (done) {
                return;
            }
            // When end() is called, the stream of bytes is done and we can parse them into
            // an appropriate payload object.
            done = true;
            if (buffer.length() == 0) {
                payloadResultHandler.handle(AsyncResultImpl.create(null));
            } else {
                payloadIO = null;
                if ("soap".equalsIgnoreCase(api.getEndpointType())) {
                    // $NON-NLS-1$
                    payloadIO = new SoapPayloadIO();
                } else if ("rest".equalsIgnoreCase(api.getEndpointType())) {
                    // $NON-NLS-1$
                    if ("xml".equalsIgnoreCase(api.getEndpointContentType())) {
                        // $NON-NLS-1$
                        payloadIO = new XmlPayloadIO();
                    } else if ("json".equalsIgnoreCase(api.getEndpointContentType())) {
                        // $NON-NLS-1$
                        payloadIO = new JsonPayloadIO();
                    }
                }
                if (payloadIO == null) {
                    payloadIO = new BytesPayloadIO();
                }
                try {
                    Object payload = payloadIO.unmarshall(buffer.getBytes());
                    payloadResultHandler.handle(AsyncResultImpl.create(payload));
                } catch (Exception e) {
                    // $NON-NLS-1$
                    payloadResultHandler.handle(AsyncResultImpl.create(new Exception("Failed to parse inbound request payload.", e)));
                }
            }
        }

        /**
         * Because of the way the parsePayload code was written, it's possible
         * with async for apiConnection to be +null+ when this is called.
         *
         * This is because parsePayload invokes inboundStreamHandler before
         * policiesLoadedHandler has had a chance to return (and assigned apiConnection).
         *
         * To work around this we check whether apiConnection is null for #drainHandler
         * and #isFull.
         */
        @Override
        public void drainHandler(IAsyncHandler<Void> drainHandler) {
            if (apiConnection != null)
                apiConnection.drainHandler(drainHandler);
        }

        @Override
        public boolean isFull() {
            if (apiConnection != null) {
                return apiConnection.isFull();
            } else {
                return false;
            }
        }
    });
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) BytesPayloadIO(io.apiman.gateway.engine.io.BytesPayloadIO) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream) ByteBuffer(io.apiman.gateway.engine.io.ByteBuffer) JsonPayloadIO(io.apiman.gateway.engine.io.JsonPayloadIO) RequestAbortedException(io.apiman.gateway.engine.beans.exceptions.RequestAbortedException) InvalidApiException(io.apiman.gateway.engine.beans.exceptions.InvalidApiException) InvalidContractException(io.apiman.gateway.engine.beans.exceptions.InvalidContractException) ApiNotFoundException(io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException) SoapPayloadIO(io.apiman.gateway.engine.io.SoapPayloadIO) XmlPayloadIO(io.apiman.gateway.engine.io.XmlPayloadIO)

Aggregations

ApiNotFoundException (io.apiman.gateway.engine.beans.exceptions.ApiNotFoundException)1 InvalidApiException (io.apiman.gateway.engine.beans.exceptions.InvalidApiException)1 InvalidContractException (io.apiman.gateway.engine.beans.exceptions.InvalidContractException)1 RequestAbortedException (io.apiman.gateway.engine.beans.exceptions.RequestAbortedException)1 ByteBuffer (io.apiman.gateway.engine.io.ByteBuffer)1 BytesPayloadIO (io.apiman.gateway.engine.io.BytesPayloadIO)1 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)1 ISignalWriteStream (io.apiman.gateway.engine.io.ISignalWriteStream)1 JsonPayloadIO (io.apiman.gateway.engine.io.JsonPayloadIO)1 SoapPayloadIO (io.apiman.gateway.engine.io.SoapPayloadIO)1 XmlPayloadIO (io.apiman.gateway.engine.io.XmlPayloadIO)1