Commit ad23400a authored by Jagadeshwar's avatar Jagadeshwar

Added Exceptions and added webflux security

parent c8b3b049
Pipeline #2709 canceled with stage
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
......@@ -37,6 +37,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-security'
// https://mvnrepository.com/artifact/io.projectreactor.ipc/reactor-netty
implementation group: 'io.projectreactor.ipc', name: 'reactor-netty', version: '0.7.15.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
// https://mvnrepository.com/artifact/org.springframework/spring-web
//implementation group: 'org.springframework', name: 'spring-web', version: '6.0.2'
......
2022-12-02 13:08:40.119 INFO 28912 --- [main] c.safeway.pricing.PricingPodApplication : Starting PricingPodApplication using Java 11.0.16 on HYD-LAP-00539 with PID 28912 (D:\Jagadesh\Safeway\Pricing POD\workspace\nisum-poc\pricing-pod-poc\build\classes\java\main started by NISUM in D:\Jagadesh\Safeway\Pricing POD\workspace\nisum-poc\pricing-pod-poc)
2022-12-02 13:08:40.123 INFO 28912 --- [main] c.safeway.pricing.PricingPodApplication : No active profile set, falling back to default profiles: default
2022-12-02 13:08:43.420 INFO 28912 --- [main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2022-12-02 13:08:43.470 INFO 28912 --- [main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 33 ms. Found 0 MongoDB repository interfaces.
2022-12-02 13:08:48.642 INFO 28912 --- [main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2022-12-02 13:08:48.803 INFO 28912 --- [cluster-ClusterId{value='6389ab88434a34443bf38957', description='null'}-localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:43}] to localhost:27017
2022-12-02 13:08:48.804 INFO 28912 --- [cluster-rtt-ClusterId{value='6389ab88434a34443bf38957', description='null'}-localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:44}] to localhost:27017
2022-12-02 13:08:48.814 INFO 28912 --- [cluster-ClusterId{value='6389ab88434a34443bf38957', description='null'}-localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=45724100}
2022-12-02 13:08:49.495 INFO 28912 --- [main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 14 endpoint(s) beneath base path '/actuator'
2022-12-02 13:08:50.508 INFO 28912 --- [main] ctiveUserDetailsServiceAutoConfiguration :
Using generated security password: 10ed3819-98fb-45eb-88ea-3f5bb2431a17
2022-12-02 13:08:53.532 INFO 28912 --- [main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port 8789
2022-12-02 13:08:56.041 INFO 28912 --- [main] c.safeway.pricing.PricingPodApplication : Started PricingPodApplication in 17.555 seconds (JVM running for 19.237)
......@@ -3,6 +3,7 @@ package com.safeway.pricing;
import org.modelmapper.ModelMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
......@@ -20,6 +21,11 @@ public class PricingPodApplication {
return new ModelMapper();
}
@Bean
public WebProperties.Resources createWebProperties(){
return new WebProperties.Resources();
}
......
package com.safeway.pricing.config;
import com.safeway.pricing.handler.PricingHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.web.server.authentication.WebFilterChainServerAuthenticationSuccessHandler;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import reactor.ipc.netty.NettyContext;
import reactor.ipc.netty.http.server.HttpServer;
@EnableWebFluxSecurity
public class SecurityConfig {
@Autowired
PricingHandler pricingHandler;
@Bean
public SecurityWebFilterChain configure( ServerHttpSecurity http ) throws Exception {
http.csrf().disable().authorizeExchange()
.pathMatchers(HttpMethod.POST,"/oauth2/**", "/login/**").permitAll()
//.hasAnyRole()
//.pathMatchers(HttpMethod.GET,"/api")
.anyExchange().authenticated().and().oauth2Login()
.authenticationSuccessHandler(new WebFilterChainServerAuthenticationSuccessHandler());
return http.build();
}
}
package com.safeway.pricing.dto;
import lombok.Builder;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Data
@Builder
public class CRCDetailsDTO {
String id;
String ProductName;
BigDecimal cost;
BigDecimal costChanges;
Date costEffectiveDate;
BigDecimal currentPrice;
BigDecimal suggestedPrice;
Date priceEffectiveDate;
String priority;
String itemRole;
String specialClassification;
String categoryRole;
Integer associations;
Integer strGp;
Integer promotions;
String itemType;
Boolean preApproved;
String guardRail;
String status;
String assignee;
FinancialMetricsDTO financialMetrics;
List<ROGPriceAreaDetailsDTO> rogDetails;
}
package com.safeway.pricing.dto;
import lombok.Builder;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
@Data
@Builder
public class PriceDetailsDTO {
BigDecimal currentPrice;
BigDecimal newPrice;
Date startDate;
Date endDate;
}
package com.safeway.pricing.dto;
import lombok.Builder;
import lombok.Data;
import java.util.List;
@Data
@Builder
public class ROGPriceAreaDetailsDTO {
String rogId;
String priceAreaId;
List<StoreDetailsDTO> storeDetails;
FinancialMetricsDTO financialMetrics;
PriceDetailsDTO priceDetailsDTO;
}
package com.safeway.pricing.dto;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class StoreDetailsDTO {
String storeNum;
PriceDetailsDTO priceDetails;
}
package com.safeway.pricing.exception;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.reactive.function.server.ServerRequest;
import java.util.HashMap;
import java.util.Map;
@Component
public class GlobalErrorAttributes extends DefaultErrorAttributes {
public Map<String,Object> getErrorAttributes(ServerRequest serverRequest, ErrorAttributeOptions errorAttributeOptions){
Map<String,Object> errorMap = new HashMap<>();
Throwable throwable = getError(serverRequest);
errorMap.put("message",throwable.getMessage());
return errorMap;
}
}
package com.safeway.pricing.exception;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.*;
import reactor.core.publisher.Mono;
import java.util.Map;
@Component
public class GlobalExceptionHandler extends AbstractErrorWebExceptionHandler {
/**
* Create a new {@code AbstractErrorWebExceptionHandler}.
*
* @param errorAttributes the error attributes
* @param resources the resources configuration properties
* @param applicationContext the application context
* @since 2.4.0
*/
public GlobalExceptionHandler(ErrorAttributes errorAttributes, WebProperties.Resources resources, ApplicationContext applicationContext,
ServerCodecConfigurer serverCodecConfigurer) {
super(errorAttributes, resources, applicationContext);
this.setMessageWriters(serverCodecConfigurer.getWriters());
this.setMessageReaders(serverCodecConfigurer.getReaders());
}
@Override
protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
return RouterFunctions.route(RequestPredicates.all(),this::renderErrorMessage);
}
private Mono<ServerResponse> renderErrorMessage(ServerRequest serverRequest){
Map<String,Object> errorProperties = getErrorAttributes(serverRequest, ErrorAttributeOptions.defaults());
return ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR).contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(errorProperties));
}
}
package com.safeway.pricing.handler;
import com.safeway.pricing.dto.CRCDetailsDTO;
import com.safeway.pricing.exception.GlobalErrorAttributes;
import com.safeway.pricing.model.FilterByDetails;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
@Service
public class PricingHandler {
public Mono<ServerResponse> getDefaultFilterDetails(ServerRequest serverRequest){
Flux<FilterByDetails> filterByDetails = Flux.range(1,10)
.delayElements(Duration.ofSeconds(1))
.map(i -> FilterByDetails.builder()
.category(Arrays.asList("3610-Traditional")).departments(Arrays.asList("314-Dairy"))
.groups(Arrays.asList("36-Refrigirator Dairy"))
.rog(Arrays.asList("VSOC"))
.priceAreas(Arrays.asList("VSOC-01"))
.storeGroups(Arrays.asList("51")).build());
return ServerResponse.ok().contentType(MediaType.TEXT_EVENT_STREAM)
.body(filterByDetails, FilterByDetails.class);
}
public Mono<ServerResponse> getCRCDetails(ServerRequest serverRequest) {
Flux<CRCDetailsDTO> crcDetails = Flux.range(1,10).filter(i -> i == 2)
.onErrorResume(e -> Mono.error(new RuntimeException("Error at Fetching Data2")))
.map(i -> CRCDetailsDTO.builder().build());
//throw new RuntimeException("Exception Testing");
return ServerResponse.ok().contentType(MediaType.TEXT_EVENT_STREAM)
.body(crcDetails,CRCDetailsDTO.class)
.onErrorResume(e -> Mono.error(new RuntimeException("Error at Fetching Data")));
}
}
package com.safeway.pricing.response;
import com.safeway.pricing.dto.CRCDetailsDTO;
import lombok.Builder;
import lombok.Data;
import java.util.List;
@Data @Builder
public class CRCDetailsResponse {
List<CRCDetailsDTO> crcDetails;
}
package com.safeway.pricing.router;
import com.safeway.pricing.handler.PricingHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
@Configuration
public class AppRouterConfig {
@Autowired
private PricingHandler pricingHandler;
@Bean
public RouterFunction<ServerResponse> routerFunction(){
return RouterFunctions.route()
.GET("/getDefaultFilterDetails",pricingHandler::getDefaultFilterDetails)
.GET("/getCRCDetails",pricingHandler::getCRCDetails)
.build();
}
}
......@@ -3,9 +3,9 @@
#Springboot Application
spring.application.name=spring-boot-management
spring.main.allow-bean-definition-overriding=true
server.port=8789
server.port=9999
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
client.host=http://localhost:8789
client.host=http://localhost:9999
#Mongodb
spring.data.mongodb.database=pricingdb
......
......@@ -14,4 +14,9 @@ spring:
client-secret: Q-Y8Q~K~Ocg-K-gxR9AjX9dAXYkEIH7BnQYuRcNs
authorization-grant-type: authorization_code
redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
scope: openid,profile
\ No newline at end of file
scope: openid,profile
server:
port: 9999
error:
include-message: always
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment