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

com.adobe.platform.operation.internal.CPFServiceRequestContext Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/*
 * Copyright 2019 Adobe
 * All Rights Reserved.
 *
 * NOTICE: Adobe permits you to use, modify, and distribute this file in
 * accordance with the terms of the Adobe license agreement accompanying
 * it. If you have received this file from a source other than Adobe,
 * then your use, modification, or distribution of it requires the prior
 * written permission of Adobe.
 */

package com.adobe.platform.operation.internal;

import java.util.HashMap;
import java.util.Map;

import com.adobe.platform.operation.internal.http.BaseHttpRequest;
import com.adobe.platform.operation.internal.http.BaseMultipartRequest;
import com.adobe.platform.operation.internal.http.DefaultRequestHeaders;
import com.adobe.platform.operation.internal.http.HttpMethod;
import com.adobe.platform.operation.internal.http.HttpRequest;
import com.adobe.platform.operation.internal.http.RequestType;
import com.adobe.platform.operation.internal.cpf.constants.RequestKey;

public class CPFServiceRequestContext {

    private static final String ACCEPT_HEADER_VALUE = "application/json, text/plain, */*";

    private Map baseRequestMap;

    public CPFServiceRequestContext(String cpfServicePredictUri, String cpfServiceOpsCreateUri) {
        baseRequestMap = new HashMap<>();
        baseRequestMap.put(RequestKey.PREDICT, new BaseMultipartRequest(cpfServicePredictUri)
                .withRequestKey(RequestKey.PREDICT));
        baseRequestMap.put(RequestKey.CREATE, new BaseMultipartRequest(cpfServiceOpsCreateUri)
                .withRequestKey(RequestKey.CREATE));
        baseRequestMap.put(RequestKey.STATUS, new BaseHttpRequest(HttpMethod.GET)
                .withRequestKey(RequestKey.STATUS));
    }

    public synchronized HttpRequest getBaseRequest(RequestKey requestKey) {
        HttpRequest baseRequest = baseRequestMap.get(requestKey);
        return copy(baseRequest);
    }

    private HttpRequest copy(HttpRequest httpRequest) {
        RequestType requestType = httpRequest.getRequestType();
        HttpRequest httpRequestCopy = null;
        switch (requestType) {
            case REGULAR:
                httpRequestCopy = new BaseHttpRequest(httpRequest.getHttpMethod())
                        .withRequestKey(httpRequest.getRequestKey())
                        .withHeaders(getV2DefaultHeaders());
                break;
            case MULTIPART:
                httpRequestCopy = new BaseMultipartRequest(httpRequest.getTemplateString())
                        .withRequestKey(httpRequest.getRequestKey())
                        .withHeaders(getV2DefaultHeaders());
                break;
        }

        return httpRequestCopy;
    }

    private Map getV2DefaultHeaders() {
        Map headers = new HashMap<>();
        headers.put(DefaultRequestHeaders.ACCEPT_HEADER_NAME, ACCEPT_HEADER_VALUE);
        headers.put(DefaultRequestHeaders.DC_APP_INFO_HEADER_KEY, GlobalConfig.getAppInfo());
        return headers;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy