Commit 20f3e75f authored by Lukas Jelonek's avatar Lukas Jelonek
Browse files

Merge branch 'feature/enhance_code' into develop

parents d854e355 19aa8712
### Purpose
This library resolves database cross references (dbxrefs) to URIs that contain detailed information.
......@@ -91,6 +91,11 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
......
......@@ -46,21 +46,21 @@ public class DbXRefResourceRegistry implements DbXRefService {
private static DbXRefResourceRegistry instance;
public static DbXRefResourceRegistry getDefault() {
if (instance == null) {
instance = new DbXRefResourceRegistry();
}
return instance;
if (instance == null) {
instance = new DbXRefResourceRegistry();
}
return instance;
}
Iterable<? extends DbXRefResolver> getResolvers() {
// service provider lookup von resolvern
Iterator<DbXRefResolver> it = ServiceLoader.load(DbXRefResolver.class, this.getClass().getClassLoader()).iterator();
List<DbXRefResolver> l = new LinkedList<>();
while (it.hasNext()) {
l.add(it.next());
}
return l;
// service provider lookup von resolvern
Iterator<DbXRefResolver> it = ServiceLoader.load(DbXRefResolver.class, this.getClass().getClassLoader()).iterator();
List<DbXRefResolver> l = new LinkedList<>();
while (it.hasNext()) {
l.add(it.next());
}
return l;
}
/**
......@@ -70,14 +70,14 @@ public class DbXRefResourceRegistry implements DbXRefService {
* @return
*/
List<? extends DbXRefResolver> getResolversFor(DbXRef ref) {
List<DbXRefResolver> result = new LinkedList<>();
for (DbXRefResolver dbXRefResolver : getResolvers()) {
if (dbXRefResolver.canHandle(ref)) {
result.add(dbXRefResolver);
}
}
return result;
List<DbXRefResolver> result = new LinkedList<>();
for (DbXRefResolver dbXRefResolver : getResolvers()) {
if (dbXRefResolver.canHandle(ref)) {
result.add(dbXRefResolver);
}
}
return result;
}
/**
......@@ -87,7 +87,7 @@ public class DbXRefResourceRegistry implements DbXRefService {
* @return
*/
public boolean hasResolverFor(DbXRef ref) {
return !getResolversFor(ref).isEmpty();
return !getResolversFor(ref).isEmpty();
}
/**
......@@ -97,62 +97,63 @@ public class DbXRefResourceRegistry implements DbXRefService {
*/
@Override
public Collection<? extends Resource> resolve(DbXRef ref) {
List<Resource> result = new LinkedList<>();
List<Resource> result = new LinkedList<>();
if (!hasResolverFor(ref)) {
throw new DbXrefResourceException("No handler found for database '" + ref.getDatabase() + "'. Supported databases: " + getSupportedDatabases());
}
if (!hasResolverFor(ref)) {
throw new DbXrefResourceException("No handler found for database '" + ref.getDatabase() + "'. Supported databases: " + getSupportedDatabases());
}
for (DbXRefResolver dbXResource : getResolversFor(ref)) {
result.addAll(dbXResource.resolve(ref));
}
return result;
for (DbXRefResolver dbXResource : getResolversFor(ref)) {
result.addAll(dbXResource.resolve(ref));
}
return result;
}
/**
* {@inheritDoc}
*
* @throws DbXrefResourceException If no handler could be found.
* @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<>();
Set<T> result = new HashSet<>();
if (!hasResolverFor(ref)) {
throw new DbXrefResourceException("No handler found for database '" + ref.getDatabase() + "'. Supported databases: " + getSupportedDatabases());
}
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));
}
for (DbXRefResolver dbXResource : getResolversFor(ref)) {
result.addAll(filter(dbXResource.resolve(ref), targetClass));
}
return result;
return result;
}
public <T extends Resource> Collection<? extends T> filter(Iterable<? extends Resource> iterable, Class<T> targetClass) {
List<T> filtered = new LinkedList<>();
for (Resource r : iterable) {
if (targetClass.isInstance(r)) {
filtered.add(targetClass.cast(r));
}
}
return filtered;
List<T> filtered = new LinkedList<>();
for (Resource r : iterable) {
if (targetClass.isInstance(r)) {
filtered.add(targetClass.cast(r));
}
}
return filtered;
}
private Set<String> getSupportedDatabases() {
Set<String> set = new LinkedHashSet<>();
for (DbXRefResolver dbXRefResolver : getResolvers()) {
Iterables.addAll(set, dbXRefResolver.getDatabasePrefixes());
}
return set;
Set<String> set = new LinkedHashSet<>();
for (DbXRefResolver dbXRefResolver : getResolvers()) {
Iterables.addAll(set, dbXRefResolver.getDatabasePrefixes());
}
return set;
}
@Override
public DbXRef create(CharSequence database, CharSequence identifier) {
return new SimpleDbXRef(database.toString(), identifier.toString());
return new SimpleDbXRef(database.toString(), identifier.toString());
}
@Override
public DbXRef create(CharSequence dbxref) {
return new DbXrefParser().parse(dbxref.toString());
return new DbXrefParser().parse(dbxref.toString());
}
}
......@@ -16,19 +16,18 @@
*/
package de.cebitec.common.dbxref.api;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
/**
*
* @author cb
*/
public abstract class AbstractResource implements Resource {
protected final URI uri;
public AbstractResource(URI uri) {
......@@ -43,36 +42,22 @@ public abstract class AbstractResource implements Resource {
@Override
public String getResourceText() {
try {
HttpURLConnection openConnection = (HttpURLConnection) uri.toURL().openConnection();
openConnection.connect();
int responseCode = openConnection.getResponseCode();
openConnection.disconnect();
if (responseCode >= 400)
throw new UnsupportedOperationException("Invalid url");
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
connection.disconnect();
if (responseCode >= 400) {
throw new UnsupportedOperationException("Can't access " + uri + " Return code [" + responseCode + "]");
}
} catch (IOException ex) {
Logger.getLogger(AbstractResource.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
throw new RuntimeException("Error accessing " + uri, ex);
}
String s = "";
try (BufferedReader br = new BufferedReader(new InputStreamReader(uri.toURL().openStream()))) {
//s = "content: "+uri.toURL().openConnection().getContent().toString();
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
s = sb.toString();
} catch (Exception ex) {
// applikations exception werfen
try (InputStream stream = uri.toURL().openStream()) {
return IOUtils.toString(stream);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
//return
return s;
}
}
/*
* Copyright (C) 2014 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
* 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;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author Lukas Jelonek {@literal <Lukas.Jelonek at computational.bio.uni-giessen.de>}
*/
public class DbXRefResourceRegistryIT {
public DbXRefResourceRegistryIT() {
}
@Test
public void testSomeMethod() {
}
}
/*
* Copyright (C) 2014 Lukas Jelonek <ljelonek at cebitec.uni-bielefeld.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
* 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.providers;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import java.util.ServiceLoader;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
*
* @author Lukas Jelonek {@literal <ljelonek at cebitec.uni-bielefeld.de>}
*/
public class ECProviderTest {
@Test
public void testIsProvidedService() {
ServiceLoader<DbXRefResolver> loader = ServiceLoader.load(DbXRefResolver.class);
boolean present = false;
for (DbXRefResolver dbXRefResolver : loader) {
if (dbXRefResolver instanceof ECProvider) {
present = true;
}
}
assertTrue(present);
}
@Test
public void testProvider() {
ECProvider provider = new ECProvider();
DbXRef validID = new SimpleDbXRef("EC", "1.1.1.1");
assertNotNull(provider.resolve(validID));
// mindestens eine resource
assertTrue(provider.resolve(validID).iterator().hasNext());
// sind die resources gueltig
Iterable<? extends Resource> resolve = provider.resolve(validID);
for (Resource resource : resolve) {
String resourceText = resource.getResourceText();
assertThat(resourceText, notNullValue());
assertFalse(resourceText.isEmpty());
}
}
}
/*
* 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.providers;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import java.util.ServiceLoader;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
*
* @author cb
*/
public class GIProviderTest {
public GIProviderTest() {
}
@Test
public void testIsProvidedService() {
ServiceLoader<DbXRefResolver> loader = ServiceLoader.load(DbXRefResolver.class);
boolean present = false;
for (DbXRefResolver dbXRefResolver : loader) {
if (dbXRefResolver instanceof GIProvider) {
present = true;
}
}
assertTrue(present);
}
@Test
public void testProvider() {
GIProvider provider = new GIProvider();
DbXRef validGI = new SimpleDbXRef("GI", "731497");
assertNotNull(provider.resolve(validGI));
// mindestens eine resource
assertTrue(provider.resolve(validGI).iterator().hasNext());
// sind die resources gueltig
Iterable<? extends Resource> resolve = provider.resolve(validGI);
for (Resource resource : resolve) {
String resourceText = resource.getResourceText();
assertThat(resourceText, notNullValue());
assertFalse(resourceText.isEmpty());
}
}
}
/*
* 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.providers;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import java.util.ServiceLoader;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
*
* @author cb
*/
public class GOProviderTest {
public GOProviderTest() {
}
@Test
public void testIsProvidedService() {
ServiceLoader<DbXRefResolver> loader = ServiceLoader.load(DbXRefResolver.class);
boolean present = false;
for (DbXRefResolver dbXRefResolver : loader) {
if (dbXRefResolver instanceof GOProvider) {
present = true;
}
}
assertTrue(present);
}
@Test
public void testProvider() {
GOProvider provider = new GOProvider();
DbXRef validID = new SimpleDbXRef("GO", "0097281");
assertNotNull(provider.resolve(validID));
// mindestens eine resource
assertTrue(provider.resolve(validID).iterator().hasNext());
// sind die resources gueltig
Iterable<? extends Resource> resolve = provider.resolve(validID);
for (Resource resource : resolve) {
String resourceText = resource.getResourceText();
assertThat(resourceText, notNullValue());
assertFalse(resourceText.isEmpty());
}
}
}
/*
* 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.providers;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import java.util.ServiceLoader;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
*
* @author cb
*/
public class GeneIDProviderTest {
/**
* Test of resolve method, of class GeneIDProvider.
*/
@Test
public void testResolve_DbXRef() {
GeneIDProvider provider = new GeneIDProvider();
DbXRef validGI = new SimpleDbXRef("GendeID", "956582");
assertNotNull(provider.resolve(validGI));
// mindestens eine resource
assertTrue(provider.resolve(validGI).iterator().hasNext());
// sind die resources gueltig
Iterable<? extends Resource> resolve = provider.resolve(validGI);
for (Resource resource : resolve) {
String resourceText = resource.getResourceText();
assertThat(resourceText, notNullValue());
assertFalse(resourceText.isEmpty());
}
}
@Test
public void testIsProvidedService() {
ServiceLoader<DbXRefResolver> loader = ServiceLoader.load(DbXRefResolver.class);
boolean present = false;
for (DbXRefResolver dbXRefResolver : loader) {
if (dbXRefResolver instanceof GeneIDProvider) {
present = true;
}
}
assertTrue(present);
}
}
/*
* Copyright (C) 2013 aseidel
*
* 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.providers;
import de.cebitec.common.dbxref.api.DbXRef;
import de.cebitec.common.dbxref.api.Resource;
import de.cebitec.common.dbxref.persistence.SimpleDbXRef;
import de.cebitec.common.dbxref.spi.DbXRefResolver;
import java.util.ServiceLoader;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;