diff --git a/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/TestServlet.java b/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/TestServlet.java index 2b3cfe5..4368305 100644 --- a/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/TestServlet.java +++ b/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/TestServlet.java @@ -19,15 +19,27 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; public class TestServlet extends HttpServlet { + + private static final long serialVersionUID = 8775172147016982644L; + private static final Logger LOGGER = Logger.getLogger(TestServlet.class.getName()); + + private static final String REQUEST_RESPONSE_FAILURE_MSG_BASE = "The request / response interaction generated an exception"; + @Override protected void doPut( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); - response.getWriter().append(request.getReader().readLine()); + try { + response.getWriter().append(request.getReader().readLine()); + } catch (IOException ex) { + LOGGER.log(Level.FINE, REQUEST_RESPONSE_FAILURE_MSG_BASE, ex); + } } @Override protected void doPost( @@ -36,6 +48,11 @@ public class TestServlet extends HttpServlet { throws ServletException, IOException { response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); - response.getWriter().append(request.getReader().readLine()); + try { + response.getWriter().append(request.getReader().readLine()); + } catch (IOException ex) { + LOGGER.log(Level.FINE, REQUEST_RESPONSE_FAILURE_MSG_BASE, ex); + } + } } diff --git a/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/validation/ValidatingHttpRequest.java b/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/validation/ValidatingHttpRequest.java new file mode 100644 index 0000000..82b8c50 --- /dev/null +++ b/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/validation/ValidatingHttpRequest.java @@ -0,0 +1,20 @@ +package com.thoughtworks.inproctester.jetty.testapp.validation; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import javax.servlet.http.HttpServletResponse; + +public class ValidatingHttpRequest extends HttpServletRequestWrapper { + + public ValidatingHttpRequest(HttpServletRequest request) { + super(request); + } + + @Override + public boolean authenticate(HttpServletResponse response) { + // Customize this + return true; + } + +} + diff --git a/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/validation/ValidationFilter.java b/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/validation/ValidationFilter.java new file mode 100644 index 0000000..4e96b83 --- /dev/null +++ b/inproctester-jetty-tests/src/main/java/com/thoughtworks/inproctester/jetty/testapp/validation/ValidationFilter.java @@ -0,0 +1,32 @@ +package com.thoughtworks.inproctester.jetty.testapp.validation; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; + + + +public class ValidationFilter implements javax.servlet.Filter { + + @Override + public void destroy() { + // Customise this + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + chain.doFilter(new ValidatingHttpRequest((HttpServletRequest) request), response); + } + + @Override + public void init(FilterConfig arg0) throws ServletException { + // Customize + } +} + diff --git a/inproctester-jetty-tests/src/main/webapp/WEB-INF/web.xml b/inproctester-jetty-tests/src/main/webapp/WEB-INF/web.xml index f92b804..68e2213 100644 --- a/inproctester-jetty-tests/src/main/webapp/WEB-INF/web.xml +++ b/inproctester-jetty-tests/src/main/webapp/WEB-INF/web.xml @@ -3,14 +3,26 @@ Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> - - TestServlet - - com.thoughtworks.inproctester.jetty.testapp.TestServlet - - - - TestServlet - /* - + + TestServlet + + com.thoughtworks.inproctester.jetty.testapp.TestServlet + + + + TestServlet + /* + + + + ValidationFilter + com.thoughtworks.inproctester.jetty.testapp.validation.ValidationFilter + + + + ValidationFilter + /* + + + \ No newline at end of file diff --git a/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/TestApplication.java b/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/TestApplication.java index 58d26a9..6df2b54 100644 --- a/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/TestApplication.java +++ b/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/TestApplication.java @@ -26,7 +26,7 @@ @Path("/") public class TestApplication { - private static Map resources = new HashMap(); + private static Map resources = new HashMap<>(); @Context private UriInfo uriInfo; diff --git a/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/validation/ValidatingHttpRequest.java b/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/validation/ValidatingHttpRequest.java new file mode 100644 index 0000000..81b0fda --- /dev/null +++ b/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/validation/ValidatingHttpRequest.java @@ -0,0 +1,19 @@ +package com.thoughtworks.inproctester.resteasy.testapp.validation; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import javax.servlet.http.HttpServletResponse; + +public class ValidatingHttpRequest extends HttpServletRequestWrapper { + + public ValidatingHttpRequest(HttpServletRequest request) { + super(request); + } + + @Override + public boolean authenticate(HttpServletResponse response) { + // Customize this + return true; + } + +} diff --git a/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/validation/ValidationFilter.java b/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/validation/ValidationFilter.java new file mode 100644 index 0000000..ebde210 --- /dev/null +++ b/inproctester-resteasy-tests/src/main/java/com/thoughtworks/inproctester/resteasy/testapp/validation/ValidationFilter.java @@ -0,0 +1,29 @@ +package com.thoughtworks.inproctester.resteasy.testapp.validation; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; + +public class ValidationFilter implements javax.servlet.Filter { + + @Override + public void destroy() { + // Customise this + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + chain.doFilter(new ValidatingHttpRequest((HttpServletRequest) request), response); + } + + @Override + public void init(FilterConfig arg0) throws ServletException { + // Customize + } +} diff --git a/inproctester-resteasy-tests/src/main/webapp/WEB-INF/web.xml b/inproctester-resteasy-tests/src/main/webapp/WEB-INF/web.xml index 3e2ea05..ed02b40 100644 --- a/inproctester-resteasy-tests/src/main/webapp/WEB-INF/web.xml +++ b/inproctester-resteasy-tests/src/main/webapp/WEB-INF/web.xml @@ -32,4 +32,13 @@ Application 2.3//EN" Resteasy /* + + ValidationFilter + com.thoughtworks.inproctester.resteasy.testapp.validation.ValidationFilter + + + + ValidationFilter + /* + \ No newline at end of file diff --git a/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/InProcessClientExecutor.java b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/InProcessClientExecutor.java index 8f5ccca..27893ae 100644 --- a/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/InProcessClientExecutor.java +++ b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/InProcessClientExecutor.java @@ -19,9 +19,14 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; public class InProcessClientExecutor implements ClientExecutor { - + + private static final Logger LOGGER = Logger.getLogger(InProcessClientExecutor.class.getName()); + private static final String CONNECTION_RELEASE_FAILURE_MSG = "An exception occured while trying to release the connection"; + private List testerRoutes = new ArrayList<>(); public InProcessClientExecutor() { @@ -63,7 +68,8 @@ public InputStream getInputStream() { public void performReleaseConnection() { try { stream.close(); - } catch (Exception ignored) { + } catch (Exception ex) { + LOGGER.log(Level.FINE,CONNECTION_RELEASE_FAILURE_MSG , ex); } } }, this); @@ -75,6 +81,7 @@ public void performReleaseConnection() { } public void close() throws Exception { + // Not implemented yet } private InProcConnection routeToTesterApplication(URI requestUri) { diff --git a/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/RestEasyClientInProcRequest.java b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/RestEasyClientInProcRequest.java index 91d4e7d..8080300 100644 --- a/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/RestEasyClientInProcRequest.java +++ b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/RestEasyClientInProcRequest.java @@ -2,6 +2,10 @@ import com.thoughtworks.inproctester.core.InProcRequest; import com.thoughtworks.inproctester.core.UrlHelper; +import com.thoughtworks.inproctester.resteasy.exceptions.RequestEntityWriteException; +import com.thoughtworks.inproctester.resteasy.exceptions.RequestHostException; +import com.thoughtworks.inproctester.resteasy.exceptions.UriRetrievalException; + import org.jboss.resteasy.client.ClientRequest; import javax.ws.rs.core.MultivaluedMap; @@ -13,17 +17,23 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; public class RestEasyClientInProcRequest implements InProcRequest { + + private static final Logger LOGGER = Logger.getLogger(RestEasyClientInProcRequest.class.getName()); + private static final String REQUEST_ENTITY_WRITE_EXCEPTION_MESSAGE = "Unable to write from request entity"; + private ClientRequest clientRequest; - private Map headers = new HashMap(); + private Map headers = new HashMap<>(); public RestEasyClientInProcRequest(ClientRequest clientRequest) { this.clientRequest = clientRequest; try { headers.put("Host", UrlHelper.getRequestHost(new URI(clientRequest.getUri()))); } catch (Exception e) { - throw new RuntimeException(e); + throw new RequestHostException(e); } if (clientRequest.getBodyContentType() != null) { headers.put("Content-type", clientRequest.getBodyContentType().toString()); @@ -41,7 +51,7 @@ public URI getUri() { try { return new URI(clientRequest.getUri()); } catch (Exception e) { - throw new RuntimeException(e); + throw new UriRetrievalException(e); } } @@ -50,7 +60,7 @@ public String getContent() { try { return new String(writeRequestEntity(clientRequest), "UTF-8"); } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); + throw new RequestEntityWriteException(e); } } @@ -70,7 +80,7 @@ public void addHeader(String headerName, String header) { } private Map asMap(MultivaluedMap headers) { - HashMap map = new HashMap(); + HashMap map = new HashMap<>(); for (Map.Entry> header : headers.entrySet()) { for (String v : header.getValue()) { map.put(header.getKey(), v); @@ -83,7 +93,7 @@ private Map asMap(MultivaluedMap headers) { private byte[] writeRequestEntity(ClientRequest clientRequest) { if (clientRequest.getBody() != null && !clientRequest.getFormParameters().isEmpty()) - throw new RuntimeException("You cannot send both form parameters and an entity body"); + throw new RequestEntityWriteException("You cannot send both form parameters and an entity body"); if (!clientRequest.getFormParameters().isEmpty()) { throw new UnsupportedOperationException("InProcessClientExecutpr doesn't support form parameters yet"); @@ -92,12 +102,13 @@ private byte[] writeRequestEntity(ClientRequest clientRequest) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (clientRequest.getBody() != null) { if ("GET".equals(clientRequest.getHttpMethod())) - throw new RuntimeException("A GET request cannot have a body."); + throw new RequestEntityWriteException("A GET request cannot have a body."); try { clientRequest.writeRequestBody(clientRequest.getHeadersAsObjects(), baos); } catch (IOException e) { - throw new RuntimeException(e); + LOGGER.log(Level.FINE, REQUEST_ENTITY_WRITE_EXCEPTION_MESSAGE, e); + throw new RequestEntityWriteException(e); } } diff --git a/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/exceptions/RequestEntityWriteException.java b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/exceptions/RequestEntityWriteException.java new file mode 100644 index 0000000..28eb950 --- /dev/null +++ b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/exceptions/RequestEntityWriteException.java @@ -0,0 +1,15 @@ +package com.thoughtworks.inproctester.resteasy.exceptions; + +public class RequestEntityWriteException extends RuntimeException { + + private static final long serialVersionUID = -3024331508265244769L; + + public RequestEntityWriteException(Exception e) { + super(e); + } + + public RequestEntityWriteException(String message) { + super(message); + } + +} diff --git a/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/exceptions/RequestHostException.java b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/exceptions/RequestHostException.java new file mode 100644 index 0000000..dafe403 --- /dev/null +++ b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/exceptions/RequestHostException.java @@ -0,0 +1,11 @@ +package com.thoughtworks.inproctester.resteasy.exceptions; + +public class RequestHostException extends RuntimeException { + + private static final long serialVersionUID = -1365627301588525745L; + + public RequestHostException(Exception ex) { + super(ex); + } + +} diff --git a/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/exceptions/UriRetrievalException.java b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/exceptions/UriRetrievalException.java new file mode 100644 index 0000000..d1c190b --- /dev/null +++ b/inproctester-resteasy/src/main/java/com/thoughtworks/inproctester/resteasy/exceptions/UriRetrievalException.java @@ -0,0 +1,11 @@ +package com.thoughtworks.inproctester.resteasy.exceptions; + +public class UriRetrievalException extends RuntimeException { + + private static final long serialVersionUID = -506077171726232405L; + + public UriRetrievalException(Exception ex) { + super(ex); + } + +} diff --git a/inproctester-tests/src/main/java/com/thoughtworks/inproctester/testapp/TestServlet.java b/inproctester-tests/src/main/java/com/thoughtworks/inproctester/testapp/TestServlet.java index 4ed9efb..0396fea 100644 --- a/inproctester-tests/src/main/java/com/thoughtworks/inproctester/testapp/TestServlet.java +++ b/inproctester-tests/src/main/java/com/thoughtworks/inproctester/testapp/TestServlet.java @@ -21,11 +21,18 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; public class TestServlet extends HttpServlet { - public static Contact contact = new Contact(); + private static final Logger LOGGER = Logger.getLogger(TestServlet.class.getName()); + private static final String FORWARD_FAILURE_MESSAGE = "Unable to forward the request "; + private static final String REDIRECT_FAILURE_MESSAGE = "Failed to redirect from the request "; + + public static final Contact contact = new Contact(); public static final String FLASH_MESSAGE_COOKIE_NAME = "FLASH_MESSAGE"; + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { @@ -37,7 +44,11 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se resp.addCookie(cookie); } req.setAttribute("contact", contact); - getServletContext().getRequestDispatcher("/test.ftl").forward(req, resp); + try { + getServletContext().getRequestDispatcher("/test.ftl").forward(req, resp); + } catch (ServletException | IOException ex) { + LOGGER.log(Level.FINE, FORWARD_FAILURE_MESSAGE, ex); + } } private Cookie getCookie(HttpServletRequest req, String cookieName) { @@ -58,7 +69,11 @@ private Cookie getCookie(HttpServletRequest req, String cookieName) { protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { contact.setName(req.getParameter("contactName")); resp.addCookie(new Cookie(FLASH_MESSAGE_COOKIE_NAME, "Success")); - resp.sendRedirect(req.getContextPath() + "/contacts/1"); + try { + resp.sendRedirect(req.getContextPath() + "/contacts/1"); + } catch (IOException ex) { + LOGGER.log(Level.FINE, REDIRECT_FAILURE_MESSAGE, ex); + } } }