Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Feature/logging Adding logging functionality to the application#17

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
harshil15999 wants to merge7 commits intocallicoder:master
base:master
Choose a base branch
Loading
fromharshil15999:feature/logging
Open
Show file tree
Hide file tree
Changes fromall 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
38 changes: 30 additions & 8 deletionspom.xml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,13 +18,29 @@
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>


<!--log4j2 dependencies -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-api.version}</version>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
Expand DownExpand Up@@ -53,6 +69,14 @@
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<slf4j.version>1.7.36</slf4j.version>
<log4j-api.version>2.17.1</log4j-api.version>
<disruptor.version>3.3.6</disruptor.version>
</properties>

<build>
<plugins>
Expand All@@ -62,6 +86,4 @@
</plugin>
</plugins>
</build>


</project>
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
package com.example.easynotes.controller;


import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
@Slf4j
public class IndexController {

@GetMapping
public String sayHello() {
return "Hello and Welcome to the EasyNotes application. You can create a new Note by making a POST request to /api/notes endpoint.";
try{
log.info("/ API hit");
return "Hello and Welcome to the EasyNotes application. You can create a new Note by making a POST request to /api/notes endpoint.";
} catch (Exception e ){
log.error("Error:{}",e);
return "Contact Administrator";
}

}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
package com.example.easynotes.controller;

import com.example.easynotes.exception.Messages;
import com.example.easynotes.exception.ResourceNotFoundException;
import com.example.easynotes.model.Note;
import com.example.easynotes.repository.NoteRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
* Created by rajeevkumarsingh on 27/06/17.
*/
@RestController
@RequestMapping("/api")
@Slf4j
public class NoteController {

@Autowired
NoteRepository noteRepository;


@Autowired
Messages messages;



private static final long MAX_FILE_SIZE = 2L * 1024 * 1024 * 1024; // 10GB


@GetMapping("/notes")
public List<Note> getAllNotes() {
return noteRepository.findAll();
Expand DownExpand Up@@ -59,4 +76,49 @@ public ResponseEntity<?> deleteNote(@PathVariable(value = "id") Long noteId) {

return ResponseEntity.ok().build();
}

@PostMapping("/upload/file")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
log.info(file.getOriginalFilename());

// #TODO : put this value in properties file
final String uploadLocation="C:\\Users\\harsh\\PROJECTS\\src\\main\\resources\\notes";

// 1. Validate file size (between 2000 and 2000 MB)
if (file.isEmpty() || file.getSize() > MAX_FILE_SIZE) {
log.info("File Size is :{}",file.getSize());
return ResponseEntity.badRequest().body(messages.FileLimit);
}

// 2. Extract filename directly from MultipartFile (assuming no specific format)
String fileName = file.getOriginalFilename();
log.info("File Name uploaded is {}",fileName);

// 3. Sanitize filename
if (fileName == null || fileName.isEmpty() || fileName.contains("..")) {
return new ResponseEntity<>(messages.InvalidFileName, HttpStatus.BAD_REQUEST);
}

// Perform additional processing with the sanitized filename and uploaded file content (not shown for brevity)
Path filePath=Paths.get(uploadLocation).resolve(file.getOriginalFilename());


// Create input stream from uploaded file
try (InputStream inputStream = file.getInputStream();
// Create output stream to write file directly to disk
OutputStream outputStream = new FileOutputStream(filePath.toFile())) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

return new ResponseEntity<>(messages.SuccessFullyUploaded,HttpStatus.OK);
} catch(Exception e ){
log.error(messages.ErrorSavingFile);
return new ResponseEntity<>(messages.ErrorSavingFile,HttpStatus.INTERNAL_SERVER_ERROR);
}

}

}
12 changes: 12 additions & 0 deletionssrc/main/java/com/example/easynotes/exception/Messages.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
package com.example.easynotes.exception;

import org.springframework.stereotype.Component;

@Component
public class Messages {
public final String FileNotFound="File Not Found";
public final String InvalidFileName="Invalid FileName";
public final String SuccessFullyUploaded="File uploaded successfully";
public final String ErrorSavingFile ="Error saving the file";
public final String FileLimit=" File must be not empty and less than 2Gb";
}
20 changes: 20 additions & 0 deletionssrc/main/java/config/AppConfig.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
package config;

import com.example.easynotes.exception.Messages;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@ComponentScan
@Configuration
public class AppConfig {
private String messages;
private HttpStatus status;
@Bean
public Messages messages(){
return new Messages();
}

}
14 changes: 13 additions & 1 deletionsrc/main/resources/application.properties
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,4 +10,16 @@ spring.datasource.password = callicoder
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.ddl-auto = update

# Loggers and Levels
logging.level.root=INFO
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=WARN
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.exception=ERROR

# File Logging Configuration
logging.file.path=logs/
logging.file.name=application.log
logging.file.max-size=10MB
Binary file addedsrc/main/resources/notes/2023-01-05 (1).png
View file
Open in desktop
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View file
Open in desktop
Binary file not shown.
Binary file addedsrc/main/resources/notes/SortWindows62.nls
View file
Open in desktop
Binary file not shown.
1 change: 1 addition & 0 deletionssrc/main/resources/notes/thoughts.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
you need to experience the bad to enjoy the good and the good to identify the bad

[8]ページ先頭

©2009-2025 Movatter.jp