Revision 60369
Added by Konstantina Galouni about 3 years ago
WorkController.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import com.google.gson.Gson; |
4 | 4 |
import com.google.gson.GsonBuilder; |
5 |
import eu.dnetlib.uoaorcidservice.configuration.properties.OrcidConfig; |
|
5 | 6 |
import eu.dnetlib.uoaorcidservice.entities.ResultIdAndWork; |
6 | 7 |
import eu.dnetlib.uoaorcidservice.entities.UserTokens; |
7 | 8 |
import eu.dnetlib.uoaorcidservice.entities.Work; |
... | ... | |
11 | 12 |
import eu.dnetlib.uoaorcidservice.services.WorkService; |
12 | 13 |
import org.apache.log4j.Logger; |
13 | 14 |
import org.springframework.beans.factory.annotation.Autowired; |
15 |
import org.springframework.data.util.Pair; |
|
14 | 16 |
import org.springframework.http.*; |
15 | 17 |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
18 |
import org.springframework.security.access.AccessDeniedException; |
|
16 | 19 |
import org.springframework.security.access.AuthorizationServiceException; |
17 | 20 |
import org.springframework.security.access.prepost.PreAuthorize; |
18 | 21 |
import org.springframework.web.bind.annotation.*; |
... | ... | |
28 | 31 |
import java.security.InvalidKeyException; |
29 | 32 |
import java.security.NoSuchAlgorithmException; |
30 | 33 |
import java.security.spec.InvalidKeySpecException; |
31 |
import java.util.ArrayList; |
|
32 |
import java.util.Date; |
|
33 |
import java.util.List; |
|
34 |
import java.util.*; |
|
34 | 35 |
|
35 | 36 |
@RestController |
36 |
@RequestMapping("/orcid") |
|
37 |
//@RequestMapping("/orcid")
|
|
37 | 38 |
@CrossOrigin(origins = "*") |
38 | 39 |
public class WorkController { |
39 | 40 |
private final Logger log = Logger.getLogger(this.getClass()); |
41 |
private final Logger orcid_log = Logger.getLogger("ORCID-"+this.getClass().getName()); |
|
40 | 42 |
|
41 | 43 |
@Autowired |
44 |
private OrcidConfig orcidConfig; |
|
45 |
|
|
46 |
@Autowired |
|
42 | 47 |
private UserTokensService userTokensService; |
43 | 48 |
|
44 | 49 |
@Autowired |
45 | 50 |
private WorkService workService; |
46 | 51 |
|
47 | 52 |
@PreAuthorize("isAuthenticated()") |
48 |
@RequestMapping(value = "put-code", method = RequestMethod.GET) |
|
53 |
@RequestMapping(value = "/local/put-code", method = RequestMethod.GET)
|
|
49 | 54 |
public List<String> getPutCode(@RequestParam String[] pids) { |
50 | 55 |
String userOrcid = userTokensService.getCurrentUserOrcid(); |
51 | 56 |
List<Work> works = workService.getWorks(pids, userOrcid); |
... | ... | |
60 | 65 |
} |
61 | 66 |
|
62 | 67 |
@PreAuthorize("isAuthenticated()") |
63 |
@RequestMapping(value = "put-codes", method = RequestMethod.POST) |
|
68 |
@RequestMapping(value = "/local/put-codes", method = RequestMethod.POST)
|
|
64 | 69 |
public List<List<String>> getPutCodes(@RequestBody String[][] pids) { |
65 | 70 |
String userOrcid = userTokensService.getCurrentUserOrcid(); |
66 | 71 |
List<List<String>> putCodes = new ArrayList(); |
... | ... | |
80 | 85 |
} |
81 | 86 |
|
82 | 87 |
@PreAuthorize("isAuthenticated()") |
83 |
@RequestMapping(value = "/work/save", method = RequestMethod.POST) |
|
84 |
public String saveWork(@RequestBody String resultString) throws IOException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException { |
|
88 |
@RequestMapping(value = "/local/works", method = RequestMethod.POST) |
|
89 |
public List<List<Work>> getLocalWorks(@RequestBody String[][] pids) { |
|
90 |
String userOrcid = userTokensService.getCurrentUserOrcid(); |
|
91 |
if(userOrcid == null) { |
|
92 |
throw new AuthorizationServiceException("User is not registered"); |
|
93 |
} |
|
94 |
|
|
95 |
List<List<Work>> returnedWorks = new ArrayList(); |
|
96 |
for(int i=0; i<pids.length; i++) { |
|
97 |
List<Work> works = workService.getWorks(pids[i], userOrcid); |
|
98 |
returnedWorks.add(works); |
|
99 |
} |
|
100 |
return returnedWorks; |
|
101 |
} |
|
102 |
|
|
103 |
@PreAuthorize("isAuthenticated()") |
|
104 |
@RequestMapping(value = "/orcid/work/save", method = RequestMethod.POST) |
|
105 |
public Work saveWork(@RequestBody String resultString) throws Exception { |
|
85 | 106 |
log.debug("saveWork: result = " + resultString); |
86 | 107 |
|
87 | 108 |
Gson gson = new GsonBuilder().create(); |
... | ... | |
98 | 119 |
throw new AuthorizationServiceException("User is not registered"); |
99 | 120 |
} |
100 | 121 |
|
101 |
log.debug("Access token: " + userAccessToken); |
|
102 |
log.debug("User orcid: " + userOrcid); |
|
122 |
// log.debug("Access token: " + userAccessToken);
|
|
123 |
// log.debug("User orcid: " + userOrcid);
|
|
103 | 124 |
|
104 | 125 |
String url = "https://api.sandbox.orcid.org/v3.0/" + userOrcid + "/work"; |
105 | 126 |
|
... | ... | |
109 | 130 |
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
110 | 131 |
protected boolean hasError(HttpStatus statusCode) { |
111 | 132 |
if(statusCode == HttpStatus.UNAUTHORIZED) { |
133 |
orcid_log.error("ORCID service returned UNAUTHORIZED: "+HttpStatus.UNAUTHORIZED); |
|
112 | 134 |
throw new AuthorizationServiceException("User is not registered"); |
113 | 135 |
} |
114 | 136 |
return false; |
... | ... | |
117 | 139 |
HttpHeaders headers = new HttpHeaders(); |
118 | 140 |
headers.add("Authorization", "Bearer " + userAccessToken); |
119 | 141 |
headers.add("Content-Type", "application/orcid+json;charset=UTF-8"); |
120 |
log.debug(headers.get("Authorization")); |
|
121 | 142 |
|
122 | 143 |
HttpEntity<String> request = new HttpEntity<>(gson.toJson(result.getWork()), headers); |
123 |
//logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class)); |
|
124 | 144 |
|
145 |
orcid_log.info("request: "+request); |
|
146 |
orcid_log.info("url: "+url); |
|
125 | 147 |
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class); |
148 |
|
|
126 | 149 |
if (response.getStatusCode() != HttpStatus.CREATED) { |
127 |
log.debug("Saving work response code is: " + response.getStatusCode()); |
|
128 |
log.debug(response.getBody()); |
|
150 |
orcid_log.error("Saving work response code is: " + response.getStatusCode()); |
|
151 |
orcid_log.error("Unexpected Response: "+response.getBody()); |
|
152 |
|
|
129 | 153 |
if(response.getStatusCode() == HttpStatus.UNAUTHORIZED) { |
130 | 154 |
throw new AuthorizationServiceException("You are not allowed to save work"); |
131 |
} |
|
132 |
if(response.getStatusCode() == HttpStatus.CONFLICT) { |
|
155 |
} else if(response.getStatusCode() == HttpStatus.CONFLICT) { |
|
133 | 156 |
throw new ConflictException("Work is already saved"); |
157 |
} else { |
|
158 |
throw new Exception("Internal server error"); |
|
134 | 159 |
} |
135 |
return null; |
|
160 |
// return null;
|
|
136 | 161 |
} else { |
162 |
orcid_log.info("Response: "+response); |
|
163 |
// log.debug("[success] Saving work response code is: " + response.getStatusCode()); |
|
164 |
// log.debug(response.toString()); |
|
137 | 165 |
|
138 |
log.debug("[success] Saving work response code is: " + response.getStatusCode()); |
|
139 |
log.debug(response.toString()); |
|
166 |
Date date = new Date(); |
|
140 | 167 |
|
141 | 168 |
Work workToSave = new Work(); |
142 | 169 |
workToSave.setPids(result.getPids()); |
143 | 170 |
workToSave.setOrcid(userOrcid); |
144 |
workToSave.setCreationDate(new Date()); |
|
171 |
workToSave.setCreationDate(date); |
|
172 |
workToSave.setUpdateDate(date); |
|
145 | 173 |
|
146 | 174 |
HttpHeaders responseHeaders = response.getHeaders(); |
147 | 175 |
String locationPath = responseHeaders.getLocation().toString(); |
148 | 176 |
String[] locationPathArray = locationPath.split("/"); |
149 | 177 |
workToSave.setPutCode(locationPathArray[locationPathArray.length - 1]); |
150 | 178 |
|
151 |
log.debug(gson.toJson(result.getPids())); |
|
152 |
log.debug(responseHeaders.getLocation().toString()); |
|
153 |
log.debug(gson.toJson(workToSave)); |
|
179 |
// log.debug(gson.toJson(result.getPids()));
|
|
180 |
// log.debug(responseHeaders.getLocation().toString());
|
|
181 |
// log.debug(gson.toJson(workToSave));
|
|
154 | 182 |
|
155 | 183 |
workService.saveWork(workToSave); |
156 | 184 |
|
157 |
return "\""+workToSave.getPutCode()+"\""; |
|
185 |
return workToSave; |
|
186 |
// return "\""+workToSave.getPutCode()+"\""; |
|
158 | 187 |
} |
159 | 188 |
} |
160 | 189 |
|
161 |
// @RequestMapping(value = "/{orcid}/work/{putCode}", method = RequestMethod.PUT) |
|
162 |
// public String updateWork(@PathVariable String orcid, |
|
163 |
// @PathVariable String putCode, |
|
164 |
// @RequestBody String work) throws AccessDeniedException { |
|
165 |
// log.debug("updateWork: orcid = " + orcid + " - putCode = " + putCode); |
|
166 |
// |
|
167 |
// String userAccessToken = userTokensService.getUserAccessToken(orcid); |
|
168 |
// log.debug("Access token: " + userAccessToken); |
|
169 |
// |
|
170 |
//// String url = "https://sandbox.orcid.org/v3.0/" + orcid.toString() + "/works"; |
|
171 |
// String url = "https://api.sandbox.orcid.org/v3.0/" + orcid + "/work/" + putCode; |
|
172 |
// |
|
173 |
// RestTemplate restTemplate = new RestTemplate(); |
|
174 |
// restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
|
175 |
// protected boolean hasError(HttpStatus statusCode) { |
|
176 |
// return false; |
|
177 |
// } |
|
178 |
// }); |
|
179 |
// HttpHeaders headers = new HttpHeaders(); |
|
180 |
//// headers.add("Accept", "application/json"); |
|
181 |
// headers.add("Authorization", "Bearer " + userAccessToken); |
|
182 |
// headers.add("Content-Type","application/orcid+json"); |
|
183 |
// |
|
184 |
// HttpEntity<String> request = new HttpEntity<>(work, headers); |
|
185 |
// //logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class)); |
|
186 |
// ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PUT, request, String.class); |
|
187 |
// if (response.getStatusCode() != HttpStatus.OK) { |
|
188 |
// log.debug("Updating work response code is: " + response.getStatusCode()); |
|
189 |
// return null; |
|
190 |
// } else { |
|
191 |
// log.debug(response); |
|
192 |
// |
|
193 |
// return null; |
|
194 |
// } |
|
195 |
// |
|
196 |
// } |
|
190 |
@RequestMapping(value = "/orcid/work/update/{putCode}", method = RequestMethod.POST) |
|
191 |
public Work updateWork(@PathVariable String putCode, |
|
192 |
@RequestBody String resultString) throws AccessDeniedException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, IOException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException { |
|
193 |
log.debug("updateWork: putCode = " + putCode); |
|
197 | 194 |
|
198 |
// @RequestMapping(value = "/works/save", method = RequestMethod.POST) |
|
199 |
// public String saveWorks(@RequestBody String works) { |
|
200 |
// log.debug("saveWorks"); |
|
201 |
// |
|
202 |
// String userAccessToken = userTokensService.getUserAccessToken(null); |
|
195 |
Gson gson = new GsonBuilder().create(); |
|
196 |
ResultIdAndWork result = gson.fromJson(resultString, ResultIdAndWork.class); |
|
197 |
|
|
198 |
UserTokens userTokens = userTokensService.getUserTokens(); |
|
199 |
if(userTokens == null) { |
|
200 |
throw new AuthorizationServiceException("User is not registered"); |
|
201 |
} |
|
202 |
String userOrcid = userTokens.getOrcid(); |
|
203 |
String userAccessToken = userTokens.getAccessToken(); |
|
204 |
|
|
205 |
if(userOrcid == null || userAccessToken == null) { |
|
206 |
throw new AuthorizationServiceException("User is not registered"); |
|
207 |
} |
|
208 |
|
|
203 | 209 |
// log.debug("Access token: " + userAccessToken); |
204 |
// log.debug(works); |
|
205 |
// |
|
206 |
// String url = "https://api.sandbox.orcid.org/v3.0/0000-0001-9541-4617/work"; |
|
207 |
// |
|
208 |
// RestTemplate restTemplate = new RestTemplate(); |
|
209 |
// restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
|
210 |
// protected boolean hasError(HttpStatus statusCode) { |
|
211 |
// return false; |
|
212 |
// } |
|
213 |
// }); |
|
214 |
// HttpHeaders headers = new HttpHeaders(); |
|
215 |
// headers.add("Authorization", "Bearer " + userAccessToken); |
|
216 |
// headers.add("Content-Type", "application/json"); |
|
217 |
//// List<MediaType> list = new ArrayList(); |
|
218 |
//// list.add(MediaType.TEXT_HTML); |
|
219 |
//// headers.setAccept(list); |
|
220 |
// |
|
221 |
// |
|
222 |
// HttpEntity<String> request = new HttpEntity<>(works.toString(), headers); |
|
223 |
// |
|
224 |
//// log.debug(restTemplate.exchange(url, HttpMethod.POST, request, Object.class)); |
|
225 |
// |
|
226 |
// ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class); |
|
227 |
// if (response.getStatusCode() != HttpStatus.OK) { |
|
228 |
// log.debug("Saving works response code is: " + response.getStatusCode()); |
|
229 |
// log.debug(response.getBody().toString()); |
|
230 |
// return null; |
|
231 |
// } else { |
|
232 |
// log.debug("[success] Saving works response code is: " + response.getStatusCode()); |
|
233 |
// log.debug(response.toString()); |
|
234 |
// |
|
235 |
// return response.toString(); |
|
236 |
// } |
|
237 |
// } |
|
210 |
// log.debug("User orcid: " + userOrcid); |
|
238 | 211 |
|
239 |
// @RequestMapping(value = "/works", method = RequestMethod.GET) |
|
240 |
// public String getAllWorks(@RequestParam String orcid) { |
|
241 |
// log.debug("getAllWorks: orcid = |" + orcid + "|"); |
|
242 |
// |
|
243 |
// String userAccessToken = userTokensService.getUserAccessToken(orcid); |
|
244 |
// log.debug("Access token: " + userAccessToken); |
|
245 |
// |
|
246 |
//// String url = "https://sandbox.orcid.org/v3.0/" + orcid.toString() + "/works"; |
|
247 |
// String url = "https://api.sandbox.orcid.org/v3.0/"+orcid+"/works"; |
|
248 |
// |
|
249 |
// RestTemplate restTemplate = new RestTemplate(); |
|
250 |
// restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
|
251 |
// protected boolean hasError(HttpStatus statusCode) { |
|
252 |
// return false; |
|
253 |
// } |
|
254 |
// }); |
|
255 |
// HttpHeaders headers = new HttpHeaders(); |
|
212 |
// String url = "https://sandbox.orcid.org/v3.0/" + orcid.toString() + "/works"; |
|
213 |
// String url = "https://api.sandbox.orcid.org/v3.0/" + userOrcid + "/work/" + putCode; |
|
214 |
String url = orcidConfig.getApiURL() + userOrcid + "/work/" + putCode; |
|
215 |
|
|
216 |
RestTemplate restTemplate = new RestTemplate(); |
|
217 |
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
|
218 |
protected boolean hasError(HttpStatus statusCode) { |
|
219 |
return false; |
|
220 |
} |
|
221 |
}); |
|
222 |
HttpHeaders headers = new HttpHeaders(); |
|
256 | 223 |
// headers.add("Accept", "application/json"); |
257 |
// headers.add("Authorization", "Bearer " + userAccessToken); |
|
258 |
// headers.add("Content-Type", "application/orcid+json"); |
|
259 |
// log.debug(headers.get("Authorization")); |
|
260 |
// HttpEntity<String> request = new HttpEntity<>(headers); |
|
261 |
// //logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class)); |
|
262 |
// ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class); |
|
263 |
// if (response.getStatusCode() != HttpStatus.OK) { |
|
264 |
// log.debug("Getting works response code is: " + response.getStatusCode()); |
|
224 |
headers.add("Authorization", "Bearer " + userAccessToken); |
|
225 |
headers.add("Content-Type", "application/orcid+json;charset=UTF-8"); |
|
226 |
|
|
227 |
HttpEntity<String> request = new HttpEntity<>(gson.toJson(result.getWork()), headers); |
|
228 |
orcid_log.info("request: "+request); |
|
229 |
orcid_log.info("url: "+url); |
|
230 |
|
|
231 |
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PUT, request, String.class); |
|
232 |
if (response.getStatusCode() != HttpStatus.OK) { |
|
233 |
orcid_log.error("Updating work response code is: " + response.getStatusCode()); |
|
234 |
orcid_log.error("Unexpected Response: "+response.getBody()); |
|
235 |
return null; |
|
236 |
} else { |
|
237 |
orcid_log.info("Response: "+response); |
|
238 |
Date date = new Date(); |
|
239 |
|
|
240 |
Work localWork = workService.getLocalWorkByPutCode(putCode); |
|
241 |
localWork.setPids(result.getPids()); |
|
242 |
localWork.setUpdateDate(date); |
|
243 |
|
|
244 |
workService.saveWork(localWork); |
|
245 |
return localWork; |
|
265 | 246 |
// return null; |
266 |
// } else { |
|
267 |
// log.debug(response); |
|
268 |
// return response.getBody().toString(); |
|
269 |
// } |
|
270 |
// |
|
271 |
//// try { |
|
272 |
//// URL obj = new URL(url); |
|
273 |
//// |
|
274 |
//// HttpURLConnection con = (HttpURLConnection) obj.openConnection(); |
|
275 |
//// con.setRequestProperty("Accept", "application/json"); |
|
276 |
//// con.setRequestProperty("Content-Type", "application/orcid+json"); |
|
277 |
//// con.setRequestProperty("Authorization", "Bearer "+userAccessToken); |
|
278 |
//// |
|
279 |
//// if (con.getResponseCode() != 200) { |
|
280 |
//// log.debug("Getting works response code is: " + con.getResponseCode()); |
|
281 |
//// return null; |
|
282 |
//// } |
|
283 |
//// BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
|
284 |
//// StringBuilder response = new StringBuilder(); |
|
285 |
//// String inputLine; |
|
286 |
//// while ((inputLine = in.readLine()) != null) { |
|
287 |
//// response.append(inputLine).append("\n"); |
|
288 |
//// } |
|
289 |
//// in.close(); |
|
290 |
//// log.debug(response); |
|
291 |
//// return response; |
|
292 |
//// } catch (Exception e) { |
|
293 |
//// log.error("An error occured while trying to fetch works for orcid: "+orcid, e); |
|
294 |
//// return null; |
|
295 |
//// } |
|
296 |
// } |
|
247 |
} |
|
297 | 248 |
|
249 |
} |
|
250 |
|
|
251 |
@RequestMapping(value = "/orcid/works", method = RequestMethod.GET) |
|
252 |
public String getWorksByPutCodes(@RequestParam String put_codes) throws BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, IOException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException { |
|
253 |
log.debug("getWorksByPutCodes: put_codes = " + put_codes); |
|
254 |
|
|
255 |
UserTokens userTokens = userTokensService.getUserTokens(); |
|
256 |
if(userTokens == null) { |
|
257 |
throw new AuthorizationServiceException("User is not registered"); |
|
258 |
} |
|
259 |
String userOrcid = userTokens.getOrcid(); |
|
260 |
String userAccessToken = userTokens.getAccessToken(); |
|
261 |
|
|
262 |
if(userOrcid == null || userAccessToken == null) { |
|
263 |
throw new AuthorizationServiceException("User is not registered"); |
|
264 |
} |
|
265 |
|
|
266 |
// log.debug("Access token: " + userAccessToken); |
|
267 |
// log.debug("User orcid: " + userOrcid); |
|
268 |
|
|
269 |
// String url = "https://sandbox.orcid.org/v3.0/" + orcid.toString() + "/works"; |
|
270 |
String url = orcidConfig.getApiURL()+userOrcid+"/works/"+put_codes; |
|
271 |
|
|
272 |
RestTemplate restTemplate = new RestTemplate(); |
|
273 |
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
|
274 |
protected boolean hasError(HttpStatus statusCode) { |
|
275 |
return false; |
|
276 |
} |
|
277 |
}); |
|
278 |
HttpHeaders headers = new HttpHeaders(); |
|
279 |
headers.add("Accept", "application/json"); |
|
280 |
headers.add("Authorization", "Bearer " + userAccessToken); |
|
281 |
headers.add("Content-Type", "application/orcid+json;charset=UTF-8"); |
|
282 |
|
|
283 |
HttpEntity<String> request = new HttpEntity<>(headers); |
|
284 |
orcid_log.info("request: "+request); |
|
285 |
orcid_log.info("url: "+url); |
|
286 |
|
|
287 |
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class); |
|
288 |
if (response.getStatusCode() != HttpStatus.OK) { |
|
289 |
orcid_log.error("Getting works response code is: " + response.getStatusCode()); |
|
290 |
orcid_log.error("Unexpected Response: "+response.getBody()); |
|
291 |
return null; |
|
292 |
} else { |
|
293 |
orcid_log.info("Response: "+response); |
|
294 |
return response.getBody().toString(); |
|
295 |
} |
|
296 |
} |
|
297 |
|
|
298 | 298 |
@PreAuthorize("isAuthenticated()") |
299 |
@RequestMapping(value = "/work/{putCode}/delete", method = RequestMethod.DELETE) |
|
299 |
@RequestMapping(value = "/orcid/work/{putCode}/delete", method = RequestMethod.DELETE)
|
|
300 | 300 |
public String deleteWork(@PathVariable String putCode) throws IOException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException { |
301 | 301 |
log.debug("deleteWork: putCode = " + putCode); |
302 | 302 |
|
... | ... | |
311 | 311 |
throw new AuthorizationServiceException("User is not registered"); |
312 | 312 |
} |
313 | 313 |
|
314 |
log.debug("Access token: " + userAccessToken); |
|
315 |
log.debug("User orcid: " + userOrcid); |
|
314 |
// log.debug("Access token: " + userAccessToken);
|
|
315 |
// log.debug("User orcid: " + userOrcid);
|
|
316 | 316 |
|
317 |
// String url = "https://sandbox.orcid.org/v3.0/" + orcid.toString() + "/works"; |
|
318 | 317 |
String url = "https://api.sandbox.orcid.org/v3.0/"+userOrcid+"/work/" + putCode; |
319 | 318 |
|
320 | 319 |
RestTemplate restTemplate = new RestTemplate(); |
... | ... | |
326 | 325 |
HttpHeaders headers = new HttpHeaders(); |
327 | 326 |
// headers.add("Accept", "application/json"); |
328 | 327 |
headers.add("Authorization", "Bearer " + userAccessToken); |
329 |
headers.add("Content-Type","application/orcid+json");
|
|
328 |
headers.add("Content-Type", "application/orcid+json;charset=UTF-8");
|
|
330 | 329 |
|
331 | 330 |
HttpEntity<String> request = new HttpEntity<>(headers); |
332 |
//logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class)); |
|
331 |
orcid_log.info("request: "+request); |
|
332 |
orcid_log.info("url: "+url); |
|
333 |
|
|
333 | 334 |
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.DELETE, request, String.class); |
334 | 335 |
if (response.getStatusCode() != HttpStatus.NO_CONTENT) { |
335 |
log.debug("Deleting work response code is: " + response.getStatusCode()); |
|
336 |
orcid_log.error("Deleting work response code is: " + response.getStatusCode()); |
|
337 |
orcid_log.error("Unexpected Response: "+response.getBody()); |
|
338 |
|
|
336 | 339 |
if(response.getStatusCode() == HttpStatus.NOT_FOUND) { |
337 | 340 |
workService.deleteWork(putCode); |
338 | 341 |
return putCode; |
... | ... | |
342 | 345 |
} |
343 | 346 |
return null; |
344 | 347 |
} else { |
345 |
log.debug(response);
|
|
348 |
orcid_log.info("Response: "+response);
|
|
346 | 349 |
|
347 | 350 |
workService.deleteWork(putCode); |
348 | 351 |
|
... | ... | |
352 | 355 |
} |
353 | 356 |
|
354 | 357 |
@PreAuthorize("isAuthenticated()") |
355 |
@RequestMapping(value = "/works/delete", method = RequestMethod.POST) |
|
358 |
@RequestMapping(value = "/orcid/works/delete", method = RequestMethod.POST)
|
|
356 | 359 |
public List<String> deleteWorks(@RequestBody List<String> putCodes) throws IOException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException { |
357 | 360 |
log.debug("deleteWorks: putCodes = " + putCodes); |
358 | 361 |
|
... | ... | |
367 | 370 |
throw new AuthorizationServiceException("User is not registered"); |
368 | 371 |
} |
369 | 372 |
|
370 |
log.debug("Access token: " + userAccessToken); |
|
371 |
log.debug("User orcid: " + userOrcid); |
|
373 |
// log.debug("Access token: " + userAccessToken);
|
|
374 |
// log.debug("User orcid: " + userOrcid);
|
|
372 | 375 |
|
373 | 376 |
RestTemplate restTemplate = new RestTemplate(); |
374 | 377 |
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
... | ... | |
379 | 382 |
HttpHeaders headers = new HttpHeaders(); |
380 | 383 |
// headers.add("Accept", "application/json"); |
381 | 384 |
headers.add("Authorization", "Bearer " + userAccessToken); |
382 |
headers.add("Content-Type","application/orcid+json");
|
|
385 |
headers.add("Content-Type", "application/orcid+json;charset=UTF-8");
|
|
383 | 386 |
|
384 | 387 |
HttpEntity<String> request = new HttpEntity<>(headers); |
385 |
//logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class)); |
|
386 | 388 |
|
387 | 389 |
List<String> deletedPutCodes = new ArrayList<>(); |
388 | 390 |
int index = 0; |
... | ... | |
390 | 392 |
String url = "https://api.sandbox.orcid.org/v3.0/"+userOrcid+"/work/" + putCode; |
391 | 393 |
// UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url); |
392 | 394 |
|
395 |
orcid_log.info("request: "+request); |
|
396 |
orcid_log.info("url: "+url); |
|
397 |
|
|
393 | 398 |
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.DELETE, request, String.class); |
394 | 399 |
if (response.getStatusCode() != HttpStatus.NO_CONTENT) { |
395 |
log.debug("Deleting work response code is: " + response.getStatusCode()); |
|
400 |
orcid_log.error("Deleting work response code is: " + response.getStatusCode()); |
|
401 |
orcid_log.error("Unexpected Response: "+response.getBody()); |
|
402 |
|
|
396 | 403 |
if(index == 0 && response.getStatusCode() == HttpStatus.UNAUTHORIZED) { |
397 | 404 |
throw new AuthorizationServiceException("You are not allowed to delete work"); |
398 | 405 |
} |
... | ... | |
403 | 410 |
deletedPutCodes.add(null); |
404 | 411 |
} |
405 | 412 |
} else { |
406 |
log.debug(response);
|
|
413 |
orcid_log.info("Response: "+response);
|
|
407 | 414 |
|
408 | 415 |
workService.deleteWork(putCode); |
409 | 416 |
|
... | ... | |
415 | 422 |
} |
416 | 423 |
|
417 | 424 |
@PreAuthorize("isAuthenticated()") |
418 |
@RequestMapping(value = "/work", method = RequestMethod.GET) |
|
425 |
@RequestMapping(value = "/orcid/work", method = RequestMethod.GET)
|
|
419 | 426 |
public List<String> getWorksOfReuslt(@RequestParam String[] pids, @RequestParam String orcid) throws BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException { |
420 | 427 |
log.debug("getWorks: pids = " + pids + " - orcid = "+orcid); |
421 | 428 |
|
422 | 429 |
List<Work> works = workService.getWorks(pids, orcid); |
423 | 430 |
|
424 | 431 |
String userAccessToken = userTokensService.getUserAccessToken(orcid); |
425 |
log.debug("Access token: " + userAccessToken); |
|
432 |
// log.debug("Access token: " + userAccessToken);
|
|
426 | 433 |
|
427 | 434 |
RestTemplate restTemplate = new RestTemplate(); |
428 | 435 |
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
... | ... | |
433 | 440 |
HttpHeaders headers = new HttpHeaders(); |
434 | 441 |
headers.add("Accept", "application/json"); |
435 | 442 |
headers.add("Authorization", "Bearer " + userAccessToken); |
436 |
headers.add("Content-Type", "application/orcid+json"); |
|
437 |
log.debug(headers.get("Authorization")); |
|
443 |
headers.add("Content-Type", "application/orcid+json;charset=UTF-8");
|
|
444 |
|
|
438 | 445 |
HttpEntity<String> request = new HttpEntity<>(headers); |
439 | 446 |
|
440 | 447 |
List<String> responseValues = new ArrayList<>(); |
441 | 448 |
|
442 | 449 |
for(Work work : works) { |
443 |
// String url = "https://api.sandbox.orcid.org/v3.0/0000-0001-9541-4617/work/"+work.getPutCode(); |
|
444 |
String url = "https://api.sandbox.orcid.org/v3.0/" + orcid + "/work/" + work.getPutCode(); |
|
445 |
log.debug(url); |
|
450 |
String url = orcidConfig.getApiURL() + orcid + "/work/" + work.getPutCode(); |
|
446 | 451 |
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url); |
452 |
orcid_log.info("request: "+request); |
|
453 |
orcid_log.info("url: "+url); |
|
447 | 454 |
|
448 |
//logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class)); |
|
449 | 455 |
ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, request, String.class); |
450 |
// ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class); |
|
451 | 456 |
if (response.getStatusCode() != HttpStatus.OK) { |
452 |
log.debug("Getting work response code is: " + response.getStatusCode()); |
|
457 |
orcid_log.error("Getting work response code is: " + response.getStatusCode()); |
|
458 |
orcid_log.error("Unexpected Response: "+response.getBody()); |
|
453 | 459 |
|
454 | 460 |
if (response.getStatusCode() == HttpStatus.NOT_FOUND) { |
455 | 461 |
// work.setPutCode(null); |
... | ... | |
459 | 465 |
|
460 | 466 |
responseValues.add(null); |
461 | 467 |
} else { |
462 |
log.debug(response);
|
|
468 |
orcid_log.info("Response: "+response);
|
|
463 | 469 |
responseValues.add(response.getBody().toString()); |
464 | 470 |
} |
465 | 471 |
} |
466 | 472 |
return responseValues; |
467 | 473 |
} |
468 | 474 |
|
475 |
// @PreAuthorize("isAuthenticated()") |
|
476 |
// @RequestMapping(value = "/local/works", method = RequestMethod.GET) |
|
477 |
// public Map<String, Object> getLocalWorks(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer size) |
|
478 |
// throws BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException { |
|
479 |
// log.debug("getWorks: page="+page+ " - size="+size); |
|
480 |
// |
|
481 |
// String userOrcid = userTokensService.getCurrentUserOrcid(); |
|
482 |
// if(userOrcid == null) { |
|
483 |
// throw new AuthorizationServiceException("User is not registered"); |
|
484 |
// } |
|
485 |
// |
|
486 |
// List<Work> works = workService.getLocalWorks(userOrcid); |
|
487 |
// |
|
488 |
// Map<String, Object> response = new HashMap<>(); |
|
489 |
// response.put("total", works.size()); |
|
490 |
// |
|
491 |
// if(page != null && page > 0 && size != null && size > 0) { |
|
492 |
// int from = (page-1)*size; |
|
493 |
// int to = page*size; |
|
494 |
// |
|
495 |
//// log.debug("size: "+works.size()); |
|
496 |
// if(to > works.size()) { |
|
497 |
// to = works.size(); |
|
498 |
// } |
|
499 |
//// log.debug("from="+from+" - to="+to); |
|
500 |
// |
|
501 |
//// int index = 0; |
|
502 |
//// Iterator<Work> iterator = works.iterator(); |
|
503 |
//// while(iterator.hasNext()) { |
|
504 |
//// iterator.next(); |
|
505 |
//// if(index < from || index > to) { |
|
506 |
//// iterator.remove(); |
|
507 |
//// } |
|
508 |
//// index++; |
|
509 |
//// } |
|
510 |
// if(from < to) { |
|
511 |
// response.put("results", works.subList(from, to)); |
|
512 |
// return response; |
|
513 |
// } |
|
514 |
// } |
|
515 |
// response.put("results", works); |
|
516 |
// return response; |
|
517 |
// } |
|
518 |
|
|
469 | 519 |
@PreAuthorize("isAuthenticated()") |
470 |
@RequestMapping(value = "/works/local", method = RequestMethod.GET) |
|
471 |
public List<Work> getLocalWorks() throws BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException { |
|
472 |
log.debug("getWorks"); |
|
473 |
|
|
474 |
UserTokens userTokens = userTokensService.getUserTokens(); |
|
475 |
if(userTokens == null) { |
|
476 |
throw new AuthorizationServiceException("User is not registered"); |
|
477 |
} |
|
478 |
String userOrcid = userTokens.getOrcid(); |
|
479 |
|
|
520 |
@RequestMapping(value = "/local/works", method = RequestMethod.GET) |
|
521 |
public List<Work> getMyLocalWorks() { |
|
522 |
log.debug("getMyWorks"); |
|
523 |
// |
|
524 |
String userOrcid = userTokensService.getCurrentUserOrcid(); |
|
480 | 525 |
if(userOrcid == null) { |
481 | 526 |
throw new AuthorizationServiceException("User is not registered"); |
482 | 527 |
} |
483 |
|
|
484 |
return workService.getLocalWorks(userOrcid); |
|
528 |
// |
|
529 |
List<Work> works = workService.getLocalWorks(userOrcid); |
|
530 |
// |
|
531 |
return works; |
|
485 | 532 |
} |
486 | 533 |
} |
Also available in: Unified diff
[Trunk | Orcid Service]: getLocalWorks(@RequestBody String[][] pids)" (/local/works).
1. orcidservice.properties: Added properties "apiURL", "tokenURL", "clientId", "clientSecret" for ORCID API.
2. log4j.properties: Added configuration for ORCID log file "uoa-orcid-service-orcid.log".
3. UoaOrcidServiceApplication.java: Added "OrcidConfig.class" in "@EnableConfigurationProperties".
4. SimpleErrorController.java: If body.path.contains("/uoa-orcid-service/orcid"), log error in ORCID log file too.
5. UserTokensService.java: Added method "getEncryptedUserTokensByAai()".
6. UserTokensController.java:
a. Added ORCID log file for responses by ORCID API.
b. Added @Autowired OrcidConfig for getting ORICD token url, client and secret from properties.
c. Added method "getUserOrcidId()" (/local/orcidId)
d. Added method "getPersonalDetailsFromOrcid()" (/orcid/personal-details)
e. Renamed "/orcid/tokens/decrypt" to "/local/tokens/decrypt" & "/orcid/tokens/encrypt" to "/local/tokens/encrypt".
7. Work.java: Added field "updateDate".
8. WorkDAO.java & MongoDBWorkDAO.java: Added methods "List<Work> findByOrcidOrderByCreationDateDesc(String Orcid);" and "Work findByPutCode(String putCode);".
9. WorkService.java:
a. Added method "getLocalWorkByPutCode()".
b. Method "getLocalWorks()" returns works ordered by most recent creation date.
10. WorkController.java:
a. Added ORCID log file for responses by ORCID API.
b. Added @Autowired OrcidConfig for getting ORICD token url, client and secret from properties.
c. Added "charset=UTF-8" in all Content-Type request headers.
d. Renamed "/orcid/put-code" to "/local/put-code".
e. Added method "List<List
f. Added method "List<Work> getMyLocalWorks()" (/local/works).
g. Added method "updateWork()" (/orcid/work/update/{putCode}).
h. Added method "getWorksByPutCodes()" (/orcid/works).