@Override public InputStream newInputStream(RepositoryFileReference ref) throws IOException { Path path = this.ref2AbsolutePath(ref); return Files.newInputStream(path); }
@Override public FileTime getLastModifiedTime(RepositoryFileReference ref) throws IOException { Path path = this.ref2AbsolutePath(ref); return Files.getLastModifiedTime(path); }
@Override public boolean exists(RepositoryFileReference ref) { return Files.exists(this.ref2AbsolutePath(ref)); }
@Override public long getSize(RepositoryFileReference ref) throws IOException { return Files.size(this.ref2AbsolutePath(ref)); }
/** * @return null if an error occurred */ @Override public Date getLastUpdate(RepositoryFileReference ref) { Path path = this.ref2AbsolutePath(ref); Date res; if (Files.exists(path)) { FileTime lastModifiedTime; try { lastModifiedTime = Files.getLastModifiedTime(path); res = new Date(lastModifiedTime.toMillis()); } catch (IOException e) { FilebasedRepository.LOGGER.debug(e.getMessage(), e); res = null; } } else { // this branch is taken if the resource directory exists, but the // configuration itself does not exist. // For instance, this happens if icons are manually put for a node // type, but no color configuration is made. res = Constants.LASTMODIFIEDDATE_FOR_404; } return res; }
@Override public void putContentToFile(RepositoryFileReference ref, String content, MediaType mediaType) throws IOException { if (mediaType == null) { // quick hack for storing mime type called this method assert (ref.getFileName().endsWith(Constants.SUFFIX_MIMETYPE)); // we do not need to store the mime type of the file containing the mime type information } else { this.setMimeType(ref, mediaType); } Path path = this.ref2AbsolutePath(ref); FileUtils.createDirectory(path.getParent()); Files.write(path, content.getBytes()); }
@Override public Configuration getConfiguration(RepositoryFileReference ref) { Path path = this.ref2AbsolutePath(ref); PropertiesConfiguration configuration = new PropertiesConfiguration(); if (Files.exists(path)) { try (Reader r = Files.newBufferedReader(path, Charset.defaultCharset())) { configuration.read(r); } catch (ConfigurationException | IOException e) { FilebasedRepository.LOGGER.error("Could not read config file", e); throw new IllegalStateException("Could not read config file", e); } } configuration.addEventListener(ConfigurationEvent.ANY, new AutoSaveListener(path, configuration)); // We do NOT implement reloading as the configuration is only accessed // in JAX-RS resources, which are created on a per-request basis return configuration; }
@Override public AccountabilityConfigurationManager getAccountabilityConfigurationManager() { RepositoryFileReference repoRef = BackendUtils.getRefOfConfiguration(new AccountabilityId()); RepositoryFileReference keystoreRef = new RepositoryFileReference(new AccountabilityId(), "CustomKeystore.json"); RepositoryFileReference defaultKeystoreRef = new RepositoryFileReference(new AccountabilityId(), "DefaultKeystore.json"); return AccountabilityConfigurationManager.getInstance(ref2AbsolutePath(repoRef).toFile(), ref2AbsolutePath(keystoreRef).toFile(), ref2AbsolutePath(defaultKeystoreRef).toFile()); }
@Override public void putContentToFile(RepositoryFileReference ref, InputStream inputStream, MediaType mediaType) throws IOException { if (mediaType == null) { // quick hack for storing mime type called this method assert (ref.getFileName().endsWith(Constants.SUFFIX_MIMETYPE)); // we do not need to store the mime type of the file containing the mime type information } else { this.setMimeType(ref, mediaType); } Path targetPath = this.ref2AbsolutePath(ref); // ensure that parent directory exists FileUtils.createDirectory(targetPath.getParent()); try { Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING); } catch (IllegalStateException e) { FilebasedRepository.LOGGER.debug("Guessing that stream with length 0 is to be written to a file", e); // copy throws an "java.lang.IllegalStateException: Stream already closed" if the InputStream contains 0 bytes // For instance, this case happens if SugarCE-6.4.2.zip.removed is tried to be uploaded // We work around the Java7 issue and create an empty file if (Files.exists(targetPath)) { // semantics of putContentToFile: existing content is replaced without notification Files.delete(targetPath); } Files.createFile(targetPath); } }
@Override public NamespaceManager getNamespaceManager() { NamespaceManager manager; RepositoryFileReference ref = BackendUtils.getRefOfJsonConfiguration(new NamespacesId()); manager = new JsonBasedNamespaceManager(ref2AbsolutePath(ref).toFile()); Configuration configuration = this.getConfiguration(new NamespacesId()); if (!configuration.isEmpty()) { ConfigurationBasedNamespaceManager old = new ConfigurationBasedNamespaceManager(configuration); manager.replaceAll(old.getAllNamespaces()); try { forceDelete(BackendUtils.getRefOfConfiguration(new NamespacesId())); } catch (IOException e) { LOGGER.error("Could not remove old namespace configuration.", e); } } return manager; }