/src/main/java/org/isatools/isacreator/orcid/impl/OrcidClientImpl.java

http://github.com/ISA-tools/ISAcreator · Java · 132 lines · 78 code · 40 blank · 14 comment · 6 complexity · f3e716273736257e3fd0bd4770b37ba0 MD5 · raw file

  1. package org.isatools.isacreator.orcid.impl;
  2. import org.apache.commons.httpclient.HttpClient;
  3. import org.apache.commons.httpclient.methods.GetMethod;
  4. import org.isatools.isacreator.orcid.OrcidClient;
  5. import org.isatools.isacreator.orcid.model.OrcidAuthor;
  6. import org.isatools.isacreator.orcid.xmlhandlers.OrcidSearchResultHandler;
  7. import org.orcid.ns.orcid.OrcidMessageDocument;
  8. import java.io.IOException;
  9. import java.net.URLEncoder;
  10. /**
  11. * Created by the ISATeam.
  12. * User: agbeltran
  13. * Date: 23/05/2013
  14. * Time: 13:37
  15. *
  16. * @author <a href="mailto:alejandra.gonzalez.beltran@gmail.com">Alejandra Gonzalez-Beltran</a>
  17. */
  18. public class OrcidClientImpl implements OrcidClient {
  19. public static final String QUERY_URL = "http://pub.orcid.org/search/orcid-bio/";
  20. public static final String ACCEPT = "Accept-Encoding";
  21. public static final String ORCID_XML = "application/orcid+xml";
  22. public static final String CONTENT_TYPE = "Content-Type";
  23. private HttpClient client = null;
  24. private GetMethod getMethod = null;
  25. private OrcidSearchResultHandler handler = null;
  26. public OrcidClientImpl() {
  27. client = new HttpClient();
  28. getMethod = new GetMethod(QUERY_URL);
  29. handler = new OrcidSearchResultHandler();
  30. }
  31. public OrcidAuthor getAuthorInfo(String orcidID) {
  32. try {
  33. getMethod.setQueryString("q=" + (orcidID.contains("-") ? "orcid" : "text") + ":" + orcidID);
  34. System.out.println("query string=" + getMethod.getQueryString());
  35. System.out.println("URI=" + getMethod.getURI());
  36. getMethod.addRequestHeader(CONTENT_TYPE, ORCID_XML);
  37. //getMethod.addRequestHeader(ACCEPT, ORCID_XML);
  38. int statusCode = client.executeMethod(getMethod);
  39. if (statusCode != -1) {
  40. String contents = getMethod.getResponseBodyAsString();
  41. System.out.println("status text=" + getMethod.getStatusText());
  42. System.out.println("contents=" + contents);
  43. getMethod.releaseConnection();
  44. return processAuthorInfo(contents);
  45. } else {
  46. System.out.println("status code is -1");
  47. }
  48. } catch (IOException ex) {
  49. ex.printStackTrace();
  50. }//catch(HttpException ex){
  51. //}
  52. return null;
  53. }
  54. public OrcidAuthor[] getOrcidProfiles(String searchString) {
  55. try {
  56. getMethod.setQueryString("q=text:" + URLEncoder.encode(searchString, "UTF-8") + "");
  57. System.out.println("query string=" + getMethod.getQueryString());
  58. System.out.println("URI=" + getMethod.getURI());
  59. getMethod.addRequestHeader(CONTENT_TYPE, ORCID_XML);
  60. int statusCode = client.executeMethod(getMethod);
  61. if (statusCode != -1) {
  62. String contents = getMethod.getResponseBodyAsString();
  63. System.out.println("status text=" + getMethod.getStatusText());
  64. System.out.println("contents=" + contents);
  65. getMethod.releaseConnection();
  66. return processOrcidProfles(contents);
  67. } else {
  68. System.out.println("status code is -1");
  69. }
  70. } catch (IOException ex) {
  71. ex.printStackTrace();
  72. }//catch(HttpException ex){
  73. //}
  74. return null;
  75. }
  76. private OrcidAuthor[] processOrcidProfles(String contents) {
  77. OrcidMessageDocument orcidMessageDocument = handler.getOrcidMessageDocument(contents);
  78. return handler.getOrcidAuthors(orcidMessageDocument);
  79. }
  80. private OrcidAuthor processAuthorInfo(String contents) {
  81. System.out.println("contents=" + contents);
  82. OrcidMessageDocument orcidMessageDocument = handler.getOrcidMessageDocument(contents);
  83. return handler.getSingleOrcidAuthor(orcidMessageDocument);
  84. }
  85. public static void main(String[] args) {
  86. OrcidClientImpl client = new OrcidClientImpl();
  87. //client.getAuthorInfo("0000-0003-3499-8262");
  88. // client.getOrcidProfiles("English");
  89. //client.getOrcidProfiles("gonzalez-beltran");
  90. client.getOrcidProfiles("0000-0003-3499-8262");
  91. }
  92. }