/******************************************************************************************************************* * ******************************************************************************************************************/ private List<ResourceFile> getFiles (final @Nonnull String prefix, final @Nonnull String propertyFileName, final @Nonnull ResourceFile folder, final @Nonnull List<String> suffixes) { final List<ResourceFile> files = new ArrayList<>(); for (final String localeSuffix : suffixes) { final String fileName = prefix + propertyFileName + localeSuffix + ".xml"; log.trace(">>>> probing {} ...", folder.getPath().asString() + "/" + fileName); try { files.add(folder.findChildren().withName(fileName).result()); } catch (NotFoundException e) { // ok. do nothing } } return files; }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @SuppressWarnings("unchecked") public <Type> Type resolveProperty (final @Nonnull Id propertyGroupId, final @Nonnull Key<Type> propertyName) throws NotFoundException, IOException { log.trace("resolveProperty({})", propertyName); final ResourceFile propertyFile = findLocalizedFile(propertyName.stringValue()); log.trace(">>>> reading from {}", propertyFile.getPath()); final String mimeType = propertyFile.getMimeType(); final String charset = mimeType.equals("application/xhtml+xml") ? "UTF-8" : Charset.defaultCharset().name(); try { // return (Type)filterSetExpander.get().filter(propertyFile.asText(charset), mimeType); return (Type)propertyFile.asText(charset); } catch (RuntimeException e) // FIXME: introduce a FilterException { throw new IOException(e); } }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull public synchronized ResourceFile createDecoratorFile (final @Nonnull ResourceFile delegateFile) { if (delegateFile == null) { return null; } ResourceFile decorator = delegateLightWeightMap.get(delegateFile); if (decorator == null) { decorator = (delegateFile.isData() ? new DecoratorResourceFile(this, delegateFile) : new DecoratorResourceFolder(this, delegates, delegateFile.getPath(), delegateFile)); delegateLightWeightMap.put(delegateFile, decorator); } return decorator; } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ private void emptyFolder (final @Nonnull ResourceFile folder) throws IOException { log.trace("emptyFolder({}, {}", folder); for (final ResourceFile child : folder.findChildren().results()) { child.delete(); } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull private ResourceFile createMockFolder (final @Nonnull String name) { final ResourceFile folder = mock(ResourceFile.class); when(folder.getName()).thenReturn(name); when(folder.getPath()).thenReturn(new ResourcePath(name)); when(folder.isData()).thenReturn(false); when(folder.isFolder()).thenReturn(true); when(folder.findChildren()).thenReturn(new ListFinder(new ArrayList<ResourceFile>())); when(folder.toString()).thenReturn(name); return folder; }
/******************************************************************************************************************* * * * ******************************************************************************************************************/ private static void dump (final @Nonnull List<String> lines, final @Nonnull ResourceFile fileObject) throws IOException { if (fileObject.isData()) { lines.add(String.format("%s: %s", fileObject.getPath().asString(), fileObject.asText("UTF-8"))); } else { for (final ResourceFile child : fileObject.findChildren().results()) { dump(lines, child); } } }
/******************************************************************************************************************* * * Traverse the file system with a {@link FilePredicate}. * * @param folder the folder to traverse * @param fileFilter the filter for directory contents * @param predicate the predicate * ******************************************************************************************************************/ private void traverse (final @Nonnull ResourceFile folder, final @Nonnull FileFilter fileFilter, final @Nonnull FilePredicate predicate) { traverse(folder.getPath(), folder, fileFilter, predicate); }
/******************************************************************************************************************* * * ******************************************************************************************************************/ private void copyFolder (final @Nonnull ResourceFile sourceFolder, final @Nonnull ResourceFile targetFolder) throws IOException { log.trace("copyFolder({}, {}", sourceFolder, targetFolder); for (final ResourceFile sourceChild : sourceFolder.findChildren().results()) { if (!sourceChild.isFolder()) { log.trace(">>>> copying {} into {} ...", sourceChild, targetFolder); sourceChild.copyTo(targetFolder); } } for (final ResourceFile sourceChild : sourceFolder.findChildren().results()) { if (sourceChild.isFolder()) { copyFolder(sourceChild, targetFolder.createFolder(sourceChild.getName())); } } } }
/******************************************************************************************************************* * ******************************************************************************************************************/ @Nonnull protected ResourceFile createMockFile (final @Nonnull ResourceFileSystem fileSystem, final @Nonnull ResourceFile parentFolder, final @Nonnull String name) { final ResourcePath path = parentFolder.getPath().appendedWith(name); final ResourceFile file = createMockFile(name); when(file.getParent()).thenReturn(parentFolder); when(file.getPath()).thenReturn(path); when(file.toString()).thenReturn(path.asString()); when(fileSystem.findFileByPath(eq(path.asString()))).thenReturn(file); final Collection<ResourceFile> children = new ArrayList<>(parentFolder.findChildren().results()); children.add(file); when(parentFolder.findChildren()).thenReturn(new ListFinder(children)); return file; }
/******************************************************************************************************************* * * {@inheritDoc} * ******************************************************************************************************************/ @Override @Nonnull protected List<? extends Type> computeResults() { final List<Type> result = new ArrayList<>(); for (final ResourceFile childFile : parentFile.findChildren().withRecursion(true).results()) { if (childFile.isFolder()) { try { final String relativeUri = childFile.getPath().relativeTo(resourceRootPath).urlDecoded().asString(); result.add(siteProvider.get().getSite().find(typeClass).withRelativePath(relativeUri).result()); } catch (NotFoundException e) { log.error("", e); } } } return result; }
log.debug("loadProperties() for {}", file.getPath().asString()); if (file.isData()) log.trace(">>>> reading properties from {} ({})...", propertyFile.getPath().asString(), locale); @Cleanup final InputStream is = propertyFile.getInputStream(); final ResourceProperties tempProperties = modelFactory.createProperties().build().as(Unmarshallable).unmarshal(is); tmpPlaceHolder &= !propertyFile.getParent().equals(file); log.debug(">>>> properties for {} ({}):", file.getPath().asString(), locale); logProperties(">>>>>>>>", properties);
final String mimeType = file.getMimeType(); final Object content = mimeType.startsWith("text/") ? macroExpander.get().filter(file.asText("UTF-8"), mimeType) : file.asBytes(); responseHolder.response().forRequest(request) .withBody(content) .withContentType(mimeType) .withLatestModifiedTime(file.getLatestModificationTime()) .withExpirationTime(duration) .put();
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull private ResourceProperties loadProperties() throws IOException { final ResourceFile file = getFile(); log.debug("loadProperties() for {}", file.getPath().asString()); ResourceProperties properties = modelFactory.createProperties().withPropertyResolver(propertyResolver).build(); try { final ResourceFile propertyFile = file.findChildren().withName("Properties.xml").result(); // FIXME reuse the inheritance helper // log.trace(">>>> reading properties from {} ({})...", propertyFile.getPath().asString(), locale); @Cleanup final InputStream is = propertyFile.getInputStream(); final ResourceProperties tempProperties = // modelFactory.createProperties().build().as(Unmarshallable).unmarshal(is); modelFactory.createProperties().withPropertyResolver(propertyResolver).build().as(Unmarshallable).unmarshal(is); // log.trace(">>>>>>>> read properties: {} ({})", tempProperties, locale); properties = properties.merged(tempProperties); } catch (NotFoundException e) { // ok, no properties } return properties; } }
/******************************************************************************************************************* * ******************************************************************************************************************/ // FIXME: this should be shared between instances private void initialize() throws IOException, NotFoundException { log.info("Retrieving XSLT templates"); final String template = IOUtils.toString(getClass().getResourceAsStream("/it/tidalwave/northernwind/core/impl/filter/XsltTemplate.xslt")); final StringBuilder xsltBuffer = new StringBuilder(); for (final Resource resource : siteProvider.get().getSite().find(Resource.class).withRelativePath(XSLT_TEMPLATES_PATH).results()) { final ResourceFile file = resource.getFile(); log.info(">>>> {}", file.getPath().asString()); xsltBuffer.append(file.asText("UTF-8")); } final ST t = new ST(template, '%', '%').add("content", xsltBuffer.toString()); xslt = t.render(); }
@Override @Nonnull protected List<? extends T> computeResults() { // FIXME: it's not flyweight final List<T> results = new ArrayList<>(); for (final ResourceFile childFile : resourceFile.findChildren().results()) { if (childFile.isFolder()) { try { results.add(productCreator.createProduct(childFile)); } catch (IOException | NotFoundException e) { throw new RuntimeException(e); } } } return results; } }
for (final ResourceFile fo : zipFolder.findChildren().results()) final String name = fo.getName(); if (name.endsWith(".zip") || name.endsWith(".jar")) zipFileSystemProvider.setZipFilePath(fo.toFile().getAbsolutePath()); fileSystemProviders.add(zipFileSystemProvider);
for (final ResourceFile fileObject : delegateDirectory.findChildren().results()) if (!childrenMap.containsKey(fileObject.getName())) childrenMap.put(fileObject.getName(), getFileSystem().createDecoratorFile(fileObject));
/******************************************************************************************************************* * ******************************************************************************************************************/ @BeforeMethod public void setupFixture() throws Exception { MockResponseBuilder.setCurrentTime(currentTime); resourceFile = mock(ResourceFile.class); when(resourceFile.asBytes()).thenReturn("FILE CONTENT".getBytes()); when(resourceFile.getMimeType()).thenReturn("text/plain"); when(resourceFile.getLatestModificationTime()).thenReturn(resourceLatestModifiedTime); headers = new HashMap<>(); request = mock(Request.class); when(request.getHeader(anyString())).thenAnswer(new Answer<String>() { @Override @Nonnull public String answer (final @Nonnull InvocationOnMock invocation) throws NotFoundException { final String name = (String)invocation.getArguments()[0]; return NotFoundException.throwWhenNull(headers.get(name), "Not found " + name); } }); underTest = new MockResponseHolder(); }