Skip to content

Commit

Permalink
Merge 0e6365a into 40e7882
Browse files Browse the repository at this point in the history
  • Loading branch information
LadislavSopko committed May 8, 2018
2 parents 40e7882 + 0e6365a commit ca303c9
Show file tree
Hide file tree
Showing 37 changed files with 1,824 additions and 1,065 deletions.
@@ -1,7 +1,9 @@
package internal.org.springframework.content.fs.boot.autoconfigure;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,44 +23,54 @@

@Configuration
@ConditionalOnClass(FilesystemStoreRegistrar.class)
@Import({FilesystemContentAutoConfigureRegistrar.class, FilesystemStoreConfiguration.class})
@Import({ FilesystemContentAutoConfigureRegistrar.class, FilesystemStoreConfiguration.class })
public class FilesystemContentAutoConfiguration {

@Autowired
private Environment env;
@Autowired
private Environment env;


@Bean
@Bean
@ConditionalOnMissingBean(FileSystemResourceLoader.class)
FileSystemResourceLoader fileSystemResourceLoader(FilesystemProperties props) {
return new FileSystemResourceLoader(props.getFilesystemRoot());
}

@Component
@ConfigurationProperties(prefix = "spring.content.fs")
@ConfigurationProperties(prefix = "spring.content.fs", ignoreInvalidFields = false, ignoreUnknownFields = false)
public static class FilesystemProperties {

private static final Logger logger = LoggerFactory.getLogger(FilesystemProperties.class);
private static final Logger logger = LoggerFactory.getLogger(FilesystemProperties.class);

/**
* The root location where file system stores place their content
*/
/**
* The root location where file system stores place their content
*/
String filesystemRoot;

public String getFilesystemRoot() {
if (filesystemRoot == null) {
try {
filesystemRoot = Files.createTempDirectory("").toString();
} catch (IOException ioe) {
logger.error(String.format("Unexpected error defaulting filesystem root to %s", filesystemRoot), ioe);
}
}

return this.filesystemRoot;
if (filesystemRoot == null) {
// first try to take system property!!!
Optional<String> o = Optional.ofNullable(System.getProperty("SPRING_CONTENT_FS_FILESYSTEM_ROOT"));
if (o.isPresent()) {
filesystemRoot = o.get();
} else {
try {
filesystemRoot = Files.createTempDirectory("").toString();
} catch (IOException ioe) {
logger.error(String.format("Unexpected error defaulting filesystem root to %s", filesystemRoot),
ioe);
}
}
}
return this.filesystemRoot;
}

public void setFilesystemRoot(String filesystemRoot) {
this.filesystemRoot = filesystemRoot;
try {
this.filesystemRoot = filesystemRoot.replaceAll("[\\/\\\\]+?",
File.separator.equals("\\") ? "\\\\" : "/");
} catch (Exception ex) {
logger.error(ex.getMessage());
}
}
}
}
@@ -1,10 +1,22 @@
package org.springframework.content.fs.boot;

import com.github.paulcwarren.ginkgo4j.Ginkgo4jConfiguration;
import com.github.paulcwarren.ginkgo4j.Ginkgo4jRunner;
import internal.org.springframework.content.fs.boot.autoconfigure.FilesystemContentAutoConfiguration;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Context;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Describe;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.It;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.File;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
Expand All @@ -19,27 +31,16 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.JpaRepository;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import com.github.paulcwarren.ginkgo4j.Ginkgo4jConfiguration;
import com.github.paulcwarren.ginkgo4j.Ginkgo4jRunner;

import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.AfterEach;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.BeforeEach;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Context;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.Describe;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.FIt;
import static com.github.paulcwarren.ginkgo4j.Ginkgo4jDSL.It;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import internal.org.springframework.content.fs.boot.autoconfigure.FilesystemContentAutoConfiguration;

@RunWith(Ginkgo4jRunner.class)
@Ginkgo4jConfiguration(threads=1)
@Ginkgo4jConfiguration(threads = 1)
public class ContentFilesystemAutoConfigurationTest {

{
Expand All @@ -57,29 +58,33 @@ public class ContentFilesystemAutoConfigurationTest {
});

Context("given an environment specifying a filesystem root using spring prefix", () -> {
BeforeEach(() -> {
System.setProperty("spring.content.fs.filesystem-root", "${java.io.tmpdir}/UPPERCASE/NOTATION/");
});
AfterEach(() -> {
System.clearProperty("spring.content.fs.filesystem-root");
});
/*
* Value come from test.properties !!!!! BeforeEach(() -> {
* System.setProperty("spring.content.fs.filesystem-root",
* "${java.io.tmpdir}/UPPERCASE/NOTATION/"); }); AfterEach(() -> {
* System.clearProperty("spring.content.fs.filesystem-root"); });
*/
It("should have a filesystem properties bean with the correct root set", () -> {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(TestConfig.class);
context.refresh();

assertThat(context.getBean(FilesystemContentAutoConfiguration.FilesystemProperties.class).getFilesystemRoot(), endsWith("/UPPERCASE/NOTATION/"));
assertThat(
context.getBean(FilesystemContentAutoConfiguration.FilesystemProperties.class)
.getFilesystemRoot(),
endsWith(File.separator + "UPPERCASE" + File.separator + "NOTATION"));

context.close();
});
});

Context("given a configuration that contributes a loader bean", () -> {
Context("given a configuration that contributes a loader bean", () -> {
It("should have that loader bean in the context", () -> {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(ConfigWithLoaderBean.class);
context.refresh();

// file system resource loader don't normalize path separator !!!!
FileSystemResourceLoader loader = context.getBean(FileSystemResourceLoader.class);
assertThat(loader.getFilesystemRoot(), is("/some/random/path/"));

Expand All @@ -90,24 +95,18 @@ public class ContentFilesystemAutoConfigurationTest {
});
}


@PropertySource("classpath:/test.properties")
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude={HibernateJpaAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
MongoDataAutoConfiguration.class,
MongoAutoConfiguration.class})
@EnableAutoConfiguration(exclude = { HibernateJpaAutoConfiguration.class, JdbcTemplateAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class, MongoDataAutoConfiguration.class, MongoAutoConfiguration.class })
public static class TestConfig {
}

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude={HibernateJpaAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
MongoDataAutoConfiguration.class,
MongoAutoConfiguration.class})
@EnableAutoConfiguration(exclude = { HibernateJpaAutoConfiguration.class, JdbcTemplateAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class, MongoDataAutoConfiguration.class, MongoAutoConfiguration.class })
public static class ConfigWithLoaderBean {

@Bean
Expand Down
@@ -0,0 +1 @@
spring.content.fs.filesystemRoot = ${java.io.tmpdir}/UPPERCASE/NOTATION
@@ -1,6 +1,5 @@
package internal.org.springframework.content.commons.renditions;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -14,15 +13,16 @@
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.content.commons.renditions.Renderable;
import org.springframework.content.commons.renditions.RenditionCapability;
import org.springframework.content.commons.renditions.RenditionProvider;
import org.springframework.content.commons.renditions.RenditionService;
import org.springframework.content.commons.repository.StoreExtension;
import org.springframework.content.commons.repository.StoreInvoker;
import org.springframework.content.commons.utils.BeanUtils;
import org.springframework.util.MimeType;
import org.springframework.core.io.Resource;

public class RenditionServiceImpl implements RenditionService, StoreExtension {

private static final Log LOGGER = LogFactory.getLog(RenditionServiceImpl.class);

private List<RenditionProvider> providers = new ArrayList<RenditionProvider>();
Expand All @@ -38,14 +38,10 @@ public void setProviders(RenditionProvider... providers) {
}

@Override
public boolean canConvert(String fromMimeType, String toMimeType) {
public boolean canConvert(String fromMimeType, String toMimeType) {
for (RenditionProvider provider : providers) {
if (MimeType.valueOf(fromMimeType).includes(MimeType.valueOf(provider.consumes()))) {
for (String produce : provider.produces()) {
if (MimeType.valueOf(toMimeType).includes(MimeType.valueOf(produce))) {
return true;
}
}
if (provider.isCapable(fromMimeType, toMimeType).isBetterThan(RenditionCapability.NOT_CAPABLE)) {
return true;
}
}
return false;
Expand All @@ -55,30 +51,24 @@ public boolean canConvert(String fromMimeType, String toMimeType) {
public String[] conversions(String fromMimeType) {
Set<String> conversions = new HashSet<>();
for (RenditionProvider provider : providers) {
if (provider.consumes().equals(fromMimeType)) {
if (provider.consumes(fromMimeType)) {
conversions.addAll(Arrays.asList(provider.produces()));
}
}
return conversions.toArray(new String[] {});
}

@Override
public InputStream convert(String fromMimeType, InputStream fromInputSource, String toMimeType) {
for (RenditionProvider provider : providers) {
if (MimeType.valueOf(fromMimeType).includes(MimeType.valueOf(provider.consumes()))) {
for (String produce : provider.produces()) {
if (MimeType.valueOf(toMimeType).includes(MimeType.valueOf(produce))) {
return provider.convert(fromInputSource, toMimeType);
}
}
}
}
public Resource convert(String fromMimeType, Resource fromInputSource, String toMimeType) {
RenditionProvider provider = getProvider(fromMimeType, toMimeType);
if (provider != null)
return provider.convert(fromInputSource, toMimeType);
return null;
}

@Override
public Set<Method> getMethods() {
Class<?> clazz = Renderable.class;
Class<?> clazz = Renderable.class;
Method getRenditionMethod;
try {
getRenditionMethod = clazz.getMethod("getRendition", Object.class, String.class);
Expand All @@ -93,21 +83,38 @@ public Set<Method> getMethods() {
@Override
public Object invoke(MethodInvocation invocation, StoreInvoker invoker) {
String fromMimeType = null;
fromMimeType = (String)BeanUtils.getFieldWithAnnotation(invocation.getArguments()[0], org.springframework.content.commons.annotations.MimeType.class);
fromMimeType = (String) BeanUtils.getFieldWithAnnotation(invocation.getArguments()[0],
org.springframework.content.commons.annotations.MimeType.class);
if (fromMimeType == null) {
return null;
}
String toMimeType = (String) invocation.getArguments()[1];

if (this.canConvert(fromMimeType, toMimeType)) {
InputStream content = null;

RenditionProvider pr = this.getProvider(fromMimeType, toMimeType);
if (pr != null) {
Resource content = null;
try {
content = invoker.invokeGetContent();
return (InputStream) this.convert(fromMimeType, content, toMimeType);
content = invoker.invokeGetResource();
return (Resource) pr.convert(content, toMimeType);
} catch (Exception e) {
LOGGER.error(String.format("Failed to get rendition from %s to %s", fromMimeType, toMimeType ), e);
LOGGER.error(String.format("Failed to get rendition from %s to %s", fromMimeType, toMimeType), e);
}
}
}
return null;
}

public RenditionProvider getProvider(String fromMimeType, String toMimeType) {
RenditionCapability bestCapability = RenditionCapability.NOT_CAPABLE;
RenditionProvider bestProvider = null;
for (RenditionProvider provider : providers) {
RenditionCapability vote = provider.isCapable(fromMimeType, toMimeType);
if (vote.isBest())
return provider; // Return the best provider.
if (vote.isBetterThan(bestCapability)) {
bestCapability = vote; // Elect a better provider.
bestProvider = provider;
}
}
return bestProvider;
}
}

0 comments on commit ca303c9

Please sign in to comment.