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

org.ow2.petals.binding.rest.utils.RESTUriTemplate Maven / Gradle / Ivy

/**
 * Copyright (c) 2007-2012 EBM WebSourcing, 2012-2020 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see http://www.gnu.org/licenses/
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.binding.rest.utils;

import java.util.Properties;
import java.util.logging.Logger;

import org.glassfish.jersey.uri.UriTemplate;
import org.ow2.petals.commons.log.Level;

import com.ebmwebsourcing.easycommons.properties.PropertiesException;
import com.ebmwebsourcing.easycommons.properties.PropertiesHelper;

/**
 * An {@link UriTemplate} supporting placeholders
 * 
 * @author Christophe DENEUX - Linagora
 *
 */
public class RESTUriTemplate {

    /**
     * The custom place holders read from the component property file to be able to configure service units.
     */
    protected final Properties componentPlaceholders;

    /**
     * The URI template as defined in the SU JBI descriptor, with placeholders
     */
    private final String uriTemplateStr;

    /**
     * The URI Template compiled, optimized for request processing. Must be updated on paceholder reloading.
     */
    private UriTemplate uriTemplate;

    protected final Logger logger;

    public RESTUriTemplate(final String uriTemplateStr, final Properties componentPlaceholders, final Logger logger)
            throws IllegalArgumentException, PropertiesException {
        this.uriTemplateStr = uriTemplateStr;
        this.componentPlaceholders = componentPlaceholders;
        this.logger = logger;

        this.loadURITemplate();
    }

    @Override
    public String toString() {
        return this.uriTemplateStr;
    }

    public String getTemplate() {
        assert this.uriTemplate != null;

        return this.uriTemplate.getTemplate();
    }

    private void loadURITemplate() throws IllegalArgumentException, PropertiesException {
        this.uriTemplate = new UriTemplate(
                PropertiesHelper.resolveString(this.uriTemplateStr, this.componentPlaceholders));
    }

    public void onPlaceHolderValuesReloaded() {
        try {
            this.loadURITemplate();
        } catch (final IllegalArgumentException | PropertiesException e) {
            this.logger
                    .log(Level.WARNING,
                            "A problem occurs creating the URI template from '" + this.uriTemplateStr
                                    + "'. The processing can be not the expected one. Try to reload placeholders again.",
                            e);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy