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

com.metreeca.rdf4j.handlers.Endpoint Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2013-2022 Metreeca srl
 *
 * 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 com.metreeca.rdf4j.handlers;

import com.metreeca.rdf4j.services.Graph;
import com.metreeca.rest.Request;
import com.metreeca.rest.handlers.Delegator;
import com.metreeca.rest.services.Logger;

import java.util.*;

import static com.metreeca.rest.Toolbox.service;

import static java.util.Arrays.asList;
import static java.util.Collections.*;


/**
 * SPARQL 1.1 endpoint handler.
 *
 * 

Provides a standard SPARQL 1.1 endpoint exposing the contents of a {@linkplain #graph(Graph) target graph}.

* *

Both {@linkplain #query(Collection) query} and {@linkplain #update(Collection) update} operations are disabled, * unless otherwise specified.

* * @see SPARQL 1.1 Overview */ public abstract class Endpoint> extends Delegator { private Graph graph=service(Graph.graph()); private Set query=singleton(new Object()); // roles enabled for query operations (unmatchable by default) private Set update=singleton(new Object()); // roles enabled for update operations (unmatchable by default) private final Logger logger=service(Logger.logger()); @SuppressWarnings("unchecked") private T self() { return (T)this; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// protected boolean queryable(final Collection roles) { return query.isEmpty() || !disjoint(query, roles); } protected boolean updatable(final Collection roles) { return update.isEmpty() || !disjoint(update, roles); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// protected Graph graph() { return graph; } protected Set query() { return unmodifiableSet(query); } protected Set update() { return unmodifiableSet(update); } protected Logger logger() { return logger; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * Configures the target graph. * *

By default configured to the shared {@linkplain Graph#graph() graph}.

* * @param graph the target graph for SPARQL operations on this endpoint * * @return this endpoint * * @throws NullPointerException if {@code graph} is null */ public T graph(final Graph graph) { if ( graph == null ) { throw new NullPointerException("null graph"); } this.graph=graph; return self(); } /** * Configures the roles for query operations. * *

By default configured to block all query operations.

* * @param roles the user {@linkplain Request#roles(Object...) roles} enabled to perform query operations on this * endpoint; empty for public access * * @return this endpoint * * @throws NullPointerException if {@code roles} is null or contains null values */ public T query(final Object... roles) { return query(asList(roles)); } /** * Configures the roles for query operations. * *

By default configured to block all query operations.

* * @param roles the user {@linkplain Request#roles(Object...) roles} enabled to perform query operations on this * endpoint; empty for public access * * @return this endpoint * * @throws NullPointerException if {@code roles} is null or contains null values */ public T query(final Collection roles) { if ( roles == null || roles.stream().anyMatch(Objects::isNull) ) { throw new NullPointerException("null roles"); } this.query=new HashSet<>(roles); return self(); } /** * Configures the roles for update operations. * *

By default configured to block all update operations.

* * @param roles the user {@linkplain Request#roles(Object...) roles} enabled to perform update operations on this * endpoint; empty for public access * * @return this endpoint * * @throws NullPointerException if {@code roles} is null or contains null values */ public T update(final Object... roles) { return update(asList(roles)); } /** * Configures the roles for update operations. * *

By default configured to block all update operations.

* * @param roles the user {@linkplain Request#roles(Object...) roles} enabled to perform update operations on this * endpoint; empty for public access * * @return this endpoint * * @throws NullPointerException if {@code roles} is null or contains null values */ public T update(final Collection roles) { if ( roles == null || roles.stream().anyMatch(Objects::isNull) ) { throw new NullPointerException("null roles"); } this.update=new HashSet<>(roles); return self(); } }