Commit 63d5dc1600a388cbda1819e5b81caee2e5731269

Authored by Luís Pedro
1 parent 9c05b8720e

response close

src/main/java/org/um/dsi/gavea/orcid/client/OrcidOAuthClient.java
@@ -29,32 +29,28 @@ public class OrcidOAuthClient extends BaseOrcidOAuthClient { @@ -29,32 +29,28 @@ public class OrcidOAuthClient extends BaseOrcidOAuthClient {
29 } 29 }
30 30
31 31
32 - public String getAuthorizeUriLogin(final List<ScopePathType> scopes, final String state, final String orcidOrEmail)  
33 - throws Exception { 32 + public String getAuthorizeUriLogin(final List<ScopePathType> scopes, final String state, final String orcidOrEmail) throws Exception {
34 33
35 String orcidId = null; 34 String orcidId = null;
36 String email = null; 35 String email = null;
37 - if (orcidOrEmail != null && !"".equals(orcidOrEmail)) {  
38 - if (isValidOrcidId(orcidOrEmail)) { 36 + if(orcidOrEmail != null && !"".equals(orcidOrEmail)) {
  37 + if(isValidOrcidId(orcidOrEmail))
39 orcidId = orcidOrEmail; 38 orcidId = orcidOrEmail;
40 - } else { 39 + else
41 email = orcidOrEmail; 40 email = orcidOrEmail;
42 - }  
43 } 41 }
44 42
45 return getAuthorizeUri(scopes, state, orcidId, null, null, email, null, true); 43 return getAuthorizeUri(scopes, state, orcidId, null, null, email, null, true);
46 } 44 }
47 45
48 - public String getAuthorizeUriRegistration(final List<ScopePathType> scopes, final String state,  
49 - final String familyNames, final String givenNames) throws Exception { 46 + public String getAuthorizeUriRegistration(final List<ScopePathType> scopes, final String state, final String familyNames, final String givenNames) throws Exception {
50 return getAuthorizeUri(scopes, state, null, familyNames, givenNames, null, null, false); 47 return getAuthorizeUri(scopes, state, null, familyNames, givenNames, null, null, false);
51 } 48 }
52 49
53 - private String getAuthorizeUri(final List<ScopePathType> scopes, final String state, final String orcidId,  
54 - final String familyNames, final String givenNames, final String email, final String lang, 50 + private String getAuthorizeUri(final List<ScopePathType> scopes, final String state, final String orcidId, final String familyNames, final String givenNames, final String email, final String lang,
55 final boolean login) throws Exception { 51 final boolean login) throws Exception {
56 52
57 - if (scopes == null) 53 + if(scopes == null)
58 throw new Exception("Cannot create Authorize Uri - missing scopes"); 54 throw new Exception("Cannot create Authorize Uri - missing scopes");
59 55
60 UriBuilder builder = UriBuilder.fromUri(loginUri); 56 UriBuilder builder = UriBuilder.fromUri(loginUri);
@@ -106,70 +102,122 @@ public class OrcidOAuthClient extends BaseOrcidOAuthClient { @@ -106,70 +102,122 @@ public class OrcidOAuthClient extends BaseOrcidOAuthClient {
106 102
107 /* Activities */ 103 /* Activities */
108 public ActivitiesSummary readActivitiesSummary(final OrcidAccessToken token) throws OrcidClientException { 104 public ActivitiesSummary readActivitiesSummary(final OrcidAccessToken token) throws OrcidClientException {
109 - return get(OrcidConstants.ENDPOINT_ACTIVITIES, token, null).readEntity(ActivitiesSummary.class); 105 + Response response = null;
  106 + try {
  107 + response = get(OrcidConstants.ENDPOINT_ACTIVITIES, token, null);
  108 +
  109 + return response.readEntity(ActivitiesSummary.class);
  110 + } finally {
  111 + if(response != null)
  112 + response.close();
  113 + }
110 } 114 }
111 115
112 116
113 /* Funding */ 117 /* Funding */
114 public Funding readFunding(final OrcidAccessToken token, final String putCode) throws OrcidClientException { 118 public Funding readFunding(final OrcidAccessToken token, final String putCode) throws OrcidClientException {
115 - return get(OrcidConstants.ENDPOINT_FUNDING, token, putCode).readEntity(Funding.class); 119 + Response response = null;
  120 + try {
  121 + response = get(OrcidConstants.ENDPOINT_FUNDING, token, putCode);
  122 +
  123 + return response.readEntity(Funding.class);
  124 + } finally {
  125 + if(response != null)
  126 + response.close();
  127 + }
116 } 128 }
117 129
118 - public String addFunding(final OrcidAccessToken token, final Funding funding)  
119 - throws OrcidClientException {  
120 - Response response = post(OrcidConstants.ENDPOINT_FUNDING, token, Entity.entity(funding, MediaType.APPLICATION_XML_TYPE));  
121 -  
122 - String location = response.getLocation().toString();  
123 - if (location != null && !"".equals(location)) {  
124 - String putCode = location.substring(location.lastIndexOf("/") + 1, location.length());  
125 - if (putCode.matches("\\d+")) {  
126 - return putCode; 130 + public String addFunding(final OrcidAccessToken token, final Funding funding) throws OrcidClientException {
  131 + Response response = null;
  132 + try {
  133 + response = post(OrcidConstants.ENDPOINT_FUNDING, token, Entity.entity(funding, MediaType.APPLICATION_XML_TYPE));
  134 +
  135 + String location = response.getLocation().toString();
  136 + if(location != null && !"".equals(location)) {
  137 + String putCode = location.substring(location.lastIndexOf("/") + 1, location.length());
  138 + if(putCode.matches("\\d+"))
  139 + return putCode;
127 } 140 }
  141 + } finally {
  142 + if(response != null)
  143 + response.close();
128 } 144 }
129 145
130 return null; 146 return null;
131 } 147 }
132 148
133 - public void updateFunding(final OrcidAccessToken token, final String putCode, final Funding funding)  
134 - throws OrcidClientException {  
135 - put(OrcidConstants.ENDPOINT_FUNDING, token, putCode, Entity.entity(funding, MediaType.APPLICATION_XML_TYPE)); 149 + public void updateFunding(final OrcidAccessToken token, final String putCode, final Funding funding) throws OrcidClientException {
  150 + Response response = null;
  151 + try {
  152 + response = put(OrcidConstants.ENDPOINT_FUNDING, token, putCode, Entity.entity(funding, MediaType.APPLICATION_XML_TYPE));
  153 + } finally {
  154 + if(response != null)
  155 + response.close();
  156 + }
136 } 157 }
137 158
138 public void deleteFunding(final OrcidAccessToken token, final String putCode) throws OrcidClientException { 159 public void deleteFunding(final OrcidAccessToken token, final String putCode) throws OrcidClientException {
139 - delete(OrcidConstants.ENDPOINT_FUNDING, token, putCode); 160 + Response response = null;
  161 + try {
  162 + response = delete(OrcidConstants.ENDPOINT_FUNDING, token, putCode);
  163 + } finally {
  164 + if(response != null)
  165 + response.close();
  166 + }
140 } 167 }
141 168
142 169
143 /* Work */ 170 /* Work */
144 public Work readWork(final OrcidAccessToken token, final String putCode) throws OrcidClientException { 171 public Work readWork(final OrcidAccessToken token, final String putCode) throws OrcidClientException {
145 - return get(OrcidConstants.ENDPOINT_WORK, token, putCode).readEntity(Work.class); 172 + Response response = null;
  173 + try {
  174 + response = get(OrcidConstants.ENDPOINT_WORK, token, putCode);
  175 +
  176 + return response.readEntity(Work.class);
  177 + } finally {
  178 + if(response != null)
  179 + response.close();
  180 + }
146 } 181 }
147 182
148 - public String addWork(final OrcidAccessToken token, final Work work)  
149 - throws OrcidClientException {  
150 - Response response = post(OrcidConstants.ENDPOINT_WORK, token, Entity.entity(work, MediaType.APPLICATION_XML_TYPE));  
151 -  
152 - String location = response.getLocation().toString();  
153 - if (location != null && !"".equals(location)) {  
154 - String putCode = location.substring(location.lastIndexOf("/") + 1, location.length());  
155 - if (putCode.matches("\\d+")) {  
156 - return putCode; 183 + public String addWork(final OrcidAccessToken token, final Work work) throws OrcidClientException {
  184 + Response response = null;
  185 + try {
  186 + response = post(OrcidConstants.ENDPOINT_WORK, token, Entity.entity(work, MediaType.APPLICATION_XML_TYPE));
  187 + String location = response.getLocation().toString();
  188 + if (location != null && !"".equals(location)) {
  189 + String putCode = location.substring(location.lastIndexOf("/") + 1, location.length());
  190 + if (putCode.matches("\\d+"))
  191 + return putCode;
157 } 192 }
  193 + } finally {
  194 + if(response != null)
  195 + response.close();
158 } 196 }
159 197
160 return null; 198 return null;
161 } 199 }
162 200
163 - public void updateWork(final OrcidAccessToken token, final String putCode, final Work work)  
164 - throws OrcidClientException {  
165 - put(OrcidConstants.ENDPOINT_WORK, token, putCode, Entity.entity(work, MediaType.APPLICATION_XML_TYPE)); 201 + public void updateWork(final OrcidAccessToken token, final String putCode, final Work work) throws OrcidClientException {
  202 + Response response = null;
  203 + try {
  204 + response = put(OrcidConstants.ENDPOINT_WORK, token, putCode, Entity.entity(work, MediaType.APPLICATION_XML_TYPE));
  205 + } finally {
  206 + if(response != null)
  207 + response.close();
  208 + }
166 } 209 }
167 210
168 public void deleteWork(final OrcidAccessToken token, final String putCode) throws OrcidClientException { 211 public void deleteWork(final OrcidAccessToken token, final String putCode) throws OrcidClientException {
169 - delete(OrcidConstants.ENDPOINT_WORK, token, putCode); 212 + Response response = null;
  213 + try {
  214 + response = delete(OrcidConstants.ENDPOINT_WORK, token, putCode);
  215 + } finally {
  216 + if(response != null)
  217 + response.close();
  218 + }
170 } 219 }
171 220
172 -  
173 private boolean isValidOrcidId(final String value) { 221 private boolean isValidOrcidId(final String value) {
174 return value.matches("([0-9]{4})-([0-9]{4})-([0-9]{4})-([0-9]{3})(?:[0-9X]{1})"); 222 return value.matches("([0-9]{4})-([0-9]{4})-([0-9]{4})-([0-9]{3})(?:[0-9X]{1})");
175 } 223 }