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

com.adobe.platform.operation.internal.http.BaseMultipartRequest Maven / Gradle / Ivy

The 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.http;

import com.adobe.platform.operation.internal.auth.AuthenticationMethod;
import com.adobe.platform.operation.internal.auth.Authenticator;
import com.adobe.platform.operation.internal.auth.SessionToken;
import com.adobe.platform.operation.internal.cpf.constants.RequestKey;
import com.damnhandy.uri.template.UriTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.adobe.platform.operation.internal.http.DefaultRequestHeaders.*;


/**
 * Returns a new instance of the multipart request on every setter
 */
public class BaseMultipartRequest implements MultiPartRequest {


    private UriTemplate uriTemplate;
    private HttpMethod httpMethod;
    private Map headers;
    private AuthenticationMethod authenticationMethod;
    private RequestType requestType;
    private List byteArrayParts;
    private StringBodyPart stringBodyPart;
    private Authenticator authenticator;
    private HttpRequestConfig requestConfig;
    private RequestKey requestKey;

    public BaseMultipartRequest(String uriTemplate) {
        this.headers = new HashMap<>();
        this.uriTemplate = UriTemplate.fromTemplate(uriTemplate);
        this.authenticationMethod = AuthenticationMethod.AUTH_HEADER_PRIMARY;
        this.requestType = RequestType.MULTIPART;
        this.httpMethod = HttpMethod.POST;

    }

    @Override
    public MultiPartRequest withContentType(String contentType) {
        return withHeader(CONTENT_TYPE_HEADER_NAME, contentType);
    }

    @Override
    public MultiPartRequest withHeader(String headerName, String headerValue) {
        headers.put(headerName, headerValue);
        return this;
    }

    @Override
    public MultiPartRequest withHeaders(Map headers) {
        Map headersCopy = getHeadersCopy(headers);
        this.headers.putAll(headersCopy);
        return this;
    }

    @Override
    public MultiPartRequest withUriParam(String name, String value) {
        this.uriTemplate.set(name, value);
        return this;
    }

    @Override
    public MultiPartRequest withBody(String body) {
        // Not applicable for multipart request
        throw new UnsupportedOperationException("Operation not supported");

    }

    @Override
    public MultiPartRequest withAuthenticationMethod(AuthenticationMethod authMethod) {
        this.authenticationMethod = authMethod;
        return this;
    }

    @Override
    public MultiPartRequest withTemplate(String uriTemplate) {
        this.uriTemplate = UriTemplate.fromTemplate(uriTemplate);
        return this;
    }

    @Override
    public MultiPartRequest withAcceptType(String acceptHeader) {
        return withHeader(ACCEPT_HEADER_NAME, acceptHeader);
    }

    @Override
    public MultiPartRequest withAuthenticator(Authenticator authenticator) {
        this.authenticator = authenticator;
        this.headers.put(DefaultRequestHeaders.X_API_KEY_HEADER_NAME, DefaultRequestHeaders.X_API_KEY_HEADER_VALUE);
        return this;
    }

    @Override
    public MultiPartRequest withConfig(HttpRequestConfig requestConfig) {
        this.requestConfig = requestConfig;
        return this;
    }

    @Override
    public HttpRequest withRequestKey(RequestKey requestKey) {
        this.requestKey = requestKey;
        return this;
    }

    /**
     * Only supports a single body part for now, based on the backend API. Internal implementation can be changed
     *
     * @param stringBodyPart
     * @return
     */
    @Override
    public MultiPartRequest withStringBodyPart(StringBodyPart stringBodyPart) {
        this.stringBodyPart = stringBodyPart;
        return this;
    }

    @Override
    public MultiPartRequest withByteArrayPart(ByteArrayPart byteArrayPart) {
        if (this.byteArrayParts == null) {
            this.byteArrayParts = new ArrayList<>();
        }
        this.byteArrayParts.add(byteArrayPart);
        return this;
    }

    @Override
    public void authenticate() {
        if (authenticationMethod.isAuthRequired()) {
            if (authenticator == null) {
                throw new IllegalStateException("No authenticator provided for request. Cannot add authorization headers");
            }
            SessionToken sessionToken = this.authenticator.getSessionToken();
            withHeader(AUTHORIZATION_HEADER_NAME, String.format("Bearer %s", sessionToken.getAccessToken()));
        }
    }

    @Override
    public void forceAuthenticate() {
        if (authenticationMethod.isAuthRequired()) {
            if (authenticator == null) {
                throw new IllegalStateException("No authenticator provided for request. Cannot add authorization headers");
            }
            SessionToken sessionToken = this.authenticator.refreshSessionToken();
            withHeader(AUTHORIZATION_HEADER_NAME, String.format("Bearer %s", sessionToken.getAccessToken()));
        }
    }

    @Override
    public HttpRequestConfig getConfig() {
        return requestConfig;
    }

    @Override
    public String getBody() {

        //not supported
        throw new UnsupportedOperationException("Operation not supported");
    }

    @Override
    public HttpMethod getHttpMethod() {
        return httpMethod;
    }


    @Override
    public AuthenticationMethod getAuthMethod() {
        return authenticationMethod;
    }

    @Override
    public Map getHeaders() {
        return headers;
    }

    @Override
    public String getFinalUri() {
        return this.uriTemplate.expand();
    }

    private Map getHeadersCopy(Map headersToCopy) {
        return headersToCopy.entrySet()
                .stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                        Map.Entry::getValue));
    }

    @Override
    public RequestType getRequestType() {
        return requestType;
    }

    @Override
    public Authenticator getAuthenticator() {
        return authenticator;
    }

    @Override
    public String getTemplateString() {
        return uriTemplate.getTemplate();
    }

    @Override
    public List getByteArrayParts() {
        return byteArrayParts;
    }

    @Override
    public StringBodyPart getStringBodyPart() {
        return stringBodyPart;
    }

    @Override
    public RequestKey getRequestKey() {
        return requestKey;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy