1
|
package eu.dnetlib.data.claims.migration.handler;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
import org.json.simple.JSONObject;
|
5
|
import sun.net.www.protocol.http.HttpURLConnection;
|
6
|
|
7
|
import java.io.*;
|
8
|
import java.net.MalformedURLException;
|
9
|
import java.net.URL;
|
10
|
import java.util.List;
|
11
|
|
12
|
|
13
|
/**
|
14
|
* Created by argirok on 26/4/2017.
|
15
|
*/
|
16
|
public class DirectIndexHandler {
|
17
|
|
18
|
private String directClaimAPIUrl;
|
19
|
private static final Logger logger = Logger.getLogger(DirectIndexHandler.class);
|
20
|
|
21
|
|
22
|
public boolean insertRecord(String record){
|
23
|
|
24
|
return this.feedIndex(record);
|
25
|
}
|
26
|
public boolean updateRecord(String record){
|
27
|
|
28
|
return this.feedIndex(record);
|
29
|
}
|
30
|
public boolean deleteRecord(String originalId, String collectedFromId){
|
31
|
|
32
|
return this.removeObjectFromIndex(originalId,collectedFromId);
|
33
|
}
|
34
|
|
35
|
private boolean feedIndex(String record) {
|
36
|
|
37
|
URL obj = null;
|
38
|
try {
|
39
|
obj = new URL(directClaimAPIUrl+"/feedObject");
|
40
|
} catch (MalformedURLException e) {
|
41
|
logger.error("Malformed direct Claim APIUrl" + directClaimAPIUrl,e);
|
42
|
return false;
|
43
|
|
44
|
}
|
45
|
HttpURLConnection con = null;
|
46
|
try {
|
47
|
con = (HttpURLConnection) obj.openConnection();
|
48
|
|
49
|
|
50
|
|
51
|
//add request header
|
52
|
con.setRequestMethod("POST");
|
53
|
//con.setRequestProperty("User-Agent", USER_AGENT);
|
54
|
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
55
|
con.setRequestProperty("Content-Type", "application/json");
|
56
|
|
57
|
logger.info("Feeding index with " + record);
|
58
|
String urlParameters = record;
|
59
|
|
60
|
// Send post request
|
61
|
con.setDoOutput(true);
|
62
|
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
63
|
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
|
64
|
writer.write(urlParameters);
|
65
|
writer.close();
|
66
|
wr.close();
|
67
|
|
68
|
int responseCode = con.getResponseCode();
|
69
|
logger.info("\nSending 'POST' request to URL : " + directClaimAPIUrl);
|
70
|
logger.info("Post parameters : " + urlParameters);
|
71
|
logger.info("Response Code : " + responseCode);
|
72
|
|
73
|
BufferedReader in = new BufferedReader(
|
74
|
new InputStreamReader(con.getInputStream()));
|
75
|
String inputLine;
|
76
|
StringBuffer response = new StringBuffer();
|
77
|
|
78
|
while ((inputLine = in.readLine()) != null) {
|
79
|
response.append(inputLine);
|
80
|
}
|
81
|
in.close();
|
82
|
if(responseCode != 200){
|
83
|
logger.error("An error occured. Record isn't saved in the Index.\n"+response.toString());
|
84
|
return false;
|
85
|
}else{
|
86
|
return true;
|
87
|
}
|
88
|
} catch (IOException e) {
|
89
|
logger.error("An error occured while trying to feed object" ,e);
|
90
|
|
91
|
return false;
|
92
|
}
|
93
|
}
|
94
|
private boolean removeObjectFromIndex(String originalId, String collectedFromId){
|
95
|
|
96
|
URL obj = null;
|
97
|
String urlParameters = "?originalId="+originalId+"&collectedFromId="+collectedFromId;
|
98
|
|
99
|
try {
|
100
|
obj = new URL(directClaimAPIUrl+"/deleteObject"+urlParameters);
|
101
|
} catch (MalformedURLException e) {
|
102
|
logger.error("Malformed direct Claim APIUrl" + directClaimAPIUrl,e);
|
103
|
return false;
|
104
|
|
105
|
}
|
106
|
HttpURLConnection con = null;
|
107
|
try {
|
108
|
con = (HttpURLConnection) obj.openConnection();
|
109
|
|
110
|
|
111
|
|
112
|
//add request header
|
113
|
con.setRequestMethod("POST");
|
114
|
//con.setRequestProperty("User-Agent", USER_AGENT);
|
115
|
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
116
|
con.setRequestProperty("Content-Type", "application/json");
|
117
|
|
118
|
|
119
|
// Send post request
|
120
|
con.setDoOutput(true);
|
121
|
// DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
122
|
// BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
|
123
|
// writer.write(urlParameters);
|
124
|
// writer.close();
|
125
|
// wr.close();
|
126
|
|
127
|
int responseCode = con.getResponseCode();
|
128
|
logger.info("\nSending 'POST' request to URL : " + directClaimAPIUrl+urlParameters);
|
129
|
logger.info("Post parameters : " + urlParameters);
|
130
|
logger.info("Response Code : " + responseCode);
|
131
|
|
132
|
BufferedReader in = new BufferedReader(
|
133
|
new InputStreamReader(con.getInputStream()));
|
134
|
String inputLine;
|
135
|
StringBuffer response = new StringBuffer();
|
136
|
|
137
|
while ((inputLine = in.readLine()) != null) {
|
138
|
response.append(inputLine);
|
139
|
}
|
140
|
in.close();
|
141
|
|
142
|
//print result
|
143
|
logger.info(response.toString());
|
144
|
if(response.toString().equals("true")){
|
145
|
return true;
|
146
|
}else{
|
147
|
return false;
|
148
|
}
|
149
|
} catch (IOException e) {
|
150
|
logger.error("An error occured while trying to delete object",e);
|
151
|
|
152
|
return false;
|
153
|
}
|
154
|
}
|
155
|
|
156
|
public String getDirectClaimAPIUrl() {
|
157
|
return directClaimAPIUrl;
|
158
|
}
|
159
|
|
160
|
public void setDirectClaimAPIUrl(String directClaimAPIUrl) {
|
161
|
this.directClaimAPIUrl = directClaimAPIUrl;
|
162
|
}
|
163
|
}
|