Commit 30641f7c authored by Piotr Gawron's avatar Piotr Gawron
Browse files

Miriam connector fixed to use https instead of old http

parent aa9f4f4a
......@@ -21,261 +21,262 @@ import uk.ac.ebi.miriam.lib.MiriamLink;
/**
* Class responsible for connection to Miriam DB. It allows to update URI of
* database being used and retreive urls for miriam uri.
* database being used and retrieve urls for miriam uri.
*
* @author Piotr Gawron
*
*/
public final class MiriamConnector extends CachableInterface implements IExternalService {
/**
* String used to distinguish cached data for links.
*/
static final String LINK_DB_PREFIX = "Link: ";
/**
* String used to distinguish cached data for links.
*/
static final String LINK_DB_PREFIX = "Link: ";
/**
* String used to distinguish cached data for checking if uri is valid.
*/
protected static final String VALID_URI_PREFIX = "Validity: ";
/**
* String used to distinguish cached data for checking if uri is valid.
*/
protected static final String VALID_URI_PREFIX = "Validity: ";
/**
* String describing invalid miriam entries that will be put into db (instead
* of null).
*/
private static final String INVALID_LINK = "INVALID";
/**
* String describing invalid miriam entries that will be put into db (instead of
* null).
*/
private static final String INVALID_LINK = "INVALID";
/**
* Default class logger.
*/
private Logger logger = Logger.getLogger(MiriamConnector.class.getName());
/**
* Default class logger.
*/
private Logger logger = Logger.getLogger(MiriamConnector.class.getName());
/**
* Miriam Registry API.
*/
private MiriamLink link;
/**
* Miriam Registry API.
*/
private MiriamLink link;
/**
* Default class constructor. Prevent initialization.
*/
public MiriamConnector() {
super(MiriamConnector.class);
link = new MiriamLink();
}
/**
* Default class constructor. Prevent initialization.
*/
public MiriamConnector() {
super(MiriamConnector.class);
link = new MiriamLink();
link.setAddress("https://www.ebi.ac.uk/miriamws/main/MiriamWebServices");
}
/**
* Returns url to the webpage represented by {@link MiriamData} param.
*
* @param miriamData
* miriam data
* @return url to resource pointed by miriam data
*/
public String getUrlString(MiriamData miriamData) {
if (miriamData.getDataType() == null) {
throw new InvalidArgumentException("Data type cannot be empty.");
} else if (miriamData.getDataType().getUris().size() == 0) {
throw new InvalidArgumentException("Url for " + miriamData.getDataType() + " cannot be retreived.");
}
String query = LINK_DB_PREFIX + miriamData.getDataType().getUris().get(0) + "\n" + miriamData.getResource();
String result = getCacheValue(query);
if (result != null) {
if (INVALID_LINK.equals(result)) {
return null;
}
return result;
}
// hardcoded specific treatment for mesh
if (MiriamType.MESH_2012.equals(miriamData.getDataType())) {
result = "http://bioportal.bioontology.org/ontologies/1351?p=terms&conceptid=" + miriamData.getResource();
} else {
String uri = miriamData.getDataType().getUris().get(0) + ":" + miriamData.getResource();
String[] urls = getLink().getLocations(uri);
if (urls == null) {
result = null;
} else if (urls.length > 0) {
result = urls[0];
}
}
if (result != null) {
setCacheValue(query, result);
return result;
} else {
logger.warn("Cannot find url for miriam: " + miriamData);
setCacheValue(query, INVALID_LINK);
return null;
}
}
/**
* Returns url to the web page represented by {@link MiriamData} parameter.
*
* @param miriamData
* miriam data
* @return url to resource pointed by miriam data
*/
public String getUrlString(MiriamData miriamData) {
if (miriamData.getDataType() == null) {
throw new InvalidArgumentException("Data type cannot be empty.");
} else if (miriamData.getDataType().getUris().size() == 0) {
throw new InvalidArgumentException("Url for " + miriamData.getDataType() + " cannot be retreived.");
}
String query = LINK_DB_PREFIX + miriamData.getDataType().getUris().get(0) + "\n" + miriamData.getResource();
String result = getCacheValue(query);
if (result != null) {
if (INVALID_LINK.equals(result)) {
return null;
}
return result;
}
// hard-coded specific treatment for mesh
if (MiriamType.MESH_2012.equals(miriamData.getDataType())) {
result = "http://bioportal.bioontology.org/ontologies/1351?p=terms&conceptid=" + miriamData.getResource();
} else {
String uri = miriamData.getDataType().getUris().get(0) + ":" + miriamData.getResource();
String[] urls = getLink().getLocations(uri);
if (urls == null) {
result = null;
} else if (urls.length > 0) {
result = urls[0];
}
}
if (result != null) {
setCacheValue(query, result);
return result;
} else {
logger.warn("Cannot find url for miriam: " + miriamData);
setCacheValue(query, INVALID_LINK);
return null;
}
}
@Override
public ExternalServiceStatus getServiceStatus() {
ExternalServiceStatus status = new ExternalServiceStatus("MIRIAM Registry", "http://www.ebi.ac.uk/miriam/main/");
GeneralCacheInterface cacheCopy = getCache();
this.setCache(null);
@Override
public ExternalServiceStatus getServiceStatus() {
ExternalServiceStatus status = new ExternalServiceStatus("MIRIAM Registry", "https://www.ebi.ac.uk/miriam/main/");
GeneralCacheInterface cacheCopy = getCache();
this.setCache(null);
try {
String url = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, MiriamType.PUBMED, "3453"));
status.setStatus(ExternalServiceStatusType.OK);
if (url == null) {
status.setStatus(ExternalServiceStatusType.DOWN);
}
} catch (Exception e) {
logger.error(status.getName() + " is down", e);
status.setStatus(ExternalServiceStatusType.DOWN);
}
this.setCache(cacheCopy);
return status;
}
try {
String url = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, MiriamType.PUBMED, "3453"));
status.setStatus(ExternalServiceStatusType.OK);
if (url == null) {
status.setStatus(ExternalServiceStatusType.DOWN);
}
} catch (Exception e) {
logger.error(status.getName() + " is down", e);
status.setStatus(ExternalServiceStatusType.DOWN);
}
this.setCache(cacheCopy);
return status;
}
/**
* Check if identifier can be transformed into {@link MiriamData}.
*
* @param string
* identifier in the format NAME:IDENTIFIER. Where NAME is the name
* from {@link MiriamType#commonName} and IDENTIFIER is resource
* identifier.
* @return <code>true</code> if identifier can be transformed into
* {@link MiriamData}
*/
public boolean isValidIdentifier(String string) {
try {
MiriamType.getMiriamDataFromIdentifier(string);
return true;
} catch (InvalidArgumentException e) {
return false;
}
}
/**
* Check if identifier can be transformed into {@link MiriamData}.
*
* @param string
* identifier in the format NAME:IDENTIFIER. Where NAME is the name
* from {@link MiriamType#commonName} and IDENTIFIER is resource
* identifier.
* @return <code>true</code> if identifier can be transformed into
* {@link MiriamData}
*/
public boolean isValidIdentifier(String string) {
try {
MiriamType.getMiriamDataFromIdentifier(string);
return true;
} catch (InvalidArgumentException e) {
return false;
}
}
@Override
public String refreshCacheQuery(Object query) throws SourceNotAvailable {
String result = null;
if (query instanceof String) {
String name = (String) query;
if (name.startsWith(LINK_DB_PREFIX)) {
String tmp = name.substring(LINK_DB_PREFIX.length());
String[] rows = tmp.split("\n");
if (rows.length != 2) {
throw new InvalidArgumentException("Miriam link query is invalid: " + query);
}
MiriamType dataType = MiriamType.getTypeByUri(rows[0]);
String resource = rows[1];
result = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, dataType, resource));
} else if (name.startsWith(VALID_URI_PREFIX)) {
String tmp = name.substring(VALID_URI_PREFIX.length());
result = "" + isValid(tmp);
} else if (name.startsWith("http")) {
try {
result = getWebPageContent(name);
} catch (IOException e) {
throw new SourceNotAvailable(e);
}
} else {
throw new InvalidArgumentException("Don't know what to do with string \"" + query + "\"");
}
} else {
throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass());
}
@Override
public String refreshCacheQuery(Object query) throws SourceNotAvailable {
String result = null;
if (query instanceof String) {
String name = (String) query;
if (name.startsWith(LINK_DB_PREFIX)) {
String tmp = name.substring(LINK_DB_PREFIX.length());
String[] rows = tmp.split("\n");
if (rows.length != 2) {
throw new InvalidArgumentException("Miriam link query is invalid: " + query);
}
MiriamType dataType = MiriamType.getTypeByUri(rows[0]);
String resource = rows[1];
result = getUrlString(new MiriamData(MiriamRelationType.BQ_BIOL_IS_DESCRIBED_BY, dataType, resource));
} else if (name.startsWith(VALID_URI_PREFIX)) {
String tmp = name.substring(VALID_URI_PREFIX.length());
result = "" + isValid(tmp);
} else if (name.startsWith("http")) {
try {
result = getWebPageContent(name);
} catch (IOException e) {
throw new SourceNotAvailable(e);
}
} else {
throw new InvalidArgumentException("Don't know what to do with string \"" + query + "\"");
}
} else {
throw new InvalidArgumentException("Don't know what to do with class: " + query.getClass());
}
return result;
}
return result;
}
/**
* Checks if uri (like the one defined in {@link MiriamType#uris}) is valid.
*
* @param uri
* uri to check
* @return <code>true</code> if uri in parameter is valid, <code>false</code>
* otherwise
*/
public boolean isValid(String uri) {
String query = VALID_URI_PREFIX + uri;
String val = getCacheValue(query);
if (val != null) {
return "true".equalsIgnoreCase(val);
}
/**
* Checks if uri (like the one defined in {@link MiriamType#uris}) is valid.
*
* @param uri
* uri to check
* @return <code>true</code> if uri in parameter is valid, <code>false</code>
* otherwise
*/
public boolean isValid(String uri) {
String query = VALID_URI_PREFIX + uri;
String val = getCacheValue(query);
if (val != null) {
return "true".equalsIgnoreCase(val);
}
boolean result = false;
String name = getLink().getName(uri);
if (name == null) {
result = false;
} else {
result = !name.isEmpty();
}
setCacheValue(query, "" + result);
return result;
}
boolean result = false;
String name = getLink().getName(uri);
if (name == null) {
result = false;
} else {
result = !name.isEmpty();
}
setCacheValue(query, "" + result);
return result;
}
/**
* This is alternative versopm od {@link #getUrlString(MiriamData)} method
* that is around 30% fatsre. But it refers to beta version of
* <a href="http://www.ebi.ac.uk/miriamws/main/rest/">miriam Rest API</a>.
*
* @param md
* miriam data for which access url will be returned
* @return url to resource pointed by miriam data
* @throws AnnotationException
* thrown when there is a problem with accessing miriam REST API
*/
protected String getUrlString2(MiriamData md) throws AnnotationException {
try {
String result = null;
String uri = md.getDataType().getUris().get(0) + ":" + md.getResource();
String query = "http://www.ebi.ac.uk/miriamws/main/rest/resolve/" + uri;
String page;
page = getWebPageContent(query);
Document document = getXmlDocumentFromString(page);
Node uris = document.getFirstChild();
NodeList nodes = uris.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (Node.ELEMENT_NODE == node.getNodeType()) {
if (getNodeAttr("deprecated", node).equals("")) {
result = node.getTextContent();
}
}
}
return result;
} catch (IOException e) {
throw new AnnotationException("Problem with accessing miriam REST API", e);
} catch (InvalidXmlSchemaException e) {
throw new AnnotationException("Problem with parsing miriam REST API response", e);
}
}
/**
* This is alternative version of {@link #getUrlString(MiriamData)} method that
* is around 30% faster. But it refers to beta version of
* <a href="https://www.ebi.ac.uk/miriamws/main/rest/">miriam Rest API</a>.
*
* @param md
* miriam data for which access url will be returned
* @return url to resource pointed by miriam data
* @throws AnnotationException
* thrown when there is a problem with accessing miriam REST API
*/
protected String getUrlString2(MiriamData md) throws AnnotationException {
try {
String result = null;
String uri = md.getDataType().getUris().get(0) + ":" + md.getResource();
String query = "https://www.ebi.ac.uk/miriamws/main/rest/resolve/" + uri;
String page;
page = getWebPageContent(query);
Document document = getXmlDocumentFromString(page);
Node uris = document.getFirstChild();
NodeList nodes = uris.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (Node.ELEMENT_NODE == node.getNodeType()) {
if (getNodeAttr("deprecated", node).equals("")) {
result = node.getTextContent();
}
}
}
return result;
} catch (IOException e) {
throw new AnnotationException("Problem with accessing miriam REST API", e);
} catch (InvalidXmlSchemaException e) {
throw new AnnotationException("Problem with parsing miriam REST API response", e);
}
}
/**
* Returns uri to miriam resource.
*
* @param md
* {@link MiriamData} object for which uri should be returned
* @return uri to miriam resource
*/
public String miriamDataToUri(MiriamData md) {
return md.getDataType().getUris().get(0) + ":" + md.getResource();
}
/**
* Returns uri to miriam resource.
*
* @param md
* {@link MiriamData} object for which uri should be returned
* @return uri to miriam resource
*/
public String miriamDataToUri(MiriamData md) {
return md.getDataType().getUris().get(0) + ":" + md.getResource();
}
@Override
protected WebPageDownloader getWebPageDownloader() {
return super.getWebPageDownloader();
}
@Override
protected WebPageDownloader getWebPageDownloader() {
return super.getWebPageDownloader();
}
@Override
protected void setWebPageDownloader(WebPageDownloader webPageDownloader) {
super.setWebPageDownloader(webPageDownloader);
}
@Override
protected void setWebPageDownloader(WebPageDownloader webPageDownloader) {
super.setWebPageDownloader(webPageDownloader);
}
/**
* @return the link
* @see #link
*/
MiriamLink getLink() {
return link;
}
/**
* @return the link
* @see #link
*/
MiriamLink getLink() {
return link;
}
/**
* @param link
* the link to set
* @see #link
*/
void setLink(MiriamLink link) {
this.link = link;
}
/**
* @param link
* the link to set
* @see #link
*/
void setLink(MiriamLink link) {
this.link = link;
}
}
disabled=06target
eclipse.preferences.version=1
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