diff --git a/csv-preview-cae/pom.xml b/csv-preview-cae/pom.xml index 1fe11df..15340c3 100644 --- a/csv-preview-cae/pom.xml +++ b/csv-preview-cae/pom.xml @@ -32,7 +32,7 @@ com.coremedia.cms - cae-contentbeanservices-api + cae-contentbeanservices com.coremedia.cms @@ -40,7 +40,7 @@ com.coremedia.cms - cae-linkservices-api + cae-linkservices com.coremedia.cms @@ -72,10 +72,6 @@ org.springframework spring-core - - org.springframework - spring-context - org.springframework.security spring-security-config diff --git a/csv-preview-cae/src/main/java/com/coremedia/csv/cae/utils/CSVWebSecurityConfiguration.java b/csv-preview-cae/src/main/java/com/coremedia/csv/cae/utils/CSVWebSecurityConfiguration.java index 74319aa..a14f0b9 100644 --- a/csv-preview-cae/src/main/java/com/coremedia/csv/cae/utils/CSVWebSecurityConfiguration.java +++ b/csv-preview-cae/src/main/java/com/coremedia/csv/cae/utils/CSVWebSecurityConfiguration.java @@ -1,6 +1,5 @@ package com.coremedia.csv.cae.utils; -import org.springframework.context.annotation.Bean; import org.springframework.core.annotation.Order; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -9,8 +8,6 @@ import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; -import java.util.Arrays; - /** * There are Spring 403 issues that crop up when doing post requests with Spring Enabled Web Security. These revolve * around CSRF tokens to validate against cross-site vulnerabilities. For the CSV Reporter, which is being called via @@ -22,15 +19,14 @@ public class CSVWebSecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { - http.cors().and().csrf().disable(); + http.csrf().disable(); + http.headers().disable(); + http.cors().configurationSource(corsConfigurationSource()); } - @Bean CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); - configuration.setAllowedOrigins(Arrays.asList("*")); - configuration.setAllowedMethods(Arrays.asList("*")); - configuration.setAllowedHeaders(Arrays.asList("*")); + configuration.applyPermitDefaultValues(); configuration.setAllowCredentials(true); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); diff --git a/csv-studio-component/pom.xml b/csv-studio-component/pom.xml index 2590847..9577ae2 100644 --- a/csv-studio-component/pom.xml +++ b/csv-studio-component/pom.xml @@ -44,10 +44,10 @@ jakarta.servlet-api provided - - - - + + org.springframework + spring-context + org.apache.httpcomponents httpclient diff --git a/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVConfiguration.java b/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVConfiguration.java new file mode 100644 index 0000000..0d7dfbb --- /dev/null +++ b/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVConfiguration.java @@ -0,0 +1,35 @@ +package com.coremedia.csv.studio; + +import com.coremedia.cap.content.ContentRepository; +import com.coremedia.rest.cap.CapRestServiceBaseConfiguration; +import com.coremedia.rest.cap.CapRestServiceSearchConfiguration; +import com.coremedia.rest.cap.content.search.CapObjectFormat; +import com.coremedia.rest.cap.content.search.SearchService; +import com.coremedia.rest.linking.LinkResolver; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.PropertySource; + +import java.util.List; + +@Configuration +@Import({ + CapRestServiceBaseConfiguration.class, + CapRestServiceSearchConfiguration.class +}) +@PropertySource("classpath:/com/coremedia/csv/studio/csv-defaults.properties") +class CSVConfiguration { + + @Bean + public CSVExportResource csvExportResource(CSVFileRetriever csvFileRetriever, ContentRepository contentRepository, SearchService searchService, CapObjectFormat capObjectFormat, LinkResolver linkResolver) { + List authorizedGroups = List.of("reporter"); + return new CSVExportResource(csvFileRetriever, contentRepository, searchService, capObjectFormat, true, authorizedGroups, linkResolver); + } + + @Bean + public CSVFileRetriever csvFileRetriever(@Value("${studio.previewUrlPrefix}") String previewUrlPrefix, @Value("${studio.previewRestUrlPrefix}") String previewRestUrlPrefix) { + return new CSVFileRetriever(previewUrlPrefix, previewRestUrlPrefix); + } +} diff --git a/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVExportResource.java b/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVExportResource.java index f26518d..725b948 100644 --- a/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVExportResource.java +++ b/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVExportResource.java @@ -38,91 +38,47 @@ public class CSVExportResource { /** * Sends a request for a CSV file to the preview CAE. */ - private CSVFileRetriever csvFileRetriever; + private final CSVFileRetriever csvFileRetriever; /** * The content repository from which to retrieve content. */ - private ContentRepository contentRepository; + private final ContentRepository contentRepository; /** * The search service with which to search for content. */ - private SearchService searchService; + private final SearchService searchService; /** * The formatter for resolving URIs. */ - private CapObjectFormat capObjectFormat; + private final CapObjectFormat capObjectFormat; /** * Flag indicating whether access to this endpoint should be restricted to authorized groups only */ - private boolean restrictToAuthorizedGroups; + private final boolean restrictToAuthorizedGroups; /** * The groups that are authorized to access this endpoint. */ - private List authorizedGroups; + private final List authorizedGroups; /** * Resolves URI's to Domain Objects. */ - @Autowired - private LinkResolver linkResolver; + private final LinkResolver linkResolver; - /** - * Sets the CSV file retriever. - * - * @param csvFileRetriever the content repository to set - */ - public void setCsvFileRetriever(CSVFileRetriever csvFileRetriever) { - this.csvFileRetriever = csvFileRetriever; - } - /** - * Sets the content repository. - * - * @param contentRepository the content repository to set - */ - public void setContentRepository(ContentRepository contentRepository) { + public CSVExportResource(CSVFileRetriever csvFileRetriever, ContentRepository contentRepository, SearchService searchService, CapObjectFormat capObjectFormat, boolean restrictToAuthorizedGroups, List authorizedGroups, LinkResolver linkResolver) { + this.csvFileRetriever = csvFileRetriever; this.contentRepository = contentRepository; - } - - /** - * Sets the search service. - * - * @param searchService the search service to set - */ - public void setSearchService(SearchService searchService) { this.searchService = searchService; - } - - /** - * Sets the CapObjectFormat. - * - * @param capObjectFormat the content repository to set - */ - public void setCapObjectFormat(CapObjectFormat capObjectFormat) { this.capObjectFormat = capObjectFormat; - } - - /** - * Set the flag indicating whether access to this endpoint should be restricted to authorized groups only. - * - * @param restrictToAuthorizedGroups the value to set - */ - public void setRestrictToAuthorizedGroups(boolean restrictToAuthorizedGroups) { this.restrictToAuthorizedGroups = restrictToAuthorizedGroups; - } - - /** - * Sets the authorized groups. - * - * @param authorizedGroups the authorized groups to set - */ - public void setAuthorizedGroups(List authorizedGroups) { this.authorizedGroups = authorizedGroups; + this.linkResolver = linkResolver; } /** diff --git a/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVFileRetriever.java b/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVFileRetriever.java index 8d12997..40ee9b9 100644 --- a/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVFileRetriever.java +++ b/csv-studio-component/src/main/java/com/coremedia/csv/studio/CSVFileRetriever.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.List; @@ -25,28 +26,15 @@ public class CSVFileRetriever { /** * The URL of the preview CAE. */ - private String previewUrl; + private final String previewUrl; /** * The URL to use for calls to the preview CAE from the studio REST API. */ - private String previewRestUrl; + private final String previewRestUrl; - /** - * Set the URL of the preview CAE. - * - * @param previewUrl The URL of the preview CAE - */ - public void setPreviewUrl(String previewUrl) { + public CSVFileRetriever(String previewUrl, String previewRestUrl) { this.previewUrl = previewUrl; - } - - /** - * Set the URL to use for calls to the preview CAE from the studio REST API. - * - * @param previewRestUrl The URL to use for calls to the preview CAE from the studio REST API - */ - public void setPreviewRestUrl(String previewRestUrl) { this.previewRestUrl = previewRestUrl; } @@ -74,7 +62,7 @@ public CSVFileResponse retrieveCSV(String csvTemplate, List contents) t // Set up a POST request to the content set export endpoint CloseableHttpClient client = HttpClients.createDefault(); - String requestUrl = getPreviewUrlPrefix() + "/contentsetexport/"+ URLEncoder.encode(csvTemplate, "UTF-8"); + String requestUrl = getPreviewUrlPrefix() + "/contentsetexport/"+ URLEncoder.encode(csvTemplate, StandardCharsets.UTF_8); HttpPost httpPost = new HttpPost(requestUrl); httpPost.setHeader("Content-Type", "application/json"); HttpEntity requestEntity = new StringEntity(contentIdsList.toString()); diff --git a/csv-studio-component/src/main/resources/META-INF/coremedia/component-csv-studio.xml b/csv-studio-component/src/main/resources/META-INF/coremedia/component-csv-studio.xml deleted file mode 100644 index 1576431..0000000 --- a/csv-studio-component/src/main/resources/META-INF/coremedia/component-csv-studio.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - reporter - - - - - diff --git a/csv-studio-component/src/main/resources/META-INF/spring.factories b/csv-studio-component/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..e9a0897 --- /dev/null +++ b/csv-studio-component/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.coremedia.csv.studio.CSVConfiguration diff --git a/csv-studio-component/src/main/resources/com/coremedia/csv/studio/csv-defaults.properties b/csv-studio-component/src/main/resources/com/coremedia/csv/studio/csv-defaults.properties new file mode 100644 index 0000000..b996dbc --- /dev/null +++ b/csv-studio-component/src/main/resources/com/coremedia/csv/studio/csv-defaults.properties @@ -0,0 +1,2 @@ +studio.previewUrlPrefix=http://localhost:40980/blueprint/servlet +studio.previewRestUrlPrefix=http://localhost:40980/blueprint/servlet