OPENNLP-528 Added method to replace the type system of a corpus.
diff --git a/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/DerbyCorpusStore.java b/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/DerbyCorpusStore.java
index 2c4b516..6bc3241 100644
--- a/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/DerbyCorpusStore.java
+++ b/corpus-server-impl/src/main/java/org/apache/opennlp/corpus_server/impl/DerbyCorpusStore.java
@@ -188,6 +188,36 @@
   }
   
   @Override
+  public void replaceTypeSystem(byte[] newTypeSystem) throws IOException {
+    try {
+      Connection conn = dataSource.getConnection();
+      // Replace the type system
+      PreparedStatement typeSystemPS = conn.prepareStatement("update " + 
+          corpusName + " set cas = ? where name = ?");
+
+      typeSystemPS.setString(2, "_typesystem");
+
+      Blob typeSystemBlob = conn.createBlob();
+      typeSystemBlob.setBytes(1, newTypeSystem);
+      typeSystemPS.setBlob(1, typeSystemBlob);
+
+      typeSystemPS.executeUpdate();
+      
+      conn.commit();
+      
+      typeSystemPS.close();
+      conn.close();
+    } catch (SQLException e) {
+      
+      if (LOGGER.isLoggable(Level.SEVERE)) {
+        LOGGER.log(Level.SEVERE, "Failed to replace the Type System!", e);
+      }
+      
+      throw new IOException(e);
+    }
+  }
+  
+  @Override
   public byte[] getTypeSystem() throws IOException {
     
     byte tsBytes[];
diff --git a/corpus-server-tools/src/main/java/org/apache/opennlp/corpus_server/tools/FileUtil.java b/corpus-server-tools/src/main/java/org/apache/opennlp/corpus_server/tools/FileUtil.java
index 1c8b00e..a3a8dd5 100644
--- a/corpus-server-tools/src/main/java/org/apache/opennlp/corpus_server/tools/FileUtil.java
+++ b/corpus-server-tools/src/main/java/org/apache/opennlp/corpus_server/tools/FileUtil.java
@@ -32,14 +32,17 @@
 
     InputStream fileIn = new FileInputStream(file);
 
-    byte buffer[] = new byte[1024];
-    int length;
-    while ((length = fileIn.read(buffer)) > 0) {
-      fileBytes.write(buffer, 0, length);
+    try {
+      byte buffer[] = new byte[1024];
+      int length;
+      while ((length = fileIn.read(buffer)) > 0) {
+        fileBytes.write(buffer, 0, length);
+      }
     }
-
-    fileIn.close();
-
+    finally {
+      fileIn.close();
+    }
+    
     return fileBytes.toByteArray();
   }
 
diff --git a/corpus-server-tools/src/main/java/org/apache/opennlp/corpus_server/tools/ReplaceTypeSystem.java b/corpus-server-tools/src/main/java/org/apache/opennlp/corpus_server/tools/ReplaceTypeSystem.java
new file mode 100644
index 0000000..57ab541
--- /dev/null
+++ b/corpus-server-tools/src/main/java/org/apache/opennlp/corpus_server/tools/ReplaceTypeSystem.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.opennlp.corpus_server.tools;
+
+import java.io.File;
+
+import javax.ws.rs.core.MediaType;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+
+public class ReplaceTypeSystem {
+
+  public static void main(String[] args) throws Exception {
+    
+    if (args.length != 2) {
+      System.out.println("ReplaceTypeSystem address typeSystemFile");
+      System.exit(-1);
+    }
+    
+    Client c = Client.create();
+
+    WebResource r = c.resource(args[0]);
+
+    File typeSystemFile = new File(args[1]);
+
+    byte typeSystemBytes[] = FileUtil.fileToBytes(typeSystemFile);
+
+    ClientResponse response = r.path("_replaceTypeSystem")
+        .accept(MediaType.TEXT_XML)
+        // TODO: How to fix this? Shouldn't accept do it?
+        .header("Content-Type", MediaType.TEXT_XML)
+        .put(ClientResponse.class, typeSystemBytes);
+    
+    System.out.println("Response: " + response.getStatus());
+  }
+}
diff --git a/corpus-server/src/main/java/org/apache/opennlp/corpus_server/CorpusResource.java b/corpus-server/src/main/java/org/apache/opennlp/corpus_server/CorpusResource.java
index 59f4017..1d0cd23 100644
--- a/corpus-server/src/main/java/org/apache/opennlp/corpus_server/CorpusResource.java
+++ b/corpus-server/src/main/java/org/apache/opennlp/corpus_server/CorpusResource.java
@@ -87,7 +87,24 @@
   public byte[] getCAS(@PathParam("casId") String casId) throws IOException {
     return corpus.getCAS(casId);
   }
-
+  
+  /**
+   * Note:
+   * The Type System is not checked for compatibility, if it does not work anymore
+   * the user needs to diagnose the problem himself and provide a new Type System to
+   * fix this.
+   * 
+   * @param newTypeSystem
+   * 
+   * @throws IOException
+   */
+  @PUT
+  @Consumes(MediaType.TEXT_XML)
+  @Path("_replaceTypeSystem")
+  public void replaceTypeSystem(byte[] newTypeSystem) throws IOException {
+    corpus.replaceTypeSystem(newTypeSystem);
+  }
+  
   /**
    * Retrieves the type system for this corpus.
    * 
diff --git a/corpus-server/src/main/java/org/apache/opennlp/corpus_server/store/CorpusStore.java b/corpus-server/src/main/java/org/apache/opennlp/corpus_server/store/CorpusStore.java
index d0a8994..6e1c3c7 100644
--- a/corpus-server/src/main/java/org/apache/opennlp/corpus_server/store/CorpusStore.java
+++ b/corpus-server/src/main/java/org/apache/opennlp/corpus_server/store/CorpusStore.java
@@ -72,6 +72,14 @@
   void removeCAS(String casID) throws IOException;
   
   /**
+   * Replaces the existing Type System with a new one.
+   * 
+   * @param newTypeSystem
+   * @throws IOException
+   */
+  void replaceTypeSystem(byte[] newTypeSystem) throws IOException;
+  
+  /**
    * Retrieves the type system description of this corpus.
    * 
    * @return