Created
August 30, 2021 11:32
-
-
Save JuliaKrivonos/e0f83885bfcc01928282ac75c17db9ef to your computer and use it in GitHub Desktop.
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
| package org.jazzteam.krivonos.fitness.servlet; | |
| import javax.servlet.ServletInputStream; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletRequestWrapper; | |
| import java.io.*; | |
| public class CachedBodyHttpServletRequest extends HttpServletRequestWrapper { | |
| public static final int BUFFER_SIZE = 4096; | |
| private static final byte[] EMPTY_CONTENT = new byte[0]; | |
| private final byte[] cachedBody; | |
| public CachedBodyHttpServletRequest(HttpServletRequest request) throws IOException { | |
| super(request); | |
| InputStream requestInputStream = request.getInputStream(); | |
| this.cachedBody = copyToByteArray(requestInputStream); | |
| } | |
| @Override | |
| public ServletInputStream getInputStream() throws IOException { | |
| return new CachedBodyServletInputStream(this.cachedBody); | |
| } | |
| @Override | |
| public BufferedReader getReader() throws IOException { | |
| ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.cachedBody); | |
| return new BufferedReader(new InputStreamReader(byteArrayInputStream)); | |
| } | |
| public static byte[] copyToByteArray(InputStream in) throws IOException { | |
| if (in == null) { | |
| return new byte[0]; | |
| } | |
| ByteArrayOutputStream out = new ByteArrayOutputStream(BUFFER_SIZE); | |
| copy(in, out); | |
| return out.toByteArray(); | |
| } | |
| public static int copy(InputStream in, OutputStream out) throws IOException { | |
| int byteCount = 0; | |
| byte[] buffer = new byte[BUFFER_SIZE]; | |
| int bytesRead = -1; | |
| while ((bytesRead = in.read(buffer)) != -1) { | |
| out.write(buffer, 0, bytesRead); | |
| byteCount += bytesRead; | |
| } | |
| out.flush(); | |
| return byteCount; | |
| } | |
| } |
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
| package org.jazzteam.krivonos.fitness.servlet; | |
| import lombok.extern.slf4j.Slf4j; | |
| import javax.servlet.ReadListener; | |
| import javax.servlet.ServletInputStream; | |
| import java.io.ByteArrayInputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| @Slf4j | |
| public class CachedBodyServletInputStream extends ServletInputStream { | |
| private final InputStream cachedBodyInputStream; | |
| public CachedBodyServletInputStream(byte[] cachedBody) { | |
| this.cachedBodyInputStream = new ByteArrayInputStream(cachedBody); | |
| } | |
| @Override | |
| public boolean isFinished() { | |
| try { | |
| return cachedBodyInputStream.available() == 0; | |
| } catch (IOException e) { | |
| log.debug("{}", e.getMessage()); | |
| throw new IllegalStateException(e); | |
| } | |
| } | |
| @Override | |
| public boolean isReady() { | |
| return true; | |
| } | |
| @Override | |
| public void setReadListener(ReadListener readListener) { | |
| } | |
| @Override | |
| public int read() throws IOException { | |
| return cachedBodyInputStream.read(); | |
| } | |
| } |
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
| package org.jazzteam.krivonos.fitness.servlet; | |
| import com.google.common.base.Charsets; | |
| import com.google.common.io.CharStreams; | |
| import lombok.extern.slf4j.Slf4j; | |
| import javax.servlet.FilterChain; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.annotation.WebFilter; | |
| import javax.servlet.http.HttpFilter; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.io.*; | |
| import java.util.stream.Collectors; | |
| @Slf4j | |
| @WebFilter(filterName = "ContentCachingFilter", urlPatterns = "/inventory") | |
| public class ContentCachingFilter extends HttpFilter { | |
| @Override | |
| protected void doFilter(HttpServletRequest httpServletRequest, | |
| HttpServletResponse httpServletResponse, | |
| FilterChain filterChain) throws ServletException, IOException { | |
| CachedBodyHttpServletRequest cachedBodyHttpServletRequest = | |
| new CachedBodyHttpServletRequest(httpServletRequest); | |
| BufferedReader bufferedReader = null; | |
| InputStream inputStream = cachedBodyHttpServletRequest.getInputStream(); | |
| filterChain.doFilter(cachedBodyHttpServletRequest, httpServletResponse); | |
| } | |
| } |
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
| package org.jazzteam.krivonos.fitness.servlet; | |
| import javax.servlet.*; | |
| import javax.servlet.http.HttpFilter; | |
| import java.io.IOException; | |
| public class FilterConnect extends HttpFilter { | |
| public void init(FilterConfig config) throws ServletException { | |
| } | |
| public void destroy() { | |
| } | |
| @Override | |
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { | |
| chain.doFilter(request, response); | |
| } | |
| } |
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
| package org.jazzteam.krivonos.fitness.servlet; | |
| import lombok.extern.slf4j.Slf4j; | |
| import javax.servlet.*; | |
| import javax.servlet.annotation.WebFilter; | |
| import javax.servlet.http.HttpFilter; | |
| import java.io.IOException; | |
| @Slf4j | |
| @WebFilter(filterName = "FilterConnectSetContentType", urlPatterns = "/*") | |
| public class FilterConnectSetContentType extends HttpFilter { | |
| public void init(FilterConfig config) throws ServletException { | |
| } | |
| @Override | |
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { | |
| response.setCharacterEncoding("UTF-8"); | |
| response.setContentType("application/json"); | |
| chain.doFilter(request, response); | |
| } | |
| } |
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
| package org.jazzteam.krivonos.fitness.servlet; | |
| import javax.servlet.annotation.WebServlet; | |
| import javax.servlet.http.HttpServlet; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.io.IOException; | |
| @WebServlet("/") | |
| public class IndexServlet extends HttpServlet { | |
| @Override | |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { | |
| resp.sendRedirect(req.getContextPath() + "/inventory"); | |
| } | |
| } |
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
| package org.jazzteam.krivonos.fitness.servlet; | |
| import com.google.gson.Gson; | |
| import com.google.gson.JsonArray; | |
| import com.sun.javafx.geom.transform.Identity; | |
| import lombok.extern.slf4j.Slf4j; | |
| import org.jazzteam.fitnesclub.dto.InventoryDto; | |
| import org.jazzteam.fitnesclub.service.InventoryService; | |
| import org.jazzteam.fitnesclub.service.impl.InventoryServiceImpl; | |
| import javax.servlet.*; | |
| import javax.servlet.http.*; | |
| import javax.servlet.annotation.*; | |
| import java.io.*; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.function.Predicate; | |
| import java.util.stream.Collectors; | |
| @Slf4j | |
| @WebServlet(name = "InventoryServlet", value = "/inventory") | |
| public class InventoryServlet extends HttpServlet { | |
| private final InventoryService inventoryService = new InventoryServiceImpl(); | |
| private final Gson gson = new Gson(); | |
| @Override | |
| public void init(ServletConfig config) throws ServletException { | |
| super.init(config); | |
| } | |
| @Override | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| try { | |
| List<InventoryDto> inventories = new ArrayList<>(inventoryService | |
| .findAllInventory()); | |
| PrintWriter out = response.getWriter(); | |
| JsonArray inventoriesJsonArray = gson.toJsonTree(inventories).getAsJsonArray(); | |
| out.print(inventoriesJsonArray); | |
| out.flush(); | |
| } catch (Exception e) { | |
| log.error("{}", e.getMessage()); | |
| response.sendError(HttpServletResponse.SC_NO_CONTENT, "No content for view"); | |
| } | |
| } | |
| @Override | |
| protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| String body = getBody(request); | |
| try { | |
| InventoryDto data = gson.fromJson(body, InventoryDto.class); | |
| InventoryDto inventoryDto = inventoryService.saveInventory(data); | |
| response.setStatus(HttpServletResponse.SC_CREATED); | |
| response.getWriter().print(gson.toJson(inventoryDto)); | |
| response.getWriter().flush(); | |
| } catch (Exception e) { | |
| log.debug(e.getMessage()); | |
| response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE); | |
| response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE); | |
| } | |
| } | |
| @Override | |
| protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| String id = request.getParameter("id"); | |
| try { | |
| inventoryService.deleteInventory(Long.parseLong(id)); | |
| response.setStatus(HttpServletResponse.SC_OK); | |
| response.sendRedirect(getServletContext() | |
| .getRealPath(request | |
| .getServletPath())); | |
| } catch (Exception e) { | |
| log.debug(e.getMessage()); | |
| response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE); | |
| response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "Specify the correct resource to delete."); | |
| } | |
| } | |
| @Override | |
| protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
| String body = getBody(request); | |
| try { | |
| InventoryDto data = gson.fromJson(body, InventoryDto.class); | |
| final InventoryDto inventoryUpdated = inventoryService.updateInventory(data); | |
| response.setStatus(HttpServletResponse.SC_CREATED); | |
| String inventoriesJsonArray = gson.toJson(inventoryUpdated); | |
| response.getWriter().print(inventoriesJsonArray); | |
| response.getWriter().flush(); | |
| } catch (Exception e) { | |
| log.debug(e.getMessage()); | |
| response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE); | |
| response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE); | |
| } | |
| } | |
| private String getBody(HttpServletRequest request) { | |
| StringBuilder stringBuilder = new StringBuilder(); | |
| BufferedReader bufferedReader = null; | |
| try (InputStream inputStream = request.getInputStream()) { | |
| if (inputStream != null) { | |
| bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); | |
| char[] charBuffer = new char[255]; | |
| int bytesRead = -1; | |
| while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { | |
| stringBuilder.append(charBuffer, 0, bytesRead); | |
| } | |
| } | |
| } catch (IOException e) { | |
| log.error("{}", e.getMessage()); | |
| return ""; | |
| } | |
| return stringBuilder.toString(); | |
| } | |
| } |
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
| package org.jazzteam.krivonos.fitness.servlet; | |
| import lombok.extern.slf4j.Slf4j; | |
| import javax.servlet.*; | |
| import javax.servlet.annotation.WebFilter; | |
| import javax.servlet.http.HttpFilter; | |
| import javax.servlet.http.HttpServletRequest; | |
| import java.io.IOException; | |
| @Slf4j | |
| @WebFilter(filterName = "LogFilter", urlPatterns = "/*") | |
| public class LogFilter extends HttpFilter { | |
| private FilterConfig filterConfigObj; | |
| public void init(FilterConfig config) throws ServletException { | |
| this.filterConfigObj = config; | |
| } | |
| public void destroy() { | |
| } | |
| @Override | |
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { | |
| String remoteAddress = request.getRemoteAddr(); | |
| String uri = ((HttpServletRequest) request).getRequestURI(); | |
| String protocol = request.getProtocol(); | |
| chain.doFilter(request, response); | |
| log.debug("Logging filter Servlet called"); | |
| log.debug("**************************"); | |
| log.debug("User IP: " + remoteAddress + | |
| " Resource File: " + uri + | |
| "Protocol: " + protocol); | |
| } | |
| } |
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
| package org.jazzteam.krivonos.fitness.servlet; | |
| import com.google.common.io.CharStreams; | |
| import lombok.extern.slf4j.Slf4j; | |
| import sun.nio.ch.IOUtil; | |
| import javax.servlet.FilterChain; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.annotation.WebFilter; | |
| import javax.servlet.http.Cookie; | |
| import javax.servlet.http.HttpFilter; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.util.UUID; | |
| @Slf4j | |
| @WebFilter(filterName = "printRequestContentFilter", urlPatterns = "/*") | |
| public class PrintRequestContentFilter extends HttpFilter { | |
| @Override | |
| protected void doFilter(HttpServletRequest httpServletRequest, | |
| HttpServletResponse httpServletResponse, | |
| FilterChain filterChain) throws ServletException, IOException { | |
| System.out.println("IN PrintRequestContentFilter "); | |
| InputStream inputStream = httpServletRequest.getInputStream(); | |
| String body = copyToByteArray(inputStream); | |
| log.debug("In PrintRequestContentFilter. Request body is: {}", body); | |
| filterChain.doFilter(httpServletRequest, httpServletResponse); | |
| } | |
| private String copyToByteArray(InputStream inputStream) throws IOException { | |
| ByteArrayOutputStream result = new ByteArrayOutputStream(); | |
| byte[] buffer = new byte[1024]; | |
| int length; | |
| while ((length = inputStream.read(buffer)) != -1) { | |
| result.write(buffer, 0, length); | |
| } | |
| return result.toString("UTF-8"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment