Java tutorial
/* * Mail Attachment Gateway * Copyright (c) 2016-2017 Carsten Rambow * mailto:developer AT elomagic DOT de * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.elomagic.mag; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.eclipse.jetty.servlet.DefaultServlet; /** * * @author Carsten Rambow */ public class ServletMock extends DefaultServlet { private static final Logger LOGGER = LogManager.getLogger(ServletMock.class); public ServletMock() { } public byte[] lastContent; public int lastContentLength = -1; public String requestedUri; public boolean received = false; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.doGet(request, response); //To change body of generated methods, choose Tools | Templates. } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.doPost(request, response); //To change body of generated methods, choose Tools | Templates. } @Override protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { received = false; lastContentLength = request.getContentLength(); requestedUri = request.getRequestURI(); if (lastContentLength > 0) { lastContent = IOUtils.readFully(request.getInputStream(), lastContentLength); } LOGGER.info("contentLength=" + FileUtils.byteCountToDisplaySize(lastContentLength)); LOGGER.info("content" + lastContent); response.setStatus(HttpServletResponse.SC_CREATED); received = true; } @Override protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doDelete(req, resp); //To change body of generated methods, choose Tools | Templates. } @Override protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doOptions(req, resp); //To change body of generated methods, choose Tools | Templates. } @Override protected void doHead(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getRequestURI().endsWith("TestFile.pdf")) { response.setStatus(HttpServletResponse.SC_OK); } else { response.setStatus(HttpServletResponse.SC_NOT_FOUND); } } }