Added setting KorAP base service URI from web.xml
Change-Id: I81f4accafa1f6fd471fce6f990373f5d49ab5c85
diff --git a/src/main/java/de/mannheim/ids/korap/sru/KorapClient.java b/src/main/java/de/mannheim/ids/korap/sru/KorapClient.java
index 71e67c3..a131e9d 100644
--- a/src/main/java/de/mannheim/ids/korap/sru/KorapClient.java
+++ b/src/main/java/de/mannheim/ids/korap/sru/KorapClient.java
@@ -37,9 +37,11 @@
*/
public class KorapClient {
- private static String SERVICE_URI;
- private static final String CONFIGURATION_FILE = "kustvakt.conf";
- private static final String SERVICE_URI_PROPERTY = "korapsru.client.service.uri";
+ private static String serviceUri;
+ private static final String CONFIGURATION_FILE =
+ "kustvakt.conf";
+ private static final String SERVICE_URI_PROPERTY =
+ "korapsru.client.service.uri";
private static final String DEFAULT_CONTEXT_TYPE = "sentence";
private static final String DEFAULT_FOUNDRY = "*";
@@ -47,8 +49,8 @@
private int defaultMaxRecords = 10;
private static ObjectMapper objectMapper = new ObjectMapper();
- private static Logger logger = (Logger) LoggerFactory
- .getLogger(KorapClient.class);
+ private static Logger logger =
+ (Logger) LoggerFactory.getLogger(KorapClient.class);
/**
@@ -65,26 +67,33 @@
throws FileNotFoundException {
this.defaultNumOfRecords = numOfRecords;
this.defaultMaxRecords = maxRecords;
-
+
Properties properties = new Properties();
+ String path = System.getenv("$HOME")+"/"+CONFIGURATION_FILE;
InputStream is = getClass().getClassLoader()
- .getResourceAsStream(CONFIGURATION_FILE);
+ .getResourceAsStream(path);
try {
properties.load(is);
}
catch (IOException e) {
throw new FileNotFoundException("Configuration file "
- + CONFIGURATION_FILE + " is not found in the classpath.");
+ + CONFIGURATION_FILE + " is not found.");
}
if (properties.containsKey(SERVICE_URI_PROPERTY)) {
- SERVICE_URI = properties.getProperty("korapsru.client.service.uri");
- logger.info(SERVICE_URI);
+ serviceUri = properties.getProperty("korapsru.client.service.uri");
+ logger.info(serviceUri);
}
else {
throw new NullPointerException("Please specify korapsru.client."
+ "service.uri in the configuration file.");
}
}
+
+ public KorapClient (String serviceUri, int numOfRecords, int maxRecords){
+ this.defaultNumOfRecords = numOfRecords;
+ this.defaultMaxRecords = maxRecords;
+ this.serviceUri = serviceUri;
+ }
/**
@@ -99,7 +108,7 @@
public JsonNode retrieveResources ()
throws URISyntaxException, IOException {
- URIBuilder builder = new URIBuilder(SERVICE_URI + "Corpus");
+ URIBuilder builder = new URIBuilder(serviceUri + "Corpus");
URI uri = builder.build();
logger.info("Resource URI: " + uri.toString());
HttpGet httpRequest = new HttpGet(uri);
@@ -120,8 +129,8 @@
response.getStatusLine().getReasonPhrase());
}
- BufferedInputStream jsonStream = new BufferedInputStream(
- response.getEntity().getContent());
+ BufferedInputStream jsonStream =
+ new BufferedInputStream(response.getEntity().getContent());
try {
resources = objectMapper.readValue(jsonStream, JsonNode.class);
}
@@ -199,8 +208,8 @@
parseError(response);
}
- BufferedInputStream jsonStream = new BufferedInputStream(
- response.getEntity().getContent());
+ BufferedInputStream jsonStream =
+ new BufferedInputStream(response.getEntity().getContent());
try {
result = objectMapper.readValue(jsonStream, KorapResult.class);
}
@@ -212,7 +221,7 @@
}
}
catch (IOException e) {
- throw new IOException("Failed executing HTTP request.",e);
+ throw new IOException("Failed executing HTTP request.", e);
}
finally {
response.close();
@@ -297,7 +306,7 @@
params.add(
new BasicNameValuePair("offset", String.valueOf(startRecord)));
- URIBuilder builder = new URIBuilder(SERVICE_URI + "search");
+ URIBuilder builder = new URIBuilder(serviceUri + "search");
builder.addParameters(params);
URI uri = builder.build();
@@ -359,8 +368,8 @@
}
HttpUriRequest httpRequest;
- httpRequest = createMatchInfoRequest(resourceId, documentId, textId, matchId,
- foundry);
+ httpRequest = createMatchInfoRequest(resourceId, documentId, textId,
+ matchId, foundry);
String annotationSnippet = null;
@@ -375,8 +384,8 @@
parseError(response);
}
- BufferedInputStream jsonStream = new BufferedInputStream(
- response.getEntity().getContent());
+ BufferedInputStream jsonStream =
+ new BufferedInputStream(response.getEntity().getContent());
try {
JsonNode root = objectMapper.readTree(jsonStream);
annotationSnippet = "<snippet>" + root.at("/snippet").asText()
@@ -416,7 +425,7 @@
throws URISyntaxException {
StringBuilder sb = new StringBuilder();
- sb.append(SERVICE_URI);
+ sb.append(serviceUri);
sb.append("corpus/");
sb.append(resourceId);
sb.append("/");
diff --git a/src/main/java/de/mannheim/ids/korap/sru/KorapSRU.java b/src/main/java/de/mannheim/ids/korap/sru/KorapSRU.java
index 534ca1d..14bd9e1 100644
--- a/src/main/java/de/mannheim/ids/korap/sru/KorapSRU.java
+++ b/src/main/java/de/mannheim/ids/korap/sru/KorapSRU.java
@@ -55,14 +55,12 @@
protected void doInit(ServletContext context, SRUServerConfig config,
SRUQueryParserRegistry.Builder parserRegistryBuilder,
Map<String, String> params) throws SRUConfigException {
- // serverConfig = config;
- try {
- korapClient = new KorapClient(config.getNumberOfRecords(),
- config.getMaximumRecords());
- }
- catch (FileNotFoundException e) {
- throw new SRUConfigException(e.getMessage());
- }
+
+ String korapURI = context.getInitParameter("korap.service.uri");
+
+ korapClient = new KorapClient(korapURI,
+ config.getNumberOfRecords(),
+ config.getMaximumRecords());
StringBuilder sb = new StringBuilder();
sb.append(config.getTransports());