Skip to content
Snippets Groups Projects
Commit 1693c01f authored by anttil's avatar anttil
Browse files

SSDM-2259: Make UniprotQuery to use jetty HTTP client instead of apache-commons HTTP client

SVN: 34785
parent 3a0d2188
No related branches found
No related tags found
No related merge requests found
...@@ -19,6 +19,7 @@ package ch.systemsx.cisd.common.net.uniprot; ...@@ -19,6 +19,7 @@ package ch.systemsx.cisd.common.net.uniprot;
import static ch.systemsx.cisd.common.net.uniprot.UniprotColumn.ID; import static ch.systemsx.cisd.common.net.uniprot.UniprotColumn.ID;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
...@@ -26,12 +27,15 @@ import java.util.Iterator; ...@@ -26,12 +27,15 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.httpclient.HttpClient; import org.eclipse.jetty.client.HttpClient;
import org.apache.commons.httpclient.HttpStatus; import org.eclipse.jetty.client.api.Request;
import org.apache.commons.httpclient.methods.GetMethod; import org.eclipse.jetty.client.api.Response;
import org.apache.commons.httpclient.params.HttpMethodParams; import org.eclipse.jetty.client.util.InputStreamResponseListener;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel; import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked; import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
...@@ -48,8 +52,6 @@ import ch.systemsx.cisd.common.parser.TabFileLoader; ...@@ -48,8 +52,6 @@ import ch.systemsx.cisd.common.parser.TabFileLoader;
*/ */
public final class UniprotQuery public final class UniprotQuery
{ {
private static final int RETRY_COUNT = 3;
private static final String BASE_URL = "http://www.uniprot.org/uniprot/"; private static final String BASE_URL = "http://www.uniprot.org/uniprot/";
private static final String QUERY_INIT_STR = "query="; private static final String QUERY_INIT_STR = "query=";
...@@ -78,8 +80,8 @@ public final class UniprotQuery ...@@ -78,8 +80,8 @@ public final class UniprotQuery
private final String columnsSpecification; private final String columnsSpecification;
/** /**
* Construct a Uniprot query, adding the given database <var>columns</var>. Note that * Construct a Uniprot query, adding the given database <var>columns</var>. Note that {@link UniprotColumn#ID} will always be added to the set of
* {@link UniprotColumn#ID} will always be added to the set of columns. * columns.
*/ */
public UniprotQuery(UniprotColumn... columns) public UniprotQuery(UniprotColumn... columns)
{ {
...@@ -87,8 +89,8 @@ public final class UniprotQuery ...@@ -87,8 +89,8 @@ public final class UniprotQuery
} }
/** /**
* Construct a Uniprot query, adding the given database <var>columns</var>. Note that * Construct a Uniprot query, adding the given database <var>columns</var>. Note that {@link UniprotColumn#ID} will always be added to the set of
* {@link UniprotColumn#ID} will always be added to the set of columns. * columns.
*/ */
public UniprotQuery(Set<UniprotColumn> columns) public UniprotQuery(Set<UniprotColumn> columns)
{ {
...@@ -158,17 +160,29 @@ public final class UniprotQuery ...@@ -158,17 +160,29 @@ public final class UniprotQuery
private Iterable<UniprotEntry> runQuery(final String queryURL) throws IOExceptionUnchecked private Iterable<UniprotEntry> runQuery(final String queryURL) throws IOExceptionUnchecked
{ {
final HttpClient client = new HttpClient(); final HttpClient client = new HttpClient();
final GetMethod method = new GetMethod(queryURL);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(RETRY_COUNT, false));
try try
{ {
final int statusCode = client.executeMethod(method); client.start();
} catch (Exception e)
{
throw CheckedExceptionTunnel.wrapIfNecessary(e);
}
final InputStreamResponseListener listener = new InputStreamResponseListener();
Request request = client.newRequest(queryURL).method(HttpMethod.GET);
final InputStream stream = listener.getInputStream();
try
{
request.send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
if (statusCode != HttpStatus.SC_OK) if (response.getStatus() != HttpStatus.Code.OK.getCode())
{ {
throw new IOExceptionUnchecked(new IOException("GET failed: " throw new IOExceptionUnchecked(new IOException("GET failed: "
+ method.getStatusLine())); + response.getStatus() + ": " + response.getReason()));
} }
final TabFileLoader<UniprotEntry> parser = final TabFileLoader<UniprotEntry> parser =
...@@ -199,8 +213,7 @@ public final class UniprotQuery ...@@ -199,8 +213,7 @@ public final class UniprotQuery
{ {
Map<String, String> defauts = Collections.emptyMap(); Map<String, String> defauts = Collections.emptyMap();
final Iterator<UniprotEntry> delegate = parser.iterate( final Iterator<UniprotEntry> delegate = parser.iterate(stream, defauts);
method.getResponseBodyAsStream(), defauts);
@Override @Override
public boolean hasNext() public boolean hasNext()
...@@ -208,7 +221,7 @@ public final class UniprotQuery ...@@ -208,7 +221,7 @@ public final class UniprotQuery
final boolean hasNext = delegate.hasNext(); final boolean hasNext = delegate.hasNext();
if (hasNext == false) if (hasNext == false)
{ {
method.releaseConnection(); IOUtils.closeQuietly(stream);
} }
return hasNext; return hasNext;
} }
...@@ -221,7 +234,7 @@ public final class UniprotQuery ...@@ -221,7 +234,7 @@ public final class UniprotQuery
return delegate.next(); return delegate.next();
} catch (RuntimeException ex) } catch (RuntimeException ex)
{ {
method.releaseConnection(); IOUtils.closeQuietly(stream);
throw ex; throw ex;
} }
} }
...@@ -229,20 +242,20 @@ public final class UniprotQuery ...@@ -229,20 +242,20 @@ public final class UniprotQuery
@Override @Override
public void remove() public void remove()
{ {
method.releaseConnection(); IOUtils.closeQuietly(stream);
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
}; };
} catch (IOException ex) } catch (Exception ex)
{ {
method.releaseConnection(); IOUtils.closeQuietly(stream);
throw CheckedExceptionTunnel.wrapIfNecessary(ex); throw CheckedExceptionTunnel.wrapIfNecessary(ex);
} }
} }
}; };
} catch (IOException ex) } catch (Exception ex)
{ {
method.releaseConnection(); IOUtils.closeQuietly(stream);
throw CheckedExceptionTunnel.wrapIfNecessary(ex); throw CheckedExceptionTunnel.wrapIfNecessary(ex);
} }
} }
...@@ -261,9 +274,8 @@ public final class UniprotQuery ...@@ -261,9 +274,8 @@ public final class UniprotQuery
/** /**
* Runs a query against Uniprot with the given <var>queryExpression</var>. * Runs a query against Uniprot with the given <var>queryExpression</var>.
* *
* @param queryExpression The query expression to use for the query. See <a * @param queryExpression The query expression to use for the query. See <a href="http://www.uniprot.org/help/text-search">Uniprot Online Help</a>
* href="http://www.uniprot.org/help/text-search">Uniprot Online Help</a> for details * for details on the query language.
* on the query language.
*/ */
public Iterable<UniprotEntry> query(String queryExpression) throws IOExceptionUnchecked public Iterable<UniprotEntry> query(String queryExpression) throws IOExceptionUnchecked
{ {
...@@ -274,12 +286,10 @@ public final class UniprotQuery ...@@ -274,12 +286,10 @@ public final class UniprotQuery
/** /**
* Runs a query against Uniprot with the given <var>queryExpression</var>. * Runs a query against Uniprot with the given <var>queryExpression</var>.
* *
* @param queryExpression The query expression to use for the query. See <a * @param queryExpression The query expression to use for the query. See <a href="http://www.uniprot.org/help/text-search">Uniprot Online Help</a>
* href="http://www.uniprot.org/help/text-search">Uniprot Online Help</a> for details * for details on the query language.
* on the query language.
* @param limit The maximum number of result entries to return. * @param limit The maximum number of result entries to return.
* @param offset The offset, that is the first result entry to return when counting starts with * @param offset The offset, that is the first result entry to return when counting starts with 0.
* 0.
*/ */
public Iterable<UniprotEntry> query(String queryExpression, int limit, int offset) public Iterable<UniprotEntry> query(String queryExpression, int limit, int offset)
throws IOExceptionUnchecked throws IOExceptionUnchecked
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment