org.opentestsystem.ap.irs.client.IvsClient.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.ap.irs.client.IvsClient.java

Source

/*
 * Copyright 2017 Regents of the University of California. Licensed under the Educational Community License, Version  2.0 (the "license"); you may not use this file except in compliance with the License. You may obtain a copy of the license at
 *
 * https://opensource.org/licenses/ECL-2.0
 *
 * Unless required under applicable law or agreed to in writing, software distributed under the License is distributed in an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for specific language governing permissions and limitations under the license.
 */

package org.opentestsystem.ap.irs.client;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.opentestsystem.ap.irs.config.IvsServiceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * Created by alexponce on 6/21/17.
 */
@Component
public class IvsClient {

    private final IvsServiceProperties ivsServiceProps;

    @Autowired
    public IvsClient(final IvsServiceProperties ivsProps) {
        ivsServiceProps = ivsProps;
    }

    /**
     * Generates a URL to the item viewer service.
     * <p>
     * Example: http://localhost:8200/item/187-205684
     *
     * @param itemDirectory The folder IVS should look in for the items.
     * @param bankKey       The bank key associated with the items.
     * @param itemId        The item to represent in the URL.
     * @return A URL to the item view service.
     */
    public String getRenderUrl(String itemDirectory, String bankKey, String itemId) {
        String loadFrom = ivsServiceProps.getInternalContentDir() + '/' + itemDirectory;
        return ivsServiceProps.getExternalHost() + ivsServiceProps.getItemMapping() + "/" + bankKey + "-" + itemId
                + "?loadFrom=" + loadFrom;
    }

    /**
     * Generates a URL to the item viewer service.  The URL generated is referred to as "loading a performance task
     * item" in the IVS docs.
     * <p>
     * Example: http://localhost:8200/items?ids=187-100001,187-200002&loadFrom=/home/tomcat7/content/187
     * <p>
     * The key to the URL is the 'ids' query parameter where it lists multiple items.
     *
     * @param itemDirectory The folder IVS should look in for the items.
     * @param bankKey       The bank key associated with the items.
     * @param itemIdList    The list of items to represent in the URL.
     * @return A URL to the item view service.
     */
    public String getRenderUrl(final String itemDirectory, final String bankKey, final List<String> itemIdList) {
        final String urlPattern = ivsServiceProps.getPerformanceTaskItemUrlPattern();
        final String idsQueryParamValue = generateIdString(bankKey, itemIdList);
        final String loadFromQueryParamValue = ivsServiceProps.getInternalContentDir() + '/' + itemDirectory;
        return String.format(urlPattern, ivsServiceProps.getExternalHost(), idsQueryParamValue,
                loadFromQueryParamValue);
    }

    /**
     * Generates a comma separated string.  The values in the generated string are the values in the 'ids' list with the
     * bnankkey prefixed to it.  The format is bankkey-id.  For example 187-100001.
     * <p>
     * An example result is "187-100001,187-200002"
     *
     * @param bankKey The bank key to prefix the ids with.
     * @param ids     The ids to be prefixed.
     * @return A comma separated string.
     */
    public String generateIdString(final String bankKey, final List<String> ids) {
        final Function<String, String> appendBankKey = id -> String.format("%s-%s", bankKey, id);
        final List<String> newIds = ids.stream().map(appendBankKey).collect(toList());
        return String.join(",", newIds);
    }
}