Newer
Older
package it.inaf.oats.ia2.tapschemamanager.webapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;
/**
* Collection of static methods for accessing to the UCD REST service and
* validating custom UCD.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class SearchUCD {
private static final String UCD_SERVICE_URL;
private static final String UCD_NOT_FOUND = "**** Could not find UCD ****";
public static final String REG_EXP_UCD;
public static final String REG_EXP_START_WITH_NAMESPACE;
private static final Pattern PATTERN_UCD;
private static final Pattern PATTERN_START_WITH_NAMESPACE;
static {
try {
Properties prop = new Properties();
try (InputStream in = SearchUCD.class.getClassLoader().getResourceAsStream("webapp.properties")) {
prop.load(in);
}
UCD_SERVICE_URL = prop.getProperty("ucd_service_url");
} catch (IOException e) {
throw new RuntimeException("Unable to load UCD service URL from webapp.properties configuration!");
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
}
String namespaceRegExpPart = "[a-zA-Z]+\\:"; // validate e.g. "mynamespace:"
String[] ucdCategories = new String[]{"arith", "em", "instr", "meta", "obs", "phot", "phys", "pos", "spect", "src", "stat", "time"};
StringBuilder sbCategories = new StringBuilder();
boolean first = true;
for (String category : ucdCategories) {
if (!first) {
sbCategories.append("|");
}
first = false;
sbCategories.append(category);
}
String regExpWordPart = String.format("(%s)([\\.][\\-a-zA-Z0-9]+)*", sbCategories.toString()); // validate single word
REG_EXP_UCD = String.format("^(%s)?%s(;%s)*$", namespaceRegExpPart, regExpWordPart, regExpWordPart);
REG_EXP_START_WITH_NAMESPACE = String.format("^%s.+$", namespaceRegExpPart);
PATTERN_UCD = Pattern.compile(REG_EXP_UCD);
PATTERN_START_WITH_NAMESPACE = Pattern.compile(REG_EXP_START_WITH_NAMESPACE);
}
private static String encodeText(String searchText) throws UCDServiceException {
try {
return URLEncoder.encode(searchText, "UTF-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
throw new UCDServiceException("Error while encoding input text");
}
}
public static String assign(String searchText) throws UCDServiceException {
searchText = encodeText(searchText);
String urlStr = UCD_SERVICE_URL + "assign?value=" + searchText;
try {
URL urlAssign = new URL(urlStr);
HttpURLConnection connectionAssign = (HttpURLConnection) urlAssign.openConnection();
int responseCodeAssign = connectionAssign.getResponseCode();
if (responseCodeAssign == 200) {
BufferedReader br;
String line, response = "";
br = new BufferedReader(new InputStreamReader(connectionAssign.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
if (response.equals(UCD_NOT_FOUND)) {
return null;
}
return response;
}
throw new UCDServiceException("Server responded with a status of " + responseCodeAssign);
} catch (MalformedURLException e) {
throw new UCDServiceException("Malformed url: " + urlStr);
} catch (IOException e) {
throw new UCDServiceException("Error while reading server response");
}
}
public static List<UCDInfo> suggest(String searchText) throws UCDServiceException {
searchText = encodeText(searchText);
String urlStr = UCD_SERVICE_URL + "suggest?value=" + searchText;
try {
URL urlSuggest = new URL(urlStr);
HttpURLConnection connectionSuggest = (HttpURLConnection) urlSuggest.openConnection();
int responseCodeSuggest = connectionSuggest.getResponseCode();
if (responseCodeSuggest == 200) {
List<UCDInfo> resultList = new ArrayList<>();
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
BufferedReader br = new BufferedReader(new InputStreamReader(connectionSuggest.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
String[] split1 = line.split("\\|");
if (split1.length == 2) {
String score = split1[0].trim();
String[] split2 = split1[1].trim().split(" ");
if (split2.length == 2) {
String flag = split2[0];
String word = split2[1];
resultList.add(new UCDInfo(score, flag, word));
}
}
}
return resultList;
} else if (responseCodeSuggest == 204) {
return null;
}
throw new UCDServiceException("Server responded with a status of " + responseCodeSuggest);
} catch (MalformedURLException e) {
throw new UCDServiceException("Malformed url: " + urlStr);
} catch (IOException e) {
throw new UCDServiceException("Error while reading server response");
}
}
public static void explain(UCDInfo ucdInfo) throws UCDServiceException {
String searchText = encodeText(ucdInfo.getWord());
String urlStr = UCD_SERVICE_URL + "explain?value=" + searchText;
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line, response = "";
while ((line = br.readLine()) != null) {
response += line;
}
ucdInfo.setDefinition(response);
} else {
throw new UCDServiceException("Server responded with a status of " + responseCode);
}
} catch (MalformedURLException e) {
throw new UCDServiceException("Malformed url: " + urlStr);
} catch (IOException e) {
throw new UCDServiceException("Error while reading server response");
}
}
public static boolean validateManualUCD(String inputText) throws UCDServiceException {
if (inputText == null) {
return false;
}
if (!PATTERN_UCD.matcher(inputText).matches()) {
return false;
}
boolean customUCD = PATTERN_START_WITH_NAMESPACE.matcher(inputText).matches();
if (customUCD) {
return true;
} else {
String searchText = encodeText(inputText);
String urlStr = UCD_SERVICE_URL + "validate?value=" + searchText;
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line, response = "";
while ((line = br.readLine()) != null) {
response += line;
}
return response.equals("0"); // String "0" means valid UCD
} else {
throw new UCDServiceException("Server responded with a status of " + responseCode);
}
} catch (MalformedURLException e) {
throw new UCDServiceException("Malformed url: " + urlStr);
} catch (IOException e) {
throw new UCDServiceException("Error while reading server response");
}
}
}
}