Revision 60492
Added by Konstantina Galouni about 3 years ago
RecordController.java | ||
---|---|---|
1 |
package eu.dnetlib.uoaorcidservice.controllers; |
|
2 |
|
|
3 |
import eu.dnetlib.uoaorcidservice.services.UserTokensService; |
|
4 |
import org.apache.log4j.Logger; |
|
5 |
import org.springframework.http.*; |
|
6 |
import org.springframework.web.bind.annotation.*; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.web.client.DefaultResponseErrorHandler; |
|
9 |
import org.springframework.web.client.RestTemplate; |
|
10 |
|
|
11 |
import javax.crypto.BadPaddingException; |
|
12 |
import javax.crypto.IllegalBlockSizeException; |
|
13 |
import javax.crypto.NoSuchPaddingException; |
|
14 |
import java.io.IOException; |
|
15 |
import java.security.InvalidAlgorithmParameterException; |
|
16 |
import java.security.InvalidKeyException; |
|
17 |
import java.security.NoSuchAlgorithmException; |
|
18 |
import java.security.spec.InvalidKeySpecException; |
|
19 |
|
|
20 |
@RestController |
|
21 |
@RequestMapping("/orcid") |
|
22 |
@CrossOrigin(origins = "*") |
|
23 |
public class RecordController { |
|
24 |
private final Logger log = Logger.getLogger(this.getClass()); |
|
25 |
|
|
26 |
@Autowired |
|
27 |
UserTokensService userTokensService; |
|
28 |
|
|
29 |
@RequestMapping(value = {"/record"}, method = RequestMethod.GET) |
|
30 |
public String getRecord() throws BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException { |
|
31 |
String testingUserToken = userTokensService.getUserAccessToken(null); |
|
32 |
String testingUserOrcid = userTokensService.getCurrentUserOrcid(); |
|
33 |
|
|
34 |
String url = "https://api.sandbox.orcid.org/v3.0/"+testingUserOrcid;//+"/record"; |
|
35 |
|
|
36 |
RestTemplate restTemplate = new RestTemplate(); |
|
37 |
restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){ |
|
38 |
protected boolean hasError(HttpStatus statusCode) { |
|
39 |
return false; |
|
40 |
}}); |
|
41 |
HttpHeaders headers = new HttpHeaders(); |
|
42 |
headers.add("Accept", "application/json"); |
|
43 |
headers.add("Authorization", "Bearer "+testingUserToken); |
|
44 |
headers.add("Content-Type","application/orcid+json"); |
|
45 |
|
|
46 |
HttpEntity<String> request = new HttpEntity<>(headers); |
|
47 |
//logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class)); |
|
48 |
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class); |
|
49 |
if(response.getStatusCode() != HttpStatus.OK) { |
|
50 |
log.debug("Getting orcid record response code is: " + response.getStatusCode()); |
|
51 |
return null; |
|
52 |
} else { |
|
53 |
log.debug(response); |
|
54 |
return response.getBody().toString(); |
|
55 |
} |
|
56 |
|
|
57 |
// try { |
|
58 |
// URL obj = new URL(url); |
|
1 |
//package eu.dnetlib.uoaorcidservice.controllers; |
|
59 | 2 |
// |
60 |
// HttpURLConnection con = (HttpURLConnection) obj.openConnection(); |
|
61 |
// con.setRequestProperty("Accept", "application/json"); |
|
62 |
// con.setRequestProperty("Content-Type", "application/json"); |
|
63 |
// con.setRequestProperty("Authorization", "Bearer "+testingUserToken); |
|
3 |
//import eu.dnetlib.uoaorcidservice.services.UserTokensService; |
|
4 |
//import org.apache.log4j.Logger; |
|
5 |
//import org.springframework.http.*; |
|
6 |
//import org.springframework.web.bind.annotation.*; |
|
7 |
//import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
//import org.springframework.web.client.DefaultResponseErrorHandler; |
|
9 |
//import org.springframework.web.client.RestTemplate; |
|
64 | 10 |
// |
65 |
// if (con.getResponseCode() != 200) { |
|
66 |
// log.debug("User record response code is: " + con.getResponseCode()); |
|
67 |
// return null; |
|
68 |
// } |
|
69 |
// BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
|
70 |
// StringBuilder response = new StringBuilder(); |
|
71 |
// String inputLine; |
|
72 |
// while ((inputLine = in.readLine()) != null) { |
|
73 |
// response.append(inputLine).append("\n"); |
|
74 |
// } |
|
75 |
// in.close(); |
|
11 |
//import javax.crypto.BadPaddingException; |
|
12 |
//import javax.crypto.IllegalBlockSizeException; |
|
13 |
//import javax.crypto.NoSuchPaddingException; |
|
14 |
//import java.io.IOException; |
|
15 |
//import java.security.InvalidAlgorithmParameterException; |
|
16 |
//import java.security.InvalidKeyException; |
|
17 |
//import java.security.NoSuchAlgorithmException; |
|
18 |
//import java.security.spec.InvalidKeySpecException; |
|
19 |
// |
|
20 |
//@RestController |
|
21 |
//@RequestMapping("/orcid") |
|
22 |
//@CrossOrigin(origins = "*") |
|
23 |
//public class RecordController { |
|
24 |
// private final Logger log = Logger.getLogger(this.getClass()); |
|
25 |
// |
|
26 |
// @Autowired |
|
27 |
// UserTokensService userTokensService; |
|
28 |
// |
|
29 |
// @RequestMapping(value = {"/record"}, method = RequestMethod.GET) |
|
30 |
// public String getRecord() throws BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException { |
|
31 |
// String testingUserToken = userTokensService.getUserAccessToken(null); |
|
32 |
// String testingUserOrcid = userTokensService.getCurrentUserOrcid(); |
|
33 |
// |
|
34 |
// String url = "https://api.sandbox.orcid.org/v3.0/"+testingUserOrcid;//+"/record"; |
|
35 |
// |
|
36 |
// RestTemplate restTemplate = new RestTemplate(); |
|
37 |
// restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){ |
|
38 |
// protected boolean hasError(HttpStatus statusCode) { |
|
39 |
// return false; |
|
40 |
// }}); |
|
41 |
// HttpHeaders headers = new HttpHeaders(); |
|
42 |
// headers.add("Accept", "application/json"); |
|
43 |
// headers.add("Authorization", "Bearer "+testingUserToken); |
|
44 |
// headers.add("Content-Type","application/orcid+json"); |
|
45 |
// |
|
46 |
// HttpEntity<String> request = new HttpEntity<>(headers); |
|
47 |
// //logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class)); |
|
48 |
// ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class); |
|
49 |
// if(response.getStatusCode() != HttpStatus.OK) { |
|
50 |
// log.debug("Getting orcid record response code is: " + response.getStatusCode()); |
|
51 |
// return null; |
|
52 |
// } else { |
|
76 | 53 |
// log.debug(response); |
77 |
// return response; |
|
78 |
// } catch (Exception e) { |
|
79 |
// log.error("An error occured while trying to fetch user record ", e); |
|
80 |
// return null; |
|
54 |
// return response.getBody().toString(); |
|
81 | 55 |
// } |
82 |
} |
|
83 |
} |
|
56 |
// |
|
57 |
//// try { |
|
58 |
//// URL obj = new URL(url); |
|
59 |
//// |
|
60 |
//// HttpURLConnection con = (HttpURLConnection) obj.openConnection(); |
|
61 |
//// con.setRequestProperty("Accept", "application/json"); |
|
62 |
//// con.setRequestProperty("Content-Type", "application/json"); |
|
63 |
//// con.setRequestProperty("Authorization", "Bearer "+testingUserToken); |
|
64 |
//// |
|
65 |
//// if (con.getResponseCode() != 200) { |
|
66 |
//// log.debug("User record response code is: " + con.getResponseCode()); |
|
67 |
//// return null; |
|
68 |
//// } |
|
69 |
//// BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
|
70 |
//// StringBuilder response = new StringBuilder(); |
|
71 |
//// String inputLine; |
|
72 |
//// while ((inputLine = in.readLine()) != null) { |
|
73 |
//// response.append(inputLine).append("\n"); |
|
74 |
//// } |
|
75 |
//// in.close(); |
|
76 |
//// log.debug(response); |
|
77 |
//// return response; |
|
78 |
//// } catch (Exception e) { |
|
79 |
//// log.error("An error occured while trying to fetch user record ", e); |
|
80 |
//// return null; |
|
81 |
//// } |
|
82 |
// } |
|
83 |
//} |
Also available in: Unified diff
[Trunk | Orcid Service]:
1. RecordController.java: Comment file RecordController.java, since it is not used by portal (to be deleted).
2. WorkController.java: Use "apiUrl" property for requests to ORCID API (not string set in the code).