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

org.zalando.riptide.UrlResolution Maven / Gradle / Ivy

The newest version!
package org.zalando.riptide;

import org.apiguardian.api.API;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Nullable;
import java.net.URI;

import static com.google.common.base.Strings.nullToEmpty;
import static org.apiguardian.api.API.Status.EXPERIMENTAL;

@API(status = EXPERIMENTAL)
public enum UrlResolution {

    /**
     * Resolves a given relative URI by following the rules defined in RFC 3986.
     *
     * @see RFC 3986
     * @see URI#resolve(URI)
     */
    RFC {
        @Override
        URI resolve(final URI baseUrl, final URI uri) {
            return baseUrl.resolve(uri);
        }
    },

    /**
     * Resolves a given relative URI by appending its path to and merging its query parameters with the given base URL.
     *
     * @see UriComponentsBuilder#pathSegment(String...)
     */
    APPEND {
        @Override
        URI resolve(final URI baseUrl, final URI uri) {
            final UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseUrl);

            @Nullable final String path = uri.getRawPath();

            if (!nullToEmpty(path).isEmpty()) {
                builder.path("/").path(path);
            }

            return builder
                    .replaceQuery(uri.getRawQuery())
                    .build()
                    .toUri();
        }
    };

    /**
     *
     * @param baseUrl absolute, non-empty base URL
     * @param uri non-absolute
     * @return the resolved, absolute request URI
     */
    abstract URI resolve(final URI baseUrl, final URI uri);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy