Skip to content
Open

Task4 #339

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions homework-g597-sigareva/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,41 @@

<artifactId>homework-g597-sigareva</artifactId>

<properties>
<spring.boot.version>1.4.2.RELEASE</spring.boot.version>
</properties>

<dependencies>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-base</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-tests</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ru.mipt.java2016.homework.g597.sigareva.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("DELETE FROM billing.users WHERE username = 'username'");
jdbcTemplate.update("INSERT INTO billing.users VALUES ('username', 'password', 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")
);
}
}
);
}

public Double getVariable(String username, String variableName) throws EmptyResultDataAccessException {
LOG.trace("Querying for user " + username);
return jdbcTemplate.queryForObject(
"SELECT username, variableName, value FROM billing.usersVariables WHERE username = '" + username + "' " +
"AND variableName = '" + variableName + "'",
new Object[]{},
new RowMapper<Double>() {
@Override
public Double mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Double(
rs.getString("value")
);
}
}
);
}

public void registerNewUser(String user, String password) {
jdbcTemplate.update("INSERT INTO billing.users VALUES ('" + user + "', '" + password + "', TRUE)");
}

public void addValue(String user, String variableName, String value) {
System.out.println(user);
System.out.println(variableName);
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.usersVariables " +
"(username VARCHAR, variableName VARCHAR PRIMARY KEY, value FLOAT)");
jdbcTemplate.execute("DELETE FROM billing.usersVariables WHERE variableName = '" + variableName + "' " +
"AND username = '" + user + "'");
jdbcTemplate.update("INSERT INTO billing.usersVariables VALUES ('" + user + "', '" + variableName + "', '" + value + "')");
}

public void deleteVariable(String user, String variableName) {
jdbcTemplate.execute("DELETE FROM billing.usersVariables WHERE variableName = '" + variableName + "' " +
"AND username = '" + user + "'");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.mipt.java2016.homework.g597.sigareva.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.g597.sigareva.task4.jdbcUrl}") String jdbcUrl,
@Value("${ru.mipt.java2016.homework.g597.sigareva.task4.username:}") String username,
@Value("${ru.mipt.java2016.homework.g597.sigareva.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ru.mipt.java2016.homework.g597.sigareva.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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package ru.mipt.java2016.homework.g597.sigareva.task4;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import ru.mipt.java2016.homework.base.task1.Calculator;
import ru.mipt.java2016.homework.base.task1.ParsingException;

import java.io.IOException;
import java.util.Vector;

@RestController
public class CalculatorController {
private static final Logger LOG = LoggerFactory.getLogger(CalculatorController.class);
@Autowired
private MyCalculator calculator;

@Autowired
private BillingDao billingDao;

@RequestMapping(path = "/ping", method = RequestMethod.GET, produces = "text/plain")
public String echo() {
return "OK\n";
}

@RequestMapping(path = "/", method = RequestMethod.GET, produces = "text/html")
public String main(@RequestParam(required = false) String name) {
if (name == null) {
name = "world";
}
return "<html>" +
"<head><title>FediqApp</title></head>" +
"<body><h1>Hello, " + name + "!</h1></body>" +
"</html>";
}

@RequestMapping(path = "/eval", method = RequestMethod.POST, consumes = "text/plain", produces = "text/plain")
public String eval(Authentication userName, @RequestBody String expression) throws ParsingException { // + AUTH
LOG.debug("Evaluation request: [" + expression + "]");
double result = calculator.calculate(expression, userName.getName()); // username
LOG.trace("Result: " + result);
return Double.toString(result) + "\n";
}

@RequestMapping(path = "/registration", method = RequestMethod.POST, consumes = "text/plain", produces = "text/plain")
public void registration(@RequestParam(value = "arguments") Vector<String> arguments) throws IOException {
try {
if (arguments.size() != 2) {
throw new IOException("Mistake");
} else {
billingDao.registerNewUser(arguments.firstElement(), arguments.lastElement());
}
} catch (IOException e) {
throw new IllegalStateException("Can't understand");
}
}

@RequestMapping(path = "/variable/{variableName}", method = RequestMethod.PUT, consumes = "text/plain", produces = "text/plain")
public void addingVariable(Authentication userName, @PathVariable String variableName, @RequestBody String value) throws IOException {
billingDao.addValue(userName.getName(), variableName, value);
}

@RequestMapping(path = "/variable/{variableName}", method = RequestMethod.DELETE, consumes = "text/plain", produces = "text/plain")
public void deleteVariable(Authentication userName, @PathVariable String variableName) throws IOException {
billingDao.deleteVariable(userName.getName(), variableName);
}

@RequestMapping(path = "/variable/{variableName}", method = RequestMethod.GET/*, consumes = "text/plain", produces = "text/plain"*/)
public String getVariable(Authentication userName, @PathVariable String variableName) throws IOException {
System.out.println("ALIVE");
return billingDao.getVariable(userName.getName(), variableName).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.mipt.java2016.homework.g597.sigareva.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;

/**
* curl http://localhost:9001/eval \
* -X POST \
* -H "Content-Type: text/plain" \
* -H "Authorization: Basic $(echo -n "username:password" | base64)" \
* --data-raw "44*3+2"
*/

@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackageClasses = MyApplication.class)
public class MyApplication {

@Bean
public MyCalculator calculator() {
return new MyCalculator();
}

@Bean
public EmbeddedServletContainerCustomizer customizer(
@Value("${ru.mipt.java2016.homework.g597.sigareva.task4.httpPort:9001}") int port) {
return container -> container.setPort(port);
}

public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
}
}
Loading