Fixed handling x-fcs-context.

Change-Id: I6836ea750e3612aa86c96511d86a2917fb05e810
diff --git a/src/main/java/de/ids_mannheim/korap/sru/KorapClient.java b/src/main/java/de/ids_mannheim/korap/sru/KorapClient.java
index 01c3b2b..db71ddb 100644
--- a/src/main/java/de/ids_mannheim/korap/sru/KorapClient.java
+++ b/src/main/java/de/ids_mannheim/korap/sru/KorapClient.java
@@ -162,7 +162,7 @@
         HttpUriRequest httpRequest = null;
         try {
             httpRequest = createSearchRequest(query, queryLanguage, version,
-                    startRecord - 1, maximumRecords);
+                    startRecord - 1, maximumRecords, corpora);
         }
         catch (URISyntaxException e) {
             throw new IOException("Failed creating http request.");
@@ -172,6 +172,8 @@
         KorapResult result = null;
         try {
             response = sendRequest(httpRequest);
+//            logger.info(response.toString());
+            
             BufferedInputStream jsonStream =
                     new BufferedInputStream(response.getEntity().getContent());
             try {
@@ -256,12 +258,13 @@
      *            the starting number of records/matches to return
      * @param maximumRecords
      *            the number of maximum records to return
+     * @param corpora 
      * @return a HttpGet request
      * @throws URISyntaxException
      */
     private HttpGet createSearchRequest (String query,
             QueryLanguage queryLanguage, String version, int startRecord,
-            int maximumRecords) throws URISyntaxException {
+            int maximumRecords, String[] corpora) throws URISyntaxException {
 
         if (maximumRecords <= 0) {
             maximumRecords = defaultNumOfRecords;
@@ -271,10 +274,24 @@
                     defaultMaxRecords);
             maximumRecords = defaultMaxRecords;
         }
+        
+        String corpusQuery = "";
+        int length = corpora.length;
+        if (corpora != null && length > 0){
+            for (int i=0; i<length; i++){
+                corpusQuery += "corpusSigle="+corpora[i];
+                if (i != length-1){
+                    corpusQuery += "|";
+                }
+            }
+        }
 
         List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add(new BasicNameValuePair("q", query));
         params.add(new BasicNameValuePair("ql", queryLanguage.toString()));
+        if (!corpusQuery.isEmpty()){
+            params.add(new BasicNameValuePair("cq", corpusQuery));
+        }
         params.add(new BasicNameValuePair("v", version));
         params.add(new BasicNameValuePair("context", DEFAULT_CONTEXT_TYPE));
         params.add(new BasicNameValuePair("count",
@@ -359,6 +376,7 @@
                         + "</snippet>";
             }
             catch (IOException e) {
+                logger.error(e.getMessage());
                 throw new IOException(
                         "Failed processing response from KorAP match info API.");
             }
diff --git a/src/main/java/de/ids_mannheim/korap/sru/KorapSRU.java b/src/main/java/de/ids_mannheim/korap/sru/KorapSRU.java
index e7271a5..f115436 100644
--- a/src/main/java/de/ids_mannheim/korap/sru/KorapSRU.java
+++ b/src/main/java/de/ids_mannheim/korap/sru/KorapSRU.java
@@ -88,7 +88,7 @@
             throw new SRUException(SRUConstants.SRU_UNSUPPORTED_PARAMETER_VALUE, 
                     "Query type "+ queryType+ " is not supported.");
         }
-        logger.info("Query language: " + queryType);
+//        logger.info("Query language: " + queryType);
         
         SRUVersion sruVersion = request.getVersion();
         // EM: actually not necessary because query type is only available in SRU 2.0
@@ -104,13 +104,13 @@
             throw new SRUException(SRUConstants.SRU_EMPTY_TERM_UNSUPPORTED,
                     "Empty term is not supported.");
         }
-        logger.info("korapsru query: " + queryStr);
+//        logger.info("korapsru query: " + queryStr);
 
         KorapResult korapResult = sendQuery(queryStr, request, version,
                 queryLanguage);
         checkKorapResultError(korapResult, queryLanguage,
                 isRewitesAllowed(request), diagnostics);
-        logger.info("Number of records: "+korapResult.getTotalResults());
+//        logger.info("Number of records: "+korapResult.getTotalResults());
 
         return new KorapSRUSearchResultSet(korapClient, diagnostics, korapResult, dataviews,
                 korapEndpointDescription.getTextLayer(),
@@ -335,6 +335,7 @@
     private String[] getCorporaList(SRURequest request) {
         try {
             String corpusPids = request.getExtraRequestData("x-fcs-context");
+//            logger.info("x-fcs-context: "+corpusPids);
             if (!corpusPids.isEmpty() && corpusPids != null) {
                 if (corpusPids.contains(",")) {
                     return corpusPids.split(",");
diff --git a/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java b/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
index 96e870b..042851c 100644
--- a/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
+++ b/src/test/java/de/ids_mannheim/korap/test/KorapSRUTest.java
@@ -116,6 +116,22 @@
 //		assertEquals(50, node.getChildNodes().getLength());
 	}
 	
+	
+	@Test
+    public void searchRetrieveWithResourceId() throws IOException, URISyntaxException, IllegalStateException, SAXException{
+        HttpClient httpclient = HttpClients.createDefault();
+        URIBuilder builder = new URIBuilder();
+        builder.setScheme("http").setHost(host).setPort(port).setPath("/KorapSRU")
+                .setParameter("operation", "searchRetrieve")
+                .setParameter("query", "fein")
+                .setParameter("x-fcs-context", "GOE");
+        
+        URI uri = builder.build();
+        HttpGet request = new HttpGet(uri);
+        HttpResponse response = httpclient.execute(request);
+        checkSRUSearchRetrieveResponse(response);
+    } 
+	
 	@Test
 	public void explainTest() throws URISyntaxException, ClientProtocolException, IOException, IllegalStateException, SAXException{
 		HttpClient httpclient = HttpClients.createDefault();