Skip to content

Hw2 #4

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
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
65 changes: 65 additions & 0 deletions FP_re/bot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.example</groupId>
<artifactId>FP</artifactId>
<version>1.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>bot</artifactId>

<properties>
<starter-validation.version>3.0.1</starter-validation.version>
<starter-web.version>2.7.6</starter-web.version>
</properties>

<dependencies>
<!--Swagger UI-->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<!--Swagger UI-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${starter-validation.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${starter-web.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.tinkoff.edu.java.bot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import ru.tinkoff.edu.java.bot.configuration.ApplicationConfig;

@SpringBootApplication
@EnableConfigurationProperties(ApplicationConfig.class)
public class BotApplication {
public static void main(String[] args) {
var ctx = SpringApplication.run(BotApplication.class, args);
ApplicationConfig config = ctx.getBean(ApplicationConfig.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.tinkoff.edu.java.bot.api;

import org.springframework.web.bind.annotation.*;
import ru.tinkoff.edu.java.bot.api.model.LinkUpdate;

@RestController
@RequestMapping("/update")
public class BotController {

@PostMapping
public String updateChat(@RequestBody LinkUpdate update) {
return update.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.tinkoff.edu.java.bot.api.exceptionHandler;

import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import ru.tinkoff.edu.java.bot.api.model.ApiErrorResponse;

@RestControllerAdvice
public class BotExceptionHandler {

private String getDescription(String message) {
ApiErrorResponse errorObj = new ApiErrorResponse(
message,
null,
null,
null,
null
);
return errorObj.description();
}

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String MessageNotReadable(HttpMessageNotReadableException Exception) {
return getDescription("Некорректные значения параметров или их нет!");
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String MethodNotSupported(HttpRequestMethodNotSupportedException Exception) {
return getDescription("Метод не разрешен!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.tinkoff.edu.java.bot.api.model;

import java.util.List;

public record ApiErrorResponse(
String description,
String code,
String exceptionName,
String exceptionMessage,
List<String> stacktrace
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ru.tinkoff.edu.java.bot.api.model;

import java.util.List;

public record LinkUpdate(long id, String url, String description, List<Long> tgChatIds) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.tinkoff.edu.java.bot.configuration;

import jakarta.validation.constraints.NotNull;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.validation.annotation.Validated;
import ru.tinkoff.edu.java.bot.schedule.Scheduler;

@Validated
@EnableScheduling
@ConfigurationProperties(prefix = "app", ignoreUnknownFields = false)
public record ApplicationConfig(@NotNull String test, @NotNull Scheduler scheduler) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.tinkoff.edu.java.bot.schedule;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.logging.Level;
import java.util.logging.Logger;

@Service
public class LinkUpdaterScheduler {
private final static Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

@Scheduled(fixedDelayString = "${app.scheduler.interval}")
public void update() {
LOGGER.log(Level.INFO, "Info bot called");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.tinkoff.edu.java.bot.schedule;

import java.time.Duration;

public record Scheduler(Duration interval) {}
3 changes: 3 additions & 0 deletions FP_re/bot/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
app.test=123
springdoc.swagger-ui.path=/swagger-ui
app.scheduler.interval=5000
59 changes: 59 additions & 0 deletions FP_re/bot/src/main/resources/botapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.1
info:
title: Bot API
version: 1.0.0
contact:
name: Alexander Biryukov
url: https://github.com
paths:
/updates:
post:
summary: Отправить обновление
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/LinkUpdate'
required: true
responses:
'200':
description: Обновление обработано
'400':
description: Некорректные параметры запроса
content:
application/json:
schema:
$ref: '#/components/schemas/ApiErrorResponse'
components:
schemas:
ApiErrorResponse:
type: object
properties:
description:
type: string
code:
type: string
exceptionName:
type: string
exceptionMessage:
type: string
stacktrace:
type: array
items:
type: string
LinkUpdate:
type: object
properties:
id:
type: integer
format: int64
url:
type: string
format: uri
description:
type: string
tgChatIds:
type: array
items:
type: integer
format: int64
14 changes: 14 additions & 0 deletions FP_re/link-parser/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.example</groupId>
<artifactId>FP</artifactId>
<version>1.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>link-parser</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.tinkoff.edu.java.linkparser;

import ru.tinkoff.edu.java.linkparser.absracts.*;

public class LinkParser {

public String getLink(String link) {

AbstractParser gitParser = new GitParser();
AbstractParser stackParser = new StackParser();
AbstractParser otherParser = new OtherParser();

gitParser.setNextParser(stackParser);
stackParser.setNextParser(otherParser);

return gitParser.logParser(link);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.tinkoff.edu.java.linkparser.absracts;

public abstract class AbstractParser {

private AbstractParser nextParser;

public void setNextParser(AbstractParser nextParser) {
this.nextParser = nextParser;
}

public String logParser (String link) {
if(nextParser != null) {
if(this.parsAbstract(link) == null) return nextParser.logParser(link);
}
return this.parsAbstract(link);
}

abstract protected String parsAbstract(String link);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.tinkoff.edu.java.linkparser.absracts;

import java.util.Objects;

public class GitParser extends AbstractParser {

@Override
protected String parsAbstract(String link) {

String[] parsed = link.split("/");

if(!Objects.equals(parsed[2], "github.com")) return null;

if (parsed.length > 4) return parsed[3] + "/" + parsed[4];

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.tinkoff.edu.java.linkparser.absracts;

public class OtherParser extends AbstractParser {

@Override
protected String parsAbstract(String link) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ru.tinkoff.edu.java.linkparser.absracts;

import java.util.Objects;

public class StackParser extends AbstractParser {

@Override
protected String parsAbstract(String link) {

String[] parsed = link.split("/");

if (!Objects.equals(parsed[2], "stackoverflow.com")) return null;
if (!Objects.equals(parsed[3], "questions")) return null;


if (parsed.length > 4) return parsed[4];

return null;
}
}
Loading