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

com.welemski.gobblerparty.GPSimpleApi Maven / Gradle / Ivy

Go to download

GobblerParty is an extension of Gobbler that contains convenience classes and methods for talking to PoolParty webservices: Thesaurus, Extractor, SPARQL end point etc. This library also provides a convenient class for building SPARQL query so that you can parameterized it.

There is a newer version: 1.1.10
Show newest version
/*
 * The MIT License
 *
 * Copyright 2016 welemski.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.welemski.gobblerparty;

import com.welemski.gobbler.Goblet;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Objects;
import org.apache.http.client.utils.URIBuilder;

/**
 *
 * @author welemski 
 */
public class GPSimpleApi extends Goblet implements Puddle{
    
    protected String basePath = null;
    protected String api = null; // The API
    protected String project = null;
    protected String operation = null; // The method to call 
    
    public GPSimpleApi(){}
    
    public GPSimpleApi(String basePath, String api, String project, String operation){
        this.basePath = basePath;
        this.api = api;
        this.project = project;
        this.operation = operation;
    }

    
    protected String removeTrailingSlashes(String subject){
        if(subject == null || "".equals(subject.trim())){
            return null;
        }
        
        
        String newSubject = subject.trim();
        if(newSubject == null){
            return null;
        }
        
        if("".equals(newSubject)){
            return newSubject;
        }
        
        int upper = newSubject.endsWith("/") ? newSubject.length() - 1 : newSubject.length();
        int lower = newSubject.startsWith("/") ? 1 : 0;
        
        newSubject = newSubject.substring(lower, upper);
        return newSubject;
    }
    
    private String generateEndpoint(){
        ArrayList paths = new ArrayList<>();
        
        // Trailing slashes
        String lBasePath = this.removeTrailingSlashes(this.basePath);
        String lApi = this.removeTrailingSlashes(this.api);
        String lProject = this.removeTrailingSlashes(this.project);
        String lOp = this.removeTrailingSlashes(this.operation);
                
        paths.add(lBasePath);
        paths.add(lApi);
        paths.add(lProject);
        paths.add(lOp);
        
        paths.removeIf(Objects::isNull);
       
        return "/" + String.join("/",paths);
    }
    
    /**
     * @return the basePath
     */
    @Override
    public String getBasePath() {
        return basePath;
    }

    /**
     * @param pathPrefix the basePath to set
     * @return GPSimpleApi
     */
    @Override
    public GPSimpleApi setBasePath(String pathPrefix) {
        this.basePath = pathPrefix;
        return this;
    }

    /**
     * @return the project
     */
    @Override
    public String getProject() {
        return project;
    }

    /**
     * @param project the project to set
     * @return GPSimpleApi
     */
    @Override
    public GPSimpleApi setProject(String project) {
        this.project = project;
        return this;
    }

    /**
     * @return the api
     */
    @Override
    public String getApi() {
        return api;
    }

    /**
     * @param api the api to set
     * @return GPSimpleApi
     */
    @Override
    public GPSimpleApi setApi(String api) {
        this.api = api;
        return this;
    }
    
    @Override
    protected void beforeRequest(){
       this.path = this.generateEndpoint();
    }
    

    @Override
    public Puddle setOperation(String operation) {
        this.operation = operation;
        return this;
    }

    @Override
    public String getOperation() {
        return this.operation;
    }

    @Override
    public String getEndpoint() {
        try {
            String partial = this.generateEndpoint();            
            URIBuilder urib = new URIBuilder()
                    .setScheme(this.getProtocol())
                    .setHost(this.getHost())
                    .setPath(partial);
            
            this.parameters.forEach(urib::setParameter);
            
            if (this.port > 0) {
                urib.setPort(this.port);
            }
            
            URI uri = urib.build();
            return uri.toString();
        } catch (URISyntaxException ex) {
            return null;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy