/**
  * @throws IOException
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) used to
  *     confirm the modifications and/or release the semaphore called by the confirm modification
  *     or if the user loads an other page, refreshes the page or switches language
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   try {
     if (request.getParameter("content") == null) { // the user load an other page
       for (SemaphoreLn sem :
           listMutex) { // Get the correct semaphore and check if it was not already released
         if (sem.getLanguage().equals(request.getParameter("language"))
             && sem.getType().equals("Syn")
             && sem.availablePermits() < 1) {
           sem.release(); // Release the semaphore
         }
       }
     } else { // The user clicked on confirm modification
       File file;
       String filePath =
           env
               + "/solr/solr_home/"
               + server
               + "/conf/synonyms_"
               + request.getParameter("language")
               + ".txt";
       file = new File(filePath);
       try {
         FileOutputStream fooStream =
             new FileOutputStream(file, false); // true to append false to overwrite.
         byte[] myBytes =
             request
                 .getParameter("content")
                 .replaceAll("&gt;", ">")
                 .replaceAll("<div>|<br>|<br >", "\n")
                 .replaceAll("</div>|</lines>|&nbsp;", "")
                 .getBytes();
         fooStream.write(myBytes); // rewrite the file
         fooStream.close();
       } catch (IOException e) {
         LOGGER.error(
             "Error while rewriting the file synonyms_"
                 + request.getParameter("language")
                 + " Synonyms Servlet's doPost. Error 69019",
             e);
         PrintWriter out = response.getWriter();
         out.append(
             "Error while rewriting the synonyms file, please make sure the file exists and retry, if the problem persists contact your system administrator. Error code : 69015");
         out.close();
         return;
       }
       for (SemaphoreLn sem :
           listMutex) { // Get the correct semaphore and check if it was not already released
         if (sem.getLanguage().equals(request.getParameter("language"))
             && sem.getType().equals("Syn")
             && sem.availablePermits() < 1) {
           sem.release(); // Release the semaphore
         }
       }
     }
   } catch (Exception e) {
     PrintWriter out = response.getWriter();
     out.append(
         "Something bad happened, please retry, if the problem persists contact your system administrator. Error code : 69507");
     out.close();
     LOGGER.error("Unindentified error in Synonyms doPost. Error 69507", e);
   }
 }
 /**
  * @throws IOException
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) used to print
  *     the content of the file or to download it If called to print it will return plain text
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   try {
     if (content.equals("")) {
       PrintWriter out = response.getWriter();
       out.append(
           "Error while opening the list of languages, please make sure the file exists and retry, if the problem persists contact your system administrator. Error code : 69016");
       out.close();
       return;
     } else if (request.getParameter("language") != null) { // Print the content of the file
       for (SemaphoreLn sem : listMutex) { // For all the semaphores
         if (sem.getLanguage().equals(request.getParameter("language"))
             && sem.getType()
                 .equals(
                     "Syn")) { // if it has the good language and type(stopwords or Synonyms for
           // now)
           try {
             if (sem.availablePermits() != 0) { // If it is available
               try {
                 sem.acquire(); // Acquire it
               } catch (InterruptedException e) {
                 LOGGER.error(
                     "Error while acquiring semaphore in Synonyms Servlet's doGet. Error 69017",
                     e);
                 PrintWriter out = response.getWriter();
                 out.append(
                     "Something bad happened, please retry, if the problem persists contact your system administrator. Error code : 69017");
                 out.close();
                 return;
               }
               String filename =
                   "synonyms_" + request.getParameter("language").toString() + ".txt";
               response.setContentType("application/octet-stream");
               String filepath = env + "/solr/solr_home/" + server + "/conf/";
               String synContent = readFile(filepath + filename, StandardCharsets.UTF_8);
               // get the file and put its content into a string
               response.setContentType("text/html");
               PrintWriter out = response.getWriter();
               out.append(synContent); // returns the content of the file
               out.close();
               return;
             } else { // if not available
               PrintWriter out = response.getWriter();
               out.append("File already in use");
               out.close();
               return;
             }
           } catch (IOException e) {
             LOGGER.error(
                 "Error while reading the synonyms_"
                     + request.getParameter("language")
                     + ".txt file in Synonyms servlet, please make sure the file exists and is located in "
                     + env
                     + "/solr/solr_home/"
                     + server
                     + "/conf/"
                     + ". Error 69018",
                 e);
             PrintWriter out = response.getWriter();
             out.append(
                 "Error while reading the synonyms file, please make sure the file exists and retry, if the problem persists contact your system administrator. Error code : 69018");
             out.close();
             return;
           }
         }
       }
     }
   } catch (Exception e) {
     PrintWriter out = response.getWriter();
     out.append(
         "Something bad happened, please retry, if the problem persists contact your system administrator. Error code : 69506");
     out.close();
     LOGGER.error("Unindentified error in Synonyms doGet. Error 69506", e);
   }
 }