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

org.opengis.cite.ogcapifeatures10.openapi3.UriBuilder Maven / Gradle / Ivy

There is a newer version: 1.7.1
Show newest version
package org.opengis.cite.ogcapifeatures10.openapi3;

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

import com.sun.jersey.api.uri.UriTemplate;
import com.sun.jersey.api.uri.UriTemplateParser;

/**
 * Builds a URL out of a TestPoint.
 *
 * @author Lyn Goltz 
 */
public class UriBuilder {

    private final TestPoint testPoint;

    private final Map templateReplacements = new HashMap<>();

    /**
     * @param testPoint
     *            never null
     */
    public UriBuilder( TestPoint testPoint ) {
        this.testPoint = testPoint;
        this.templateReplacements.putAll( testPoint.getPredefinedTemplateReplacement() );
    }

    /**
     * Adds the collectionName to the URI
     * 
     * @param collectionName
     *            never null
     * @return this UrlBuilder
     */
    public UriBuilder collectionName( String collectionName ) {
        String templateName = retrieveCollectionNameTemplateName();
        addTemplateReplacement( collectionName, templateName );
        return this;
    }

    /**
     * Adds the featureId to the URI
     *
     * @param featureId
     *            never null
     * @return this UrlBuilder
     */
    public UriBuilder featureId( String featureId ) {
        String templateName = retrieveFeatureIdTemplateName();
        addTemplateReplacement( featureId, templateName );
        return this;
    }

    /**
     * @return this URI, never null
     */
    public String buildUrl() {
        UriTemplate uriTemplate = new UriTemplate( testPoint.getServerUrl() + testPoint.getPath() );
        return uriTemplate.createURI( templateReplacements );
    }

    private void addTemplateReplacement( String collectionName, String templateName ) {
        if ( templateName != null )
            templateReplacements.put( templateName, collectionName );
    }

    private String retrieveCollectionNameTemplateName() {
        String path = testPoint.getPath();
        UriTemplateParser uriTemplateParser = new UriTemplateParser( path );
        for ( String templateName : uriTemplateParser.getNames() ) {
            if ( path.contains( "/collections/{" + templateName + "}" ) )
                return templateName;
        }
        return null;
    }

    private String retrieveFeatureIdTemplateName() {
        String path = testPoint.getPath();
        UriTemplateParser uriTemplateParser = new UriTemplateParser( path );
        for ( String templateName : uriTemplateParser.getNames() ) {
            if ( path.endsWith( "items/{" + templateName + "}" ) )
                return templateName;
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy