Projekt

Obecné

Profil

« Předchozí | Další » 

Revize b72dd2ba

Přidáno uživatelem stepanekp před asi 1 rok

prepared for communication with detection service

Zobrazit rozdíly:

src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/detecting/DetectingController.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.controller.detecting;
2

  
3
import cz.zcu.fav.kiv.antipatterndetectionapp.service.detecting.DetectingService;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.beans.factory.annotation.Value;
6
import org.springframework.http.*;
7
import org.springframework.web.bind.annotation.*;
8
import org.springframework.web.client.RestTemplate;
9
import org.springframework.web.servlet.HandlerMapping;
10

  
11
import javax.servlet.http.HttpServletRequest;
12

  
13
@RestController
14
@RequestMapping("v2/detecting")
15
public class DetectingController {
16

  
17
    @Autowired
18
    private DetectingService detectingService;
19

  
20
    @Value("${detecting.service.url}")
21
    private String serviceUrl;
22

  
23
    @RequestMapping(value = "/**", method = {RequestMethod.GET, RequestMethod.POST})
24
    public ResponseEntity<String> handleRequest(HttpServletRequest request, @RequestBody(required = false) String requestBody, HttpMethod method) {
25
        try {
26
            String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
27
            String url = serviceUrl + path.substring("/v2/detecting".length());
28

  
29
            ResponseEntity<String> response = detectingService.exchangeWithExternalService(url, requestBody, method);
30

  
31
            return response;
32
        } catch (Exception e) {
33
            e.printStackTrace();
34
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error");
35
        }
36
    }
37
}
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/security/AuthConfiguration.java
25 25
        http
26 26
                .cors()
27 27
                .and()
28
                .authorizeRequests(authz -> authz
29
                        .antMatchers("/v2/app/metadata/**", "/v2/user/register").permitAll() // Allow all requests to /v2/app/metadata/**
28
                .authorizeRequests(authz -> authz //TODO odstranit detecting
29
                        .antMatchers("/v2/app/metadata/**", "/v2/user/register", "/v2/detecting/**").permitAll() // Allow all requests to /v2/app/metadata/**
30 30
                        .antMatchers("/v2/**").hasAnyRole("spade_basic")
31 31
                        .anyRequest().authenticated()
32 32
                )
33 33
                .oauth2ResourceServer(oauth2 -> oauth2.jwt(jwtConfigurer -> jwtConfigurer.jwtAuthenticationConverter(jwtAuthenticationTokenConverter)))
34
                .sessionManagement(session -> session.sessionCreationPolicy(STATELESS));
34
                .sessionManagement(session -> session.sessionCreationPolicy(STATELESS))
35
                .csrf().disable();
35 36
        return http.build();
36 37
    }
37 38

  
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/detecting/DetectingService.java
1
package cz.zcu.fav.kiv.antipatterndetectionapp.service.detecting;
2

  
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.http.*;
5
import org.springframework.stereotype.Service;
6
import org.springframework.web.client.RestTemplate;
7

  
8
@Service
9
public class DetectingService {
10

  
11
    @Autowired
12
    private RestTemplate restTemplate;
13

  
14
    public ResponseEntity<String> exchangeWithExternalService(String url, String requestBody, HttpMethod method) {
15
        HttpHeaders headers = new HttpHeaders();
16
        headers.setContentType(MediaType.APPLICATION_JSON);
17

  
18
        HttpEntity<String> requestEntity = null;
19
        if (requestBody != null) {
20
            requestEntity = new HttpEntity<>(requestBody, headers);
21
        } else {
22
            requestEntity = new HttpEntity<>(headers);
23
        }
24

  
25
        return restTemplate.exchange(url, method, requestEntity, String.class);
26
    }
27
}
28

  
src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/spring/AppConfig.java
4 4
import org.springframework.context.annotation.Bean;
5 5
import org.springframework.context.annotation.ComponentScan;
6 6
import org.springframework.context.annotation.Configuration;
7
import org.springframework.web.client.RestTemplate;
7 8
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
8 9
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
9 10
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
......
53 54
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
54 55
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
55 56
    }
57

  
58
    @Bean
59
    public RestTemplate restTemplate() {
60
        return new RestTemplate();
61
    }
56 62
}
src/main/resources/application.properties
3 3
spring.jpa.show-sql=true
4 4
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect
5 5

  
6
# mysql database connection:
6
# first mysql database connection:
7 7
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/spade
8 8
spring.datasource.username=root
9 9
spring.datasource.password=
10 10
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
11 11

  
12
# mssql database connection:
12
# second mysql database connection:
13 13
spring.second-datasource.jdbc-url=jdbc:mysql://localhost:3306/authspade
14 14
spring.second-datasource.username=root
15 15
spring.second-datasource.password=
16 16
spring.second-datasource.driverClassName=com.mysql.cj.jdbc.Driver
17 17

  
18
detecting.service.url=http://localhost:8081
18 19

  
19 20
# custom error pages
20 21
server.error.whitelabel.enabled=false

Také k dispozici: Unified diff