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

com.hp.autonomy.hod.client.api.analysis.extractstructure.ExtractStructureServiceImpl Maven / Gradle / Ivy

There is a newer version: 0.25.3
Show newest version
/*
 * Copyright 2015-2016 Hewlett-Packard Development Company, L.P.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
 */

package com.hp.autonomy.hod.client.api.analysis.extractstructure;

import com.hp.autonomy.hod.client.api.authentication.AuthenticationToken;
import com.hp.autonomy.hod.client.api.authentication.EntityType;
import com.hp.autonomy.hod.client.api.authentication.TokenType;
import com.hp.autonomy.hod.client.config.HodServiceConfig;
import com.hp.autonomy.hod.client.config.Requester;
import com.hp.autonomy.hod.client.error.HodErrorException;
import com.hp.autonomy.hod.client.token.TokenProxy;
import com.hp.autonomy.hod.client.util.TypedByteArrayWithFilename;
import org.apache.commons.io.IOUtils;
import retrofit.client.Response;
import retrofit.mime.TypedFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;

public class ExtractStructureServiceImpl implements ExtractStructureService {
    private final ExtractStructureBackend extractStructureBackend;
    private final Requester requester;

    /**
     * Create a new ExtractStructureServiceImpl with the given configuration
     * @param hodServiceConfig The configuration to use
     */
    public ExtractStructureServiceImpl(final HodServiceConfig hodServiceConfig) {
        extractStructureBackend = hodServiceConfig.getRestAdapter().create(ExtractStructureBackend.class);
        requester = hodServiceConfig.getRequester();
    }

    @Override
    public List> extractFromFile(final byte[] bytes) throws HodErrorException {
        final Structure structure = requester.makeRequest(Structure.class, getByteArrayBackendCaller(bytes));
        return structure.getContent();
    }

    @Override
    public List> extractFromFile(final TokenProxy tokenProxy, final byte[] bytes) throws HodErrorException {
        final Structure structure = requester.makeRequest(tokenProxy, Structure.class, getByteArrayBackendCaller(bytes));
        return structure.getContent();
    }

    @Override
    public List> extractFromFile(final InputStream inputStream) throws HodErrorException {
        final Structure structure = requester.makeRequest(Structure.class, getInputStreamBackendCaller(inputStream));
        return structure.getContent();
    }

    @Override
    public List> extractFromFile(final TokenProxy tokenProxy, final InputStream inputStream) throws HodErrorException {
        final Structure structure = requester.makeRequest(tokenProxy, Structure.class, getInputStreamBackendCaller(inputStream));
        return structure.getContent();
    }

    @Override
    public List> extractFromFile(final File file) throws HodErrorException {
        final Structure structure = requester.makeRequest(Structure.class, getFileBackendCaller(file));
        return structure.getContent();
    }

    @Override
    public List> extractFromFile(final TokenProxy tokenProxy, final File file) throws HodErrorException {
        final Structure structure = requester.makeRequest(tokenProxy, Structure.class, getFileBackendCaller(file));
        return structure.getContent();
    }

    @Override
    public List> extractFromReference(final TokenProxy tokenProxy, final String reference) throws HodErrorException {
        final Structure structure = requester.makeRequest(tokenProxy, Structure.class, getReferenceBackendCaller(reference));
        return structure.getContent();
    }

    @Override
    public List> extractFromReference(final String reference) throws HodErrorException {
        final Structure structure = requester.makeRequest(Structure.class, getReferenceBackendCaller(reference));
        return structure.getContent();
    }

    @Override
    public List> extractFromUrl(final TokenProxy tokenProxy, final String url) throws HodErrorException {
        final Structure structure = requester.makeRequest(tokenProxy, Structure.class, getUrlBackendCaller(url));
        return structure.getContent();
    }

    @Override
    public List> extractFromUrl(final String url) throws HodErrorException {
        final Structure structure = requester.makeRequest(Structure.class, getUrlBackendCaller(url));
        return structure.getContent();
    }

    private Requester.BackendCaller getByteArrayBackendCaller(final byte[] file) {
        return authenticationToken -> extractStructureBackend.extractFromFile(authenticationToken, new TypedByteArrayWithFilename("text/plain", file));
    }

    private Requester.BackendCaller getInputStreamBackendCaller(final InputStream inputStream) {
        return authenticationToken -> {
            try {
                return extractStructureBackend.extractFromFile(authenticationToken, new TypedByteArrayWithFilename("text/plain", IOUtils.toByteArray(inputStream)));
            } catch (final IOException e) {
                throw new RuntimeException("Error reading bytes from stream", e);
            }
        };
    }

    private Requester.BackendCaller getFileBackendCaller(final File file) {
        return authenticationToken -> extractStructureBackend.extractFromFile(authenticationToken, new TypedFile("text/plain", file));
    }

    private Requester.BackendCaller getReferenceBackendCaller(final String reference) {
        return authenticationToken -> extractStructureBackend.extractFromReference(authenticationToken, reference);
    }

    private Requester.BackendCaller getUrlBackendCaller(final String url) {
        return authenticationToken -> extractStructureBackend.extractFromUrl(authenticationToken, url);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy