All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marklogic.client.dataservices.impl.CallContextImpl Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2024 MarkLogic Corporation. All Rights Reserved.
 */
package com.marklogic.client.dataservices.impl;

import com.marklogic.client.DatabaseClient;
import com.marklogic.client.SessionState;
import com.marklogic.client.dataservices.IOEndpoint;
import com.marklogic.client.impl.DatabaseClientImpl;
import com.marklogic.client.impl.NodeConverter;
import com.marklogic.client.impl.Utilities;
import com.marklogic.client.io.BytesHandle;
import com.marklogic.client.io.marker.BufferableHandle;
import com.marklogic.client.io.marker.ContentHandle;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

class CallContextImpl implements IOEndpoint.CallContext {
    private final IOEndpointImpl endpoint;
    private final boolean legacyContext;

    private BytesHandle endpointState;
    private BytesHandle endpointConstants;
    private SessionState session;

    CallContextImpl(IOEndpointImpl endpoint, boolean legacyContext) {
        this.endpoint = endpoint;
        this.legacyContext = legacyContext;
        if (endpoint.allowsEndpointState()) {
            endpointState = new BytesHandle().withFormat(endpoint.getEndpointStateParamdef().getFormat());
        }
        if (endpoint.allowsEndpointConstants()) {
            endpointConstants = new BytesHandle().withFormat(endpoint.getEndpointConstantsParamdef().getFormat());
        }
    }

    IOEndpointImpl getEndpoint() {
        return endpoint;
    }

    DatabaseClient getClient() {
        return getEndpoint().getClient();
    }

    boolean isLegacyContext() {
        return legacyContext;
    }
    String getEndpointConstantsParamName() {
        return isLegacyContext() ? "workUnit" : "endpointConstants";
    }

    @Override
    public BytesHandle getEndpointState() {
        return this.endpointState;
    }
    InputStream getEndpointStateAsInputStream() {
        byte[] bytes = this.endpointState.get();
        return (bytes == null || bytes.length == 0) ? null : new ByteArrayInputStream(bytes);
    }
    @Override
    public CallContextImpl withEndpointState(BufferableHandle endpointState) {
        return assignBytes(this.endpointState, endpointState);
    }
    CallContextImpl withEndpointStateAs(InputStream endpointState) {
        return assignBytes(this.endpointState, endpointState);
    }
    @Override
    public CallContextImpl withEndpointStateAs(Object endpointState) {
        return assignBytes(this.endpointState, endpointState);
    }

    @Override
    public BytesHandle getEndpointConstants() {
        return this.endpointConstants;
    }
    @Override
    public CallContextImpl withEndpointConstants(BufferableHandle constants) {
        return assignBytes(this.endpointConstants, constants);
    }
    CallContextImpl withEndpointConstantsAs(InputStream contants) {
        return assignBytes(this.endpointConstants, contants);
    }
    @Override
    public CallContextImpl withEndpointConstantsAs(Object constants) {
        return assignBytes(this.endpointConstants, constants);
    }

    @Override
    public SessionState getSessionState() {
        return this.session;
    }
    @Override
    public CallContextImpl withSessionState(SessionState sessionState) {
        this.session = sessionState;
        return this;
    }

    private CallContextImpl assignBytes(BytesHandle bytesHandle, byte[] bytes) {
        if (bytesHandle == null) {
            throw new IllegalArgumentException("cannot configure");
        }
        bytesHandle.set((bytes == null || bytes.length == 0) ? null : bytes);
        return this;
    }
    private CallContextImpl assignBytes(BytesHandle bytesHandle, BufferableHandle bufferableHandle) {
        return assignBytes(bytesHandle, (bufferableHandle == null) ? null : bufferableHandle.toBuffer());
    }
    private CallContextImpl assignBytes(BytesHandle bytesHandle, InputStream inputStream) {
        return assignBytes(bytesHandle, NodeConverter.InputStreamToBytes(inputStream));
    }
    private CallContextImpl assignBytes(BytesHandle bytesHandle, Object content) {
        if (content == null) {
            return assignBytes(bytesHandle, (byte[]) null);
        } else if (content instanceof byte[]) {
            return assignBytes(bytesHandle, (byte[]) content);
        }

        Class as = content.getClass();
        ContentHandle handle =
                ((DatabaseClientImpl) getClient()).getHandleRegistry().makeHandle(as);

        Utilities.setHandleContent(handle, content);
        if (!(handle instanceof BufferableHandle)) {
            throw new IllegalArgumentException("content handle must be bufferable");
        }

        return assignBytes(bytesHandle, (BufferableHandle) handle);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy