Commit 933f53da authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Replaced Resource classes by Resource.Type

parent 484d79dd
......@@ -3,7 +3,7 @@
<groupId>de.cebitec.common</groupId>
<artifactId>dbxref</artifactId>
<version>1.3.1-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>bundle</packaging>
<name>DbXRef library</name>
......
......@@ -19,6 +19,7 @@ package de.cebitec.common.dbxref;
import com.google.common.collect.Iterables;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import java.util.Collection;
......@@ -115,25 +116,25 @@ public class DbXRefResourceRegistry implements DbXRefService {
* @throws DbXrefResourceException If no handler can be found.
*/
@Override
public <T extends Resource> Collection<? extends T> resolve(DbXRef ref, Class<T> targetClass) {
Set<T> result = new HashSet<>();
public Collection<? extends Resource> resolve(DbXRef ref, Type targetType) {
Set<Resource> result = new HashSet<>();
if (!hasResolverFor(ref)) {
throw new DbXrefResourceException("No handler found for database '" + ref.getDatabase() + "'. Supported databases: " + getSupportedDatabases());
}
for (DbXRefResolver dbXResource : getResolversFor(ref)) {
result.addAll(filter(dbXResource.resolve(ref), targetClass));
result.addAll(filter(dbXResource.resolve(ref), targetType));
}
return result;
}
public <T extends Resource> Collection<? extends T> filter(Iterable<? extends Resource> iterable, Class<T> targetClass) {
List<T> filtered = new LinkedList<>();
public Collection<? extends Resource> filter(Iterable<? extends Resource> iterable, Type targetType) {
List<Resource> filtered = new LinkedList<>();
for (Resource r : iterable) {
if (targetClass.isInstance(r)) {
filtered.add(targetClass.cast(r));
if (r.getType() == targetType) {
filtered.add(r);
}
}
return filtered;
......
......@@ -79,11 +79,10 @@ public interface DbXRefService {
* Resolves resource links for the given reference and filtered by the given type. If multiple resolvers are
* available for the resource an aggregation of the resources is returned.
*
* @param <T> The type of the requested resources.
* @param ref The dbxref. Must not be null.
* @param targetClass The requested resource type. May be null. Then all resources are retrieved.
* @param targetType The requested resource type. May be null. Then all resources are retrieved.
* @return A collection of resources for the dbxref. If no resources can be resolved the collection will be empty.
*/
<T extends Resource> Collection<? extends T> resolve(DbXRef ref, Class<T> targetClass);
Collection<? extends Resource> resolve(DbXRef ref, Type targetType);
}
/*
* Copyright (C) 2012 cb
* Copyright (C) 2016 Lukas Jelonek - Lukas.Jelonek at computational.bio.uni-giessen.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -16,6 +16,7 @@
*/
package de.cebitec.common.dbxref.api;
import com.google.common.base.Preconditions;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
......@@ -24,21 +25,37 @@ import org.apache.commons.io.IOUtils;
/**
*
* @author cb
*/
public abstract class AbstractResource implements Resource {
public class GenericResource implements Resource {
private final Type type;
private final DbXRef dbxref;
protected final URI uri;
public AbstractResource(URI uri) {
public GenericResource(Type type, DbXRef dbxref, URI uri) {
Preconditions.checkNotNull(type);
Preconditions.checkNotNull(dbxref);
Preconditions.checkNotNull(uri);
this.type = type;
this.dbxref = dbxref;
this.uri = uri;
}
@Override
public Type getType() {
return type;
}
@Override
public URI getUri() {
return uri;
}
@Override
public DbXRef getDbXRef() {
return dbxref;
}
@Override
public String getResourceText() {
try {
......
/*
* Copyright (C) 2012 cb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.api;
import java.net.URI;
/**
* A HTMLResource provides access to a website.
*
* @author cb
*/
public class HTMLResource extends AbstractResource {
public HTMLResource(URI uri) {
super(uri);
}
}
......@@ -27,12 +27,22 @@ import java.net.URI;
*/
public interface Resource {
public enum Type {
Xml,
Text,
Html
}
URI getUri();
/**
* Returns the Recourcetext.
*
* @return String that represents the Recourcetext.
*/
String getResourceText();
DbXRef getDbXRef();
Type getType();
}
/*
* Copyright (C) 2012 cb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.api;
import java.net.URI;
/**
* A resource that points to a plain text document.
*
* @author cb
*/
public class TextResource extends AbstractResource {
public TextResource(URI uri) {
super(uri);
}
}
/*
* Copyright (C) 2012 cb
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.cebitec.common.dbxref.api;
import java.net.URI;
/**
* A resource that points to an xml document.
*
* @author cb
*/
public class XMLResource extends AbstractResource {
public XMLResource(URI uri) {
super(uri);
}
}
......@@ -16,7 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -33,7 +33,7 @@ public class ECProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("EC", "Enzyme");
resolverData.addResourceTemplate(HTMLResource.class, "http://enzyme.expasy.org/EC/%i");
resolverData.addResourceTemplate(Type.Html, "http://enzyme.expasy.org/EC/%i");
return resolverData;
}
}
......@@ -16,8 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.XMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -34,8 +33,8 @@ public class GIProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("GI");
resolverData.addResourceTemplate(HTMLResource.class, "http://www.ncbi.nlm.nih.gov/protein/GI:%i");
resolverData.addResourceTemplate(XMLResource.class, "http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&db=protein&dopt=xml&sort=&val=%i&retmode=file");
resolverData.addResourceTemplate(Type.Html, "http://www.ncbi.nlm.nih.gov/protein/GI:%i");
resolverData.addResourceTemplate(Type.Xml, "http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&db=protein&dopt=xml&sort=&val=%i&retmode=file");
return resolverData;
}
}
......@@ -16,8 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.XMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -34,8 +33,8 @@ public class GOProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("GO");
resolverData.addResourceTemplate(HTMLResource.class, "http://www.ebi.ac.uk/QuickGO/GTerm?id=GO:%i");
resolverData.addResourceTemplate(XMLResource.class, "http://www.ebi.ac.uk/QuickGO/GTerm?id=GO:%i&format=oboxml");
resolverData.addResourceTemplate(Type.Html, "http://www.ebi.ac.uk/QuickGO/GTerm?id=GO:%i");
resolverData.addResourceTemplate(Type.Xml, "http://www.ebi.ac.uk/QuickGO/GTerm?id=GO:%i&format=oboxml");
return resolverData;
}
}
......@@ -16,8 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.XMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -34,8 +33,8 @@ public class GeneIDProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("GeneID");
resolverData.addResourceTemplate(HTMLResource.class, "http://www.ncbi.nlm.nih.gov/gene/%i");
resolverData.addResourceTemplate(XMLResource.class, "http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&db=gene&dopt=xml&sort=&val=%i&retmode=file");
resolverData.addResourceTemplate(Type.Html, "http://www.ncbi.nlm.nih.gov/gene/%i");
resolverData.addResourceTemplate(Type.Xml, "http://www.ncbi.nlm.nih.gov/sviewer/viewer.cgi?tool=portal&db=gene&dopt=xml&sort=&val=%i&retmode=file");
return resolverData;
}
}
......@@ -19,18 +19,16 @@ package de.cebitec.common.dbxref.providers;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Lists;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.GenericResource;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A generic provider that resolves dbxrefs. The resolving code is implemented in a generic way and can be configured
......@@ -51,7 +49,7 @@ public class GenericDbXRefProvider implements DbXRefResolver {
* Mapping of resource types to URI templates. The URI template shall use %i as a placeholder for the
* identifier.
*/
private final LinkedHashMultimap<Class<? extends Resource>, String> mapping;
private final LinkedHashMultimap<Type, String> mapping;
public ResolverData() {
this((List<String>) null, null);
......@@ -65,14 +63,14 @@ public class GenericDbXRefProvider implements DbXRefResolver {
this(prefixes, null);
}
public ResolverData(List<String> prefixes, LinkedHashMultimap<Class<? extends Resource>, String> mapping) {
public ResolverData(List<String> prefixes, LinkedHashMultimap<Type, String> mapping) {
if (prefixes == null) {
this.prefixes = Lists.<String>newLinkedList();
} else {
this.prefixes = prefixes;
}
if (mapping == null) {
this.mapping = LinkedHashMultimap.<Class<? extends Resource>, String>create();
this.mapping = LinkedHashMultimap.<Type, String>create();
} else {
this.mapping = mapping;
}
......@@ -82,7 +80,7 @@ public class GenericDbXRefProvider implements DbXRefResolver {
return prefixes.add(prefix);
}
public boolean addResourceTemplate(Class<? extends Resource> type, String template) {
public boolean addResourceTemplate(Type type, String template) {
return mapping.put(type, template);
}
......@@ -90,7 +88,7 @@ public class GenericDbXRefProvider implements DbXRefResolver {
return prefixes;
}
public LinkedHashMultimap<Class<? extends Resource>, String> getMapping() {
public LinkedHashMultimap<Type, String> getMapping() {
return mapping;
}
......@@ -122,15 +120,8 @@ public class GenericDbXRefProvider implements DbXRefResolver {
public static class ResourceFactory {
public <T extends Resource> T create(Class<T> type, URI uri) {
try {
Constructor<T> constructor = type.getConstructor(URI.class);
T newInstance = constructor.newInstance(uri);
return newInstance;
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
Logger.getLogger(GenericDbXRefProvider.class.getName()).log(Level.SEVERE, null, ex);
}
throw new IllegalArgumentException("Can't instantiate a resource of type " + type + " for URI " + uri);
public Resource create(Type type, DbXRef dbxref, URI uri) {
return new GenericResource(type, dbxref, uri);
}
}
......@@ -160,11 +151,11 @@ public class GenericDbXRefProvider implements DbXRefResolver {
public Collection<? extends Resource> resolve(DbXRef ref) {
List<Resource> list = new LinkedList<>();
for (Map.Entry<Class<? extends Resource>, Collection<String>> entry : resolverdata.getMapping().asMap().entrySet()) {
for (Map.Entry<Type, Collection<String>> entry : resolverdata.getMapping().asMap().entrySet()) {
for (String string : entry.getValue()) {
String uriString = string.replace("%i", ref.getId());
URI uri = URI.create(uriString);
list.add(rf.create(entry.getKey(), uri));
list.add(rf.create(entry.getKey(), ref, uri));
}
}
return list;
......
......@@ -18,8 +18,8 @@ package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.DbXrefResourceException;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.GenericResource;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.Resource;
import java.net.URI;
import java.net.URISyntaxException;
......@@ -48,8 +48,7 @@ public class HttpProvider implements DbXRefResolver {
@Override
public Collection<? extends Resource> resolve(DbXRef ref) {
try {
return Collections.singleton(new HTMLResource(
new URI(ref.toString())));
return Collections.singleton(new GenericResource(Resource.Type.Html, ref, new URI(ref.toString())));
} catch (URISyntaxException ex) {
throw new DbXrefResourceException("Could not handle the dbxref " + ref.toString());
}
......
......@@ -16,7 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -33,7 +33,7 @@ public class InterProProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("InterPro");
resolverData.addResourceTemplate(HTMLResource.class, "http://www.ebi.ac.uk/interpro/entry/%i");
resolverData.addResourceTemplate(Type.Html, "http://www.ebi.ac.uk/interpro/entry/%i");
return resolverData;
}
}
......@@ -16,8 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.XMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -34,8 +33,8 @@ public class PDBProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("PDB");
resolverData.addResourceTemplate(HTMLResource.class, "http://www.rcsb.org/pdb/explore/explore.do?structureId=%i");
resolverData.addResourceTemplate(XMLResource.class, "http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=xml&compression=NO&structureId=%i");
resolverData.addResourceTemplate(Type.Html, "http://www.rcsb.org/pdb/explore/explore.do?structureId=%i");
resolverData.addResourceTemplate(Type.Xml, "http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=xml&compression=NO&structureId=%i");
return resolverData;
}
}
......@@ -16,7 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -33,7 +33,7 @@ public class PFamProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("PFAM");
resolverData.addResourceTemplate(HTMLResource.class, "http://pfam.xfam.org/family/%i");
resolverData.addResourceTemplate(Type.Html, "http://pfam.xfam.org/family/%i");
return resolverData;
}
}
......@@ -16,7 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -33,7 +33,7 @@ public class PubMedProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("pubmed", "Pubmed");
resolverData.addResourceTemplate(HTMLResource.class, "http://www.ncbi.nlm.nih.gov/pubmed/%i");
resolverData.addResourceTemplate(Type.Html, "http://www.ncbi.nlm.nih.gov/pubmed/%i");
return resolverData;
}
}
......@@ -16,8 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.XMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -34,8 +33,8 @@ public class RFamProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("RFAM");
resolverData.addResourceTemplate(HTMLResource.class, "http://rfam.sanger.ac.uk/family/%i");
resolverData.addResourceTemplate(XMLResource.class, "http://rfam.sanger.ac.uk/family/%i?content-type=text%2Fxml");
resolverData.addResourceTemplate(Type.Html, "http://rfam.sanger.ac.uk/family/%i");
resolverData.addResourceTemplate(Type.Xml, "http://rfam.sanger.ac.uk/family/%i?content-type=text%2Fxml");
return resolverData;
}
}
......@@ -16,7 +16,7 @@
*/
package de.cebitec.common.dbxref.providers;
import de.cebitec.common.dbxref.api.HTMLResource;
import de.cebitec.common.dbxref.api.Resource.Type;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import org.kohsuke.MetaInfServices;
......@@ -33,7 +33,7 @@ public class SOProvider extends GenericDbXRefProvider {
public static ResolverData createResolverData() {
ResolverData resolverData = new ResolverData("SO");
resolverData.addResourceTemplate(HTMLResource.class, "http://www.sequenceontology.org/browser/current_svn/term/SO:%i");
resolverData.addResourceTemplate(Type.Html, "http://www.sequenceontology.org/browser/current_svn/term/SO:%i");
return resolverData;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment