Package org.apache.marmotta.platform.ldcache.webservices

Source Code of org.apache.marmotta.platform.ldcache.webservices.LinkedDataCachingWebService

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.marmotta.platform.ldcache.webservices;

import org.apache.marmotta.platform.ldcache.api.endpoint.LinkedDataEndpointService;
import org.apache.marmotta.platform.ldcache.services.ldcache.LDCacheSailProvider;
import org.apache.marmotta.commons.sesame.model.Namespaces;
import org.apache.marmotta.platform.core.api.triplestore.SesameService;
import org.apache.marmotta.ldclient.api.endpoint.Endpoint;
import org.apache.marmotta.ldclient.api.provider.DataProvider;
import org.apache.marmotta.ldclient.model.ClientResponse;
import org.apache.marmotta.ldclient.provider.rdf.LinkedDataProvider;
import org.openrdf.model.URI;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.rdfxml.util.RDFXMLPrettyWriter;
import org.slf4j.Logger;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
* Add file description here!
* <p/>
* User: sschaffe
*/
@ApplicationScoped
@Path("/cache")
public class LinkedDataCachingWebService {

    @Inject
    private Logger log;

    @Inject
    private LinkedDataEndpointService endpointService;

    @Inject
    private SesameService sesameService;

    @Inject
    private LDCacheSailProvider cacheSailProvider;


    @GET
    @Path("/live")
    public Response retrieveLive(@QueryParam("uri") String uri) {
        if(cacheSailProvider.isEnabled()) {
            try {
                ClientResponse response = cacheSailProvider.getLDClient().retrieveResource(uri);

                RepositoryConnection con = response.getTriples().getConnection();
                con.begin();

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                RDFHandler handler = new RDFXMLPrettyWriter(out);
                con.export(handler);

                con.commit();
                con.close();


                return Response.ok().entity( new String(out.toByteArray(), "utf-8")).build();
            } catch (Exception e) {
                log.error("exception while retrieving resource",e);
                return Response.status(500).entity(e.getMessage()).build();
            }
        } else {
            return Response.status(Status.SERVICE_UNAVAILABLE).entity("caching is disabled").build();
        }
    }

    @GET
    @Path("/cached")
    public Response retrieveCached(@QueryParam("uri") String uri) {
        if(cacheSailProvider.isEnabled()) {
            URI resource = sesameService.getValueFactory().createURI(uri);


            try {
                cacheSailProvider.getLDCache().refreshResource(resource, false);

                return Response.ok().build();
            } catch (Exception e) {
                log.error("exception while retrieving resource",e);
                return Response.status(500).entity(e.getMessage()).build();
            }
        } else {
            return Response.status(Status.SERVICE_UNAVAILABLE).entity("caching is disabled").build();
        }

    }

    @GET
    @Path("/refresh")
    public Response refreshCached(@QueryParam("uri") String uri) {

        if(cacheSailProvider.isEnabled()) {
            URI resource = sesameService.getValueFactory().createURI(uri);


            try {
                cacheSailProvider.getLDCache().refreshResource(resource, true);

                return Response.ok().build();
            } catch (Exception e) {
                log.error("exception while retrieving resource",e);
                return Response.status(500).entity(e.getMessage()).build();
            }
        } else {
            return Response.status(Status.SERVICE_UNAVAILABLE).entity("caching is disabled").build();
        }

    }

    @POST
    @Path("/expire")
    public Response expireCache(@QueryParam("uri") String uri) {

        if(cacheSailProvider.isEnabled()) {
            if (uri != null) {
                URI resource = sesameService.getValueFactory().createURI(uri);
                cacheSailProvider.getLDCache().expire(resource);
            } else {
                cacheSailProvider.getLDCache().expireAll();
            }

            return Response.ok().build();
        } else {
            return Response.status(Status.SERVICE_UNAVAILABLE).entity("caching is disabled").build();
        }
    }

    @POST
    @Path("/endpoint")
    public Response registerEndpoint(@QueryParam("name") String name,
                                     @QueryParam("prefix") String prefix,
                                     @QueryParam("endpoint") String endpointUrl,
                                     @QueryParam("kind") String type,
                                     @QueryParam("mimetype") String mimetype,
                                     @QueryParam("expiry") long expiry) {

        if(cacheSailProvider.isEnabled()) {
            if (type == null || !getProviderNames().contains(type.toLowerCase())) {
                type = LinkedDataProvider.PROVIDER_NAME;
            }

            // Check for valid Regex
            if (prefix != null) {
                try {
                    if (prefix.startsWith(Endpoint.REGEX_INDICATOR)) {
                        Pattern.compile(prefix.substring(Endpoint.REGEX_INDICATOR.length()));
                    } else {
                        Pattern.compile(prefix);
                    }
                } catch (PatternSyntaxException pse) {
                    return Response.status(Status.BAD_REQUEST).entity("Invalid Regex in prefix detected").build();
                }
            }
            if (endpointUrl != null) {
                endpointUrl = endpointUrl.replace('<', '{').replace('>', '}');
            } else {
                endpointUrl = "";
            }
            if (mimetype == null) {
                mimetype ="";
            }
            Endpoint endpoint = new Endpoint(name, type, prefix, endpointUrl, mimetype, expiry);
            endpointService.addEndpoint(endpoint);

            cacheSailProvider.updateEndpoints();


            return Response.ok().build();
        } else {
            return Response.status(Status.SERVICE_UNAVAILABLE).entity("caching is disabled").build();
        }

    }

    @GET
    @Path("/endpoint/list")
    @Produces("application/json")
    public Response listEndpoints() {

        List<Map<String, Object>> result = new LinkedList<Map<String, Object>>();
        for(Endpoint endpoint : endpointService.listEndpoints()) {
            result.add(buildEndpointJSON(endpoint, false));
        }
        for(Endpoint endpoint : cacheSailProvider.getVolatileEndpoints()) {
            result.add(buildEndpointJSON(endpoint, true));
        }

        return Response.ok().entity(result).build();
    }

    @GET
    @Path("/provider/list")
    @Produces(Namespaces.MIME_TYPE_JSON)
    public Response listProviders() {
        if(cacheSailProvider.isEnabled()) {
            return Response.ok(getProviderNames()).build();
        } else {
            return Response.status(Status.SERVICE_UNAVAILABLE).entity("caching is disabled").build();
        }
    }


    @GET
    @Path("/endpoint/{id}")
    @Produces("application/json")
    public Response retrieveEndpoint(@PathParam("id") String id) {

        Endpoint endpoint = endpointService.getEndpoint(id);
        if (endpoint == null) return notFound(id);


        return Response.ok().entity(buildEndpointJSON(endpoint, false)).build();
    }

    @DELETE
    @Path("/endpoint/{id}")
    public Response removeEndpoint(@PathParam("id") String id) {

        Endpoint endpoint = endpointService.getEndpoint(id);
        if (endpoint == null) return notFound(id);

        endpointService.removeEndpoint(endpoint);

        cacheSailProvider.updateEndpoints();

        return Response.ok().build();
    }

    @POST
    @Path("/endpoint/{id}/enable")
    public Response enableEndpoint(@PathParam("id") String id, @QueryParam("enable") @DefaultValue("true") boolean enable) {
        Endpoint endpoint = endpointService.getEndpoint(id);
        if (endpoint == null) return notFound(id);

        endpoint.setActive(enable);
        endpointService.updateEndpoint(endpoint);

        cacheSailProvider.updateEndpoints();

        return Response.ok().build();
    }

    private Response notFound(String id) {
        return Response.status(Status.NOT_FOUND).entity("No endpoint with id " + id + " found!").build();
    }

    @POST
    @Path("/endpoint/{id}/disable")
    public Response disableEndpoint(@PathParam("id") String id) {
        return enableEndpoint(id, false);
    }


    private Map<String, Object> buildEndpointJSON(Endpoint endpoint, boolean isVolatile) {
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("id",endpoint.getName().replaceAll("[^A-Za-z0-9 ]", "").toLowerCase());
        resultMap.put("name",endpoint.getName());
        resultMap.put("endpoint",endpoint.getEndpointUrl());
        resultMap.put("expiry", endpoint.getDefaultExpiry());
        resultMap.put("prefix",endpoint.getUriPattern());
        resultMap.put("kind",endpoint.getType().toString());
        resultMap.put("mimetype",endpoint.getContentTypes());
        resultMap.put("active", endpoint.isActive());
        resultMap.put("volatile", isVolatile);

        return resultMap;
    }


    private Set<String> getProviderNames() {
        Set<String> result = new HashSet<String>();

        for(DataProvider provider : cacheSailProvider.getLDClient().getDataProviders()) {
            result.add(provider.getName().toLowerCase());
        }

        return result;
    }
}
TOP

Related Classes of org.apache.marmotta.platform.ldcache.webservices.LinkedDataCachingWebService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.