// TODO, assume the url is a file path for now.
// System.out.println(preferenceFileUrl + " url!!!!\n");
// Use a temporary for atomic writing
@POST
  @Path("setPreference")
  public synchronized void setPreference(
    @FormParam("preferencename") String preferenceName,
    @FormParam("preferencevalue") String preferenceValue) {
    if ((preferenceName == null) || (preferenceValue == null))
      return;
    Object newValue = null;
    // Validate boolean preferences
    String[] booleanPrefs = {"advanced-mode", "allow-anonymous-usage-tracking", "fs-reverse"};
    if (Arrays.asList(booleanPrefs).contains(preferenceName)){
      switch (preferenceValue){
        case "true":
          newValue = Boolean.TRUE;
          break;
        case "false":
          newValue = Boolean.FALSE;
          break;
        default:
          return;
      }
      if (preferenceName.equals("advanced-mode"))
        this.isUseAdvancedMode = (Boolean) newValue;
      else if (preferenceName == "allow")
        this.isAllowAnonymousTracking = (Boolean) newValue;
    }
    // Validate edit mode
    else if (preferenceName.equals("edit-mode")){
      String[] validModes = {"vim", "emacs", "default"};
      if (Arrays.asList(validModes).contains(preferenceValue)){
        newValue = preferenceValue;
        this.editMode = preferenceValue;
      }
    }
    // Validate edit mode
    else if (preferenceName.equals("fs-order-by")){
      String[] validModes = {"uri", "modified"};
      if (Arrays.asList(validModes).contains(preferenceValue)){
        newValue = preferenceValue;
      }
    }
    final String preferenceFileUrl = this.bkConfig.getPreferenceFileUrl();
    // TODO, assume the url is a file path for now.
    // System.out.println(preferenceFileUrl + " url!!!!\n");
    // Use a temporary for atomic writing
    java.nio.file.Path preferenceFileTmp = Paths.get(preferenceFileUrl + ".tmp");
    java.nio.file.Path preferenceFile = Paths.get(preferenceFileUrl);
    try {
      ObjectMapper om = new ObjectMapper();
      TypeReference readType = new TypeReference<HashMap<String, Object>>() {
      };
      Map<String, Object> prefs = om.readValue(preferenceFile.toFile(), readType);
      Object oldValue = (Object) prefs.get(preferenceName);
      // If value changed, write it to the file too
      if (!Objects.equals(newValue, oldValue)) {
        Files.deleteIfExists(preferenceFileTmp);
        prefs.put(preferenceName, newValue);
        om.writerWithDefaultPrettyPrinter().writeValue(preferenceFileTmp.toFile(), prefs);
        // Move tmp to normal
        Files.move(preferenceFileTmp, preferenceFile, REPLACE_EXISTING);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
final String preferenceFileUrl = this.bkConfig.getPreferenceFileUrl();
// TODO, assume the url is a file path for now.
// System.out.println(preferenceFileUrl + " url!!!!\n");
// Use a temporary for atomic writing
java.nio.file.Path preferenceFileTmp = Paths.get(preferenceFileUrl + ".tmp");
java.nio.file.Path preferenceFile = Paths.get(preferenceFileUrl);
}
}
// Validate edit mode
else if (preferenceName.equals("fs-order-by")){
String[] validModes = {"uri", "modified"};
if (Arrays.asList(validModes).contains(preferenceValue)){
newValue = preferenceValue;
}
}
final String preferenceFileUrl = this.bkConfig.getPreferenceFileUrl();
// TODO, assume the url is a file path for now.
// System.out.println(preferenceFileUrl + " url!!!!\n");
// Use a temporary for atomic writing
java.nio.file.Path preferenceFileTmp = Paths.get(preferenceFileUrl + ".tmp");
java.nio.file.Path preferenceFile = Paths.get(preferenceFileUrl);
try {
ObjectMapper om = new ObjectMapper();
TypeReference readType = new TypeReference<HashMap<String, Object>>() {
};
Map<String, Object> prefs = om.readValue(preferenceFile.toFile(), readType);
Object oldValue = (Object) prefs.get(preferenceName);
// If value changed, write it to the file too
if (!Objects.equals(newValue, oldValue)) {
this.isUseAdvancedMode = (Boolean) newValue;
else if (preferenceName == "allow")
this.isAllowAnonymousTracking = (Boolean) newValue;
}
// Validate edit mode
else if (preferenceName.equals("edit-mode")){
String[] validModes = {"vim", "emacs", "default"};
if (Arrays.asList(validModes).contains(preferenceValue)){
newValue = preferenceValue;
this.editMode = preferenceValue;
}
}
// Validate edit mode
else if (preferenceName.equals("fs-order-by")){
String[] validModes = {"uri", "modified"};
if (Arrays.asList(validModes).contains(preferenceValue)){
newValue = preferenceValue;
}
}
final String preferenceFileUrl = this.bkConfig.getPreferenceFileUrl();
// TODO, assume the url is a file path for now.
// System.out.println(preferenceFileUrl + " url!!!!\n");
// Use a temporary for atomic writing
java.nio.file.Path preferenceFileTmp = Paths.get(preferenceFileUrl + ".tmp");
java.nio.file.Path preferenceFile = Paths.get(preferenceFileUrl);
try {
ObjectMapper om = new ObjectMapper();
TypeReference readType = new TypeReference<HashMap<String, Object>>() {
};
Map<String, Object> prefs = om.readValue(preferenceFile.toFile(), readType);
Object oldValue = (Object) prefs.get(preferenceName);
// If value changed, write it to the file too
if (!Objects.equals(newValue, oldValue)) {
Files.deleteIfExists(preferenceFileTmp);
prefs.put(preferenceName, newValue);
om.writerWithDefaultPrettyPrinter().writeValue(preferenceFileTmp.toFile(), prefs);
// Move tmp to normal
Files.move(preferenceFileTmp, preferenceFile, REPLACE_EXISTING);
}
} catch (IOException e) {
e.printStackTrace();
}
}