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

io.sphere.sdk.commands.CreateCommandImpl Maven / Gradle / Ivy

There is a newer version: 1.0.0-M26
Show newest version
package io.sphere.sdk.commands;

import com.fasterxml.jackson.databind.JavaType;
import io.sphere.sdk.client.HttpRequestIntent;
import io.sphere.sdk.http.HttpMethod;
import io.sphere.sdk.client.JsonEndpoint;
import io.sphere.sdk.json.SphereJsonUtils;

import static io.sphere.sdk.json.SphereJsonUtils.toJsonString;
import static java.util.Objects.requireNonNull;

/**
 * Base class to implement commands which create an entity in SPHERE.IO.
 *
 * @param  the type of the result of the command, most likely the updated entity without expanded references
 * @param  class which will serialized as JSON command body, most likely a template
 */
public abstract class CreateCommandImpl extends CommandImpl implements CreateCommand{

    private final C body;
    private final String path;
    private final JavaType javaType;

    public CreateCommandImpl(final C draft, final String path, final JavaType javaType) {
        this.body = requireNonNull(draft);
        this.path = requireNonNull(path);
        this.javaType = requireNonNull(javaType);
    }

    public CreateCommandImpl(final C draft, final JsonEndpoint endpoint) {
        this(draft, endpoint.endpoint(), SphereJsonUtils.convertToJavaType(endpoint.typeReference()));
    }

    @Override
    public HttpRequestIntent httpRequestIntent() {
        return HttpRequestIntent.of(httpMethod(), path, httpBody());
    }

    protected HttpMethod httpMethod() {
        return HttpMethod.POST;
    }

    protected String httpBody() {
        return toJsonString(body);
    }

    @Override
    protected JavaType jacksonJavaType() {
        return javaType;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy