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

io.github.factoryfx.microservice.rest.client.MicroserviceRestClient Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package io.github.factoryfx.microservice.rest.client;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Locale;
import java.util.function.Supplier;

import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;

import io.github.factoryfx.factory.FactoryBase;
import io.github.factoryfx.factory.FactoryTreeBuilderBasedAttributeSetup;
import io.github.factoryfx.factory.log.FactoryUpdateLog;
import io.github.factoryfx.factory.merge.MergeDiffInfo;
import io.github.factoryfx.factory.storage.DataUpdate;
import io.github.factoryfx.factory.storage.StoredDataMetadata;
import io.github.factoryfx.microservice.common.CheckUserResponse;
import io.github.factoryfx.microservice.common.MicroserviceResourceApi;
import io.github.factoryfx.microservice.common.UserAwareRequest;
import io.github.factoryfx.microservice.common.UserLocaleResponse;
import io.github.factoryfx.microservice.common.VoidUserAwareRequest;
import io.github.factoryfx.server.Microservice;
import jakarta.ws.rs.InternalServerErrorException;


/**
 *
 * @param  Root factory
 */
public class MicroserviceRestClient> {

    private final MicroserviceResourceApi microserviceResourceApi;
    private final String user;
    private final String passwordHash;
    private final FactoryTreeBuilderBasedAttributeSetup factoryTreeBuilderBasedAttributeSetup;

    public MicroserviceRestClient(MicroserviceResourceApi microserviceResourceApi, String user, String passwordHash, FactoryTreeBuilderBasedAttributeSetup factoryTreeBuilderBasedAttributeSetup) {
        this.microserviceResourceApi = microserviceResourceApi;
        this.user=user;
        this.passwordHash=passwordHash;
        this.factoryTreeBuilderBasedAttributeSetup = factoryTreeBuilderBasedAttributeSetup;
    }

    public FactoryUpdateLog updateCurrentFactory(DataUpdate update, String comment) {
        try {
            DataUpdate updateMetadata = new DataUpdate<>(
                    update.root,
                    update.user,
                    comment,
                    update.baseVersionId
            );

            return microserviceResourceApi.updateCurrentFactory(new UserAwareRequest<>(user, passwordHash, updateMetadata));
        } catch (InternalServerErrorException e) {
            String respString = e.getResponse().readEntity(String.class);
            return new FactoryUpdateLog<>(respString);
        }
    }

    public MergeDiffInfo simulateUpdateCurrentFactory(DataUpdate update) {
        return executeWithServerExceptionReporting(()-> microserviceResourceApi.simulateUpdateCurrentFactory(new UserAwareRequest<>(user,passwordHash,update)));
    }

    /**
     * @see Microservice#prepareNewFactory()
     *
     * @return new factory for editing, server assign new id for the update
     */
    public DataUpdate prepareNewFactory() {
        DataUpdate update = executeWithServerExceptionReporting(()-> microserviceResourceApi.prepareNewFactory(new VoidUserAwareRequest(user,passwordHash)));
        update.root.internal().finalise();
        if (factoryTreeBuilderBasedAttributeSetup!=null){
            factoryTreeBuilderBasedAttributeSetup.applyToRootFactoryDeep(update.root);
        }
        if (factoryTreeBuilderBasedAttributeSetup!=null){
            factoryTreeBuilderBasedAttributeSetup.applyToRootFactoryDeep(update.root);
        }

        return update;
    }

    public MergeDiffInfo getDiff(StoredDataMetadata historyEntry) {
        return executeWithServerExceptionReporting(()-> microserviceResourceApi.getDiff(new UserAwareRequest<>(user, passwordHash, historyEntry)));
    }


    public R getHistoryFactory(String id) {
        R historyFactory = executeWithServerExceptionReporting(()-> microserviceResourceApi.getHistoryFactory(new UserAwareRequest<>(user, passwordHash, id))).value;
        historyFactory.internal().finalise();
        return historyFactory;
    }

    public Collection getHistoryFactoryList() {
        return executeWithServerExceptionReporting(()-> microserviceResourceApi.getHistoryFactoryList(new VoidUserAwareRequest(user, passwordHash)));
    }

    public CheckUserResponse checkUser() {
        return executeWithServerExceptionReporting(()-> microserviceResourceApi.checkUser(new VoidUserAwareRequest(user,passwordHash)));
    }

    public Locale getLocale() {
        UserLocaleResponse response = executeWithServerExceptionReporting(()-> microserviceResourceApi.getUserLocale(new VoidUserAwareRequest(user,passwordHash)));
        return response.locale;
    }

    public FactoryUpdateLog revert(StoredDataMetadata historyFactory) {
        return executeWithServerExceptionReporting(()-> microserviceResourceApi.revert(new UserAwareRequest<>(user,passwordHash,historyFactory)));
    }

    /**execute a jersey proxy client action and get server error  */
    private  T executeWithServerExceptionReporting(Supplier action){
        try {
            return action.get();
        } catch (InternalServerErrorException e) {

            String respString= null;
            if (e.getResponse().getEntity() instanceof ByteArrayInputStream ){
                try {
                    respString = CharStreams.toString(new InputStreamReader(((ByteArrayInputStream)e.getResponse().getEntity()), Charsets.UTF_8));
                } catch (IOException e1) {
                    throw new RuntimeException(e1);
                }
            }
            throw new RuntimeException("Server exception:\n----------------"+respString+"\n----------------",e);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy