net.paslavsky.springrest.UriBuilder.java Source code

Java tutorial

Introduction

Here is the source code for net.paslavsky.springrest.UriBuilder.java

Source

/*
 * Copyright (c) 2014 Andrey Paslavsky.
 *
 * Licensed under the Apache 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.paslavsky.springrest;

import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * This class helps to build a new {@link URI} using {@link RestMethodMetadata}
 *
 * @author Andrey Paslavsky
 * @version 1.0
 */
public class UriBuilder {
    private final Object[] arguments;
    private UriComponentsBuilder builder;
    private final URI baseUrl;

    public UriBuilder(URI baseUrl, Object[] arguments) {
        this.baseUrl = baseUrl;
        this.arguments = arguments;
    }

    public URI build(RestMethodMetadata metadata) {
        builder = newBuilder(metadata);
        addQueryParameters(metadata);
        return builder.buildAndExpand(getUriVariables(metadata)).toUri();
    }

    private UriComponentsBuilder newBuilder(RestMethodMetadata metadata) {
        UriComponentsBuilder builder = null;

        if (baseUrl != null) {
            builder = UriComponentsBuilder.fromUri(baseUrl);
        }

        builder = createOrAdd(builder, metadata.getCommonPath());
        builder = createOrAdd(builder, metadata.getAdditionalPath());

        return builder;
    }

    private UriComponentsBuilder createOrAdd(UriComponentsBuilder builder, String path) {
        if (StringUtils.hasText(path)) {
            if (builder != null) {
                if (!path.startsWith("/")) {
                    builder.path("/");
                }
                builder.path(path);
            } else {
                builder = UriComponentsBuilder.fromHttpUrl(path);
            }
        }
        return builder;
    }

    private void addQueryParameters(RestMethodMetadata metadata) {
        for (String queryParameterName : metadata.getQueryParameters().keySet()) {
            Object queryValue = arguments[metadata.getQueryParameters().get(queryParameterName)];
            if (queryValue != null) {
                if (queryValue.getClass().isArray()) {
                    builder.queryParam(queryParameterName, (Object[]) queryValue);
                } else if (queryValue instanceof Collection) {
                    builder.queryParam(queryParameterName, ((Collection) queryValue).toArray());
                } else {
                    builder.queryParam(queryParameterName, queryValue);
                }
            }
        }
    }

    private Map<String, ?> getUriVariables(RestMethodMetadata metadata) {
        Map<String, Integer> uriVarParameters = metadata.getUriVarParameters();
        Map<String, Object> variables = new HashMap<String, Object>(uriVarParameters.size());
        for (String pathVar : uriVarParameters.keySet()) {
            variables.put(pathVar, arguments[uriVarParameters.get(pathVar)]);
        }
        return variables;
    }
}