Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 44765e5e

Přidáno uživatelem Petr Urban před téměř 2 roky(ů)

#10521 - created controller, service, repository and models for handling requests for v2/app/metadata/ endpoint

Zobrazit rozdíly:

pom.xml
176 176
			<artifactId>mssql-jdbc</artifactId>
177 177
			<version>12.2.0.jre11</version>
178 178
		</dependency>
179
        <dependency>
180
            <groupId>com.google.code.gson</groupId>
181
            <artifactId>gson</artifactId>
182
            <version>2.10.1</version>
183
        </dependency>
179 184

  
180
	</dependencies>
185
    </dependencies>
181 186

  
182 187
	<build>
183 188
		<plugins>
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/config/SecurityBasics.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.config;
2

  
3
public class SecurityBasics {
4

  
5
    public static final String[] NO_AUTHORIZATION_NEEDED = new String[]{
6
            "/v2/user/register",
7
            "/v2/user/login",
8
            "/v2/user/refresh",
9
            "/v2/user/logout",
10
            "/v2/app/about"
11
    };
12

  
13
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/controller/AboutPageController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.controller;
2

  
3
import com.google.gson.Gson;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.AboutPageDto;
5
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.Metadata;
6
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.AboutPageService;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.http.HttpStatus;
9
import org.springframework.http.ResponseEntity;
10
import org.springframework.web.bind.annotation.GetMapping;
11
import org.springframework.web.bind.annotation.RequestMapping;
12
import org.springframework.web.bind.annotation.RestController;
13

  
14
import java.util.ArrayList;
15
import java.util.List;
16

  
17
/**
18
 * Controller for dealing with requests that wants to obtain specific data related to the project.
19
 * Those data are mostly metadata to provide basic information, so there is no need for authentication at most cases.
20
 *
21
 * @author Petr Urban
22
 * @since 2023-04-26
23
 * @version 1.0.0
24
 */
25

  
26
@RestController
27
@RequestMapping("/v2/app/metadata")
28
public class AboutPageController {
29

  
30
    @Autowired
31
    private AboutPageService aboutPageService;
32

  
33
    private final String BASIC_INFO_COLUMN_KEY_VALUE = "basics";
34

  
35
    @GetMapping("/about")
36
    public ResponseEntity<String> getAboutPageData() {
37
        List<Metadata> data = aboutPageService.findAll();
38
        return ResponseEntity.status(data.size() > 0 ? HttpStatus.OK : HttpStatus.NO_CONTENT).body(new Gson().toJson(findAboutData(data)));
39
    }
40

  
41
    private List<AboutPageDto> findAboutData(List<Metadata> metadata) {
42
        if (metadata == null || metadata.isEmpty()) {
43
            return new ArrayList<>();
44
        }
45

  
46
        for (Metadata cursor : metadata) {
47
            if (cursor.getAppDataKey().equals(BASIC_INFO_COLUMN_KEY_VALUE)) {
48
                return new Gson().fromJson(cursor.getAppDataValue(), List.class);
49
            }
50
        }
51

  
52
        return new ArrayList<>();
53
    }
54

  
55
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/AboutPageDto.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.model;
2

  
3
import java.util.List;
4

  
5
/**
6
 *
7
 * Dto for deserialization of the obtained data from the dbo.app_metadata table, respectively for the "basics" key value
8
 *
9
 * @author Petr Urban
10
 * @since 2023-04-26
11
 * @version 1.0.0
12
 */
13
public class AboutPageDto {
14

  
15
    private int version;
16

  
17
    private List<String> authors;
18

  
19
    private String description;
20

  
21
    public int getVersion() {
22
        return version;
23
    }
24

  
25
    public void setVersion(int version) {
26
        this.version = version;
27
    }
28

  
29
    public List<String> getAuthors() {
30
        return authors;
31
    }
32

  
33
    public void setAuthors(List<String> authors) {
34
        this.authors = authors;
35
    }
36

  
37
    public String getDescription() {
38
        return description;
39
    }
40

  
41
    public void setDescription(String description) {
42
        this.description = description;
43
    }
44
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/model/Metadata.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.model;
2

  
3
import javax.persistence.*;
4

  
5
/**
6
 * @author Petr Urban
7
 * @since 2023-04-26
8
 * @version 1.0.0
9
 */
10
@Entity
11
@Table(name = "app_metadata")
12
public class Metadata {
13

  
14
    @Id
15
    @GeneratedValue(strategy = GenerationType.IDENTITY)
16
    private Long id;
17

  
18
    private String appDataKey;
19

  
20
    private String appDataValue;
21

  
22
    public String getAppDataKey() {
23
        return appDataKey;
24
    }
25

  
26
    public void setAppDataKey(String appDataKey) {
27
        this.appDataKey = appDataKey;
28
    }
29

  
30
    public String getAppDataValue() {
31
        return appDataValue;
32
    }
33

  
34
    public void setAppDataValue(String appDataValue) {
35
        this.appDataValue = appDataValue;
36
    }
37

  
38
    public void setId(Long id) {
39
        this.id = id;
40
    }
41

  
42
    public Long getId() {
43
        return id;
44
    }
45
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/repository/AboutPageRepository.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.Metadata;
4
import org.springframework.data.jpa.repository.JpaRepository;
5

  
6
public interface AboutPageRepository extends JpaRepository<Metadata, Long> {
7

  
8
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/JwtAuthenticationFilter.java
1 1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.security;
2 2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.config.SecurityBasics;
3 4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.httpExceptions.CustomExceptionHandler;
4 5
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthService;
5 6
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.utils.JSONBuilder;
......
20 21
import javax.servlet.http.HttpServletRequest;
21 22
import javax.servlet.http.HttpServletResponse;
22 23
import java.io.IOException;
24
import java.util.Arrays;
23 25
import java.util.Collections;
24 26

  
25 27
@Component
......
80 82

  
81 83
    @Override
82 84
    protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
83
            String path = request.getRequestURI().substring(request.getContextPath().length());
84
        return path.startsWith("/v2/user/login") || path.startsWith("/v2/user/register");
85
        String path = request.getRequestURI().substring(request.getContextPath().length());
86
        return Arrays.stream(SecurityBasics.NO_AUTHORIZATION_NEEDED).anyMatch(path::startsWith);
85 87
    }
86 88

  
87 89

  
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/security/WebSecurityConfig.java
1 1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.security;
2 2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.config.SecurityBasics;
3 4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.AuthProvider;
4 5
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.service.OAuthService;
5 6
import org.springframework.beans.factory.annotation.Autowired;
......
74 75
                .and()
75 76
                .csrf().disable()
76 77
                .authorizeRequests()
77
                .mvcMatchers("/v2/user/register", "/v2/user/login", "/v2/user/refresh", "/v2/user/logout").permitAll()
78
                .mvcMatchers(SecurityBasics.NO_AUTHORIZATION_NEEDED).permitAll()
78 79
                .anyRequest().authenticated()
79 80
                .and()
80 81
                .sessionManagement()
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/AboutPageService.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.Metadata;
4

  
5
import java.util.List;
6

  
7
/**
8
 * Interface used in the AboutPageServiceImpl
9
 *
10
 * @author Petr Urban
11
 * @since 2023-04-26
12
 * @version 1.0.0
13
 */
14
public interface AboutPageService {
15

  
16
    /**
17
     * method in which it's a great time to call findAll to jdbc repository
18
     * @return list of obtained rows from the database
19
     */
20
    List<Metadata> findAll();
21

  
22
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/v2/service/AboutPageServiceImpl.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.v2.service;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.model.Metadata;
4
import cz.zcu.fav.kiv.antipatterndetectionapp.v2.repository.AboutPageRepository;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.stereotype.Service;
7

  
8
import java.util.List;
9

  
10
/**
11
 * Implementation of service that processes data required from AboutPageController.java
12
 *
13
 * @author Petr Urban
14
 * @since 2023-04-26
15
 * @version 1.0.0
16
 */
17
@Service
18
public class AboutPageServiceImpl implements AboutPageService {
19

  
20
    @Autowired
21
    private AboutPageRepository repository;
22

  
23
    @Override
24
    public List<Metadata> findAll() {
25
        return repository.findAll();
26
    }
27

  
28
}

Také k dispozici: Unified diff