-
Notifications
You must be signed in to change notification settings - Fork 79
g596.ivanova.task4 #332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lajulienn
wants to merge
9
commits into
fediq:master
Choose a base branch
from
lajulienn:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
g596.ivanova.task4 #332
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
007b5da
Add task 4
lajulienn cd90f55
Remove unused imports
lajulienn a3467b7
Fixed naming
lajulienn 4836b89
Remove unused import
lajulienn 91f40c2
Add query template
lajulienn 93582cb
Add variable and function putting
lajulienn 78e22e7
Small fix
lajulienn 5fde354
Fix codestyle
lajulienn 3292fd9
Fix codestyle
lajulienn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...k-g596-ivanova/src/main/java/ru/mipt/java2016/homework/g596/ivanova/task4/BillingDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package ru.mipt.java2016.homework.g596.ivanova.task4; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.dao.EmptyResultDataAccessException; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import javax.annotation.PostConstruct; | ||
| import javax.sql.DataSource; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
|
|
||
| @Repository | ||
| public class BillingDao { | ||
| private static final Logger LOG = LoggerFactory.getLogger(BillingDao.class); | ||
|
|
||
| @Autowired | ||
| private DataSource dataSource; | ||
|
|
||
| private JdbcTemplate jdbcTemplate; | ||
|
|
||
| @PostConstruct | ||
| public void postConstruct() { | ||
| jdbcTemplate = new JdbcTemplate(dataSource, false); | ||
| initSchema(); | ||
| } | ||
|
|
||
| public void initSchema() { | ||
| LOG.trace("Initializing schema"); | ||
| jdbcTemplate.execute("CREATE SCHEMA IF NOT EXISTS billing"); | ||
| jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.users " + | ||
| "(username VARCHAR PRIMARY KEY, password VARCHAR, enabled BOOLEAN)"); | ||
| jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.variables " + | ||
| "(username VARCHAR, name VARCHAR, value DOUBLE)"); | ||
| jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.functions " + | ||
| "(username VARCHAR, name VARCHAR, arguments VARCHAR, expression VARCHAR)"); | ||
| addUserIfNotExists("username", "password", true); | ||
| } | ||
|
|
||
| boolean addUserIfNotExists(String username, String password, boolean enabled) { | ||
| try { | ||
| loadUser(username); | ||
| return false; | ||
| } catch (EmptyResultDataAccessException e) { | ||
| jdbcTemplate.update("INSERT INTO billing.users VALUES (?, ?, ?)", | ||
| new Object[]{username, password, enabled}); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| public BillingUser loadUser(String username) throws EmptyResultDataAccessException { | ||
| LOG.trace("Querying for user " + username); | ||
| return jdbcTemplate.queryForObject( | ||
| "SELECT username, password, enabled FROM billing.users WHERE username = ?", | ||
| new Object[]{username}, | ||
| new RowMapper<BillingUser>() { | ||
| @Override | ||
| public BillingUser mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
| return new BillingUser( | ||
| rs.getString("username"), | ||
| rs.getString("password"), | ||
| rs.getBoolean("enabled") | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
.../main/java/ru/mipt/java2016/homework/g596/ivanova/task4/BillingDatabaseConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ru.mipt.java2016.homework.g596.ivanova.task4; | ||
|
|
||
| import com.zaxxer.hikari.HikariConfig; | ||
| import com.zaxxer.hikari.HikariDataSource; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import javax.sql.DataSource; | ||
|
|
||
| @Configuration | ||
| public class BillingDatabaseConfiguration { | ||
| @Bean | ||
| public DataSource billingDataSource( | ||
| @Value("${ru.mipt.java2016.homework.g596.ivanova.task4.jdbcUrl}") String jdbcUrl, | ||
| @Value("${ru.mipt.java2016.homework.g596.ivanova.task4.username:}") String username, | ||
| @Value("${ru.mipt.java2016.homework.g596.ivanova.task4.password:}") String password | ||
| ) { | ||
| HikariConfig config = new HikariConfig(); | ||
| config.setDriverClassName(org.h2.Driver.class.getName()); | ||
| config.setJdbcUrl(jdbcUrl); | ||
| config.setUsername(username); | ||
| config.setPassword(password); | ||
| return new HikariDataSource(config); | ||
| } | ||
| } |
69 changes: 69 additions & 0 deletions
69
...-g596-ivanova/src/main/java/ru/mipt/java2016/homework/g596/ivanova/task4/BillingUser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package ru.mipt.java2016.homework.g596.ivanova.task4; | ||
|
|
||
| public class BillingUser { | ||
| private final String username; | ||
| private final String password; | ||
| private final boolean enabled; | ||
|
|
||
| public BillingUser(String username, String password, boolean enabled) { | ||
| if (username == null) { | ||
| throw new IllegalArgumentException("Null username is not allowed"); | ||
| } | ||
| if (password == null) { | ||
| throw new IllegalArgumentException("Null password is not allowed"); | ||
| } | ||
| this.username = username; | ||
| this.password = password; | ||
| this.enabled = enabled; | ||
| } | ||
|
|
||
| public String getUsername() { | ||
| return username; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| public boolean isEnabled() { | ||
| return enabled; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "BillingUser{" + | ||
| "username='" + username + '\'' + | ||
| ", password='" + password + '\'' + | ||
| ", enabled=" + enabled + | ||
| '}'; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| BillingUser that = (BillingUser) o; | ||
|
|
||
| if (enabled != that.enabled) { | ||
| return false; | ||
| } | ||
| if (!username.equals(that.username)) { | ||
| return false; | ||
| } | ||
| return password.equals(that.password); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = username.hashCode(); | ||
| result = 31 * result + password.hashCode(); | ||
| result = 31 * result + (enabled ? 1 : 0); | ||
| return result; | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
...src/main/java/ru/mipt/java2016/homework/g596/ivanova/task4/RestCalculatorApplication.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package ru.mipt.java2016.homework.g596.ivanova.task4; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.boot.Banner; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.ComponentScan; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import ru.mipt.java2016.homework.g596.ivanova.task1.BestCalculatorEver; | ||
|
|
||
| @EnableAutoConfiguration | ||
| @Configuration | ||
| @ComponentScan(basePackageClasses = RestCalculatorApplication.class) | ||
| public class RestCalculatorApplication { | ||
| @Bean | ||
| public BestCalculatorEver calculator() { | ||
| return new BestCalculatorEver(); | ||
| } | ||
|
|
||
| @Bean | ||
| public EmbeddedServletContainerCustomizer customizer( | ||
| @Value("${ru.mipt.java2016.homework.g596.ivanova.task4.httpPort:9001}") int port) { | ||
| return container -> container.setPort(port); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication application = new SpringApplication(RestCalculatorApplication.class); | ||
| application.setBannerMode(Banner.Mode.OFF); | ||
| application.run(args); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
.../src/main/java/ru/mipt/java2016/homework/g596/ivanova/task4/RestCalculatorController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ru.mipt.java2016.homework.g596.ivanova.task4; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.security.core.Authentication; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import ru.mipt.java2016.homework.base.task1.ParsingException; | ||
| import ru.mipt.java2016.homework.g596.ivanova.task1.BestCalculatorEver; | ||
|
|
||
| @RestController | ||
| public class RestCalculatorController { | ||
| @Autowired | ||
| private BestCalculatorEver calculator; | ||
|
|
||
| @Autowired | ||
| private BillingDao billingDao; | ||
|
|
||
| @RequestMapping(path = "/calculate", method = RequestMethod.POST, | ||
| consumes = "text/plain", produces = "text/plain") | ||
| public String calculate(Authentication authentication, @RequestBody String expression) throws | ||
| ParsingException { | ||
| return calculator.calculate(expression) + "\n"; | ||
| } | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
.../main/java/ru/mipt/java2016/homework/g596/ivanova/task4/SecurityServiceConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package ru.mipt.java2016.homework.g596.ivanova.task4; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.dao.EmptyResultDataAccessException; | ||
| import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
| import org.springframework.security.core.userdetails.User; | ||
| import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
|
|
||
| import java.util.Collections; | ||
|
|
||
| @Configuration | ||
| @EnableWebSecurity | ||
| public class SecurityServiceConfiguration extends WebSecurityConfigurerAdapter { | ||
| private static final Logger LOG = LoggerFactory.getLogger(SecurityServiceConfiguration.class); | ||
|
|
||
| @Autowired | ||
| private BillingDao billingDao; | ||
|
|
||
| @Override | ||
| protected void configure(HttpSecurity http) throws Exception { | ||
| LOG.info("Configuring security"); | ||
| http | ||
| .httpBasic().realmName("Calculator").and() | ||
| .formLogin().disable() | ||
| .logout().disable() | ||
| .csrf().disable() | ||
| .authorizeRequests() | ||
| .antMatchers("/calculate/**").authenticated() | ||
| .anyRequest().permitAll(); | ||
| } | ||
|
|
||
| @Autowired | ||
| public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception { | ||
| LOG.info("Registering global user details service"); | ||
| auth.userDetailsService(username -> { | ||
| try { | ||
| BillingUser user = billingDao.loadUser(username); | ||
| return new User( | ||
| user.getUsername(), | ||
| user.getPassword(), | ||
| Collections.singletonList(() -> "AUTH") | ||
| ); | ||
| } catch (EmptyResultDataAccessException e) { | ||
| LOG.warn("No such user: " + username); | ||
| throw new UsernameNotFoundException(username); | ||
| } | ||
| }); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А где все те операции из задания с добавлением/удалением переменных и функций?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Их пока что нет :(