Skip to content

Commit

Permalink
synchronize main code with joinfaces-maven-jar-example
Browse files Browse the repository at this point in the history
  • Loading branch information
persapiens committed Dec 19, 2023
1 parent 63a29fa commit b21aeab
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/joinfaces/example/ApplicationUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* JoinFaces Example Configuration class.
* Application users credentials.
* @author Marcelo Fernandes
*/
@ConfigurationProperties("application-users")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

/**
* JoinFaces Example Configuration class.
* @author Marcelo Fernandes
*/
@SpringBootApplication
public class JoinFacesExampleApplication extends SpringBootServletInitializer {
public class JoinFacesExampleApplication {

/**
* Main method.
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/org/joinfaces/example/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@
public class SecurityConfig {

/**
* Setups a security filter chain that will be applied to all the requests.
* Since JSF 2.2 there is already a CSRF protection, so the Spring CSRF protection must be disabled,
* because it blocks AJAX requests.
* @param http - autowired HttpSecurity object
* @return SecurityFilterChain that contains all the security filters
* @throws BeanCreationException if something in the configuration is wrong
*/
* Configure security.
**/
@Bean
public SecurityFilterChain configure(HttpSecurity http, MvcRequestMatcher.Builder mvc) {
try {
Expand All @@ -63,13 +58,14 @@ public SecurityFilterChain configure(HttpSecurity http, MvcRequestMatcher.Builde
.requestMatchers(new AntPathRequestMatcher("/**.faces")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/jakarta.faces.resource/**")).permitAll()
.anyRequest().authenticated())
.formLogin((formLogin) -> formLogin
.loginPage("/login.faces")
.formLogin((formLogin) ->
formLogin.loginPage("/login.faces")
.permitAll()
.failureUrl("/login.faces?error=true")
.defaultSuccessUrl("/starter.faces"))
.logout((logout) -> logout
.logoutSuccessUrl("/login.faces"));
.logout((logout) ->
logout.logoutSuccessUrl("/login.faces")
.deleteCookies("JSESSIONID"));
return http.build();
}
catch (Exception ex) {
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/org/joinfaces/example/view/FileMBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.joinfaces.example.view;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;

Expand All @@ -29,12 +30,12 @@
import org.primefaces.model.file.UploadedFile;

import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

/**
* FileMBean to test primefaces upload component.
* @author Marcelo Fernandes
*/
@SuppressFBWarnings("THROWS_METHOD_THROWS_RUNTIMEEXCEPTION")
@Component
@ViewScoped
public class FileMBean implements Serializable {
Expand All @@ -52,17 +53,11 @@ public class FileMBean implements Serializable {
/**
* Upload file action.
*/
public void upload() {
public void upload() throws IOException {
if (this.uploadedFile != null) {
byte[] data = FileCopyUtils.copyToByteArray(this.uploadedFile.getInputStream());
this.downloadFile = DefaultStreamedContent.builder()
.stream(() -> {
try {
return this.uploadedFile.getInputStream();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.stream(() -> new ByteArrayInputStream(data))
.contentType(this.uploadedFile.getContentType())
.name(this.uploadedFile.getFileName())
.build();
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/joinfaces/example/view/FreemarkerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,22 @@
import java.io.IOException;
import java.util.Map;

import jakarta.enterprise.context.ApplicationScoped;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.context.annotation.ApplicationScope;

/**
* Freemarker utility class to merge map and templates.
* Freemarker utilily class to merge map and templates.
*
* @author Marcelo Fernandes
*/
@Component
@ApplicationScoped
@ApplicationScope
public class FreemarkerUtils {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package org.joinfaces.example.view;

import java.io.IOException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -108,7 +109,7 @@ public class JoinFacesStarterService {
* Initialize default properties.
*/
@PostConstruct
public void init() throws IOException, XmlPullParserException {
public void init() throws IOException, XmlPullParserException, URISyntaxException {
findVersions();

this.joinFacesStartersComponents = new ArrayList<>();
Expand All @@ -131,17 +132,17 @@ public void init() throws IOException, XmlPullParserException {
}

@SuppressFBWarnings("URLCONNECTION_SSRF_FD")
private void findVersions() throws IOException, XmlPullParserException {
private void findVersions() throws IOException, XmlPullParserException, URISyntaxException {
findVersionsDependencies(versionMap(createModel("joinfaces-dependencies")));
findVersionsMojarra(versionMap(createModel("mojarra-spring-boot-starter")));
findVersionsMyfaces(versionMap(createModel("myfaces-spring-boot-starter")));
}

@SuppressFBWarnings("URLCONNECTION_SSRF_FD")
private Model createModel(String name) throws IOException, XmlPullParserException {
private Model createModel(String name) throws IOException, XmlPullParserException, URISyntaxException {
MavenXpp3Reader mavenXpp3Reader = new MavenXpp3Reader();
return mavenXpp3Reader.read(new URL("https://repo1.maven.org/maven2/org/joinfaces/" + name + "/"
+ this.joinfacesVersion + "/" + name + "-" + this.joinfacesVersion + ".pom").openStream());
return mavenXpp3Reader.read(new URI("https://repo1.maven.org/maven2/org/joinfaces/" + name + "/"
+ this.joinfacesVersion + "/" + name + "-" + this.joinfacesVersion + ".pom").toURL().openStream());
}

private Map<String, String> versionMap(Model pom) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/joinfaces/example/view/StarterMBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public List<String> getComponentArtifactIds() {
}
}
if (result.isEmpty()) {
result.add("jsf");
result.add("faces");
}

return result;
Expand Down Expand Up @@ -172,4 +172,10 @@ public String getPom() throws IOException, TemplateException {
map.put("starterMBean", this);
return this.freemarkerUtils.mergeTemplate(map, "pom.ftl");
}

/**
* Set pom map.
*/
public void setPom(String pom) {
}
}

0 comments on commit b21aeab

Please sign in to comment.