Compare commits

...

2 Commits

Author SHA1 Message Date
3faa214d18 feat: add rest 2026-02-24 06:03:42 +03:00
d52a1d7914 feat: add download profile pdf 2026-02-24 05:17:38 +03:00
12 changed files with 195 additions and 19 deletions

BIN
lib/itextpdf-5.5.13.4.jar Normal file

Binary file not shown.

Binary file not shown.

BIN
lib/jackson-core-2.21.0.jar Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -45,10 +45,10 @@
<td><h3>${wordBundle.getWord("photo")}</h3></td>
<td>
<c:if test="${profile.photo != null}">
<img src="content/profiles/${requestScope.profile.id}/${requestScope.profile.photo}" alt="" height="300">
</c:if>
<img src="content${profile.photo}" height="500">
<br>
<input type="button" value="${wordBundle.getWord('update')}" onclick="document.getElementById('file').click();" />
</c:if>
<input type="button" value="${wordBundle.getWord('upload')}" onclick="document.getElementById('file').click();" />
<input type="file" name="photo" id="file" style="display:none;">
</td>
</tr>
@ -56,10 +56,13 @@
<table>
<tr class="hiddenRow">
<td>
<input type="image" src="content/app/img/floppy-disk.png" width="75" alt="submit" class="icon" />
<input type="image" src="content/app/img/floppy-disk.png" width="75" alt="submit" class="icon"/>
</td>
<td>
<a href="/email?id=${profile.id}"><img src="content/app/img/key.png" alt="" width="75"></a>
<a href="/credentials?id=${profile.id}"><img src="content/app/img/at-sign.png" width="75"></a>
</td>
<td>
<a href="/profile/pdf?id=${profile.id}"><img src="content/app/img/id.png" alt="" width="75"></a>
</td>
</tr>
</table>

View File

@ -1,5 +1,9 @@
package ru.charm.back.controller;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
@ -12,11 +16,12 @@ import ru.charm.back.mapper.RequestToProfileUpdateDtoMapper;
import ru.charm.back.service.ProfileService;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Optional;
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
@WebServlet("/profile")
@WebServlet("/profile/*")
@MultipartConfig
public class ProfileController extends HttpServlet {
private final ProfileService service = ProfileService.getInstance();
@ -26,22 +31,43 @@ public class ProfileController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String sId = req.getParameter("id");
String forwardUri = null;
if (sId != null) {
Optional<ProfileGetDto> optProfileDto = service.findById(Long.parseLong(sId));
System.out.println("Optional profile" + optProfileDto.toString());
if (optProfileDto.isPresent()) {
req.setAttribute("profile", optProfileDto.get());
forwardUri = "/WEB-INF/jsp/profile.jsp";
ProfileGetDto profileGetDto = optProfileDto.get();
if (req.getRequestURI().equals("/profile/pdf")) {
resp.setHeader("Content-Disposition", "attachment; filename=\"profile.pdf\"");
resp.setContentType("application/pdf");
resp.setCharacterEncoding("UTF-8");
try(OutputStream os = resp.getOutputStream()) {
Document document = new Document();
PdfWriter.getInstance(document, os);
document.open();
PdfPTable table = new PdfPTable(2);
table.addCell("Email");
table.addCell(profileGetDto.getEmail());
table.addCell("Name");
table.addCell(profileGetDto.getName());
table.addCell("Surname");
table.addCell(profileGetDto.getSurname());
document.add(table);
document.close();
} catch (DocumentException e) {
throw new IOException(e);
}
} else {
req.setAttribute("profile", profileGetDto);
req.getRequestDispatcher("/WEB-INF/jsp/profile.jsp").forward(req, resp);
}
} else {
resp.sendError(SC_NOT_FOUND);
}
} else {
req.setAttribute("profiles", service.findAll());
forwardUri = "/WEB-INF/jsp/profiles.jsp";
}
if (forwardUri == null) {
resp.sendError(SC_NOT_FOUND);
} else {
req.getRequestDispatcher(forwardUri).forward(req, resp);
req.getRequestDispatcher("/WEB-INF/jsp/profiles.jsp").forward(req, resp);
}
}

View File

@ -14,6 +14,9 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import static ru.charm.back.utils.UrlUtils.LOGIN_REST_URL;
import static ru.charm.back.utils.UrlUtils.REST_URL;
@WebFilter("/*")
@Slf4j
public class AuthFilter implements Filter {
@ -24,12 +27,15 @@ public class AuthFilter implements Filter {
HttpServletRequest req = (HttpServletRequest) servletRequest;
HttpServletResponse res = (HttpServletResponse) servletResponse;
if (PRIVATE_PATHS.contains(req.getRequestURI())) {
if (!LOGIN_REST_URL.equals(req.getRequestURI()) && PRIVATE_PATHS.contains(req.getRequestURI())) {
ProfileGetDto userDetails = (ProfileGetDto) req.getSession().getAttribute("userDetails");
log.info("User {} is trying to access {}", userDetails, req.getRequestURI());
if (userDetails == null) {
res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
} else if (userDetails.getRole() == Role.ADMIN || userDetails.getId().toString().equals( req.getParameter("id"))) {
} else if (!req.getRequestURI().startsWith(REST_URL) &&
userDetails.getRole() == Role.ADMIN ||
userDetails.getId().toString().equals( req.getParameter("id"))
) {
filterChain.doFilter(req, res);
} else {
res.sendError(HttpServletResponse.SC_FORBIDDEN);

View File

@ -9,6 +9,8 @@ import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import static jakarta.servlet.RequestDispatcher.ERROR_EXCEPTION;
import static jakarta.servlet.RequestDispatcher.ERROR_REQUEST_URI;
import static ru.charm.back.utils.UrlUtils.REST_URL;
@WebFilter(value = "/*", dispatcherTypes = DispatcherType.ERROR)
@Slf4j
@ -26,6 +28,14 @@ public class ErrorFilter implements Filter {
log.warn("Exception code {}", res.getStatus());
}
String requestUri = (String) req.getAttribute(ERROR_REQUEST_URI);
if (requestUri.startsWith(REST_URL)) {
res.setContentType("application/json");
res.setCharacterEncoding("UTF-8");
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
}
}

View File

@ -0,0 +1,46 @@
package ru.charm.back.controller.rest;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import ru.charm.back.dto.LoginDto;
import ru.charm.back.dto.ProfileGetDto;
import ru.charm.back.mapper.RequestToLoginDtoMapper;
import ru.charm.back.service.ProfileService;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Optional;
import static ru.charm.back.utils.UrlUtils.LOGIN_REST_URL;
@WebServlet(LOGIN_REST_URL)
@Slf4j
public class LoginController extends HttpServlet {
private final ProfileService service = ProfileService.getInstance();
private final ObjectMapper objectMapper = new ObjectMapper();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
try (BufferedReader reader = req.getReader()) {
LoginDto dto = objectMapper.readValue(reader, LoginDto.class);
Optional<ProfileGetDto> userDetailOpt = service.login(dto);
if (userDetailOpt.isPresent()) {
req.getSession().setAttribute("userDetails", userDetailOpt.get());
return;
} else {
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
} catch (DatabindException e) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}
}

View File

@ -0,0 +1,52 @@
package ru.charm.back.controller.rest;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import ru.charm.back.dto.ProfileGetDto;
import ru.charm.back.service.ProfileService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Optional;
import static jakarta.servlet.http.HttpServletResponse.SC_NOT_FOUND;
import static ru.charm.back.utils.UrlUtils.REST_URL;
@WebServlet(REST_URL + "/profile")
@MultipartConfig
public class ProfileController extends HttpServlet {
private final ProfileService service = ProfileService.getInstance();
private final ObjectMapper objectMapper = JsonMapper.builder()
.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)
.addModule(new JavaTimeModule())
.build();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
try (PrintWriter writer = resp.getWriter()) {
resp.setContentType("application/json");
resp.setCharacterEncoding("UTF-8");
String sId = req.getParameter("id");
if (sId != null) {
Optional<ProfileGetDto> optProfileDto = service.findById(Long.parseLong(sId));
if (optProfileDto.isPresent()) {
ProfileGetDto profileGetDto = optProfileDto.get();
objectMapper.writeValue(writer, profileGetDto);
} else {
resp.sendError(SC_NOT_FOUND);
}
} else {
objectMapper.writeValue(writer, service.findAll());
}
}
}
}

View File

@ -0,0 +1,33 @@
package ru.charm.back.utils;
import lombok.experimental.UtilityClass;
import java.util.Set;
@UtilityClass
public class UrlUtils {
public static final String PROFILE_URL = "/profile";
public static final String CREDENTIALS_URL = "/credentials";
public static final String LOGIN_URL = "/login";
public static final String LOGOUT_URL = "/logout";
public static final String REGISTRATION_URL = "/registration";
public static final String LANG_URL = "/lang";
public static final String CONTENT_URL = "/content";
public static final String BASE_CONTENT_PATH = "/Users/andrey.s.eliseev/Downloads";
public static final Set<String> PRIVATE_PATHS = Set.of(PROFILE_URL, CREDENTIALS_URL);
public static final Set<String> ENTRY_PATHS = Set.of(LOGIN_URL, REGISTRATION_URL);
public static String getJspPath(String url) {
return "/WEB-INF/jsp" + url + ".jsp";
}
public static final String REST_URL = "/api/v1";
public static final String LOGIN_REST_URL = REST_URL + LOGIN_URL;
public static String getProfilePhotoPath(Long id, String fileName) {
return "/profiles/" + id + "/" + fileName;
}
}